From c61e890444b892cf9fb822971ae3d61c5f39ec73 Mon Sep 17 00:00:00 2001 From: andreivan245 Date: Thu, 19 Sep 2024 11:38:52 -0300 Subject: [PATCH 1/2] bug findbyname --- .../challenge/controller/PlaceController.java | 20 ++++--- .../com/clickbus/challenge/dto/PlaceDTO.java | 33 ++++++++++ .../com/clickbus/challenge/entity/Place.java | 60 +++++++++++++++++++ .../challenge/service/PlaceService.java | 16 +++-- 4 files changed, 114 insertions(+), 15 deletions(-) diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java index bbd5d5c..ad038b6 100644 --- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java +++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java @@ -9,21 +9,18 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; +import java.util.stream.Collectors; @Api("places") @RestController -@RequestMapping("places") +@RequestMapping("/places") public class PlaceController { + @Autowired private PlaceService service; @PostMapping @@ -31,7 +28,7 @@ public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) { return new ResponseEntity(service.save(dto.buildPlace()).convertToDTO(), HttpStatus.CREATED); } - @GetMapping("{id}") + @GetMapping("/{id}") public ResponseEntity findById(@PathVariable Long id) { return service.findById(id) .map(place -> ResponseEntity.ok(place.convertToDTO())) @@ -49,4 +46,9 @@ public ResponseEntity alter(@PathVariable Long id, @RequestBody @Valid PlaceDTO Place place = service.findById(id).orElseThrow(null); return new ResponseEntity(service.alter(place, placeDTO).convertToDTO(), HttpStatus.OK); } + + @GetMapping("/?name={name}") + public ResponseEntity findByName(@PathVariable String name){ + + } } diff --git a/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java b/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java index 26e878d..cf79132 100644 --- a/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java +++ b/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java @@ -35,6 +35,7 @@ public static PlaceDTO of(String name, String slug, String city, String state) { return new PlaceDTO(name, slug, city, state); } + public static Iterable convertToList(List places) { return places.stream().map(Place::convertToDTO).collect(Collectors.toList()); } @@ -42,4 +43,36 @@ public static Iterable convertToList(List places) { public Place buildPlace() { return Place.of(this.name, this.slug, this.city, this.state); } + + public @NotNull String getName() { + return name; + } + + public void setName(@NotNull String name) { + this.name = name; + } + + public @NotNull String getSlug() { + return slug; + } + + public void setSlug(@NotNull String slug) { + this.slug = slug; + } + + public @NotNull String getCity() { + return city; + } + + public void setCity(@NotNull String city) { + this.city = city; + } + + public @NotNull String getState() { + return state; + } + + public void setState(@NotNull String state) { + this.state = state; + } } diff --git a/src/main/java/br/com/clickbus/challenge/entity/Place.java b/src/main/java/br/com/clickbus/challenge/entity/Place.java index ccfc550..daee781 100644 --- a/src/main/java/br/com/clickbus/challenge/entity/Place.java +++ b/src/main/java/br/com/clickbus/challenge/entity/Place.java @@ -39,6 +39,10 @@ public class Place { private LocalDateTime updatedAt; + public Place(){ + + } + public Place(String name, String slug, String city, String state) { this.name = name; this.slug = slug; @@ -51,7 +55,63 @@ public static Place of(String name, String slug, String city, String state) { return new Place(name, slug, city, state); } + public @NotNull String getName() { + return name; + } + public PlaceDTO convertToDTO() { return PlaceDTO.of(this.name, this.slug, this.city, this.state); } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public void setName(@NotNull String name) { + this.name = name; + } + + public @NotNull String getSlug() { + return slug; + } + + public void setSlug(@NotNull String slug) { + this.slug = slug; + } + + public @NotNull String getCity() { + return city; + } + + public void setCity(@NotNull String city) { + this.city = city; + } + + public @NotNull String getState() { + return state; + } + + public void setState(@NotNull String state) { + this.state = state; + } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(LocalDateTime createdAt) { + this.createdAt = createdAt; + } + + public LocalDateTime getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(LocalDateTime updatedAt) { + this.updatedAt = updatedAt; + } } diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java index 799d1a8..6fa8e1e 100644 --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -5,36 +5,40 @@ import br.com.clickbus.challenge.entity.Place; import br.com.clickbus.challenge.repository.PlaceRepository; import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; import org.springframework.stereotype.Service; import javax.validation.constraints.NotNull; import java.util.List; import java.util.Optional; -import org.apache.commons.lang3.NotImplementedException; +import org.springframework.beans.BeanUtils; @Service @AllArgsConstructor public class PlaceService { + @Autowired private PlaceRepository repository; public List findAll() { - throw new NotImplementedException("Metodo nao implementado"); + return repository.findAll(); } public Optional findById(@NotNull Long id) { - throw new NotImplementedException("Metodo nao implementado"); + return repository.findById(id); } public Place save(@NotNull Place place) { - throw new NotImplementedException("Metodo nao implementado"); + return repository.save(place); } public List findByName(@NotNull String name) { - throw new NotImplementedException("Metodo nao implementado"); + return findByName(name); } public Place alter(@NotNull Place place,@NotNull PlaceDTO placeDTO) { - throw new NotImplementedException("Metodo nao implementado"); + BeanUtils.copyProperties(placeDTO,place,"id"); + return repository.save(place); } } From c7873339ee64ae254cdeeab46c9cfb67cdaeef6f Mon Sep 17 00:00:00 2001 From: andreivan245 Date: Thu, 19 Sep 2024 23:33:10 -0300 Subject: [PATCH 2/2] test: add findAll tests --- pom.xml | 10 +++++++- .../challenge/controller/PlaceController.java | 11 +++++++-- .../challenge/service/PlaceService.java | 14 +++++++---- .../challenge/service/PlaceServiceTest.java | 23 ++++++++++++++++--- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index cf21f4f..7ff1b1d 100755 --- a/pom.xml +++ b/pom.xml @@ -109,7 +109,15 @@ org.springframework.boot spring-boot-maven-plugin - + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java index ad038b6..e03b462 100644 --- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java +++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java @@ -13,6 +13,7 @@ import javax.validation.Valid; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Api("places") @@ -47,8 +48,14 @@ public ResponseEntity alter(@PathVariable Long id, @RequestBody @Valid PlaceDTO return new ResponseEntity(service.alter(place, placeDTO).convertToDTO(), HttpStatus.OK); } - @GetMapping("/?name={name}") - public ResponseEntity findByName(@PathVariable String name){ + @GetMapping("/") + public ResponseEntity findByName(@RequestParam String name){ + + if(service.findByName(name).isEmpty()){ + throw new PlaceNotFoundException(HttpStatus.NOT_FOUND); + } + + return ResponseEntity.ok(service.findByName(name).stream().map(Place::convertToDTO)); } } diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java index 6fa8e1e..2c141de 100644 --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -6,13 +6,13 @@ import br.com.clickbus.challenge.repository.PlaceRepository; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Example; import org.springframework.stereotype.Service; import javax.validation.constraints.NotNull; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; -import org.springframework.beans.BeanUtils; + @Service @AllArgsConstructor @@ -34,11 +34,17 @@ public Place save(@NotNull Place place) { } public List findByName(@NotNull String name) { - return findByName(name); + return repository.findByName(name); } public Place alter(@NotNull Place place,@NotNull PlaceDTO placeDTO) { - BeanUtils.copyProperties(placeDTO,place,"id"); + + place.setName(placeDTO.getName()); + place.setSlug(placeDTO.getSlug()); + place.setCity(place.getCity()); + place.setState(placeDTO.getState()); + place.setUpdatedAt(LocalDateTime.now()); + return repository.save(place); } } diff --git a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java index cf0f0a9..1ce83d4 100644 --- a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java +++ b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java @@ -1,6 +1,5 @@ package br.com.clickbus.challenge.service; - import br.com.clickbus.challenge.dto.PlaceDTO; import br.com.clickbus.challenge.entity.Place; import br.com.clickbus.challenge.repository.PlaceRepository; @@ -11,15 +10,14 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.atLeastOnce; @@ -112,4 +110,23 @@ void whenAlterPlaceOk() { assertEquals(place.getCreatedAt(), edited.getCreatedAt()); assertNotNull(edited.getUpdatedAt()); } + + @Test + void whenFindAllOk() { + when(repository.findAll()).thenReturn(Collections.singletonList(place)); + + List actual = service.findAll(); + + assertNotNull(actual); + assertEquals(1, actual.size()); + verify(repository, atLeastOnce()).findAll(); + } + + @Test + void whenFindAllNotFound() { + when(repository.findAll()).thenReturn(null); + + assertNull(service.findAll()); + verify(repository, atLeastOnce()).findAll(); + } }