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 #33 from snyk/fix/ROAD-730_fileUpload_and_getAnaly…
Browse files Browse the repository at this point in the history
…sis_resilience

make analysis retrieval more resilient [ROAD-730]
  • Loading branch information
ArtsiomCh authored Mar 28, 2022
2 parents 980128b + 1a9b7e8 commit 33da807
Show file tree
Hide file tree
Showing 18 changed files with 782 additions and 433 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## [2.2.2] - 2022-03
## [2.3.0] - 2022-03
- fix: make 5 consequent attempts to getAnalysis during polling if operation does not succeed with 404
- fix: make 5 attempts to re-upload files if operation does not succeed
- fix: do not try to getAnalysis if `upload files` is not succeed (i.e. `missingFiles` is not empty after uploads)
- fix: avoid remove operation for empty immutable List
- fix: check file in marker for nullability before proceed
- feat: provide unique (per project) `shard` to getAnalysis call
- chore: reshape/refactor REST API wrapper to be replaceable through constructor base DI

## [2.2.1] - 2021-12-10
- fix: don't upload empty files
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

group = "io.snyk.code.sdk"
archivesBaseName = "snyk-code-client"
version = "2.2.2"
version = "2.3.0"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.junit.Assert.assertTrue;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DeepCodeRestApiTest {
public class DeepCodeRestApiImplTest {

private final String testFileContent =
"public class AnnotatorTest {\n"
Expand All @@ -55,6 +55,8 @@ public class DeepCodeRestApiTest {

private static String bundleId = null;

private static final DeepCodeRestApi restApiClient = new DeepCodeRestApiImpl();

@Test
public void _022_setBaseUrl() {
System.out.println("\n--------------Set base URL----------------\n");
Expand All @@ -63,13 +65,13 @@ public void _022_setBaseUrl() {
doSetBaseUrlTest("https://www.google.com/", 404);
doSetBaseUrlTest("https://deeproxy.snyk.io/", 401);
} finally {
DeepCodeRestApi.setBaseUrl("");
restApiClient.setBaseUrl("", false, false);
}
}

private void doSetBaseUrlTest(String baseUrl, int expectedStatusCode) {
DeepCodeRestApi.setBaseUrl(baseUrl, false, true);
EmptyResponse response = DeepCodeRestApi.checkBundle("blabla", "irrelevant");
restApiClient.setBaseUrl(baseUrl, false, true);
EmptyResponse response = restApiClient.checkBundle("blabla", "irrelevant");
int status = response.getStatusCode();
String description = response.getStatusDescription();
System.out.printf(
Expand All @@ -81,7 +83,7 @@ private void doSetBaseUrlTest(String baseUrl, int expectedStatusCode) {
@Test
public void _025_getFilters() {
System.out.println("\n--------------Get Filters----------------\n");
GetFiltersResponse response = DeepCodeRestApi.getFilters(loggedToken);
GetFiltersResponse response = restApiClient.getFilters(loggedToken);
assertNotNull(response);
final String errorMsg =
"Get Filters return status code: ["
Expand All @@ -99,7 +101,7 @@ public void _025_getFilters() {
@Test
public void _030_createBundle_from_source() throws NoSuchAlgorithmException {
System.out.println("\n--------------Create Bundle from Source----------------\n");
DeepCodeRestApi.setBaseUrl(baseUrl, false, true);
restApiClient.setBaseUrl(baseUrl, false, true);
CreateBundleResponse response = createBundleFromSource(loggedToken);
assertNotNull(response);
System.out.printf(
Expand All @@ -116,7 +118,7 @@ private CreateBundleResponse createBundleFromSource(String token)
fileContent.put(
"/AnnotatorTest.java",
new FileHash2ContentRequest(getHash(testFileContent), testFileContent));
CreateBundleResponse response = DeepCodeRestApi.createBundle(token, fileContent);
CreateBundleResponse response = restApiClient.createBundle(token, fileContent);
return response;
}

Expand Down Expand Up @@ -145,7 +147,7 @@ public void _035_createBundle_with_hash() {
@NotNull
private CreateBundleResponse createBundleWithHash() {
FileHashRequest files = createFileHashRequest(null);
CreateBundleResponse response = DeepCodeRestApi.createBundle(loggedToken, files);
CreateBundleResponse response = restApiClient.createBundle(loggedToken, files);
assertNotNull(response);
System.out.printf(
"Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -161,7 +163,7 @@ public void _036_Check_Bundle() {
System.out.println("\n--------------Check Bundle----------------\n");
FileHashRequest fileHashRequest = createFileHashRequest(null);
CreateBundleResponse createBundleResponse =
DeepCodeRestApi.createBundle(loggedToken, fileHashRequest);
restApiClient.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",
Expand All @@ -173,7 +175,7 @@ public void _036_Check_Bundle() {
assertFalse("List of missingFiles is empty.", createBundleResponse.getMissingFiles().isEmpty());

CreateBundleResponse checkBundleResponse =
DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleHash());
restApiClient.checkBundle(loggedToken, createBundleResponse.getBundleHash());
assertNotNull(checkBundleResponse);
System.out.printf(
"\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -199,7 +201,7 @@ public void _036_Check_Bundle() {
assertEquals(200, uploadFileResponse.getStatusCode());

CreateBundleResponse createBundleResponse1 =
DeepCodeRestApi.checkBundle(loggedToken, createBundleResponse.getBundleHash());
restApiClient.checkBundle(loggedToken, createBundleResponse.getBundleHash());
assertNotNull(createBundleResponse1);
System.out.printf(
"\nCheck Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand All @@ -217,7 +219,7 @@ public void _036_Check_Bundle() {
}

private FileHashRequest createFileHashRequest(String fakeFileName) {
DeepCodeRestApi.setBaseUrl(baseUrl, false, true);
restApiClient.setBaseUrl(baseUrl, false, true);
final File testFile =
new File(getClass().getClassLoader().getResource("AnnotatorTest.java").getFile());
String absolutePath = testFile.getAbsolutePath();
Expand Down Expand Up @@ -284,7 +286,7 @@ public void _037_ExtendBundle() {
ExtendBundleWithHashRequest extendBundleWithHashRequest =
new ExtendBundleWithHashRequest(newFileHashRequest, Collections.emptyList());
CreateBundleResponse extendBundleResponse =
DeepCodeRestApi.extendBundle(
restApiClient.extendBundle(
loggedToken, createBundleResponse.getBundleHash(), extendBundleWithHashRequest);
assertNotNull(extendBundleResponse);
System.out.printf(
Expand All @@ -302,7 +304,7 @@ public void _040_UploadFiles() {
System.out.println("\n--------------Upload Files by Hash----------------\n");
FileHashRequest fileHashRequest = createFileHashRequest(null);
CreateBundleResponse createBundleResponse =
DeepCodeRestApi.createBundle(loggedToken, fileHashRequest);
restApiClient.createBundle(loggedToken, fileHashRequest);
assertNotNull(createBundleResponse);
System.out.printf(
"Create Bundle call return:\nStatus code [%1$d] %3$s \n bundleId: %2$s\n missingFiles: %4$s\n",
Expand Down Expand Up @@ -345,7 +347,7 @@ private EmptyResponse doUploadFile(
map.put(filePath, new FileHash2ContentRequest(fileHash, fileText));
ExtendBundleWithContentRequest ebr =
new ExtendBundleWithContentRequest(map, Collections.emptyList());
return DeepCodeRestApi.extendBundle(loggedToken, createBundleResponse.getBundleHash(), ebr);
return restApiClient.extendBundle(loggedToken, createBundleResponse.getBundleHash(), ebr);
}

@Test
Expand All @@ -370,7 +372,7 @@ private GetAnalysisResponse doAnalysisAndWait(List<String> analysedFiles, Intege
throws InterruptedException {
GetAnalysisResponse response = null;
for (int i = 0; i < 120; i++) {
response = DeepCodeRestApi.getAnalysis(loggedToken, bundleId, severity, analysedFiles, bundleId);
response = restApiClient.getAnalysis(loggedToken, bundleId, severity, analysedFiles, bundleId);
if (response.getStatus().equals("COMPLETE")) break;
Thread.sleep(1000);
}
Expand Down
Loading

0 comments on commit 33da807

Please sign in to comment.