Skip to content

Commit

Permalink
feat: return conflict when trying to create duplicate manager
Browse files Browse the repository at this point in the history
  • Loading branch information
gjperes committed Dec 6, 2023
1 parent 39fa2dc commit 65d3f34
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public interface ManagerRepository extends JpaRepository<Manager, Long> {
Optional<Manager> findManagerByEmail(@NonNull String email);
Optional<Manager> findManagerByCpf(@NonNull String cpf);
int countAllByEmailOrCpf(String email, String cpf);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public ManagerResponse findManagerById(@Valid @NonNull @Positive Long id) {
public Long create(@Valid @NonNull CreateManager createManager) {
log.debug("[creating] {}", createManager);

boolean managerAlreadyExists = this.repository.countAllByEmailOrCpf(createManager.email(), createManager.cpf()) > 0;
if (managerAlreadyExists) {
throw new ResponseStatusException(HttpStatus.CONFLICT);
}


var entity = new Manager(createManager);
return this.repository.save(entity).getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public record CreateManager(@NotEmpty String firstName,
@NotEmpty String lastName,
@NotEmpty @Email String email,
@NotEmpty
@Length(min = 11, max = 11)
@Pattern(regexp = "^\\d{11}$", message = "field must be made of numbers")
String cpf,
@NotEmpty @Email String email,
@NotEmpty
@Length(min = 11, max = 11)
@Pattern(regexp = "^\\d{11}$", message = "field must be made of numbers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -43,8 +44,8 @@ void return400_createWithoutFirstName(String emptyFirstName) throws Exception {
var request = new CreateManager(
emptyFirstName,
"lastName",
"12312312311",
"manager@email.com",
"12312312311",
"11999999999",
"admin@email.com"
);
Expand All @@ -68,8 +69,8 @@ void return400_createWithoutLastName(String emptyLastName) throws Exception {
var request = new CreateManager(
"firstName",
emptyLastName,
"12312312311",
"manager@email.com",
"12312312311",
"11999999999",
"admin@email.com"
);
Expand All @@ -94,8 +95,8 @@ void return400_createWithInvalidCpf(String invalidCpf) throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
invalidCpf,
"manager@email.com",
invalidCpf,
"11999999999",
"admin@email.com"
);
Expand All @@ -120,8 +121,8 @@ void return400_createWithInvalidEmail(String invalidEmail) throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
"12345678901",
invalidEmail,
"12345678901",
"11999999999",
"admin@email.com"
);
Expand All @@ -146,8 +147,8 @@ void return400_createWithInvalidPhone(String invalidPhone) throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
"12345678901",
"manager@email.com",
"12345678901",
invalidPhone,
"admin@email.com"
);
Expand All @@ -164,14 +165,56 @@ void return400_createWithInvalidPhone(String invalidPhone) throws Exception {
assertEquals(0, this.repository.count());
}

@ParameterizedTest
@CsvSource(value = {
"manager@email.com,12312312311,manager@email.com,12312312311",
"manager@email.com,12312312311,manager@email.com,99999999999",
"manager@email.com,12312312311,different@email.com,12312312311",
})
@DisplayName("should not create a duplicate manager")
void return409_createDuplicate(String createEmail,
String createCpf,
String email,
String cpf) throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
createEmail,
createCpf,
"12345678901",
"admin@email.com"
);
var json = objectMapper.writeValueAsString(request);

var saved = new CreateManager(
"anotherName",
"anotherLast",
email,
cpf,
"12345678901",
"admin@email.com"
);
this.repository.save(new Manager(saved));

this.mockMvc
.perform(post(URL)
.content(json)
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpect(status().isConflict());

assertEquals(1, this.repository.count());
}

@Test
@DisplayName("should create a new manager successfully")
void return201_createSuccessfully() throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
"12312312311",
"manager@email.com",
"12312312311",
"12345678901",
"admin@email.com"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ static void beforeAll() {
var createManager = new CreateManager(
"firstName",
"lastName",
"12312312300",
"email@email.com",
"12312312300",
"1112341234",
"admin@admin.com");
validEntity = new Manager(createManager);
Expand Down

0 comments on commit 65d3f34

Please sign in to comment.