Repository navigation
[PETCONTROL-001] Adicionar listagem de espécies #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/br/com/db/petcontrol/controller/SpeciesController.java
This file contains hidden or 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,30 @@ | ||
| package br.com.db.petcontrol.controller; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import java.util.List; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
|
|
||
| @Tag(name = "Species", description = "Operações para listagem de espécies disponíveis") | ||
| @RequestMapping("/species") | ||
| public interface SpeciesController { | ||
|
|
||
| @Operation( | ||
| summary = "Listar todas as espécies", | ||
| description = "Retorna todas as espécies cadastradas no sistema em ordem alfabética") | ||
| @ApiResponse( | ||
| responseCode = "200", | ||
| description = "Lista de espécies retornada com sucesso", | ||
| content = | ||
| @Content( | ||
| mediaType = "application/json", | ||
| schema = @Schema(implementation = SpeciesResponseDTO.class))) | ||
| @GetMapping | ||
| ResponseEntity<List<SpeciesResponseDTO>> getAll(); | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/br/com/db/petcontrol/controller/impl/SpeciesControllerImpl.java
This file contains hidden or 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,24 @@ | ||
| package br.com.db.petcontrol.controller.impl; | ||
|
|
||
| import br.com.db.petcontrol.controller.SpeciesController; | ||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.service.SpeciesService; | ||
| import java.util.List; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class SpeciesControllerImpl implements SpeciesController { | ||
|
|
||
| private final SpeciesService service; | ||
|
|
||
| public SpeciesControllerImpl(SpeciesService service) { | ||
| this.service = service; | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<List<SpeciesResponseDTO>> getAll() { | ||
| List<SpeciesResponseDTO> species = service.getAllSpecies(); | ||
| return ResponseEntity.ok(species); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/br/com/db/petcontrol/dto/response/SpeciesResponseDTO.java
This file contains hidden or 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,14 @@ | ||
| package br.com.db.petcontrol.dto.response; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import java.util.UUID; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| @Schema(description = "Resposta com informações da espécie") | ||
| public record SpeciesResponseDTO( | ||
| @Schema( | ||
| description = "Identificador único da espécie", | ||
| example = "d6acb364-305e-4883-964f-0e7aee360684") | ||
| UUID id, | ||
| @Schema(description = "Nome da espécie", example = "Cachorro") String name) {} |
11 changes: 11 additions & 0 deletions
11
src/main/java/br/com/db/petcontrol/mapper/SpeciesMapper.java
This file contains hidden or 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,11 @@ | ||
| package br.com.db.petcontrol.mapper; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.model.Species; | ||
| import org.mapstruct.Mapper; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| public interface SpeciesMapper { | ||
|
|
||
| SpeciesResponseDTO toDTO(Species species); | ||
| } |
This file contains hidden or 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,29 @@ | ||
| package br.com.db.petcontrol.model; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import java.util.UUID; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Table(name = "tb_species") | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class Species { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| private UUID id; | ||
|
|
||
| @Column(nullable = false, unique = true) | ||
| private String name; | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/br/com/db/petcontrol/repository/SpeciesRepository.java
This file contains hidden or 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,13 @@ | ||
| package br.com.db.petcontrol.repository; | ||
|
|
||
| import br.com.db.petcontrol.model.Species; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface SpeciesRepository extends JpaRepository<Species, UUID> { | ||
|
|
||
| List<Species> findAllByOrderByNameAsc(); | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/br/com/db/petcontrol/service/SpeciesService.java
This file contains hidden or 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,8 @@ | ||
| package br.com.db.petcontrol.service; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import java.util.List; | ||
|
|
||
| public interface SpeciesService { | ||
| List<SpeciesResponseDTO> getAllSpecies(); | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/main/java/br/com/db/petcontrol/service/impl/SpeciesServiceImpl.java
This file contains hidden or 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,25 @@ | ||
| package br.com.db.petcontrol.service.impl; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.mapper.SpeciesMapper; | ||
| import br.com.db.petcontrol.repository.SpeciesRepository; | ||
| import br.com.db.petcontrol.service.SpeciesService; | ||
| import java.util.List; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class SpeciesServiceImpl implements SpeciesService { | ||
|
|
||
| private final SpeciesRepository repository; | ||
| private final SpeciesMapper mapper; | ||
|
|
||
| public SpeciesServiceImpl(SpeciesRepository repository, SpeciesMapper mapper) { | ||
| this.repository = repository; | ||
| this.mapper = mapper; | ||
| } | ||
|
|
||
| @Override | ||
| public List<SpeciesResponseDTO> getAllSpecies() { | ||
| return repository.findAllByOrderByNameAsc().stream().map(mapper::toDTO).toList(); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,32 @@ | ||
| spring: | ||
| application: | ||
| name: petcontrol | ||
|
|
||
| datasource: | ||
| url: ${DB_URL:jdbc:postgresql://localhost:5432/petcontrol} | ||
| username: ${DB_USERNAME:user} | ||
| password: ${DB_PASSWORD:password} | ||
| driver-class-name: org.postgresql.Driver | ||
|
|
||
| jpa: | ||
| hibernate: | ||
| ddl-auto: validate | ||
| show-sql: true | ||
| properties: | ||
| hibernate: | ||
| format_sql: true | ||
|
|
||
| flyway: | ||
| enabled: true | ||
| locations: classpath:db/migrations | ||
|
|
||
| web: | ||
| cors: | ||
| allowed-origins: ${WEB_API:http://localhost:3000} | ||
|
|
||
| management: | ||
| endpoints: | ||
| web: | ||
| cors: | ||
| allowed-origins: ${WEB_API:http://localhost:3000} | ||
| allowed-methods: OPTIONS, GET, POST | ||
| allowed-methods: OPTIONS, GET, POST |
1 change: 1 addition & 0 deletions
1
src/main/resources/db/migrations/V20260423164428__create_extension_uuid.sql
This file contains hidden or 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 @@ | ||
| CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; |
4 changes: 4 additions & 0 deletions
4
src/main/resources/db/migrations/V20260423164526__create_table_species.sql
This file contains hidden or 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,4 @@ | ||
| CREATE TABLE IF NOT EXISTS tb_species ( | ||
| id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); |
10 changes: 10 additions & 0 deletions
10
src/main/resources/db/migrations/V20260423164728__insert_default_species.sql
This file contains hidden or 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,10 @@ | ||
| INSERT INTO tb_species (name) VALUES | ||
| ('Calopsita'), | ||
| ('Cachorro'), | ||
| ('Canário'), | ||
| ('Coelho'), | ||
| ('Gato'), | ||
| ('Hamster'), | ||
| ('Peixe'), | ||
| ('Porquinho-da-índia') | ||
| ON CONFLICT (name) DO NOTHING; |
76 changes: 76 additions & 0 deletions
76
src/test/java/br/com/db/petcontrol/controller/SpeciesControllerTest.java
This file contains hidden or 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,76 @@ | ||
| package br.com.db.petcontrol.controller; | ||
|
|
||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import br.com.db.petcontrol.controller.impl.SpeciesControllerImpl; | ||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.mocks.SpeciesFixture; | ||
| import br.com.db.petcontrol.service.SpeciesService; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| @WebMvcTest(SpeciesControllerImpl.class) | ||
| class SpeciesControllerTest { | ||
|
|
||
| @Autowired private MockMvc mockMvc; | ||
|
|
||
| @MockBean private SpeciesService service; | ||
|
|
||
| private static final String SPECIES_URL = "/species"; | ||
|
|
||
| @Nested | ||
| class GetAllSpeciesTests { | ||
|
|
||
| @Test | ||
| void shouldListAllSpeciesSuccessfully() throws Exception { | ||
| SpeciesResponseDTO species1 = SpeciesFixture.dtoBuilder().name("Cachorro").build(); | ||
| SpeciesResponseDTO species2 = SpeciesFixture.dtoBuilder().name("Gato").build(); | ||
| SpeciesResponseDTO species3 = SpeciesFixture.dtoBuilder().name("Pássaro").build(); | ||
|
|
||
| when(service.getAllSpecies()).thenReturn(List.of(species1, species2, species3)); | ||
|
|
||
| mockMvc | ||
| .perform(get(SPECIES_URL).contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(jsonPath("$", hasSize(3))) | ||
| .andExpect(jsonPath("$[0].name").value("Cachorro")) | ||
| .andExpect(jsonPath("$[1].name").value("Gato")) | ||
| .andExpect(jsonPath("$[2].name").value("Pássaro")); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnListWithOneSpecies() throws Exception { | ||
| SpeciesResponseDTO species = SpeciesFixture.dtoBuilder().name("Coelho").build(); | ||
|
|
||
| when(service.getAllSpecies()).thenReturn(List.of(species)); | ||
|
|
||
| mockMvc | ||
| .perform(get(SPECIES_URL).contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(1))) | ||
| .andExpect(jsonPath("$[0].name").value("Coelho")); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnEmptyListWhenNoSpeciesExist() throws Exception { | ||
| when(service.getAllSpecies()).thenReturn(List.of()); | ||
|
|
||
| mockMvc | ||
| .perform(get(SPECIES_URL).contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(0))); | ||
| } | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/test/java/br/com/db/petcontrol/integration/SpeciesIntegrationTest.java
This file contains hidden or 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,71 @@ | ||
| package br.com.db.petcontrol.integration; | ||
|
gabicastrum marked this conversation as resolved.
Resolved
|
||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.model.Species; | ||
| import br.com.db.petcontrol.repository.SpeciesRepository; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.test.web.client.TestRestTemplate; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
|
|
||
|
gabicastrum marked this conversation as resolved.
Resolved
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
| @ActiveProfiles("test") | ||
| class SpeciesIntegrationTest { | ||
|
|
||
| @Autowired private TestRestTemplate restTemplate; | ||
|
|
||
| @Autowired private SpeciesRepository repository; | ||
|
|
||
| private static final String SPECIES_URL = "/species"; | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| repository.deleteAll(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnSpeciesOrderedByNameAsc() { | ||
| repository.save(new Species(null, "Gato")); | ||
| repository.save(new Species(null, "Cachorro")); | ||
| repository.save(new Species(null, "Calopsita")); | ||
|
|
||
| ResponseEntity<SpeciesResponseDTO[]> response = | ||
| restTemplate.getForEntity(SPECIES_URL, SpeciesResponseDTO[].class); | ||
|
|
||
| assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
| assertThat(response.getBody()).hasSize(3); | ||
| assertThat(response.getBody()[0].name()).isEqualTo("Cachorro"); | ||
| assertThat(response.getBody()[1].name()).isEqualTo("Calopsita"); | ||
| assertThat(response.getBody()[2].name()).isEqualTo("Gato"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnEmptyListWhenNoSpeciesExist() { | ||
| ResponseEntity<SpeciesResponseDTO[]> response = | ||
| restTemplate.getForEntity(SPECIES_URL, SpeciesResponseDTO[].class); | ||
|
|
||
| assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
| assertThat(response.getBody()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnMultipleSpeciesWithCorrectIds() { | ||
| Species species1 = repository.save(new Species(null, "Ave")); | ||
| Species species2 = repository.save(new Species(null, "Cachorro")); | ||
|
|
||
| ResponseEntity<SpeciesResponseDTO[]> response = | ||
| restTemplate.getForEntity(SPECIES_URL, SpeciesResponseDTO[].class); | ||
|
|
||
| assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
| assertThat(response.getBody()).hasSize(2); | ||
| assertThat(response.getBody()[0].id()).isEqualTo(species1.getId()); | ||
| assertThat(response.getBody()[1].id()).isEqualTo(species2.getId()); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/test/java/br/com/db/petcontrol/mocks/SpeciesFixture.java
This file contains hidden or 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,20 @@ | ||
| package br.com.db.petcontrol.mocks; | ||
|
|
||
| import br.com.db.petcontrol.dto.response.SpeciesResponseDTO; | ||
| import br.com.db.petcontrol.model.Species; | ||
| import java.util.UUID; | ||
|
|
||
| public final class SpeciesFixture { | ||
|
|
||
| private static final String DEFAULT_NAME = "Cachorro"; | ||
|
|
||
| private SpeciesFixture() {} | ||
|
|
||
| public static Species.SpeciesBuilder builder() { | ||
| return Species.builder().id(UUID.randomUUID()).name(DEFAULT_NAME); | ||
| } | ||
|
|
||
| public static SpeciesResponseDTO.SpeciesResponseDTOBuilder dtoBuilder() { | ||
| return SpeciesResponseDTO.builder().id(UUID.randomUUID()).name(DEFAULT_NAME); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.