Skip to content

Commit

Permalink
feat: integration tests for retrieving manager data
Browse files Browse the repository at this point in the history
  • Loading branch information
gjperes committed Dec 6, 2023
1 parent 65d3f34 commit e82c753
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 43 deletions.
25 changes: 12 additions & 13 deletions src/main/java/br/ufpr/tads/msbantadsmanager/manager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import br.ufpr.tads.msbantadsmanager.manager.inbound.CreateManager;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand All @@ -18,8 +16,6 @@
@Data
@Entity
@Table(name = "managers")
@NoArgsConstructor
@AllArgsConstructor
public class Manager implements Serializable {
@Serial private static final long serialVersionUID = 5397338376573272269L;

Expand Down Expand Up @@ -59,16 +55,19 @@ public class Manager implements Serializable {
@Column(nullable = false)
private LocalDateTime updatedAt;

public Manager(@NonNull CreateManager createManager) {
this.setFirstName(createManager.firstName());
this.setLastName(createManager.lastName());
this.setCpf(createManager.cpf());
this.setEmail(createManager.email());
this.setPhone(createManager.phone());
this.setCreatedBy(createManager.createdBy());
public static Manager create(@NonNull CreateManager createManager) {
var entity = new Manager();
entity.setFirstName(createManager.firstName());
entity.setLastName(createManager.lastName());
entity.setCpf(createManager.cpf());
entity.setEmail(createManager.email());
entity.setPhone(createManager.phone());
entity.setCreatedBy(createManager.createdBy());

LocalDateTime now = LocalDateTime.now();
this.setCreatedAt(now);
this.setUpdatedAt(now);
entity.setCreatedAt(now);
entity.setUpdatedAt(now);

return entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public class ManagerController {
private final ManagerService service;

@GetMapping
public ResponseEntity<?> findManager(@RequestParam @Valid @Email String email,
@RequestParam @Valid @Length(min = 11, max = 11) String cpf) {
public ResponseEntity<?> findManager(@RequestParam(required = false) @Valid @Email @Length(min = 6) String email,
@RequestParam(required = false) @Length(min = 11, max = 11) String cpf) {
log.debug("[request] findManager(email: {} , cpf: {})", email, cpf);

if (Objects.nonNull(email)) {
log.debug("[request] findManagerByEmail '{}'", email);
return ResponseEntity.ok(this.service.findManagerByEmail(email));
}

if (Objects.nonNull(cpf)) {
log.debug("[request] findManagerByCpf '{}'", cpf);
return ResponseEntity.ok(this.service.findManagerByCpf(cpf));
}

if (Objects.nonNull(email)) {
log.debug("[request] findManagerByEmail '{}'", email);
return ResponseEntity.ok(this.service.findManagerByEmail(email));
}

return ResponseEntity.ok(this.service.findAllManagers());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public Long create(@Valid @NonNull CreateManager createManager) {
throw new ResponseStatusException(HttpStatus.CONFLICT);
}


var entity = new Manager(createManager);
var entity = Manager.create(createManager);
return this.repository.save(entity).getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package br.ufpr.tads.msbantadsmanager.manager;

import br.ufpr.tads.msbantadsmanager.manager.inbound.CreateManager;
import br.ufpr.tads.msbantadsmanager.manager.outbound.ManagerResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
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.junit.jupiter.params.provider.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -24,13 +25,18 @@
@AutoConfigureMockMvc
class ManagerControllerIntegrationTest {
private static final String URL = ManagerController.URL;
public static final CreateManager CREATE_MANAGER = new CreateManager(
"firstName",
"lastName",
"manager@email.com",
"12312312311",
"12345678901",
"admin@email.com"
);

@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private ManagerRepository repository;
@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;
@Autowired private ManagerRepository repository;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -194,7 +200,7 @@ void return409_createDuplicate(String createEmail,
"12345678901",
"admin@email.com"
);
this.repository.save(new Manager(saved));
this.repository.save(Manager.create(saved));

this.mockMvc
.perform(post(URL)
Expand All @@ -210,15 +216,7 @@ void return409_createDuplicate(String createEmail,
@Test
@DisplayName("should create a new manager successfully")
void return201_createSuccessfully() throws Exception {
var request = new CreateManager(
"firstName",
"lastName",
"manager@email.com",
"12312312311",
"12345678901",
"admin@email.com"
);
var json = objectMapper.writeValueAsString(request);
var json = objectMapper.writeValueAsString(CREATE_MANAGER);

var result = this.mockMvc
.perform(post(URL)
Expand All @@ -231,7 +229,131 @@ void return201_createSuccessfully() throws Exception {

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

var saved = this.repository.findManagerByEmail(request.email()).orElseThrow(AssertionError::new);
var saved = this.repository.findManagerByEmail(CREATE_MANAGER.email()).orElseThrow(AssertionError::new);
assertEquals(result.getResponse().getHeader("Location"), URL + "/" + saved.getId());
}

@ParameterizedTest
@NullSource
@ValueSource(longs = {0L, -1L, -999L})
@DisplayName("should not find a manager with invalid id")
void return400_findManagerByInvalidId(Long invalidId) throws Exception {
this.repository.save(Manager.create(CREATE_MANAGER));

this.mockMvc
.perform(get(URL + "/" + invalidId))
.andDo(print())
.andExpect(status().isBadRequest());
}

@Test
@DisplayName("should not find a manager with nonexistent id")
void return404_findManagerByNonExistentId() throws Exception {
var saved = this.repository.save(Manager.create(CREATE_MANAGER));
long invalidId = saved.getId() + 1L;

this.mockMvc
.perform(get(URL + "/" + invalidId))
.andDo(print())
.andExpect(status().isNotFound());
}

@Test
@DisplayName("should find a manager by id")
void return200_findManagerById() throws Exception {
var saved = this.repository.save(Manager.create(CREATE_MANAGER));
var json = objectMapper.writeValueAsString(ManagerResponse.of(saved));

var result = this.mockMvc
.perform(get(URL + "/" + saved.getId()))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertEquals(result.getResponse().getContentAsString(), json);
}

@Test
@DisplayName("should not find a manager with nonexistent email and cpf")
void return404_findManagerByNonExistentEmailOrCpf() throws Exception {
this.repository.save(Manager.create(CREATE_MANAGER));

this.mockMvc
.perform(get(URL).param("email", "another@email.com"))
.andDo(print())
.andExpect(status().isNotFound());

this.mockMvc
.perform(get(URL).param("cpf", "00000000000"))
.andDo(print())
.andExpect(status().isNotFound());
}

@ParameterizedTest
@EmptySource
@ValueSource(strings = {"1234567890", "123456789012", "not email", "123@.com", ".com@.br"})
@DisplayName("should not find a manager with invalid email or cpf")
void return400_findManagerByInvalidEmailOrCpf(String invalidValue) throws Exception {
this.repository.save(Manager.create(CREATE_MANAGER));

this.mockMvc
.perform(get(URL).param("email", invalidValue))
.andDo(print())
.andExpect(status().isBadRequest());

this.mockMvc
.perform(get(URL).param("cpf", invalidValue))
.andDo(print())
.andExpect(status().isBadRequest());
}

@Test
@DisplayName("should find a manager by email and cpf")
void return200_findManagerByEmailAndCpf() throws Exception {
var saved = this.repository.save(Manager.create(CREATE_MANAGER));
var json = objectMapper.writeValueAsString(ManagerResponse.of(saved));

var emailResult = this.mockMvc
.perform(get(URL).param("email", CREATE_MANAGER.email()))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertEquals(emailResult.getResponse().getContentAsString(), json);

var cpfResult = this.mockMvc
.perform(get(URL).param("cpf", CREATE_MANAGER.cpf()))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertEquals(cpfResult.getResponse().getContentAsString(), json);
}

@Test
@DisplayName("should find all managers or return empty list")
void return200_findAllManagers() throws Exception {
var emptyResult = this.mockMvc
.perform(get(URL))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
assertEquals("[]", emptyResult.getResponse().getContentAsString());

for (int i = 0; i < 3; i++) {
var entity = Manager.create(CREATE_MANAGER);
entity.setCpf("1234567890" + i);
entity.setEmail("manager" + i + "@email.com");

this.repository.save(entity);
}

var result = this.mockMvc
.perform(get(URL))
.andDo(print())
.andExpect(status().isOk())
.andReturn();

assertNotEquals(result.getResponse().getContentAsString(), "[]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void beforeAll() {
"12312312300",
"1112341234",
"admin@admin.com");
validEntity = new Manager(createManager);
validEntity = Manager.create(createManager);
}

@BeforeEach
Expand Down

0 comments on commit e82c753

Please sign in to comment.