add integration test to create new user controle with sucess
This commit is contained in:
parent
36ec46a2ea
commit
13ad4c8d45
2 changed files with 28 additions and 0 deletions
|
|
@ -10,4 +10,6 @@ import org.springframework.stereotype.Repository;
|
||||||
public interface UserRepository extends MongoRepository<User, String> {
|
public interface UserRepository extends MongoRepository<User, String> {
|
||||||
|
|
||||||
Optional<User> findByEmail(final String email);
|
Optional<User> findByEmail(final String email);
|
||||||
|
|
||||||
|
void deleteByEmail(final String validEmail);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,12 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
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.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.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
@ -18,6 +22,7 @@ import java.util.List;
|
||||||
|
|
||||||
import br.com.rayankonecny.userserviceapi.entity.User;
|
import br.com.rayankonecny.userserviceapi.entity.User;
|
||||||
import br.com.rayankonecny.userserviceapi.repository.UserRepository;
|
import br.com.rayankonecny.userserviceapi.repository.UserRepository;
|
||||||
|
import models.requests.CreateUserRequest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
|
|
@ -67,7 +72,28 @@ public class UserControllerImpTest {
|
||||||
.andExpect(jsonPath("$[0].profiles").isArray());
|
.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());
|
||||||
|
|
||||||
|
userRepository.deleteByEmail(validEmail);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String toJson(final Object object) throws Exception {
|
||||||
|
try {
|
||||||
|
return new ObjectMapper().writeValueAsString(object);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new Exception("Error to convert object to json", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue