Skip to content

Commit

Permalink
fix: update tests to use the new requestId
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Oct 10, 2023
1 parent 51251be commit 5859ef6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
43 changes: 23 additions & 20 deletions src/test/java/ai/deepcode/javaclient/core/AnalysisDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.UUID;

import static ai.deepcode.javaclient.core.AnalysisDataBase.COMPLETE;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -71,17 +72,17 @@ private static class RestApiMockWithBrokenFileUpload extends DeepCodeRestApiMock
new CreateBundleResponse("bundleHash", Collections.singletonList("/filePath"));

@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
return bundleResponseWithMissedFile;
}

@Override
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request) {
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request) {
return bundleResponseWithMissedFile;
}

@Override
public @NotNull CreateBundleResponse checkBundle(String orgName, String bundleId) {
public @NotNull CreateBundleResponse checkBundle(String orgName, String requestId, String bundleId) {
return bundleResponseWithMissedFile;
}
}
Expand All @@ -92,9 +93,9 @@ public void re_createBundle_if_createBundle_fail_with_404() {
final int[] reUploadCounter = {0};
restApi = new RestApiMockWithBrokenFileUpload() {
@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
reUploadCounter[0] = reUploadCounter[0] + 1;
return super.createBundle(orgName, files);
return super.createBundle(orgName, requestId, files);
}
};

Expand All @@ -105,7 +106,7 @@ public void re_createBundle_if_createBundle_fail_with_404() {
final String project = "Project4";
final String progress = "Progress Indicator";

analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress);
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress, UUID.randomUUID().toString());

assertEquals("Should be made 3 attempts to re-create bundle if operation does not succeed", 3, reUploadCounter[0]);
}
Expand All @@ -120,13 +121,14 @@ public void re_createBundle_if_extendBundle_fail_with_404() {
analysisData = new AnalysisDataBaseMock(pdUtils, hashContentUtils, deepCodeParams, dcLogger, restApi);
final String project = "Project5";
final String progress = "Progress Indicator";
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress);
String requestId = UUID.randomUUID().toString();
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress, requestId);

// try to extend expired bundle
final int[] reUploadCounter = {0};
restApi = new RestApiMockWithBrokenFileUpload() {
@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
reUploadCounter[0] = reUploadCounter[0] + 1;
final CreateBundleResponse response =
new CreateBundleResponse("bundleHash", Collections.singletonList("/filePath"));
Expand All @@ -135,7 +137,7 @@ public void re_createBundle_if_extendBundle_fail_with_404() {
}

@Override
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request) {
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request) {
final CreateBundleResponse response = new CreateBundleResponse();
response.setStatusCode(404);
return response;
Expand All @@ -144,7 +146,7 @@ public void re_createBundle_if_extendBundle_fail_with_404() {
analysisData = new AnalysisDataBaseMock(pdUtils, hashContentUtils, deepCodeParams, dcLogger, restApi);

// --------------------------- actual test --------------------
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress);
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress, requestId);

assertEquals(
"Should be made 2 attempts to re-create bundle if extendBundle does not succeed",
Expand All @@ -159,17 +161,17 @@ public void reupload_files_if_initial_upload_does_not_succeed() throws IOExcepti
final int[] reUploadCounter = {0};
restApi = new RestApiMockWithBrokenFileUpload() {
@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
final CreateBundleResponse response =
new CreateBundleResponse("bundleHash", Collections.singletonList("/filePath"));
response.setStatusCode(200);
return response;
}

@Override
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request) {
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request) {
reUploadCounter[0] = reUploadCounter[0] + 1;
return super.extendBundle(orgName, bundleId, request);
return super.extendBundle(orgName, requestId, bundleId, request);
}
};

Expand All @@ -184,7 +186,7 @@ public void reupload_files_if_initial_upload_does_not_succeed() throws IOExcepti
file.deleteOnExit();
Files.writeString(file.toPath(), "testtestest");

