diff --git a/src/main/java/guru/springframework/msscbeerservice/bootstrap/BeerLoader.java b/src/main/java/guru/springframework/msscbeerservice/bootstrap/BeerLoader.java index 1bd77cbc..902f3266 100644 --- a/src/main/java/guru/springframework/msscbeerservice/bootstrap/BeerLoader.java +++ b/src/main/java/guru/springframework/msscbeerservice/bootstrap/BeerLoader.java @@ -13,6 +13,10 @@ @Component public class BeerLoader implements CommandLineRunner { + public static final String BEER_1_UPC = "0631234200036"; + public static final String BEER_2_UPC = "0631234300019"; + public static final String BEER_3_UPC = "0083783375213"; + private final BeerRepository beerRepository; public BeerLoader(BeerRepository beerRepository) { @@ -32,7 +36,7 @@ private void loadBeerObjects() { .beerStyle("IPA") .quantityToBrew(200) .minOnHand(12) - .upc(337010000001L) + .upc(BEER_1_UPC) .price(new BigDecimal("12.95")) .build()); @@ -41,7 +45,16 @@ private void loadBeerObjects() { .beerStyle("PALE_ALE") .quantityToBrew(200) .minOnHand(12) - .upc(337010000002L) + .upc(BEER_2_UPC) + .price(new BigDecimal("11.95")) + .build()); + + beerRepository.save(Beer.builder() + .beerName("No Hammers On The Bar") + .beerStyle("PALE_ALE") + .quantityToBrew(200) + .minOnHand(12) + .upc(BEER_3_UPC) .price(new BigDecimal("11.95")) .build()); } diff --git a/src/main/java/guru/springframework/msscbeerservice/domain/Beer.java b/src/main/java/guru/springframework/msscbeerservice/domain/Beer.java index 86f24d08..7d040a08 100644 --- a/src/main/java/guru/springframework/msscbeerservice/domain/Beer.java +++ b/src/main/java/guru/springframework/msscbeerservice/domain/Beer.java @@ -41,7 +41,7 @@ public class Beer { private String beerStyle; @Column(unique = true) - private Long upc; + private String upc; private BigDecimal price; diff --git a/src/main/java/guru/springframework/msscbeerservice/web/model/BeerDto.java b/src/main/java/guru/springframework/msscbeerservice/web/model/BeerDto.java index e9fc3e12..94b0cddf 100644 --- a/src/main/java/guru/springframework/msscbeerservice/web/model/BeerDto.java +++ b/src/main/java/guru/springframework/msscbeerservice/web/model/BeerDto.java @@ -44,9 +44,8 @@ public class BeerDto { @NotNull private BeerStyleEnum beerStyle; - @Positive @NotNull - private Long upc; + private String upc; @JsonFormat(shape = JsonFormat.Shape.STRING) @Positive diff --git a/src/test/java/guru/springframework/msscbeerservice/web/controller/BeerControllerTest.java b/src/test/java/guru/springframework/msscbeerservice/web/controller/BeerControllerTest.java index 7d230fb6..4e5ea8d0 100644 --- a/src/test/java/guru/springframework/msscbeerservice/web/controller/BeerControllerTest.java +++ b/src/test/java/guru/springframework/msscbeerservice/web/controller/BeerControllerTest.java @@ -1,17 +1,22 @@ package guru.springframework.msscbeerservice.web.controller; import com.fasterxml.jackson.databind.ObjectMapper; +import guru.springframework.msscbeerservice.bootstrap.BeerLoader; +import guru.springframework.msscbeerservice.services.BeerService; import guru.springframework.msscbeerservice.web.model.BeerDto; import guru.springframework.msscbeerservice.web.model.BeerStyleEnum; 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; import java.math.BigDecimal; import java.util.UUID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -24,9 +29,14 @@ class BeerControllerTest { @Autowired ObjectMapper objectMapper; + @MockBean + BeerService beerService; + @Test void getBeerById() throws Exception { + given(beerService.getById(any())).willReturn(getValidBeerDto()); + mockMvc.perform(get("/api/v1/beer/" + UUID.randomUUID().toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); @@ -38,6 +48,8 @@ void saveNewBeer() throws Exception { BeerDto beerDto = getValidBeerDto(); String beerDtoJson = objectMapper.writeValueAsString(beerDto); + given(beerService.saveNewBeer(any())).willReturn(getValidBeerDto()); + mockMvc.perform(post("/api/v1/beer/") .contentType(MediaType.APPLICATION_JSON) .content(beerDtoJson)) @@ -46,6 +58,8 @@ void saveNewBeer() throws Exception { @Test void updateBeerById() throws Exception { + given(beerService.updateBeer(any(), any())).willReturn(getValidBeerDto()); + BeerDto beerDto = getValidBeerDto(); String beerDtoJson = objectMapper.writeValueAsString(beerDto); @@ -60,7 +74,7 @@ BeerDto getValidBeerDto(){ .beerName("My Beer") .beerStyle(BeerStyleEnum.ALE) .price(new BigDecimal("2.99")) - .upc(123123123123L) + .upc(BeerLoader.BEER_1_UPC) .build(); } } \ No newline at end of file