Skip to content
Snippets Groups Projects
Commit 4126a606 authored by Jan Pokorný's avatar Jan Pokorný :lifter_tone2: Committed by Martin Gargalovič
Browse files

Creating certificate tests

parent 31a50751
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
...@@ -7,6 +7,7 @@ import lombok.Getter; ...@@ -7,6 +7,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.fuseri.model.dto.common.DomainObjectDto; import org.fuseri.model.dto.common.DomainObjectDto;
import org.fuseri.model.dto.course.CourseDto; import org.fuseri.model.dto.course.CourseDto;
import org.fuseri.model.dto.user.UserDto;
import java.time.Instant; import java.time.Instant;
...@@ -20,7 +21,7 @@ import java.time.Instant; ...@@ -20,7 +21,7 @@ import java.time.Instant;
public class CertificateDto extends DomainObjectDto { public class CertificateDto extends DomainObjectDto {
@NotBlank @NotBlank
@NotNull @NotNull
private String user; private UserDto user;
@NotBlank @NotBlank
@NotNull @NotNull
private Instant generatedAt; private Instant generatedAt;
...@@ -30,4 +31,8 @@ public class CertificateDto extends DomainObjectDto { ...@@ -30,4 +31,8 @@ public class CertificateDto extends DomainObjectDto {
@NotNull @NotNull
@Valid @Valid
private CertificateFileDto certificateFile; private CertificateFileDto certificateFile;
public CertificateDto() {
setId("0");
}
} }
package org.fuseri.modulecertificate.service; package org.fuseri.modulecertificate.service;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.PositiveOrZero;
import org.fuseri.model.dto.certificate.CertificateCreateDto; import org.fuseri.model.dto.certificate.CertificateCreateDto;
import org.fuseri.model.dto.certificate.CertificateDto; import org.fuseri.model.dto.certificate.CertificateDto;
import org.fuseri.model.dto.common.Result;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.fuseri.model.dto.common.Result;
import java.util.List; import java.util.List;
...@@ -18,6 +17,7 @@ import java.util.List; ...@@ -18,6 +17,7 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/certificates") @RequestMapping("/certificates")
public class CertificateController { public class CertificateController {
@Autowired @Autowired
public CertificateController() { public CertificateController() {
} }
...@@ -59,7 +59,7 @@ public class CertificateController { ...@@ -59,7 +59,7 @@ public class CertificateController {
/** /**
* Retrieves previously generated certificate ID. * 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. * @param courseId ID of course to retrieve certificates for.
* @return List of CertificateDto objects with previously generated certificates * @return List of CertificateDto objects with previously generated certificates
* for specified User. * for specified User.
...@@ -69,16 +69,6 @@ public class CertificateController { ...@@ -69,16 +69,6 @@ public class CertificateController {
return "0"; 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. * Retrieves previously generated certificate ID.
* *
...@@ -95,7 +85,7 @@ public class CertificateController { ...@@ -95,7 +85,7 @@ public class CertificateController {
* @return a Result object containing a list of CertificateDto objects and pagination information * @return a Result object containing a list of CertificateDto objects and pagination information
*/ */
@GetMapping("/findAll") @GetMapping("/findAll")
public Result<CertificateDto> findAllCertificates(@PositiveOrZero @RequestParam int page) { public Result<CertificateDto> findAllCertificates(@RequestParam int page) {
return new Result<>(); return new Result<>();
} }
} }
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);
}
}
}
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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment