Skip to content

Commit

Permalink
Updated submissionControllerTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed May 17, 2024
1 parent 87fa0a8 commit fe8daf8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,18 @@ public void testSubmitFile() throws Exception {
File file = createTestFile();
try (MockedStatic<Filehandler> mockedFileHandler = mockStatic(Filehandler.class)) {
mockedFileHandler.when(() -> Filehandler.getSubmissionPath(submission.getProjectId(), groupEntity.getId(), submission.getId())).thenReturn(path);
mockedFileHandler.when(() -> Filehandler.saveFile(path, mockMultipartFile)).thenReturn(file);
mockedFileHandler.when(() -> Filehandler.saveFile(path, mockMultipartFile, Filehandler.SUBMISSION_FILENAME)).thenReturn(file);
mockedFileHandler.when(() -> Filehandler.getSubmissionArtifactPath(anyLong(), anyLong(), anyLong())).thenReturn(artifactPath);

when(testRunner.runStructureTest(any(), eq(testEntity), any())).thenReturn(null);
when(testRunner.runDockerTest(any(), eq(testEntity), eq(artifactPath), any())).thenReturn(null);
when(testRunner.runDockerTest(any(), eq(testEntity), eq(artifactPath), any(), eq(submission.getProjectId()))).thenReturn(null);

when(entityToJsonConverter.getSubmissionJson(submission)).thenReturn(submissionJson);

when(testRepository.findByProjectId(submission.getProjectId())).thenReturn(Optional.of(testEntity));
when(entityToJsonConverter.getSubmissionJson(submission)).thenReturn(submissionJson);


mockMvc.perform(MockMvcRequestBuilders.multipart(url)
.file(mockMultipartFile))
.andExpect(status().isOk())
Expand All @@ -295,7 +296,7 @@ public void testSubmitFile() throws Exception {

/* assertEquals(DockerTestState.running, submission.getDockerTestState()); */ // This executes too quickly so we can't test this

Thread.sleep(1000);
Thread.sleep(2000);

// File repository needs to save again after setting path
verify(fileRepository, times(1)).save(argThat(
Expand Down Expand Up @@ -356,7 +357,7 @@ public void testSubmitFile() throws Exception {
testEntity.setDockerImage("dockerImage");
testEntity.setDockerTestScript("dockerTestScript");
DockerOutput dockerOutput = new DockerTestOutput( List.of("dockerFeedback-test"), true);
when(testRunner.runDockerTest(any(), eq(testEntity), eq(artifactPath), any())).thenReturn(dockerOutput);
when(testRunner.runDockerTest(any(), eq(testEntity), eq(artifactPath), any(), eq(submission.getProjectId()))).thenReturn(dockerOutput);
submission.setDockerAccepted(false);
submission.setDockerFeedback("dockerFeedback-test");
mockMvc.perform(MockMvcRequestBuilders.multipart(url)
Expand All @@ -380,7 +381,7 @@ public void testSubmitFile() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(objectMapper.writeValueAsString(submissionJson)));
verify(testRunner, times(0)).runStructureTest(any(), eq(testEntity), any());
verify(testRunner, times(0)).runDockerTest(any(), eq(testEntity), eq(artifactPath), any());
verify(testRunner, times(0)).runDockerTest(any(), eq(testEntity), eq(artifactPath), any(), eq(submission.getProjectId()));

/* Unexpected error */
reset(fileRepository);
Expand Down Expand Up @@ -416,6 +417,7 @@ public void testGetSubmissionFile() throws Exception {
when(submissionUtil.canGetSubmission(submission.getId(), getMockUser())).thenReturn(new CheckResult<>(HttpStatus.OK, "", submission));
when(fileRepository.findById(submission.getFileId())).thenReturn(Optional.of(fileEntity));
mockedFileHandler.when(() -> Filehandler.getFileAsResource(path)).thenReturn(mockedResource);
mockedFileHandler.when(() -> Filehandler.getZipFileAsResponse(path, fileEntity.getName())).thenCallRealMethod();

mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(status().isOk())
Expand All @@ -424,23 +426,12 @@ public void testGetSubmissionFile() throws Exception {
HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileEntity.getName()))
.andExpect(content().bytes(mockedResource.getInputStream().readAllBytes()));

/* Resource not found */
mockedFileHandler.when(() -> Filehandler.getFileAsResource(path)).thenReturn(null);
mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(status().isNotFound());

/* file not found */
when(fileRepository.findById(submission.getFileId())).thenReturn(Optional.empty());
mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(status().isNotFound());

/* Unexpected error */
when(fileRepository.findById(submission.getFileId())).thenReturn(Optional.of(fileEntity));
mockedFileHandler.reset();
mockedFileHandler.when(() -> Filehandler.getFileAsResource(path)).thenThrow(new RuntimeException());
mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(status().isInternalServerError());

/* User can't get submission */
when(submissionUtil.canGetSubmission(submission.getId(), getMockUser())).thenReturn(new CheckResult<>(HttpStatus.I_AM_A_TEAPOT, "", null));
mockMvc.perform(MockMvcRequestBuilders.get(url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public void setup() {
test.getDockerImage(),
test.getDockerTestScript(),
test.getDockerTestTemplate(),
test.getStructureTemplate()
test.getStructureTemplate(),
"extraFilesUrl",
"extraFilesName"

);

}
Expand All @@ -132,7 +135,9 @@ public void testUpdateTest() throws Exception {
dockerImage,
dockerTestScript,
dockerTestTemplate,
structureTemplate
structureTemplate,
"extraFilesUrl",
"extraFilesName"
);
/* All checks succeed */
when(testUtil.checkForTestUpdate(
Expand Down Expand Up @@ -174,7 +179,9 @@ public void testUpdateTest() throws Exception {
null,
null,
null,
null
null,
"extraFilesUrl",
"extraFilesName"
);
testUpdateJson = new TestUpdateJson(
dockerImageBlank,
Expand Down Expand Up @@ -285,7 +292,9 @@ public void testPutTest() throws Exception {
dockerImage,
dockerTestScript,
dockerTestTemplate,
structureTemplate
structureTemplate,
"extraFilesUrl",
"extraFilesName"
);
/* All checks succeed */
when(testUtil.checkForTestUpdate(
Expand Down Expand Up @@ -337,7 +346,9 @@ public void testPutTest() throws Exception {
null,
null,
null,
null
null,
"extraFilesUrl",
"extraFilesName"
);
reset(testUtil);
when(testUtil.checkForTestUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setUp() throws IOException {

@Test
public void testSaveSubmission() throws Exception {
File savedFile = Filehandler.saveFile(tempDir, file);
File savedFile = Filehandler.saveFile(tempDir, file, Filehandler.SUBMISSION_FILENAME);

assertTrue(savedFile.exists());
assertEquals(Filehandler.SUBMISSION_FILENAME, savedFile.getName());
Expand All @@ -77,7 +77,7 @@ public void testSaveSubmission() throws Exception {

@Test
public void testSaveSubmission_dirDoesntExist() throws Exception {
File savedFile = Filehandler.saveFile(tempDir.resolve("nonexistent"), file);
File savedFile = Filehandler.saveFile(tempDir.resolve("nonexistent"), file, Filehandler.SUBMISSION_FILENAME);

assertTrue(savedFile.exists());
assertEquals(Filehandler.SUBMISSION_FILENAME, savedFile.getName());
Expand All @@ -88,28 +88,28 @@ public void testSaveSubmission_dirDoesntExist() throws Exception {

@Test
public void testSaveSubmission_errorWhileCreatingDir() throws Exception {
assertThrows(IOException.class, () -> Filehandler.saveFile(Path.of(""), file));
assertThrows(IOException.class, () -> Filehandler.saveFile(Path.of(""), file, Filehandler.SUBMISSION_FILENAME));
}

@Test
public void testSaveSubmission_notAZipFile() {
MockMultipartFile notAZipFile = new MockMultipartFile(
"notAZipFile.txt", "This is not a zip file".getBytes()
);
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, notAZipFile));
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, notAZipFile, Filehandler.SUBMISSION_FILENAME));
}

@Test
public void testSaveSubmission_fileEmpty() {
MockMultipartFile emptyFile = new MockMultipartFile(
"emptyFile.txt", new byte[0]
);
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, emptyFile));
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, emptyFile, Filehandler.SUBMISSION_FILENAME));
}

@Test
public void testSaveSubmission_fileNull() {
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, null));
assertThrows(IOException.class, () -> Filehandler.saveFile(tempDir, null, Filehandler.SUBMISSION_FILENAME));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class TestRunnerTest {
private SubmissionResult submissionResult;
private DockerTestOutput dockerTestOutput;
private DockerTemplateTestOutput dockerTemplateTestOutput;
private final long projectId = 876L;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -108,7 +109,7 @@ public void testRunDockerTest() throws IOException {
.thenReturn(dockerTemplateTestOutput);
when(dockerModel.getArtifacts()).thenReturn(artifacts);

DockerOutput result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel);
DockerOutput result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId);
assertEquals(dockerTemplateTestOutput, result);

verify(dockerModel, times(1)).addZipInputFiles(file);
Expand All @@ -117,15 +118,15 @@ public void testRunDockerTest() throws IOException {

/* artifacts are empty */
when(dockerModel.getArtifacts()).thenReturn(Collections.emptyList());
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId);
assertEquals(dockerTemplateTestOutput, result);
verify(dockerModel, times(2)).addZipInputFiles(file);
verify(dockerModel, times(2)).cleanUp();
assertEquals(1, filehandlerCalled.get());

/* aritifacts are null */
when(dockerModel.getArtifacts()).thenReturn(null);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId);
assertEquals(dockerTemplateTestOutput, result);
verify(dockerModel, times(3)).addZipInputFiles(file);
verify(dockerModel, times(3)).cleanUp();
Expand All @@ -134,19 +135,19 @@ public void testRunDockerTest() throws IOException {
/* No template */
testEntity.setDockerTestTemplate(null);
when(dockerModel.runSubmission(testEntity.getDockerTestScript())).thenReturn(dockerTestOutput);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId);
assertEquals(dockerTestOutput, result);
verify(dockerModel, times(4)).addZipInputFiles(file);
verify(dockerModel, times(4)).cleanUp();

/* Error gets thrown */
when(dockerModel.runSubmission(testEntity.getDockerTestScript())).thenThrow(new RuntimeException("Error"));
assertThrows(Exception.class, () -> new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel));
assertThrows(Exception.class, () -> new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId));
verify(dockerModel, times(5)).cleanUp();

/* No script */
testEntity.setDockerTestScript(null);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel);
result = new TestRunner().runDockerTest(file, testEntity, outputPath, dockerModel, projectId);
assertNull(result);
}

Expand Down

0 comments on commit fe8daf8

Please sign in to comment.