Skip to content

Commit 4422011

Browse files
MarceloMarcelo
authored andcommitted
criado metodo de edicao de produto por sku
1 parent dbb26bf commit 4422011

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/main/java/br/com/blz/testjava/business/ProductBusiness.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ public Product findBySku(Long sku) {
2929
return Optional.ofNullable(this.productService.findBySku(sku))
3030
.orElseThrow(() -> new SkuNotExistsException(ErrorEnum.SKU_NOT_EXISTS.getDescription()));
3131
}
32+
33+
public Product update(Long sku, Product product) {
34+
var productSaved = Optional.ofNullable(this.productService.findBySku(sku))
35+
.orElseThrow(() -> new SkuNotExistsException(ErrorEnum.SKU_NOT_EXISTS.getDescription()));
36+
productSaved.setInventory(product.getInventory());
37+
productSaved.setName(product.getName());
38+
return this.productService.save(productSaved);
39+
}
3240
}

src/main/java/br/com/blz/testjava/resources/v1/ProductResource.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ public ResponseEntity<Product> findBySku(@PathVariable(value = "sku") Long sku)
4141
return new ResponseEntity<>(this.productBusiness.findBySku(sku), HttpStatus.OK);
4242
}
4343

44+
@ApiOperation(value = "Api resposável por editar um produto por sku", nickname = "Edição de produto")
45+
@ApiResponses({
46+
@ApiResponse(code = 200, message = "Retorno de sucesso na edição de um produto"),
47+
@ApiResponse(code = 422, message = "Retorno de erro de regras de negocio"),
48+
@ApiResponse(code = 500, message = "Retorno de erro interno da aplicação")
49+
})
50+
@PutMapping(value = "/{sku}")
51+
@ResponseStatus(HttpStatus.OK)
52+
public ResponseEntity<Product> update(@PathVariable(value = "sku") Long sku,
53+
@RequestBody Product product) {
54+
return new ResponseEntity<>(this.productBusiness.update(sku, product), HttpStatus.OK);
55+
}
56+
4457
}

src/test/java/br/com/blz/testjava/business/ProductBusinessTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package br.com.blz.testjava.business;
22

33
import br.com.blz.testjava.TestJavaApplication;
4+
import br.com.blz.testjava.domain.Product;
45
import br.com.blz.testjava.enums.WarehouseType;
56
import br.com.blz.testjava.exception.SkuExistsException;
67
import br.com.blz.testjava.exception.SkuNotExistsException;
@@ -98,4 +99,33 @@ void mustThrowExceptionBecauseProductBySkuNotExistsInBase() {
9899
Assertions.assertThrows(SkuNotExistsException.class, () -> this.productBusiness.findBySku(12234l));
99100
}
100101

102+
@Test
103+
void deveEstourarExcecaoAoFazerUpdatePoisProdutoNaoExiste() {
104+
Assertions.assertThrows(SkuNotExistsException.class, () -> this.productBusiness.update(12234l,
105+
Product.builder().build()));
106+
}
107+
108+
@Test
109+
void deveAlterarProdutoComSucesso() {
110+
var warehouses = List.of(createWarehouse("SP", WarehouseType.PHYSICAL_STORE,
111+
250l));
112+
var inventory = createInventory(warehouses);
113+
var product = createProduct("ps5 digital edition", 12234l, inventory);
114+
115+
var newWarehouses = List.of(createWarehouse("SP", WarehouseType.PHYSICAL_STORE,
116+
250l), createWarehouse("SC", WarehouseType.PHYSICAL_STORE,
117+
250l));
118+
var newInventory = createInventory(newWarehouses);
119+
var newProduct = createProduct("ps5 digital edition", 12234l, newInventory);
120+
121+
this.productBusiness.save(product);
122+
var newProductSaved = this.productBusiness.update(12234l, newProduct);
123+
124+
Assertions.assertAll(
125+
() -> Assertions.assertTrue(newProductSaved.isMarketable()),
126+
() -> Assertions.assertEquals(500l, newProductSaved.getInventory().getQuantity()),
127+
() -> Assertions.assertEquals(2, newProductSaved.getInventory().getWarehouses().size()),
128+
() -> Assertions.assertEquals(newProduct.getName(), newProductSaved.getName()));
129+
}
130+
101131
}

0 commit comments

Comments
 (0)