From db8b4d1f76802538fddc1794341cfbd243f4be61 Mon Sep 17 00:00:00 2001 From: Olgasyla Date: Wed, 21 Aug 2024 11:59:19 +0200 Subject: [PATCH] add groseries backend --- .../java/org/example/backend/IdService.java | 15 +++++++ .../java/org/example/backend/NewProduct.java | 9 ++++ .../example/backend/ProductController.java | 11 +++-- .../org/example/backend/ProductService.java | 5 +++ .../backend/ProductControllerTest.java | 30 +++++++++++-- .../example/backend/ProductServiceTest.java | 43 +++++++++++++------ 6 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 backend/src/main/java/org/example/backend/IdService.java create mode 100644 backend/src/main/java/org/example/backend/NewProduct.java diff --git a/backend/src/main/java/org/example/backend/IdService.java b/backend/src/main/java/org/example/backend/IdService.java new file mode 100644 index 0000000..e632869 --- /dev/null +++ b/backend/src/main/java/org/example/backend/IdService.java @@ -0,0 +1,15 @@ +package org.example.backend; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import java.util.UUID; + +@Service +@RequiredArgsConstructor + +public class IdService { + + public String randomId() { + return UUID.randomUUID().toString(); + } +} diff --git a/backend/src/main/java/org/example/backend/NewProduct.java b/backend/src/main/java/org/example/backend/NewProduct.java new file mode 100644 index 0000000..e837220 --- /dev/null +++ b/backend/src/main/java/org/example/backend/NewProduct.java @@ -0,0 +1,9 @@ +package org.example.backend; + +import lombok.With; + +@With +public record NewProduct( + String name, + int amount +){} diff --git a/backend/src/main/java/org/example/backend/ProductController.java b/backend/src/main/java/org/example/backend/ProductController.java index 4249ac7..bac5797 100644 --- a/backend/src/main/java/org/example/backend/ProductController.java +++ b/backend/src/main/java/org/example/backend/ProductController.java @@ -1,12 +1,10 @@ package org.example.backend; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; + @RequiredArgsConstructor @RestController @RequestMapping("/api/products") @@ -17,6 +15,11 @@ public class ProductController { public List getAllGroceries() { return productService.findAllGroceries(); } + + @PostMapping + public Product saveProduct(@RequestBody NewProduct newProduct) { + return productService.saveProduct(newProduct); + } @GetMapping("{id}") public Product getGroceryProductById(@PathVariable String id){ return productService.findGroceriesById(id); diff --git a/backend/src/main/java/org/example/backend/ProductService.java b/backend/src/main/java/org/example/backend/ProductService.java index 2a1f923..1ea3240 100644 --- a/backend/src/main/java/org/example/backend/ProductService.java +++ b/backend/src/main/java/org/example/backend/ProductService.java @@ -11,12 +11,17 @@ @Service public class ProductService { private final ProductRepository productRepository; + private final IdService idService; public List findAllGroceries(){ return productRepository.findAll(); } + public Product saveProduct(NewProduct newProduct){ + Product product = new Product(idService.randomId(), newProduct.name(), newProduct.amount()); + return productRepository.save(product); + } public Product findGroceriesById(String id) { return productRepository.findById(id) diff --git a/backend/src/test/java/org/example/backend/ProductControllerTest.java b/backend/src/test/java/org/example/backend/ProductControllerTest.java index d44611b..1336e69 100644 --- a/backend/src/test/java/org/example/backend/ProductControllerTest.java +++ b/backend/src/test/java/org/example/backend/ProductControllerTest.java @@ -4,12 +4,13 @@ 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.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @SpringBootTest @@ -21,8 +22,9 @@ class ProductControllerTest { @Autowired ProductRepository productRepository; - @DirtiesContext + @Test + @DirtiesContext void getAllGroceries() throws Exception { //GIVEN //WHEN @@ -35,6 +37,28 @@ void getAllGroceries() throws Exception { """)); } + @Test + @DirtiesContext + void addNewProduct() throws Exception { + //GIVEN + + //WHEN + mockMvc.perform(post("/api/products") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "name": "Milch", + "amount": 1 + } + """)) + //THEN + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.name").value("Milch")) + .andExpect(jsonPath("$.amount").value(1)); + } + + @Test @DirtiesContext void getGroceryProductById() throws Exception { diff --git a/backend/src/test/java/org/example/backend/ProductServiceTest.java b/backend/src/test/java/org/example/backend/ProductServiceTest.java index 66ad6e5..4f22360 100644 --- a/backend/src/test/java/org/example/backend/ProductServiceTest.java +++ b/backend/src/test/java/org/example/backend/ProductServiceTest.java @@ -1,7 +1,6 @@ package org.example.backend; import org.junit.jupiter.api.Test; - import java.util.List; import java.util.Optional; @@ -9,8 +8,11 @@ import static org.mockito.Mockito.*; class ProductServiceTest { + + IdService idService = mock(IdService.class); ProductRepository productRepository = mock(ProductRepository.class); - ProductService productService = new ProductService(productRepository); + ProductService productService = new ProductService(productRepository, idService); + @Test void findAllGroceries() { @@ -27,17 +29,32 @@ void findAllGroceries() { verify(productRepository).findAll(); assertEquals(products, actual); } - @Test - void findGroceriesById() { - //GIVEN - String id = "4"; - Product product = new Product("4","apple", 7); - when(productRepository.findById(id)).thenReturn(Optional.of(product)); - //WHEN - Product actual = productService.findGroceriesById(id); - //THEN - verify(productRepository).findById(id); - assertEquals(product, actual); + void addProdukt() { + // GIVEN + NewProduct newProduct = new NewProduct("Orangen", 5); + Product saveProduct = new Product(idService.randomId(), newProduct.name(), newProduct.amount()); + when(productRepository.save(saveProduct)).thenReturn(saveProduct); + when(idService.randomId()).thenReturn(saveProduct.id()); + + // WHEN + Product actual = productService.saveProduct(newProduct); + + // THEN + Product expected = new Product(idService.randomId(), newProduct.name(), newProduct.amount()); + verify(productRepository).save(saveProduct); + assertEquals(expected, actual); + } + @Test + void findGroceriesById() { + //GIVEN + String id = "4"; + Product product = new Product("4","apple", 7); + when(productRepository.findById(id)).thenReturn(Optional.of(product)); + //WHEN + Product actual = productService.findGroceriesById(id); + //THEN + verify(productRepository).findById(id); + assertEquals(product, actual); } } \ No newline at end of file