analysisData.updateCachedResultsForFiles(project, Collections.singleton(file), progress);
analysisData.updateCachedResultsForFiles(project, Collections.singleton(file), progress, UUID.randomUUID().toString());

assertEquals("Should have made 10 attempts to re-upload files if operation does not succeed", 10, reUploadCounter[0]);
}
Expand All @@ -196,6 +198,7 @@ public void if_file_upload_fail_getAnalysis_should_not_be_invoked() {
@Override
public @NotNull GetAnalysisResponse getAnalysis(
String orgName,
String requestId,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
Expand All @@ -213,7 +216,7 @@ public void if_file_upload_fail_getAnalysis_should_not_be_invoked() {
final String project = "Project2";
final String progress = "Progress Indicator";

analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress);
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress, UUID.randomUUID().toString());
}

// getAnalysis recover (continue polling) if operation sometimes does not succeed with 404
Expand Down Expand Up @@ -247,23 +250,23 @@ public void getAnalysis_recover_during_polling_if_operation_sometimes_does_not_s

restApi = new RestApiMockWithBrokenFileUpload() {
@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
final CreateBundleResponse response =
new CreateBundleResponse("bundleHash", Collections.singletonList("/filePath"));
response.setStatusCode(200);
return response;
}

@Override
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request) {
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request) {
final CreateBundleResponse response =
new CreateBundleResponse("bundleHash", Collections.emptyList());
response.setStatusCode(200);
return response;
}

@Override
public @NotNull CreateBundleResponse checkBundle(String orgName, String bundleId) {
public @NotNull CreateBundleResponse checkBundle(String orgName, String requestId, String bundleId) {
final CreateBundleResponse response = new CreateBundleResponse("bundleHash", Collections.emptyList());
response.setStatusCode(200);
response.setStatusDescription("Fake successful bundle check");
Expand All @@ -273,6 +276,7 @@ public void getAnalysis_recover_during_polling_if_operation_sometimes_does_not_s
@Override
public @NotNull GetAnalysisResponse getAnalysis(
String orgName,
String requestId,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
Expand All @@ -293,14 +297,13 @@ public long getTimeoutForGettingAnalysesMs() {
return 10_000L; // 10 sec
}
};
;
analysisData = new AnalysisDataBaseMock(pdUtils, hashContentUtils, deepCodeParams, dcLogger, restApi);

// --------------------------- actual test --------------------
final String project = "Project3";
final String progress = "Progress Indicator";

analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress);
analysisData.updateCachedResultsForFiles(project, Collections.singleton("File"), progress, UUID.randomUUID().toString());

assertTrue(
"Analysis should complete despite getting 404s in the middle of polling",
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/ai/deepcode/javaclient/core/RunUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void isRescanShownAsNotRequestedAfterScanFinishedTest() {
public void updateCachedResultsForFiles(
@NotNull Object project,
@NotNull Collection<Object> allProjectFiles,
@NotNull Object progress) {
@NotNull Object progress,
@NotNull String requestId) {
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@

public class DeepCodeRestApiMock implements DeepCodeRestApi {
@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileContentRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileContentRequest files) {
throw new UnsupportedOperationException();
}

@Override
public @NotNull CreateBundleResponse createBundle(String orgName, FileHashRequest files) {
public @NotNull CreateBundleResponse createBundle(String orgName, String requestId, FileHashRequest files) {
throw new UnsupportedOperationException();
}

@Override
public @NotNull CreateBundleResponse checkBundle(String orgName, String bundleId) {
public @NotNull CreateBundleResponse checkBundle(String orgName, String requestId, String bundleId) {
throw new UnsupportedOperationException();
}

@Override
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String bundleId, Req request) {
public @NotNull <Req> CreateBundleResponse extendBundle(String orgName, String requestId, String bundleId, Req request) {
throw new UnsupportedOperationException();
}

@Override
public @NotNull GetAnalysisResponse getAnalysis(
String orgName,
String requestId,
String bundleId,
Integer severity,
List<String> filesToAnalyse,
Expand Down

0 comments on commit 5859ef6

Please sign in to comment.