diff --git a/backend/app/src/test/java/com/ugent/pidgeon/controllers/SubmissionControllerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/controllers/SubmissionControllerTest.java index 51d24553..4ac934ac 100644 --- a/backend/app/src/test/java/com/ugent/pidgeon/controllers/SubmissionControllerTest.java +++ b/backend/app/src/test/java/com/ugent/pidgeon/controllers/SubmissionControllerTest.java @@ -276,17 +276,18 @@ public void testSubmitFile() throws Exception { File file = createTestFile(); try (MockedStatic 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()) @@ -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( @@ -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) @@ -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); @@ -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()) @@ -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)) diff --git a/backend/app/src/test/java/com/ugent/pidgeon/controllers/TestControllerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/controllers/TestControllerTest.java index a360aaff..a21c3c63 100644 --- a/backend/app/src/test/java/com/ugent/pidgeon/controllers/TestControllerTest.java +++ b/backend/app/src/test/java/com/ugent/pidgeon/controllers/TestControllerTest.java @@ -106,7 +106,10 @@ public void setup() { test.getDockerImage(), test.getDockerTestScript(), test.getDockerTestTemplate(), - test.getStructureTemplate() + test.getStructureTemplate(), + "extraFilesUrl", + "extraFilesName" + ); } @@ -132,7 +135,9 @@ public void testUpdateTest() throws Exception { dockerImage, dockerTestScript, dockerTestTemplate, - structureTemplate + structureTemplate, + "extraFilesUrl", + "extraFilesName" ); /* All checks succeed */ when(testUtil.checkForTestUpdate( @@ -174,7 +179,9 @@ public void testUpdateTest() throws Exception { null, null, null, - null + null, + "extraFilesUrl", + "extraFilesName" ); testUpdateJson = new TestUpdateJson( dockerImageBlank, @@ -285,7 +292,9 @@ public void testPutTest() throws Exception { dockerImage, dockerTestScript, dockerTestTemplate, - structureTemplate + structureTemplate, + "extraFilesUrl", + "extraFilesName" ); /* All checks succeed */ when(testUtil.checkForTestUpdate( @@ -337,7 +346,9 @@ public void testPutTest() throws Exception { null, null, null, - null + null, + "extraFilesUrl", + "extraFilesName" ); reset(testUtil); when(testUtil.checkForTestUpdate( diff --git a/backend/app/src/test/java/com/ugent/pidgeon/util/FileHandlerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/util/FileHandlerTest.java index 83392fef..e7cd6668 100644 --- a/backend/app/src/test/java/com/ugent/pidgeon/util/FileHandlerTest.java +++ b/backend/app/src/test/java/com/ugent/pidgeon/util/FileHandlerTest.java @@ -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()); @@ -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()); @@ -88,7 +88,7 @@ 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 @@ -96,7 +96,7 @@ 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 @@ -104,12 +104,12 @@ 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 diff --git a/backend/app/src/test/java/com/ugent/pidgeon/util/TestRunnerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/util/TestRunnerTest.java index 828a0f7d..d9a7434f 100644 --- a/backend/app/src/test/java/com/ugent/pidgeon/util/TestRunnerTest.java +++ b/backend/app/src/test/java/com/ugent/pidgeon/util/TestRunnerTest.java @@ -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() { @@ -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); @@ -117,7 +118,7 @@ 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(); @@ -125,7 +126,7 @@ public void testRunDockerTest() throws IOException { /* 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(); @@ -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); }