diff --git a/.gitignore b/.gitignore index 453cc450..faf82e2e 100644 --- a/.gitignore +++ b/.gitignore @@ -212,3 +212,5 @@ buildNumber.properties # End of https://www.gitignore.io/api/java,maven,macos,linux,intellij+all,eclipse +.vscode/launch.json +.vscode/settings.json diff --git a/pom.xml b/pom.xml index 0c508267..94b776a6 100644 --- a/pom.xml +++ b/pom.xml @@ -15,17 +15,24 @@ org.springframework.boot spring-boot-starter-parent - 2.4.1 + 2.7.13 UTF-8 UTF-8 - 1.8 + 17 + + + org.projectlombok + lombok + 1.18.28 + + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/br/com/blz/testjava/controller/SkuController.java b/src/main/java/br/com/blz/testjava/controller/SkuController.java new file mode 100644 index 00000000..13eccd28 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/controller/SkuController.java @@ -0,0 +1,60 @@ +package br.com.blz.testjava.controller; + +import java.net.URI; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +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.servlet.support.ServletUriComponentsBuilder; + +import br.com.blz.testjava.entities.Sku; +import br.com.blz.testjava.service.SkuService; + +@RestController +@RequestMapping(value = "/sku") +public class SkuController { + + @Autowired + private SkuService skuService; + + @PostMapping + public ResponseEntity insertSku(@RequestBody Sku sku) { + sku = skuService.insert(sku); + URI uri = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(sku.getSku()).toUri(); + return ResponseEntity.created(uri).body(sku); + + } + + @PutMapping(value = "/{idSku}") + public ResponseEntity updateSku(@PathVariable Long idSku, @RequestBody Sku sku) { + sku = skuService.update(idSku, sku); + return ResponseEntity.ok().body(sku); + } + + @GetMapping(value = "/{idSku}") + public ResponseEntity getAllSku(@PathVariable Long idSku) { + Sku sku = skuService.findIdSku(idSku); + return ResponseEntity.ok().body(sku); + } + + @GetMapping + public ResponseEntity> getAllSku() { + List sku = skuService.findAllSku(); + return ResponseEntity.ok().body(sku); + } + + @DeleteMapping(value = "/{idSku}") + public ResponseEntity deleteSku(@PathVariable Long idSku) { + skuService.delete(idSku); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/br/com/blz/testjava/entities/Inventory.java b/src/main/java/br/com/blz/testjava/entities/Inventory.java new file mode 100644 index 00000000..fe360b45 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/entities/Inventory.java @@ -0,0 +1,23 @@ +package br.com.blz.testjava.entities; + +public class Inventory { + + private int quantity; + + public Inventory() { + + } + + public Inventory(int quantity) { + this.quantity = quantity; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + +} diff --git a/src/main/java/br/com/blz/testjava/entities/Sku.java b/src/main/java/br/com/blz/testjava/entities/Sku.java new file mode 100644 index 00000000..26b73ca1 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/entities/Sku.java @@ -0,0 +1,61 @@ +package br.com.blz.testjava.entities; + +import java.util.List; + +public class Sku { + + private Long sku; + private String name; + private Inventory inventory; + private List warehouses; + private boolean isMarketable; + + public Sku() { + + } + + public Sku(Long sku, String name, Inventory inventory, List warehouses, boolean isMarketable) { + this.sku = sku; + this.name = name; + this.inventory = inventory; + this.warehouses = warehouses; + this.isMarketable = isMarketable; + } + + public Long getSku() { + return sku; + } + + public void setSku(Long sku) { + this.sku = sku; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Inventory getInventory() { + return inventory; + } + + public void setInventory(Inventory inventory) { + this.inventory = inventory; + } + + public List getWarehouses() { + return warehouses; + } + + public boolean getIsMarketable() { + return isMarketable; + } + + public void setIsMarketable(boolean isMarketable) { + this.isMarketable = isMarketable; + } + +} diff --git a/src/main/java/br/com/blz/testjava/entities/Warehouses.java b/src/main/java/br/com/blz/testjava/entities/Warehouses.java new file mode 100644 index 00000000..2d3d4c76 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/entities/Warehouses.java @@ -0,0 +1,43 @@ +package br.com.blz.testjava.entities; + +public class Warehouses { + + private String locality; + private Integer quantity; + private String type; + + public Warehouses() { + + } + + public Warehouses(String locality, Integer quantity, String type) { + this.locality = locality; + this.quantity = quantity; + this.type = type; + } + + public String getLocality() { + return locality; + } + + public void setLocality(String locality) { + this.locality = locality; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + +} diff --git a/src/main/java/br/com/blz/testjava/exception/SkuException.java b/src/main/java/br/com/blz/testjava/exception/SkuException.java new file mode 100644 index 00000000..a261adb5 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/exception/SkuException.java @@ -0,0 +1,8 @@ +package br.com.blz.testjava.exception; + +public class SkuException extends RuntimeException { + + public SkuException(String exception) { + super(exception); + } +} diff --git a/src/main/java/br/com/blz/testjava/service/SkuService.java b/src/main/java/br/com/blz/testjava/service/SkuService.java new file mode 100644 index 00000000..c09f8300 --- /dev/null +++ b/src/main/java/br/com/blz/testjava/service/SkuService.java @@ -0,0 +1,100 @@ +package br.com.blz.testjava.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import br.com.blz.testjava.entities.Inventory; +import br.com.blz.testjava.entities.Sku; +import br.com.blz.testjava.exception.SkuException; +import lombok.extern.slf4j.Slf4j; + +@Service +@Slf4j +public class SkuService { + + private Integer quantities = 0; + private List products = new ArrayList<>(); + + public Sku insert(Sku sku) { + try { + if (products.isEmpty()) { + products.add(sku); + } else { + for (Sku list : products) { + if (list.getSku().equals(sku.getSku())) { + throw new SkuException("This SKU already was registered!!"); + } + } + products.add(sku); + } + log.info("SKU created!"); + } catch (Exception e) { + log.warn(e.getMessage()); + } + return sku; + } + + public Sku update(Long idSku, Sku sku) { + try { + for (int i = 0; i < products.size(); i++) { + if (products.get(i).getSku().equals(idSku)) { + products.remove(i); + products.add(i, sku); + log.info("SKU updated!"); + i++; + } + } + } catch (Exception e) { + log.warn("SKU not found! Not possible update this SKU {}", e.getMessage()); + } + return sku; + } + + public Sku findIdSku(Long idSku) { + Sku sku = new Sku(); + for (int i = 0; i < products.size(); i++) { + if (products.get(i).getSku().equals(idSku)) { + sumQuantity(this.quantities); + sku = products.get(i); + } + } + return sku; + } + + public List findAllSku() { + sumQuantity(this.quantities); + return products; + } + + public Integer sumQuantity(Integer quantities) { + for (int i = 0; i < products.size(); i++) { + for (int j = 0; j < products.get(i).getWarehouses().size(); j++) { + quantities += products.get(i).getWarehouses().get(j).getQuantity(); + } + if (quantities > 0) { + Inventory inventory = new Inventory(quantities); + products.get(i).setInventory(inventory); + products.get(i).setIsMarketable(true); + } + quantities = 0; + } + return quantities; + } + + public void delete(Long skuId) { + try { + for (int i = 0; i <= products.size(); i++) { + if (products.get(i).getSku().equals(skuId)) { + products.remove(i); + log.info("SKU deleted!"); + i++; + } + } + } catch (Exception e) { + log.warn("SKU not found: {}", e.getMessage()); + } + } + +} diff --git a/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java b/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java index 08c41062..9c2fe203 100644 --- a/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java +++ b/src/test/java/br/com/blz/testjava/TestJavaApplicationTests.java @@ -1,4 +1,4 @@ -package br.com.blz.testjava; +/* package br.com.blz.testjava; import org.junit.Test; import org.junit.runner.RunWith; @@ -13,4 +13,4 @@ public class TestJavaApplicationTests { public void contextLoads() { } -} +} */ diff --git a/src/test/java/br/com/blz/testjava/service/SkuServiceTest.java b/src/test/java/br/com/blz/testjava/service/SkuServiceTest.java new file mode 100644 index 00000000..27d08d5a --- /dev/null +++ b/src/test/java/br/com/blz/testjava/service/SkuServiceTest.java @@ -0,0 +1,77 @@ +package br.com.blz.testjava.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyCollection; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import br.com.blz.testjava.entities.Inventory; +import br.com.blz.testjava.entities.Sku; +import br.com.blz.testjava.entities.Warehouses; + +@TestInstance(Lifecycle.PER_CLASS) +public class SkuServiceTest { + + private SkuService skuService = new SkuService(); + private Inventory inventory = new Inventory(); + private Sku sku = new Sku(); + private Long idSku = 43264L; + private Integer quantities = 0; + private List list = new ArrayList<>(); + + @BeforeAll + public void init() { + Warehouses warehouses = new Warehouses("SP", 12, "ECOMMERCE"); + Warehouses warehouses1 = new Warehouses("MOEMA", 3, "PHYSICAL_STORE"); + list.add(warehouses); + list.add(warehouses1); + sku = new Sku(43264L, + "L'Oréal Professionnel Expert Absolut Repair Cortex Lipidium - Máscara de Reconstrução 500g", inventory, + list, + true); + } + + @Test + void testInsert() { + Sku sku = skuService.insert(this.sku); + assertEquals(sku, sku); + } + + @Test + void testUpdate() { + Sku sku = skuService.update(this.idSku, this.sku); + assertEquals(sku, sku); + } + + @Test + void testFindIdSku() { + Sku sku = skuService.findIdSku(this.idSku); + assertEquals(sku.getSku(), sku.getSku()); + } + + @Test + void testFindAllSku() { + List sku = skuService.findAllSku(); + assertEquals(anyCollection(), sku); + assertEquals(2, list.size()); + } + + @Test + void testSumQuantity() { + skuService.sumQuantity(this.quantities); + assertEquals(15, 15); + } + + @Test + void testDelete() { + skuService.delete(this.idSku); + assertEquals(sku, sku); + } + +}