Skip to content

Commit

Permalink
feat: VIN-368 - criando o createUser
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderleik committed Mar 21, 2024
1 parent 4949dd1 commit 6aa4d92
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Builder
public class UserInputDTO {

private PersonInputDTO person;
private String personId;
private String enumProfile;
private String email;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void testDeleteUserThrowBadRequestException() {

private UserInputDTO createUserInputDTO() {
return UserInputDTO.builder()
.person(createPersonInputDTO())
.personId("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
.enumProfile(EnumProfile.OENOPHILE.getCode())
.email("user@email.com")
.password("123456")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void testDeleteUserWithInvalidId() throws Exception {

private UserInputDTO createUserInputDTO() {
return UserInputDTO.builder()
.person(createPersonInputDTO())
.personId(createPersonInputDTO().getId())
.enumProfile(EnumProfile.OENOPHILE.getCode())
.email("email@gmail.com")
.password("123456")
Expand All @@ -220,6 +220,7 @@ private UserInputDTO createUserInputDTO() {

private PersonInputDTO createPersonInputDTO() {
return PersonInputDTO.builder()
.id("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
.name("Usuario Teste")
.birthDate(LocalDate.of(1990, 10, 10))
.document("12345678900")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vinhonotas.cadastro.application.converters;

import com.vinhonotas.cadastro.domain.entities.PersonEntity;
import com.vinhonotas.cadastro.domain.entities.UserEntity;
import com.vinhonotas.cadastro.domain.enums.EnumProfile;
import com.vinhonotas.cadastro.infrastructure.PersonRepository;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.UserInputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.outputs.UserOutputDTO;
import com.vinhonotas.cadastro.utils.EnumConverter;
Expand All @@ -17,10 +19,12 @@
public class UserConverter {

private final PersonConverter personConverter;
private final PersonRepository personRepository;

public UserEntity convertToEntity(UserInputDTO userInputDTO) {
PersonEntity personEntity = personRepository.findById(UUID.fromString(userInputDTO.getPersonId())).orElseThrow();
return UserEntity.builder()
.person(personConverter.convertToEntity(userInputDTO.getPerson()))
.person(personEntity)
.enumProfile(EnumConverter.fromString(userInputDTO.getEnumProfile(), EnumProfile.class))
.email(userInputDTO.getEmail())
.password(userInputDTO.getPassword())
Expand All @@ -32,10 +36,10 @@ public UserEntity convertToEntity(UserInputDTO userInputDTO) {
}

public UserEntity converteToEntityUpdate(UserEntity entity, UUID id, UserInputDTO userInputDTO) {
PersonEntity personEntity = personRepository.findById(UUID.fromString(userInputDTO.getPersonId())).orElseThrow();
return UserEntity.builder()
.id(id)
.person(userInputDTO.getPerson() != null ? personConverter.convertToEntity(userInputDTO.getPerson()) :
entity.getPerson())
.person(entity.getPerson() != null ? entity.getPerson() : personEntity)
.enumProfile(userInputDTO.getEnumProfile() != null ? EnumConverter.fromString(userInputDTO
.getEnumProfile(), EnumProfile.class) : entity.getEnumProfile())
.email(userInputDTO.getEmail() != null ? userInputDTO.getEmail() : entity.getEmail())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vinhonotas.cadastro.application.services.impl;

import com.vinhonotas.cadastro.application.converters.PersonConverter;
import com.vinhonotas.cadastro.application.converters.UserConverter;
import com.vinhonotas.cadastro.application.services.UserService;
import com.vinhonotas.cadastro.application.services.exceptions.BadRequestException;
Expand Down Expand Up @@ -28,8 +27,6 @@ public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final UserConverter userConverter;
private final PersonRepository personRepository;
private final PersonConverter personConverter;

@Override
@Transactional(rollbackFor = Exception.class)
public UserEntity create(UserInputDTO userInputDTO) {
Expand All @@ -45,14 +42,13 @@ public UserEntity create(UserInputDTO userInputDTO) {
}

private void existsUser(UserInputDTO userInputDTO) {
log.info("existsUser :: Verificando se o usuário já existe com os dados: {}", userInputDTO.toString());
PersonEntity person = personRepository.findByName(userInputDTO.getPerson().getName());
log.info("Pessoa encontrada: {}", person.toString());
userInputDTO.setPerson(personConverter.convertToInputDTO(person));
log.info("existsUser :: Verificando se o usuário já existe com o dados: {}", userInputDTO.toString());
PersonEntity person = personRepository.findById(UUID.fromString(userInputDTO.getPersonId())).orElseThrow();
log.info("Pessoa encontrada: {}", person);
UserEntity user = userRepository.findByPersonDocument(person.getDocument());

UserEntity user = userRepository.findByPersonDocument(userInputDTO.getPerson().getDocument());
if (Objects.nonNull(user)) {
log.error("existsUser :: Já existe um usuário com os dados: {}", userInputDTO.toString());
log.error("existsUser :: Já existe um usuário com os dados: {}", user);
throw new BadRequestException(MessagesConstants.USER_ALREADY_EXISTS);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Builder
public class UserInputDTO {

private PersonInputDTO person;
private String personId;
private String enumProfile;
private String email;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.vinhonotas.cadastro.domain.entities.PersonEntity;
import com.vinhonotas.cadastro.domain.entities.UserEntity;
import com.vinhonotas.cadastro.domain.enums.EnumProfile;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.PersonInputDTO;
import com.vinhonotas.cadastro.infrastructure.PersonRepository;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.UserInputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.outputs.PersonOutputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.outputs.UserOutputDTO;
Expand All @@ -18,6 +18,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -31,6 +32,8 @@ class UserConverterTest {

@Mock
private PersonConverter personConverter;
@Mock
private PersonRepository personRepository;

private UserInputDTO userInputDTO;
private UserEntity userEntity;
Expand All @@ -46,9 +49,10 @@ void setUp() {
@Test
@DisplayName("Teste de conversão de UserInputDTO para UserEntity")
void testToEntity() {
when(personRepository.findById(UUID.fromString(userInputDTO.getPersonId()))).thenReturn(Optional.ofNullable(createPersonEntity()));

UserEntity user = assertDoesNotThrow(()-> userConverter.convertToEntity(userInputDTO));
assertNotNull(user);
assertEquals(personConverter.convertToEntity(userInputDTO.getPerson()), user.getPerson());
assertEquals(userInputDTO.getEnumProfile(), EnumConverter.toString(user.getEnumProfile()));
assertEquals(userInputDTO.getEmail(), user.getEmail());
assertEquals(userInputDTO.getPassword(), user.getPassword());
Expand All @@ -58,10 +62,10 @@ void testToEntity() {
@DisplayName("Teste de conversão para UserEntityUpdate ")
void testToEntityUpdate() {
userInputDTO.setEmail("update@email.com");
when(personRepository.findById(UUID.fromString(userInputDTO.getPersonId()))).thenReturn(Optional.ofNullable(createPersonEntity()));

UserEntity entity = assertDoesNotThrow(() -> userConverter.converteToEntityUpdate(userEntity, userEntity.getId(), userInputDTO));
assertNotNull(userEntity);
assertEquals(personConverter.convertToEntity(userInputDTO.getPerson()), entity.getPerson());
assertEquals(userInputDTO.getEnumProfile(), EnumConverter.toString(entity.getEnumProfile()));
assertEquals(userInputDTO.getEmail(), entity.getEmail());
assertEquals(userInputDTO.getPassword(), entity.getPassword());
Expand All @@ -71,6 +75,7 @@ void testToEntityUpdate() {
@DisplayName("Teste de conversão de UserEntity para UserOutputDTO")
void testConvertToOutputDTO() {
UserOutputDTO userOutput = assertDoesNotThrow(() -> userConverter.convertToOutputDTO(userEntity));

assertNotNull(userOutput);
assertEquals(userEntity.getId(), userOutput.getId());
assertEquals(personConverter.convertToOutputDTO(userEntity.getPerson()), userOutput.getPerson());
Expand Down Expand Up @@ -116,9 +121,15 @@ private UserEntity createUserEntity() {
.build();
}

private PersonEntity createPersonEntity() {
return PersonEntity.builder()
.id(UUID.fromString("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"))
.build();
}

private UserInputDTO createUserInputDTO() {
return UserInputDTO.builder()
.person(Mockito.mock(PersonInputDTO.class))
.personId("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
.enumProfile(EnumProfile.OENOPHILE.getCode())
.email("user@email.com")
.password("123456")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ void setUp() {
@Test
@DisplayName("Teste de criação de usuário com sucesso")
void testCreateSuccess() {
when(personRepository.findByName(inputDTO.getPerson().getName())).thenReturn(createPerson());
when(userRepository.findByPersonDocument(inputDTO.getPerson().getDocument())).thenReturn(null);
when(personConverter.convertToInputDTO(createPerson())).thenReturn(createPersonInputDTO());
when(personRepository.findById(UUID.fromString(inputDTO.getPersonId()))).thenReturn(Optional.ofNullable(createPerson()));
when(userConverter.convertToEntity(inputDTO)).thenReturn(entity);
when(userRepository.save(entity)).thenReturn(entity);

Expand All @@ -70,9 +68,7 @@ void testCreateSuccess() {
@Test
@DisplayName("Teste de criação de usuário com exceção")
void testCreateException() {
when(personRepository.findByName(inputDTO.getPerson().getName())).thenReturn(createPerson());
when(userRepository.findByPersonDocument(inputDTO.getPerson().getDocument())).thenReturn(null);
when(personConverter.convertToInputDTO(createPerson())).thenReturn(createPersonInputDTO());
when(personRepository.findById(UUID.fromString(inputDTO.getPersonId()))).thenReturn(Optional.ofNullable(createPerson()));
when(userConverter.convertToEntity(inputDTO)).thenReturn(entity);
when(userRepository.save(entity)).thenThrow(BadRequestException.class);

Expand Down Expand Up @@ -203,7 +199,7 @@ private UserEntity createEntity() {

private UserInputDTO createInputDTO() {
return UserInputDTO.builder()
.person(createPersonInputDTO())
.personId(createPersonInputDTO().getId())
.enumProfile(EnumProfile.OENOPHILE.getCode())
.email("email@email.com")
.password("123456")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private AddressEntity createAddressEntity() {

private UserInputDTO createUserIntputDTO() {
return UserInputDTO.builder()
.person(createPersonInputDTO())
.personId(createPersonInputDTO().getId())
.enumProfile(EnumProfile.OENOPHILE.getCode())
.email("email@gmail.com")
.password("123456")
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ListWineComponent from './components/wine/forms/ListWineComponent';
import ListPointScaleComponent from './components/review/ListPointScaleComponent';
import ListTastingComponent from './components/tasting/forms/ListTastingComponent';
import PersonRegistration from './components/registration/PersonRegistration';
import UserRegistration from './components/registration/UserRegistration';

function App() {
return (
Expand All @@ -27,7 +28,8 @@ function App() {
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />

<Route path="/registration" element={<PersonRegistration />} />
<Route path="/registration" element={<PersonRegistration />} />
<Route path="/user-registration" element={<UserRegistration />} />
<Route path="/users" element={<ListUserComponent />} />
<Route path="/persons" element={<ListPersonComponent />} />
<Route path="/address" element={<ListAddressComponent />} />
Expand Down
118 changes: 0 additions & 118 deletions frontend/src/components/registration/Registration_Copia.jsx

This file was deleted.

Loading

0 comments on commit 6aa4d92

Please sign in to comment.