add integration test to create new user controller with conflict
This commit is contained in:
parent
13ad4c8d45
commit
e852dbea7b
3 changed files with 54 additions and 34 deletions
|
|
@ -11,5 +11,5 @@ public interface UserRepository extends MongoRepository<User, String> {
|
|||
|
||||
Optional<User> findByEmail(final String email);
|
||||
|
||||
void deleteByEmail(final String validEmail);
|
||||
void deleteByEmail(final String VALID_EMAIL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,23 @@
|
|||
package br.com.rayankonecny.userserviceapi.controller.impl;
|
||||
|
||||
import static br.com.rayankonecny.userserviceapi.creator.CreatorUtils.generateMock;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.http.HttpStatus.CONFLICT;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
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.HttpStatus;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import br.com.rayankonecny.userserviceapi.entity.User;
|
||||
import br.com.rayankonecny.userserviceapi.repository.UserRepository;
|
||||
import models.requests.CreateUserRequest;
|
||||
|
|
@ -29,6 +27,10 @@ import models.requests.CreateUserRequest;
|
|||
@ActiveProfiles("test")
|
||||
public class UserControllerImpTest {
|
||||
|
||||
private static final String VALID_EMAIL = "k42kak2ok@mail.com";
|
||||
|
||||
private static final String BASE_URI = "/api/users";
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
|
|
@ -40,8 +42,9 @@ public class UserControllerImpTest {
|
|||
final var entity = generateMock(User.class);
|
||||
final var userId = userRepository.save(entity).getId();
|
||||
|
||||
mockMvc.perform(get("/api/users/{id}", userId)).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(userId))
|
||||
.andExpect(jsonPath("$.name").value(entity.getName())).andExpect(jsonPath("$.email").value(entity.getEmail()))
|
||||
mockMvc.perform(get(BASE_URI + "/{id}", userId)).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(userId)).andExpect(jsonPath("$.name").value(entity.getName()))
|
||||
.andExpect(jsonPath("$.email").value(entity.getEmail()))
|
||||
.andExpect(jsonPath("$.password").value(entity.getPassword())).andExpect(jsonPath("$.profiles").isArray());
|
||||
|
||||
userRepository.deleteById(userId);
|
||||
|
|
@ -49,11 +52,12 @@ public class UserControllerImpTest {
|
|||
|
||||
@Test
|
||||
void testFindByIdWithNotFoundException() throws Exception {
|
||||
mockMvc.perform(get("/api/users/{id}", "123")).andExpect(status().isNotFound())
|
||||
mockMvc.perform(get(BASE_URI + "/{id}", "123"))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.message").value("Object not found! Id: 123, Type: UserResponse"))
|
||||
.andExpect(jsonPath("$.error").value(HttpStatus.NOT_FOUND.getReasonPhrase()))
|
||||
.andExpect(jsonPath("$.path").value("/api/users/123"))
|
||||
.andExpect(jsonPath("$.status").value(HttpStatus.NOT_FOUND.value()))
|
||||
.andExpect(jsonPath("$.error").value(NOT_FOUND.getReasonPhrase()))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI + "/123"))
|
||||
.andExpect(jsonPath("$.status").value(NOT_FOUND.value()))
|
||||
.andExpect(jsonPath("$.timestamp").isNotEmpty());
|
||||
}
|
||||
|
||||
|
|
@ -64,28 +68,24 @@ public class UserControllerImpTest {
|
|||
|
||||
userRepository.saveAll(List.of(entity1, entity2));
|
||||
|
||||
mockMvc.perform(get("/api/users"))
|
||||
.andExpect(status().isOk())
|
||||
mockMvc.perform(get(BASE_URI)).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$").isArray())
|
||||
.andExpect(jsonPath("$[0]").isNotEmpty())
|
||||
.andExpect(jsonPath("$[1]").isNotEmpty())
|
||||
.andExpect(jsonPath("$[0].profiles").isArray());
|
||||
|
||||
userRepository.deleteAll(List.of(entity1,entity2));
|
||||
userRepository.deleteAll(List.of(entity1, entity2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveUserWithSuccess() throws Exception {
|
||||
final var validEmail = "k42kak2ok@mail.com";
|
||||
final var request = generateMock(CreateUserRequest.class).withEmail(validEmail);
|
||||
|
||||
mockMvc.perform(
|
||||
post("/api/users")
|
||||
.contentType(APPLICATION_JSON)
|
||||
.content(toJson(request))
|
||||
).andExpect(status().isCreated());
|
||||
final var request = generateMock(CreateUserRequest.class).withEmail(VALID_EMAIL);
|
||||
|
||||
userRepository.deleteByEmail(validEmail);
|
||||
mockMvc.perform(post(BASE_URI).contentType(APPLICATION_JSON).content(toJson(request)))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
userRepository.deleteByEmail(VALID_EMAIL);
|
||||
}
|
||||
|
||||
private String toJson(final Object object) throws Exception {
|
||||
|
|
@ -96,4 +96,24 @@ public class UserControllerImpTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveUserWithConflict() throws Exception {
|
||||
|
||||
final var entity = generateMock(User.class).withEmail(VALID_EMAIL);
|
||||
|
||||
userRepository.save(entity);
|
||||
|
||||
final var request = generateMock(CreateUserRequest.class).withEmail(VALID_EMAIL);
|
||||
|
||||
mockMvc.perform(post(BASE_URI).contentType(APPLICATION_JSON).content(toJson(request)))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.message").value("Email [" + VALID_EMAIL + "] already exists"))
|
||||
.andExpect(jsonPath("$.error").value(CONFLICT.getReasonPhrase()))
|
||||
.andExpect(jsonPath("$.path").value(BASE_URI))
|
||||
.andExpect(jsonPath("$.status").value(CONFLICT.value()))
|
||||
.andExpect(jsonPath("$.timestamp").isNotEmpty());
|
||||
|
||||
userRepository.deleteById(entity.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ public class UserServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void whenCallSaveWithInvalidEmailThenThrowDataIntegrityViolationException() {
|
||||
void whenCallSaveWithInVALID_EMAILThenThrowDataIntegrityViolationException() {
|
||||
|
||||
final var request = generateMock(CreateUserRequest.class);
|
||||
final var entity = generateMock(User.class);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue