Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change naming and endpoints #80

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/puzzle/eft/controller/SiteController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,9 +13,9 @@
@Controller
public class SiteController {

ExamFileService examFileService;
ExamService examFileService;

public SiteController(ExamFileService examFileService) {
public SiteController(ExamService examFileService) {
this.examFileService = examFileService;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package ch.puzzle.eft.model;

import ch.puzzle.eft.service.ExamFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand All @@ -44,7 +44,7 @@ public List<File> getAllExamFiles() {
.toList();
}

public List<ExamFileModel> getMatchingExams(String examNumber, String matriculationNumber) {
public List<ExamModel> getMatchingExams(String examNumber, String matriculationNumber) {
if (!validationService.validateExamNumber(examNumber)) {
logger.info("Invalid Exam Number: {}", examNumber);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
Expand All @@ -63,7 +63,7 @@ public List<ExamFileModel> getMatchingExams(String examNumber, String matriculat
examNumber));
}
return matchingFiles.stream()
.map(ExamFileModel::new)
.map(ExamModel::new)
.toList();
}

Expand All @@ -88,9 +88,9 @@ public File getFileToDownload(String subjectName, String filename) {
return examToDownload;
}

public ResponseEntity<Object> convertFilesToZip(List<ExamFileModel> examFileList, ServletOutputStream outputStream) {
public ResponseEntity<Object> convertFilesToZip(List<ExamModel> 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);
Expand All @@ -115,7 +115,7 @@ public ResponseEntity<Object> convertFilesToZip(List<ExamFileModel> examFileList

public void convertSelectedFilesToZip(String examNumber, ServletOutputStream outputStream) {
// TODO: Replace hardcoded marticulationNumber 11112222 with dynamic number after login is implemented
List<ExamFileModel> matchingExams = getMatchingExams(examNumber, "11112222");
List<ExamModel> matchingExams = getMatchingExams(examNumber, "11112222");
convertFilesToZip(matchingExams, outputStream);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/fragments/exam-file.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<body>
<div>
<a th:href="@{|/download/${examFile.getDownloadPath}|}" th:text="${examFile.getSubjectName()}"></a>
<a th:href="@{|/exams/download/${examFile.getDownloadPath}|}" th:text="${examFile.getSubjectName()}"></a>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/main/resources/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1>Suche nach einer Prüfung...</h1>
<div th:replace="~{fragments/exam-file}"></div>
</div>
<div th:if="${examFiles != null && examFiles.size() >= 2}">
<a th:href="@{'/download-zip/' + ${examNumberForm.getExamNumber()}}" class="btn btn-primary">
<a th:href="@{'/exams/download-zip/' + ${examNumberForm.getExamNumber()}}" class="btn btn-primary">
Alle als ZIP herunterladen
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
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;
Expand All @@ -23,23 +22,23 @@
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 {
String examNumber = "11000";
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"));

Expand All @@ -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())));
}
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/ch/puzzle/eft/controller/SiteControllerTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ class SiteControllerTest {
private MockMvc mockMvc;

@MockBean
private ExamFileService examFileService;
private ExamService examFileService;

@Test
void defaultRouteShouldReturnIndexPage() throws Exception {
Expand Down Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -87,14 +87,14 @@ void shouldReturnMatchingExamNamesAndAmount() {
List<String> expectedFileNames = filesToCheck.stream()
.map(p -> Paths.get(p)
.toFile())
.map(ExamFileModel::new)
.map(ExamFileModel::getFileName)
.map(ExamModel::new)
.map(ExamModel::getFileName)
.toList();

List<ExamFileModel> result = examFileService.getMatchingExams("11001", "22223333");
List<ExamModel> result = examFileService.getMatchingExams("11001", "22223333");

List<String> resultFileNames = result.stream()
.map(ExamFileModel::getFileName)
.map(ExamModel::getFileName)
.toList();

assertEquals(resultFileNames, expectedFileNames);
Expand Down Expand Up @@ -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());
Expand All @@ -148,7 +148,7 @@ void shouldConvertFilesToZip() throws IOException {
when(fileModel2.getSubjectName()).thenReturn("Strafrecht");
when(fileModel2.getFile()).thenReturn(tempFile2);

List<ExamFileModel> examFileList = Arrays.asList(fileModel1, fileModel2);
List<ExamModel> examFileList = Arrays.asList(fileModel1, fileModel2);

examFileService.convertFilesToZip(examFileList, mockOutputStream);

Expand Down
Loading