Skip to content

Commit

Permalink
tests: user controller
Browse files Browse the repository at this point in the history
Signed-off-by: MatheusVict <[email protected]>
  • Loading branch information
MatheusVict committed Feb 27, 2024
1 parent ccb1368 commit 532d7c5
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import jakarta.validation.Valid;
import org.apache.catalina.connector.Response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -22,7 +23,7 @@ public UserController(UserService userService) {
@PostMapping
public ResponseEntity<?> createUser(@Valid @RequestBody UserDTO user) {
userService.save(user.toDomain());
return ResponseEntity.status(Response.SC_CREATED).build();
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping("/{id}")
Expand All @@ -40,12 +41,12 @@ public ResponseEntity<UserResponse> findByRegistration(@PathVariable String reg)
@PutMapping("/{id}")
public ResponseEntity<?> updateUser(@PathVariable int id, @RequestBody UserDTO user) {
userService.update(id, user.toDomain());
return ResponseEntity.status(Response.SC_NO_CONTENT).build();
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@DeleteMapping("/{id}")
public ResponseEntity<?> deleteUser(@PathVariable int id) {
userService.delete(id);
return ResponseEntity.status(Response.SC_NO_CONTENT).build();
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserServiceImplTest {

@BeforeEach
void setUp() {
User userValid = UserMocks.returnValidUser();
User userValid = UserMocks.returnValidUserEntity();

lenient().when(userRepository.findById(ArgumentMatchers.anyInt())).thenReturn(Optional.of(userValid));
lenient().when(userRepository.findByRegistration(ArgumentMatchers.anyString())).thenReturn(Optional.of(userValid));
Expand Down Expand Up @@ -67,7 +67,7 @@ void should_Delete_User_Successfully() {
@Test
@DisplayName("Should Find User By Registration Successfully")
void should_findUserByRegistration_Successfully() {
User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();
User userFound = userService.findByRegistration(expectedUser.getRegistration());

assertEquals(expectedUser.getRegistration(), userFound.getRegistration());
Expand All @@ -78,7 +78,7 @@ void should_findUserByRegistration_Successfully() {
@Test
@DisplayName("Should Find User By Email Successfully")
void should_findUserByEmail_Successfully() {
User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();
User userFound = userService.findByEmail(expectedUser.getContact().getEmail());

assertEquals(expectedUser.getContact().getEmail(), userFound.getContact().getEmail());
Expand All @@ -89,7 +89,7 @@ void should_findUserByEmail_Successfully() {
@Test
@DisplayName("Should Find User By Id Successfully")
void should_findUserById_Successfully() {
User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();
User userFound = userService.findById(1);

assertEquals(expectedUser.getRegistration(), userFound.getRegistration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class JpaUserRepositoryAdapterTest {

@BeforeEach
void setUp() {
User userValid = UserMocks.returnValidUser();
User userValid = UserMocks.returnValidUserEntity();

lenient().when(userRepository.findById(ArgumentMatchers.anyInt())).thenReturn(Optional.of(userValid));
lenient().when(userRepository.findByRegistration(ArgumentMatchers.anyString())).thenReturn(Optional.of(userValid));
Expand All @@ -56,7 +56,7 @@ void should_Save_UserSuccessfully() {
@Test
@DisplayName("Should Find User By Id Successfully")
void should_findUserById_Successfully() {
User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();

User user = jpaUserRepositoryAdapter.findById(1).orElse(new User());

Expand All @@ -83,7 +83,7 @@ void should_Delete_A_User_Successfully() {
@Test
@DisplayName("Should Find User By Registration Successfully")
void should_findUserByRegistration_Successfully() {
User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();
String expectedRegistration = expectedUser.getRegistration();
User userFound = jpaUserRepositoryAdapter.findByRegistration(expectedRegistration).orElse(new User());

Expand All @@ -97,7 +97,7 @@ void should_findUserByRegistration_Successfully() {
@DisplayName("Should Find User By Email Successfully")
void should_findUserByEmail_Successfully() {

User expectedUser = UserMocks.returnValidUser();
User expectedUser = UserMocks.returnValidUserEntity();
String expectedEmail = expectedUser.getContact().getEmail();
User userFound = jpaUserRepositoryAdapter.findByEmail(expectedEmail).orElse(new User());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.institutosemprealerta.semprealerta.infrastructure.controllers;

import com.institutosemprealerta.semprealerta.application.service.UserService;
import com.institutosemprealerta.semprealerta.domain.model.UserDTO;
import com.institutosemprealerta.semprealerta.domain.ports.out.responses.UserResponse;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.User;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.mocks.UserMocks;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Objects;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("User Controller")
class UserControllerTest {

@InjectMocks
private UserController userController;

@Mock
private UserService userService;

@BeforeEach
void setUp() {
User validUser = UserMocks.returnValidUserEntity();

lenient().doNothing().when(userService).save(ArgumentMatchers.any(User.class));
lenient().when(userService.findById(ArgumentMatchers.anyInt())).thenReturn(validUser);
lenient().when(userService.findByRegistration(ArgumentMatchers.anyString())).thenReturn(validUser);
lenient().doNothing().when(userService).update(ArgumentMatchers.anyInt(), ArgumentMatchers.any(User.class));
lenient().doNothing().when(userService).delete(ArgumentMatchers.anyInt());

}

@AfterEach
void tearDown() {
reset(userService);
}

@Test
@DisplayName("Should create user successfully")
void should_CreateUser_Successfully() {
UserDTO userDTO = UserMocks.returnValidUserDTO();

ResponseEntity<?> response = userController.createUser(userDTO);

assertNotNull(response);
assertTrue(response.getStatusCode().is2xxSuccessful());
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
assertFalse(response.hasBody());
}

@Test
@DisplayName("Should find user by id successfully")
void should_FindUserById_Successfully() {
ResponseEntity<UserResponse> response = userController.findById(1);
User expectedUser = UserMocks.returnValidUserEntity();

assertTrue(response.getStatusCode().is2xxSuccessful());
assertTrue(response.hasBody());
assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
assertEquals(expectedUser.getName(), Objects.requireNonNull(response.getBody()).name());
}

@Test
@DisplayName("Should find user by registration successfully")
void should_findByUserRegistration_Successfully() {
User expectedUser = UserMocks.returnValidUserEntity();
String expectedRegistration = expectedUser.getRegistration();

ResponseEntity<UserResponse> response = userController.findByRegistration(expectedRegistration);

assertTrue(response.getStatusCode().is2xxSuccessful());
assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
assertTrue(response.hasBody());
assertEquals(expectedUser.getName(), Objects.requireNonNull(response.getBody()).name());
}

@Test
@DisplayName("Should update user successfully")
void should_updateUser_Successfully() {
UserDTO userDTO = UserMocks.returnValidUserDTO();
ResponseEntity<?> response = userController.updateUser(1, userDTO);

assertNotNull(response);
assertTrue(response.getStatusCode().is2xxSuccessful());
assertEquals(HttpStatus.NO_CONTENT.value(), response.getStatusCode().value());
assertFalse(response.hasBody());
}

@Test
@DisplayName("Should delete user successfully")
void should_deleteUser_Successfully() {
ResponseEntity<?> response = userController.deleteUser(1);

assertNotNull(response);
assertTrue(response.getStatusCode().is2xxSuccessful());
assertEquals(HttpStatus.NO_CONTENT.value(), response.getStatusCode().value());
assertFalse(response.hasBody());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.institutosemprealerta.semprealerta.infrastructure.entity.user.mocks;

import com.institutosemprealerta.semprealerta.domain.model.UserDTO;
import com.institutosemprealerta.semprealerta.infrastructure.entity.user.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class UserMocks {
public static User returnValidUser() {
public static User returnValidUserEntity() {
User user = UserEntityFactory.INSTANCE.newUser();

LocalDate birthDate = LocalDate.of(1990, 1, 1);
Expand All @@ -26,13 +27,13 @@ public static User returnValidUser() {
}

public static User returnValidUserToCreate() {
User user = returnValidUser();
User user = returnValidUserEntity();
user.setId(null);
return user;
}

public static User returnValidUserToUpdate() {
User user = returnValidUser();
User user = returnValidUserEntity();
user.setRegistration("654321");
return user;
}
Expand All @@ -44,4 +45,22 @@ public static Contact returnValidContact() {
public static Address returnValidAddress() {
return new Address("Street", "123", "NY", "123546");
}


public static UserDTO returnValidUserDTO() {
User user = returnValidUserEntity();
return new UserDTO(
user.getName(),
user.getContact().getEmail(),
user.getPassword(),
user.getContact().getPhone(),
user.getGender(),
user.getBirthDate(),
user.getRoles(),
user.getAddress().getStreet(),
user.getAddress().getNumber(),
user.getAddress().getCity(),
user.getAddress().getZipCode()
);
}
}

0 comments on commit 532d7c5

Please sign in to comment.