diff --git a/src/main/java/ch/puzzle/eft/controller/ExamDownloadController.java b/src/main/java/ch/puzzle/eft/controller/ExamController.java similarity index 75% rename from src/main/java/ch/puzzle/eft/controller/ExamDownloadController.java rename to src/main/java/ch/puzzle/eft/controller/ExamController.java index e1a837167..48bc3595b 100644 --- a/src/main/java/ch/puzzle/eft/controller/ExamDownloadController.java +++ b/src/main/java/ch/puzzle/eft/controller/ExamController.java @@ -1,23 +1,21 @@ package ch.puzzle.eft.controller; -import ch.puzzle.eft.service.ExamFileService; +import ch.puzzle.eft.service.ExamService; import jakarta.servlet.http.HttpServletResponse; import org.springframework.core.io.FileSystemResource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.io.File; import java.io.IOException; @RestController -public class ExamDownloadController { - ExamFileService examFileService; +@RequestMapping("/exams") +public class ExamController { + ExamService examFileService; - public ExamDownloadController(ExamFileService examFileService) { + public ExamController(ExamService examFileService) { this.examFileService = examFileService; } diff --git a/src/main/java/ch/puzzle/eft/controller/SiteController.java b/src/main/java/ch/puzzle/eft/controller/SiteController.java index dbaf9c0f5..41235c5ce 100644 --- a/src/main/java/ch/puzzle/eft/controller/SiteController.java +++ b/src/main/java/ch/puzzle/eft/controller/SiteController.java @@ -1,7 +1,7 @@ package ch.puzzle.eft.controller; import ch.puzzle.eft.model.ExamNumberForm; -import ch.puzzle.eft.service.ExamFileService; +import ch.puzzle.eft.service.ExamService; import jakarta.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -13,9 +13,9 @@ @Controller public class SiteController { - ExamFileService examFileService; + ExamService examFileService; - public SiteController(ExamFileService examFileService) { + public SiteController(ExamService examFileService) { this.examFileService = examFileService; } diff --git a/src/main/java/ch/puzzle/eft/model/ExamFileModel.java b/src/main/java/ch/puzzle/eft/model/ExamModel.java similarity index 88% rename from src/main/java/ch/puzzle/eft/model/ExamFileModel.java rename to src/main/java/ch/puzzle/eft/model/ExamModel.java index 0f656c7aa..00674846c 100644 --- a/src/main/java/ch/puzzle/eft/model/ExamFileModel.java +++ b/src/main/java/ch/puzzle/eft/model/ExamModel.java @@ -1,6 +1,5 @@ package ch.puzzle.eft.model; -import ch.puzzle.eft.service.ExamFileService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -8,12 +7,12 @@ import java.io.File; -public class ExamFileModel { +public class ExamModel { private final File file; - private static final Logger logger = LoggerFactory.getLogger(ExamFileModel.class); + private static final Logger logger = LoggerFactory.getLogger(ExamModel.class); - public ExamFileModel(File file) { + public ExamModel(File file) { this.file = file; } diff --git a/src/main/java/ch/puzzle/eft/service/ExamFileService.java b/src/main/java/ch/puzzle/eft/service/ExamService.java similarity index 88% rename from src/main/java/ch/puzzle/eft/service/ExamFileService.java rename to src/main/java/ch/puzzle/eft/service/ExamService.java index 92d4261d8..1593541ea 100644 --- a/src/main/java/ch/puzzle/eft/service/ExamFileService.java +++ b/src/main/java/ch/puzzle/eft/service/ExamService.java @@ -1,6 +1,6 @@ package ch.puzzle.eft.service; -import ch.puzzle.eft.model.ExamFileModel; +import ch.puzzle.eft.model.ExamModel; import jakarta.servlet.ServletOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,12 +20,12 @@ import java.util.zip.ZipOutputStream; @Service -public class ExamFileService { - private static final Logger logger = LoggerFactory.getLogger(ExamFileService.class); +public class ExamService { + private static final Logger logger = LoggerFactory.getLogger(ExamService.class); private final Environment environment; private final ValidationService validationService; - public ExamFileService(Environment environment, ValidationService validationService) { + public ExamService(Environment environment, ValidationService validationService) { this.environment = environment; this.validationService = validationService; } @@ -44,7 +44,7 @@ public List getAllExamFiles() { .toList(); } - public List getMatchingExams(String examNumber, String matriculationNumber) { + public List getMatchingExams(String examNumber, String matriculationNumber) { if (!validationService.validateExamNumber(examNumber)) { logger.info("Invalid Exam Number: {}", examNumber); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, @@ -63,7 +63,7 @@ public List getMatchingExams(String examNumber, String matriculat examNumber)); } return matchingFiles.stream() - .map(ExamFileModel::new) + .map(ExamModel::new) .toList(); } @@ -88,9 +88,9 @@ public File getFileToDownload(String subjectName, String filename) { return examToDownload; } - public ResponseEntity convertFilesToZip(List examFileList, ServletOutputStream outputStream) { + public ResponseEntity convertFilesToZip(List examFileList, ServletOutputStream outputStream) { try (ZipOutputStream zos = new ZipOutputStream(outputStream)) { - for (ExamFileModel examFile : examFileList) { + for (ExamModel examFile : examFileList) { String name = examFile.getSubjectName() + ".pdf"; ZipEntry zipEntry = new ZipEntry(name); zos.putNextEntry(zipEntry); @@ -115,7 +115,7 @@ public ResponseEntity convertFilesToZip(List examFileList public void convertSelectedFilesToZip(String examNumber, ServletOutputStream outputStream) { // TODO: Replace hardcoded marticulationNumber 11112222 with dynamic number after login is implemented - List matchingExams = getMatchingExams(examNumber, "11112222"); + List matchingExams = getMatchingExams(examNumber, "11112222"); convertFilesToZip(matchingExams, outputStream); } } diff --git a/src/main/resources/templates/fragments/exam-file.html b/src/main/resources/templates/fragments/exam-file.html index 72efd16cd..1810deb70 100644 --- a/src/main/resources/templates/fragments/exam-file.html +++ b/src/main/resources/templates/fragments/exam-file.html @@ -2,7 +2,7 @@
- +
\ No newline at end of file diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html index c393421f8..22b812de9 100644 --- a/src/main/resources/templates/search.html +++ b/src/main/resources/templates/search.html @@ -13,7 +13,7 @@

Suche nach einer Prüfung...

diff --git a/src/test/java/ch/puzzle/eft/ExamFeedbackToolApplicationTests.java b/src/test/java/ch/puzzle/eft/ExamFeedbackToolApplicationTests.java index 30dabf776..4e27ce59e 100644 --- a/src/test/java/ch/puzzle/eft/ExamFeedbackToolApplicationTests.java +++ b/src/test/java/ch/puzzle/eft/ExamFeedbackToolApplicationTests.java @@ -1,8 +1,8 @@ package ch.puzzle.eft; -import ch.puzzle.eft.controller.ExamDownloadController; +import ch.puzzle.eft.controller.ExamController; import ch.puzzle.eft.controller.SiteController; -import ch.puzzle.eft.service.ExamFileService; +import ch.puzzle.eft.service.ExamService; import ch.puzzle.eft.service.ValidationService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -23,11 +23,11 @@ void contextLoads(ApplicationContext context) { void checkControllers(ApplicationContext context) { assertThat(context.getBean(SiteController.class)).isNotNull(); - assertThat(context.getBean(ExamDownloadController.class)).isNotNull(); + assertThat(context.getBean(ExamController.class)).isNotNull(); } void checkServices(ApplicationContext context) { assertThat(context.getBean(ValidationService.class)).isNotNull(); - assertThat(context.getBean(ExamFileService.class)).isNotNull(); + assertThat(context.getBean(ExamService.class)).isNotNull(); } } diff --git a/src/test/java/ch/puzzle/eft/controller/ExamDownloadControllerTest.java b/src/test/java/ch/puzzle/eft/controller/ExamControllerTest.java similarity index 88% rename from src/test/java/ch/puzzle/eft/controller/ExamDownloadControllerTest.java rename to src/test/java/ch/puzzle/eft/controller/ExamControllerTest.java index de49c606c..cd206df95 100644 --- a/src/test/java/ch/puzzle/eft/controller/ExamDownloadControllerTest.java +++ b/src/test/java/ch/puzzle/eft/controller/ExamControllerTest.java @@ -1,6 +1,6 @@ package ch.puzzle.eft.controller; -import ch.puzzle.eft.service.ExamFileService; +import ch.puzzle.eft.service.ExamService; import ch.puzzle.eft.test.MockServletOutputStream; import jakarta.servlet.ServletOutputStream; import org.junit.jupiter.api.Test; @@ -8,7 +8,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @@ -23,15 +22,15 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -@WebMvcTest(ExamDownloadController.class) +@WebMvcTest(ExamController.class) @WithMockUser -class ExamDownloadControllerTest { +class ExamControllerTest { MockServletOutputStream outputStream = new MockServletOutputStream(); @Autowired private MockMvc mockMvc; @MockBean - private ExamFileService examFileService; + private ExamService examFileService; @Test void downloadZipShouldReturnZip() throws Exception { @@ -39,7 +38,7 @@ void downloadZipShouldReturnZip() throws Exception { doNothing().when(examFileService) .convertSelectedFilesToZip(examNumber, outputStream); - mockMvc.perform(get("/download-zip/{examNumber}", examNumber)) + mockMvc.perform(get("/exams/download-zip/{examNumber}", examNumber)) .andExpect(status().isOk()) .andExpect(header().string("Content-Disposition", "attachment; filename=11000.zip")); @@ -50,7 +49,7 @@ void downloadZipShouldReturnZip() throws Exception { void shouldDownloadFileAccordingToSubjectAndFileName() throws Exception { File file = new File("static/Privatrecht/11001_22223333.pdf"); when(examFileService.getFileToDownload("Privatrecht", "11001_22223333.pdf")).thenReturn(file); - this.mockMvc.perform(get("/download/Privatrecht/11001_22223333.pdf")) + this.mockMvc.perform(get("/exams/download/Privatrecht/11001_22223333.pdf")) .andExpect(status().isOk()) .andExpect(content().bytes(Files.readAllBytes(file.toPath()))); } @@ -61,7 +60,7 @@ void shouldThrowExceptionIfFileNotFound() throws Exception { new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Keine Unterordner im Pfad %s gefunden", "Privatrecht"))); - ResultActions notFoundResult = this.mockMvc.perform(get("/download/Privatrecht/11000_22223333.pdf")); + ResultActions notFoundResult = this.mockMvc.perform(get("/exams/download/Privatrecht/11000_22223333.pdf")); assertEquals(HttpStatus.NOT_FOUND.value(), notFoundResult.andReturn() .getResponse() diff --git a/src/test/java/ch/puzzle/eft/controller/SiteControllerTest.java b/src/test/java/ch/puzzle/eft/controller/SiteControllerTest.java index 6bd6edc29..080071277 100644 --- a/src/test/java/ch/puzzle/eft/controller/SiteControllerTest.java +++ b/src/test/java/ch/puzzle/eft/controller/SiteControllerTest.java @@ -1,7 +1,7 @@ package ch.puzzle.eft.controller; -import ch.puzzle.eft.model.ExamFileModel; -import ch.puzzle.eft.service.ExamFileService; +import ch.puzzle.eft.model.ExamModel; +import ch.puzzle.eft.service.ExamService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -29,7 +29,7 @@ class SiteControllerTest { private MockMvc mockMvc; @MockBean - private ExamFileService examFileService; + private ExamService examFileService; @Test void defaultRouteShouldReturnIndexPage() throws Exception { @@ -69,8 +69,8 @@ void shouldNotAcceptMoreThan5Digits() throws Exception { @Test void shouldAcceptValidString() throws Exception { - when(examFileService.getMatchingExams("11000", "11112222")).thenReturn(List.of(new ExamFileModel(new File( - "./Privatrecht/11000_11112222.pdf")))); + when(examFileService.getMatchingExams("11000", "11112222")).thenReturn(List.of(new ExamModel(new File( + "./Privatrecht/11000_11112222.pdf")))); this.mockMvc.perform(post("/search").with(csrf()) .contentType(MediaType.APPLICATION_JSON) .param("examNumber", "11000")) diff --git a/src/test/java/ch/puzzle/eft/service/ExamFileServiceTest.java b/src/test/java/ch/puzzle/eft/service/ExamServiceTest.java similarity index 92% rename from src/test/java/ch/puzzle/eft/service/ExamFileServiceTest.java rename to src/test/java/ch/puzzle/eft/service/ExamServiceTest.java index 34d37594e..102c8bb0e 100644 --- a/src/test/java/ch/puzzle/eft/service/ExamFileServiceTest.java +++ b/src/test/java/ch/puzzle/eft/service/ExamServiceTest.java @@ -1,6 +1,6 @@ package ch.puzzle.eft.service; -import ch.puzzle.eft.model.ExamFileModel; +import ch.puzzle.eft.model.ExamModel; import ch.puzzle.eft.test.MockServletOutputStream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -24,15 +24,15 @@ import static org.mockito.Mockito.*; @SpringBootTest -class ExamFileServiceTest { +class ExamServiceTest { @Spy - ExamFileService examFileService; + ExamService examFileService; MockServletOutputStream mockOutputStream = new MockServletOutputStream(); @Autowired - public ExamFileServiceTest(ExamFileService examFileService) { + public ExamServiceTest(ExamService examFileService) { this.examFileService = examFileService; } @@ -87,14 +87,14 @@ void shouldReturnMatchingExamNamesAndAmount() { List expectedFileNames = filesToCheck.stream() .map(p -> Paths.get(p) .toFile()) - .map(ExamFileModel::new) - .map(ExamFileModel::getFileName) + .map(ExamModel::new) + .map(ExamModel::getFileName) .toList(); - List result = examFileService.getMatchingExams("11001", "22223333"); + List result = examFileService.getMatchingExams("11001", "22223333"); List resultFileNames = result.stream() - .map(ExamFileModel::getFileName) + .map(ExamModel::getFileName) .toList(); assertEquals(resultFileNames, expectedFileNames); @@ -133,8 +133,8 @@ void shouldReturnFileToDownload() { @Test void shouldConvertFilesToZip() throws IOException { - ExamFileModel fileModel1 = mock(ExamFileModel.class); - ExamFileModel fileModel2 = mock(ExamFileModel.class); + ExamModel fileModel1 = mock(ExamModel.class); + ExamModel fileModel2 = mock(ExamModel.class); File tempFile1 = File.createTempFile("testFile1", ".pdf"); Files.write(tempFile1.toPath(), "Test Content 1".getBytes()); @@ -148,7 +148,7 @@ void shouldConvertFilesToZip() throws IOException { when(fileModel2.getSubjectName()).thenReturn("Strafrecht"); when(fileModel2.getFile()).thenReturn(tempFile2); - List examFileList = Arrays.asList(fileModel1, fileModel2); + List examFileList = Arrays.asList(fileModel1, fileModel2); examFileService.convertFilesToZip(examFileList, mockOutputStream);