-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Ar3secchim/feature/create-test-register-cu…
…stomer Feature/create test register customer
- Loading branch information
Showing
3 changed files
with
122 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/com/crud/modules/customers/usecase/RegisterCustomerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |