Skip to content

Commit

Permalink
Merge pull request #6 from Ar3secchim/feature/create-test-register-cu…
Browse files Browse the repository at this point in the history
…stomer

Feature/create test register customer
  • Loading branch information
Ar3secchim authored Dec 4, 2023
2 parents a17125e + 4598240 commit 0279c8f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
import com.crud.modules.customers.repository.CustomerRepository;
import com.crud.utils.CustomerConvert;
import com.crud.utils.Validator;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RegisterCustomer {
@Autowired
CustomerRepository repository;

@Autowired
PasswordEncoder passwordEncoder;
private final CustomerRepository repository;
private final PasswordEncoder passwordEncoder;

public CustomerResponse execute(Customer customer) throws PasswordValidationError, ValidationError {
String encodePassword = passwordEncoder.encode(customer.getPassword());
customer.setPassword(encodePassword);

if(!Validator.name(customer.getEmail())) throw new ValidationError(customer.getName(), "Nome menor que dois " +
if(!Validator.name(customer.getName())) throw new ValidationError(customer.getName(), "Nome menor que dois " +
"character");
if(!Validator.emailValidate(customer.getEmail())) throw new ValidationError(customer.getEmail(), "Email inválido");

if(!Validator.passwordValidate(customer.getPassword())) throw new PasswordValidationError("Senha deve seguir o " +
"padrão");
return CustomerConvert.toResponse(repository.save(customer));

if(!Validator.emailValidate(customer.getEmail())) throw new ValidationError(customer.getEmail(), "Email inválido");

String encodePassword = passwordEncoder.encode(customer.getPassword());
customer.setPassword(encodePassword);

repository.save(customer);

return CustomerConvert.toResponse(customer);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/crud/utils/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static Boolean emailValidate(String email){
return email.matches("^\\S{3,}@\\w{2,}\\.[a-zA-Z]{2,}$");
}

public static Boolean name(String email){
return email.matches("^\\S{3,}");
public static Boolean name(String name){
return name.matches("^\\S{3,}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.crud.modules.customers.usecase;

import com.crud.infra.exception.PasswordValidationError;
import com.crud.infra.exception.ValidationError;
import com.crud.modules.customers.entity.Customer;
import com.crud.modules.customers.repository.CustomerRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.UUID;

import static org.mockito.Mockito.*;


class RegisterCustomerTest {
private CustomerRepository repository;
private PasswordEncoder passwordEncoder;
private RegisterCustomer registerCustomer;
private Customer customer;

@BeforeEach
public void setup(){
repository = Mockito.mock(CustomerRepository.class);
passwordEncoder = Mockito.mock(PasswordEncoder.class);

customer = new Customer();
customer.setSku(UUID.randomUUID().toString());
customer.setEmail("[email protected]");
customer.setAddress("validAddress,999");
customer.setName("ValidName");
customer.setPassword("@validPassword123");

registerCustomer = new RegisterCustomer(repository, passwordEncoder);
}


@Test
@DisplayName("should register customer when everything success")
public void registeCustomerWithSuccess() throws PasswordValidationError, ValidationError {

when(passwordEncoder.encode(customer.getPassword())).thenReturn(customer.getPassword());

registerCustomer.execute(customer);

verify(repository, times(1)).save(any());
verify(passwordEncoder, times(1)).encode(any());
}

@Test
@DisplayName("should throw exception customer when name is invalid")
public void registerCustomerWithNameLessThanTwoCharacters() {
customer.setName("As");
when(passwordEncoder.encode(customer.getPassword())).thenReturn(customer.getPassword());

ValidationError exeption = Assertions.assertThrows(
ValidationError.class, () -> {
registerCustomer.execute(customer);
}
);

Assertions.assertEquals("Nome menor que dois character", exeption.getMessage());
}

@Test
@DisplayName("should throw exception customer when email is invalid")
public void registerCustomerWithEmailInvalid() {
customer.setEmail("emailInvalid.com");
when(passwordEncoder.encode(customer.getPassword())).thenReturn(customer.getPassword());

ValidationError exeption = Assertions.assertThrows(
ValidationError.class, () -> {
registerCustomer.execute(customer);
}
);

Assertions.assertEquals("Email inválido", exeption.getMessage());
}

@Test
@DisplayName("should throw exception customer when password is invalid")
public void registerCustomerWithPasswordInvalid() {
customer.setPassword("12345");

when(passwordEncoder.encode(customer.getPassword())).thenReturn(customer.getPassword());
PasswordValidationError exeption = Assertions.assertThrows(
PasswordValidationError.class, () -> {
registerCustomer.execute(customer);
}
);

Assertions.assertEquals("Senha deve seguir o padrão", exeption.getDescription());
}

@Test
@DisplayName("should ctustomer when password is cryptographic")
public void registerCustomerWithPasswordEncode() throws PasswordValidationError, ValidationError {
registerCustomer.execute(customer);
verify(passwordEncoder, times(1)).encode(any());
}
}

0 comments on commit 0279c8f

Please sign in to comment.