diff --git a/CHANGELOG.md b/CHANGELOG.md index a7b57d1..b5fdd5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.0.13] - 2020-05-27 +- `Check Bundle` API function added + ## [0.0.12] - 2020-05-22 - Added support for Java 8, required for Android Studio diff --git a/build.gradle b/build.gradle index e6a3c99..2906cff 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { jcenter() } -version '0.0.12' +version '0.0.13' sourceCompatibility = 1.8 dependencies { diff --git a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java index 9b3b5e1..8e145f8 100644 --- a/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java +++ b/src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java @@ -202,6 +202,57 @@ public static CreateBundleResponse createBundle(String token, FileHashRequest fi return doCreateBundle(token, files); } + private interface CheckBundleCall { + // @retrofit2.http.Headers("Content-Type: application/json") + @GET("bundle/{bundleId}") + Call doCheckBundle( + @Header("Session-Token") String token, + @Path(value = "bundleId", encoded = true) String bundleId); + } + + /** + * Checks the status of a bundle. + * + * @param bundleId the parent bundle to extend + * @return {@link CreateBundleResponse} instance + */ + @NotNull + public static CreateBundleResponse checkBundle( + String token, String bundleId) { + CheckBundleCall checkBundleCall = retrofit.create(CheckBundleCall.class); + Response retrofitResponse; + try { + retrofitResponse = + checkBundleCall.doCheckBundle(token, bundleId).execute(); + } catch (IOException e) { + return new CreateBundleResponse(); + } + CreateBundleResponse result = retrofitResponse.body(); + if (result == null) { + result = new CreateBundleResponse(); + } + result.setStatusCode(retrofitResponse.code()); + switch (retrofitResponse.code()) { + case 200: + result.setStatusDescription("The bundle checked successfully"); + break; + case 401: + result.setStatusDescription("Missing sessionToken or incomplete login process"); + break; + case 403: + result.setStatusDescription("Unauthorized access to parent bundle"); + break; + case 404: + result.setStatusDescription("Uploaded bundle has expired"); + break; + default: + result.setStatusDescription("Unknown Status Code: " + retrofitResponse.code()); + break; + } + return result; + } + + private interface ExtendBundleCall { @retrofit2.http.Headers("Content-Type: application/json") @PUT("bundle/{bundleId}") diff --git a/src/test/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java b/src/test/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java index 97f294c..f0edd64 100644 --- a/src/test/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java +++ b/src/test/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java @@ -207,6 +207,69 @@ public void _035_createBundle_with_hash() { assertEquals(200, response.getStatusCode()); } + @Test + public void _036_Check_Bundle() { + System.out.println("\n--------------Check Bundle----------------\n"); + FileHashRequest fileHashRequest = createFileHashRequest(null); + CreateBundleResponse createBundleResponse = + DeepCodeRestApi.createBundle(loggedToken, fileHashRequest); + assertNotNull(createBundleResponse); + System.out.printf( + "\nCreate Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n uploadUrl: %5$s\n", + createBundleResponse.getStatusCode(), + createBundleResponse.getBundleId(), + createBundleResponse.getStatusDescription(), + createBundleResponse.getMissingFiles(), + createBundleResponse.getUploadURL()); + assertEquals(200, createBundleResponse.getStatusCode()); + assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty()); + + CreateBundleResponse checkBundleResponse = + DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleId()); + assertNotNull(checkBundleResponse); + System.out.printf( + "\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n uploadUrl: %5$s\n", + checkBundleResponse.getStatusCode(), + checkBundleResponse.getBundleId(), + checkBundleResponse.getStatusDescription(), + checkBundleResponse.getMissingFiles(), + checkBundleResponse.getUploadURL()); + assertEquals(200, checkBundleResponse.getStatusCode()); + assertFalse("List of missingFiles is empty.", checkBundleResponse.getMissingFiles().isEmpty()); + assertEquals( + "Checked and returned bundleId's are different.", + createBundleResponse.getBundleId(), + checkBundleResponse.getBundleId()); + + EmptyResponse uploadFileResponse = doUploadFile(createBundleResponse, fileHashRequest); + + assertNotNull(uploadFileResponse); + System.out.printf( + "\nUpload Files call for file %3$s \nStatus code [%1$d] %2$s\n", + uploadFileResponse.getStatusCode(), + uploadFileResponse.getStatusDescription(), + createBundleResponse.getMissingFiles().get(0)); + assertEquals(200, uploadFileResponse.getStatusCode()); + + CreateBundleResponse createBundleResponse1 = + DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleId()); + assertNotNull(createBundleResponse1); + System.out.printf( + "\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n uploadUrl: %5$s\n", + createBundleResponse1.getStatusCode(), + createBundleResponse1.getBundleId(), + createBundleResponse1.getStatusDescription(), + createBundleResponse1.getMissingFiles(), + createBundleResponse1.getUploadURL()); + assertEquals(200, createBundleResponse1.getStatusCode()); + assertTrue( + "List of missingFiles is NOT empty.", createBundleResponse1.getMissingFiles().isEmpty()); + assertEquals( + "Checked and returned bundleId's are different.", + createBundleResponse.getBundleId(), + checkBundleResponse.getBundleId()); + } + private FileHashRequest createFileHashRequest(String fakeFileName) { int status = DeepCodeRestApi.checkSession(loggedToken).getStatusCode(); assertEquals(200, status); @@ -232,6 +295,7 @@ private FileHashRequest createFileHashRequest(String fakeFileName) { String fileText; try { + // ?? com.intellij.openapi.util.io.FileUtil#loadFile(java.io.File, java.nio.charset.Charset) fileText = Files.readString(Paths.get(absolutePath)); digest = MessageDigest.getInstance("SHA-256"); } catch (IOException | NoSuchAlgorithmException e) { @@ -309,10 +373,24 @@ public void _040_UploadFiles() { assertEquals(200, createBundleResponse.getStatusCode()); assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty()); + EmptyResponse response = doUploadFile(createBundleResponse, fileHashRequest); + + assertNotNull(response); + System.out.printf( + "\nUpload Files call for file %3$s \nStatus code [%1$d] %2$s\n", + response.getStatusCode(), + response.getStatusDescription(), + createBundleResponse.getMissingFiles().get(0)); + assertEquals(200, response.getStatusCode()); + } + + private EmptyResponse doUploadFile( + CreateBundleResponse createBundleResponse, FileHashRequest fileHashRequest) { final File testFile = new File(getClass().getClassLoader().getResource("test1.js").getFile()); final String absolutePath = testFile.getAbsolutePath(); String fileText; try { + // ?? com.intellij.openapi.util.io.FileUtil#loadFile(java.io.File, java.nio.charset.Charset) fileText = Files.readString(Paths.get(absolutePath)); } catch (IOException e) { throw new RuntimeException(e); @@ -322,14 +400,8 @@ public void _040_UploadFiles() { final String fileHash = fileHashRequest.getFiles().get(filePath); final List requestBody = Collections.singletonList(new FileHash2ContentRequest(fileHash, fileText)); - EmptyResponse response = - DeepCodeRestApi.UploadFiles(loggedToken, createBundleResponse.getBundleId(), requestBody); - - assertNotNull(response); - System.out.printf( - "Upload Files call return: Status code [%1$d] %2$s\n", - response.getStatusCode(), response.getStatusDescription()); - assertEquals(200, response.getStatusCode()); + return DeepCodeRestApi.UploadFiles( + loggedToken, createBundleResponse.getBundleId(), requestBody); } @Test