Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from DeepCodeAI/dev
Browse files Browse the repository at this point in the history
Check Bundle API function added
  • Loading branch information
Arvi3d authored May 28, 2020
2 parents 54f6a16 + 5da7231 commit 117d1c0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {
jcenter()
}

version '0.0.12'
version '0.0.13'
sourceCompatibility = 1.8

dependencies {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/ai/deepcode/javaclient/DeepCodeRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateBundleResponse> 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<CreateBundleResponse> 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}")
Expand Down
88 changes: 80 additions & 8 deletions src/test/java/ai/deepcode/javaclient/DeepCodeRestApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -322,14 +400,8 @@ public void _040_UploadFiles() {
final String fileHash = fileHashRequest.getFiles().get(filePath);
final List<FileHash2ContentRequest> 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
Expand Down

0 comments on commit 117d1c0

Please sign in to comment.