From 4126a6062f06822a5be181cbbc94245153e95f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <xpokorn8@fi.muni.cz> Date: Sun, 26 Mar 2023 14:46:02 +0200 Subject: [PATCH] Creating certificate tests --- .../model/dto/certificate/CertificateDto.java | 7 +- .../service/CertificateController.java | 18 +- .../CertificateControllerTests.java | 165 ++++++++++++++++++ ...CertificateCertificateControllerTests.java | 66 ------- 4 files changed, 175 insertions(+), 81 deletions(-) create mode 100644 application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java delete mode 100644 application/module-certificate/src/test/java/org/fuseri/modulecertificate/ModuleCertificateCertificateControllerTests.java diff --git a/application/model/src/main/java/org/fuseri/model/dto/certificate/CertificateDto.java b/application/model/src/main/java/org/fuseri/model/dto/certificate/CertificateDto.java index 4983e6ab..52597af3 100644 --- a/application/model/src/main/java/org/fuseri/model/dto/certificate/CertificateDto.java +++ b/application/model/src/main/java/org/fuseri/model/dto/certificate/CertificateDto.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.Setter; import org.fuseri.model.dto.common.DomainObjectDto; import org.fuseri.model.dto.course.CourseDto; +import org.fuseri.model.dto.user.UserDto; import java.time.Instant; @@ -20,7 +21,7 @@ import java.time.Instant; public class CertificateDto extends DomainObjectDto { @NotBlank @NotNull - private String user; + private UserDto user; @NotBlank @NotNull private Instant generatedAt; @@ -30,4 +31,8 @@ public class CertificateDto extends DomainObjectDto { @NotNull @Valid private CertificateFileDto certificateFile; + + public CertificateDto() { + setId("0"); + } } diff --git a/application/module-certificate/src/main/java/org/fuseri/modulecertificate/service/CertificateController.java b/application/module-certificate/src/main/java/org/fuseri/modulecertificate/service/CertificateController.java index 4dbb93bc..bfb64473 100644 --- a/application/module-certificate/src/main/java/org/fuseri/modulecertificate/service/CertificateController.java +++ b/application/module-certificate/src/main/java/org/fuseri/modulecertificate/service/CertificateController.java @@ -1,12 +1,11 @@ package org.fuseri.modulecertificate.service; import jakarta.validation.Valid; -import jakarta.validation.constraints.PositiveOrZero; import org.fuseri.model.dto.certificate.CertificateCreateDto; import org.fuseri.model.dto.certificate.CertificateDto; +import org.fuseri.model.dto.common.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import org.fuseri.model.dto.common.Result; import java.util.List; @@ -18,6 +17,7 @@ import java.util.List; @RestController @RequestMapping("/certificates") public class CertificateController { + @Autowired public CertificateController() { } @@ -59,7 +59,7 @@ public class CertificateController { /** * Retrieves previously generated certificate ID. * - * @param userId ID of user to retrieve certificates for. + * @param userId ID of user to retrieve certificates for. * @param courseId ID of course to retrieve certificates for. * @return List of CertificateDto objects with previously generated certificates * for specified User. @@ -69,16 +69,6 @@ public class CertificateController { return "0"; } - /** - * Retrieves previously generated certificate ID. - * - * @param certificateDto certificate Dto with certificate ID of certificate that should be updated - * and updated certificate data. - */ - @PutMapping("/update") - public void update(@Valid @RequestBody CertificateDto certificateDto) { - } - /** * Retrieves previously generated certificate ID. * @@ -95,7 +85,7 @@ public class CertificateController { * @return a Result object containing a list of CertificateDto objects and pagination information */ @GetMapping("/findAll") - public Result<CertificateDto> findAllCertificates(@PositiveOrZero @RequestParam int page) { + public Result<CertificateDto> findAllCertificates(@RequestParam int page) { return new Result<>(); } } diff --git a/application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java b/application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java new file mode 100644 index 00000000..08ba4e44 --- /dev/null +++ b/application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java @@ -0,0 +1,165 @@ +package org.fuseri.modulecertificate; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.fuseri.model.dto.certificate.CertificateCreateDto; +import org.fuseri.model.dto.certificate.CertificateDto; +import org.fuseri.model.dto.course.CourseDto; +import org.fuseri.model.dto.course.LanguageTypeDto; +import org.fuseri.model.dto.course.ProficiencyLevelDto; +import org.fuseri.model.dto.user.AddressDto; +import org.fuseri.model.dto.user.UserDto; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + + +@SpringBootTest +@AutoConfigureMockMvc +class CertificateControllerTests { + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private MockMvc mockMvc; + + private final UserDto USER = new UserDto("novakovat", + "novakova@gamil.com", "Tereza", "Nováková", new AddressDto()); + private final CourseDto COURSE = new CourseDto("AJ1", 10, + LanguageTypeDto.ENGLISH, ProficiencyLevelDto.A1); + + @Test + void generateCertificate() throws Exception { + mockMvc.perform(post("/certificates/generate") + .content(asJsonString(new CertificateCreateDto(USER, COURSE))) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + void generateCertificateWithNullUser() throws Exception { + mockMvc.perform(post("/certificates/generate") + .content(asJsonString(new CertificateCreateDto(null, COURSE))) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void generateCertificateWithNullCourse() throws Exception { + mockMvc.perform(post("/certificates/generate") + .content(asJsonString(new CertificateCreateDto(USER, null))) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void generateCertificateWithoutParams() throws Exception { + mockMvc.perform(post("/certificates/generate") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void findCertificate() throws Exception { + String response = mockMvc.perform(post("/certificates/generate") + .content(asJsonString(new CertificateCreateDto(USER, COURSE))) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + String id = objectMapper.readValue(response, CertificateDto.class).getId(); + + mockMvc.perform(get("/certificates/find").param("id", id)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(id)); + } + + @Test + void findCertificateWithoutId() throws Exception { + mockMvc.perform(get("/certificates/find")) + .andExpect(status().is4xxClientError()); + } + + @Test + void findCertificatesForUser() throws Exception { + mockMvc.perform(get("/certificates/findForUser").param("userId", "0")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$").isNotEmpty()); + } + + @Test + void findCertificatesWithoutUserId() throws Exception { + mockMvc.perform(get("/certificates/findForUser")) + .andExpect(status().is4xxClientError()); + } + + @Test + void findCertificateIdForUserAndCourse() throws Exception { + mockMvc.perform(get("/certificates/getId") + .param("userId", "0") + .param("courseId", "0")) + .andExpect(status().isOk()) + .andExpect(content().string("0")); + } + + @Test + void findCertificateIdWithoutUserId() throws Exception { + mockMvc.perform(get("/certificates/getId") + .param("courseId", "0")) + .andExpect(status().is4xxClientError()); + } + + @Test + void findCertificateIdWithoutCourseId() throws Exception { + mockMvc.perform(get("/certificates/getId") + .param("userId", "0")) + .andExpect(status().is4xxClientError()); + } + + @Test + void findCertificateIdWithoutParams() throws Exception { + mockMvc.perform(get("/certificates/getId")) + .andExpect(status().is4xxClientError()); + } + + @Test + void deleteCertificate() throws Exception { + mockMvc.perform(delete("/certificates/delete") + .param("id", "0")) + .andExpect(status().isOk()); + } + + @Test + void deleteCertificateWithoutParam() throws Exception { + mockMvc.perform(delete("/certificates/delete")) + .andExpect(status().is4xxClientError()); + } + + + @Test + void findAllCertificates() throws Exception { + mockMvc.perform(get("/certificates/findAll") + .param("page", "1")) + .andExpect(status().isOk()); + } + + @Test + void findAllCertificatesWithoutParam() throws Exception { + mockMvc.perform(get("/certificates/findAll")) + .andExpect(status().is4xxClientError()); + } + + private static String asJsonString(final Object obj) { + try { + return new ObjectMapper().writeValueAsString(obj); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/application/module-certificate/src/test/java/org/fuseri/modulecertificate/ModuleCertificateCertificateControllerTests.java b/application/module-certificate/src/test/java/org/fuseri/modulecertificate/ModuleCertificateCertificateControllerTests.java deleted file mode 100644 index b6bf1c86..00000000 --- a/application/module-certificate/src/test/java/org/fuseri/modulecertificate/ModuleCertificateCertificateControllerTests.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.fuseri.modulecertificate; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.fuseri.model.dto.certificate.CertificateCreateDto; -import org.fuseri.model.dto.certificate.CertificateDto; -import org.fuseri.model.dto.course.CourseDto; -import org.fuseri.model.dto.course.LanguageTypeDto; -import org.fuseri.model.dto.course.ProficiencyLevelDto; -import org.fuseri.model.dto.user.AddressDto; -import org.fuseri.model.dto.user.UserDto; -import org.fuseri.modulecertificate.service.CertificateController; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - - -@SpringBootTest -@AutoConfigureMockMvc -class ModuleCertificateCertificateControllerTests { - - @Autowired - private MockMvc mockMvc; - - @Autowired - ObjectMapper objectMapper; - - @Test - void generateCertificate() throws Exception { - CertificateDto expectedResponse = new CertificateDto(); - String response = mockMvc.perform(post("/certificates/generate") - .content(asJsonString(new CertificateCreateDto( - new UserDto("novakovat","novakova@gamil.com", "Tereza", - "Nováková", new AddressDto()), - new CourseDto("AJ1", 10, LanguageTypeDto.ENGLISH, ProficiencyLevelDto.A1)))) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn().getResponse().getContentAsString(); - CertificateController certificateController = objectMapper.readValue(response, CertificateController.class); - assertThat("response", certificateController.find("0"), is(instanceOf(expectedResponse.getClass()))); - } - - - @Test - void deleteCertificate() throws Exception { - String response = mockMvc.perform(delete("/certificates/delete") - .param("id", "1")) - .andExpect(status().isOk()) - .andReturn().getResponse().getContentAsString(); - } - - public static String asJsonString(final Object obj) { - try { - return new ObjectMapper().writeValueAsString(obj); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} -- GitLab