From fabeceece72c6c32f01345ee417f4dfab4bad1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <xpokorn8@fi.muni.cz> Date: Sun, 9 Apr 2023 17:27:48 +0200 Subject: [PATCH] moddified CertificateControllerTests --- .../CertificateControllerTests.java | 99 ++++++++++++------- 1 file changed, 62 insertions(+), 37 deletions(-) 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 index 20a1ec68..23f381d2 100644 --- a/application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java +++ b/application/module-certificate/src/test/java/org/fuseri/modulecertificate/CertificateControllerTests.java @@ -2,21 +2,27 @@ 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.certificate.CertificateSimpleDto; 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.springdoc.core.converters.models.Pageable; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; 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.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import java.time.Instant; import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -27,23 +33,42 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @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); + private final CertificateCreateDto certificateCreateDto = new CertificateCreateDto(USER, COURSE); + private final CertificateSimpleDto certificateDto = new CertificateSimpleDto(0L, USER.getId(), + Instant.now(), COURSE.getId(), "", ""); + @Autowired + private MockMvc mockMvc; + @MockBean + private CertificateController certificateController; + + private static String asJsonString(final Object obj) { + try { + return new ObjectMapper().writeValueAsString(obj); + } catch (Exception e) { + throw new RuntimeException(e); + } + } @Test void generateCertificate() throws Exception { + Mockito.when(certificateController.generate(ArgumentMatchers.any(CertificateCreateDto.class))) + .thenReturn(certificateDto); + mockMvc.perform(post("/certificates/generate") - .content(asJsonString(new CertificateCreateDto(USER, COURSE))) + .content(asJsonString(certificateCreateDto)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(certificateDto.getId())) + .andExpect(jsonPath("$.userId").value(certificateDto.getUserId())) + .andExpect(jsonPath("$.generatedAt").value(certificateDto.getGeneratedAt().toString())) + .andExpect(jsonPath("$.courseId").value(certificateDto.getCourseId())) + .andExpect(jsonPath("$.certificateFile").value(certificateDto.getCertificateFile())) + .andExpect(jsonPath("$.certificateFileName").value(certificateDto.getCertificateFileName())); + } @Test @@ -71,16 +96,11 @@ class CertificateControllerTests { @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(); + Mockito.when(certificateController.find(ArgumentMatchers.anyLong())).thenReturn(certificateDto); - Long id = objectMapper.readValue(response, CertificateDto.class).getId(); - - mockMvc.perform(get("/certificates/find").param("id", id.toString())) + mockMvc.perform(get("/certificates/find").param("id", certificateDto.getId().toString())) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id").value(id)); + .andExpect(jsonPath("$.id").value(certificateDto.getId())); } @Test @@ -91,6 +111,8 @@ class CertificateControllerTests { @Test void findCertificatesForUser() throws Exception { + Mockito.when(certificateController.findForUser(ArgumentMatchers.anyLong())).thenReturn(List.of(certificateDto)); + mockMvc.perform(get("/certificates/findForUser").param("userId", "0")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) @@ -105,11 +127,16 @@ class CertificateControllerTests { @Test void findCertificateIdForUserAndCourse() throws Exception { + Mockito.when(certificateController.findForUserAndCourse(ArgumentMatchers.anyLong(), + ArgumentMatchers.anyLong())) + .thenReturn(List.of(certificateDto)); + mockMvc.perform(get("/certificates/findForUserAndCourse") .param("userId", "0") .param("courseId", "0")) .andExpect(status().isOk()) - .andExpect(content().string("[]")); + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$").isNotEmpty()); } @Test @@ -134,8 +161,10 @@ class CertificateControllerTests { @Test void deleteCertificate() throws Exception { + Mockito.doNothing().when(certificateController).delete(ArgumentMatchers.anyLong()); + mockMvc.perform(delete("/certificates/delete") - .param("id", "0")) + .param("id", String.valueOf(0L))) .andExpect(status().isOk()); } @@ -145,31 +174,27 @@ class CertificateControllerTests { .andExpect(status().is4xxClientError()); } - @Test void findAllCertificates() throws Exception { - mockMvc.perform(post("/certificates/generate") - .content(asJsonString(new CertificateCreateDto(USER, COURSE))) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + Mockito.when(certificateController.findAllCertificates(ArgumentMatchers.any(Pageable.class))) + .thenReturn(Page.empty(PageRequest.of(0, 1))); mockMvc.perform(get("/certificates/findAll") - .content("{ \"page\": 0, \"size\": 1, \"sort\": []}") - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .param("page", "0") + .param("size", "10")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.content").isEmpty()); } @Test void findAllCertificatesWithoutParam() throws Exception { - mockMvc.perform(get("/certificates/findAll")) - .andExpect(status().is4xxClientError()); - } + Mockito.when(certificateController.findAllCertificates(ArgumentMatchers.any(Pageable.class))) + .thenReturn(Page.empty(PageRequest.of(0, 1))); - private static String asJsonString(final Object obj) { - try { - return new ObjectMapper().writeValueAsString(obj); - } catch (Exception e) { - throw new RuntimeException(e); - } + mockMvc.perform(get("/certificates/findAll")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.content").isEmpty()); } } -- GitLab