Skip to content

Commit 687a4ae

Browse files
author
hospel
authored
adjust delete as per API contract (#111)
1 parent b3c86f5 commit 687a4ae

File tree

9 files changed

+40
-52
lines changed

9 files changed

+40
-52
lines changed

src/main/java/com/answerdigital/answerking/controller/CategoryController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public ResponseEntity<CategoryResponse> updateCategory(@Valid @RequestBody final
137137
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
138138
})
139139
@DeleteMapping("/{categoryId}")
140-
public ResponseEntity<CategoryResponse> retireCategory(@PathVariable @NotNull final Long categoryId) {
141-
return new ResponseEntity<>(categoryService.retireCategory(categoryId), HttpStatus.OK);
140+
public ResponseEntity<Void> retireCategory(@PathVariable @NotNull final Long categoryId) {
141+
categoryService.retireCategory(categoryId);
142+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
142143
}
143144
}

src/main/java/com/answerdigital/answerking/controller/OrderController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ public ResponseEntity<Order> addProductToBasket(@PathVariable @NotNull final Lon
6464
}
6565

6666
@DeleteMapping(path = "/{orderId}/product/{productId}")
67-
public ResponseEntity<Order> deleteProductInBasket(@PathVariable @NotNull final Long orderId,
67+
public ResponseEntity<Void> deleteProductInBasket(@PathVariable @NotNull final Long orderId,
6868
@PathVariable @NotNull final Long productId) {
69-
return new ResponseEntity<>(orderService.deleteProductInBasket(orderId, productId), HttpStatus.OK);
69+
orderService.deleteProductInBasket(orderId, productId);
70+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
7071
}
7172

7273
@PutMapping(path = "/{orderId}/product/{productId}/quantity/{quantity}")

src/main/java/com/answerdigital/answerking/controller/ProductController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public ResponseEntity<ProductResponse> updateProduct(@PathVariable @NotNull fina
105105
schema = @Schema(implementation = ErrorResponse.class)) })
106106
})
107107
@DeleteMapping("/{id}")
108-
public ResponseEntity<ProductResponse> retireProduct(@PathVariable @NotNull final Long id) {
109-
return new ResponseEntity<>(productService.retireProduct(id), HttpStatus.OK);
108+
public ResponseEntity<Void> retireProduct(@PathVariable @NotNull final Long id) {
109+
productService.retireProduct(id);
110+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
110111
}
111112
}

src/main/java/com/answerdigital/answerking/service/CategoryService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,13 @@ public CategoryResponse removeProductFromCategory(final Long categoryId, final L
104104
return categoryMapper.convertCategoryEntityToCategoryResponse(savedCategory);
105105
}
106106

107-
public CategoryResponse retireCategory(final Long categoryId) {
107+
public void retireCategory(final Long categoryId) {
108108
final Category category = findById(categoryId);
109109
if(category.isRetired()) {
110110
throw new RetirementException(String.format("The category with ID %d is already retired", categoryId));
111111
}
112112
category.setRetired(true);
113113
final Category savedCategory = categoryRepository.save(category);
114-
return categoryMapper.convertCategoryEntityToCategoryResponse(savedCategory);
115114
}
116115

117116
public List<ProductResponse> findProductsByCategoryId(final Long categoryId) {

src/main/java/com/answerdigital/answerking/service/OrderService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public Order updateProductQuantity(final Long orderId, final Long productId, fin
100100
}
101101

102102
@Transactional
103-
public Order deleteProductInBasket(final Long orderId, final Long productId) {
103+
public void deleteProductInBasket(final Long orderId, final Long productId) {
104104
final Order order = findById(orderId);
105105
final Product product = productService.findById(productId);
106106

@@ -116,6 +116,6 @@ public Order deleteProductInBasket(final Long orderId, final Long productId) {
116116
}
117117

118118
order.getLineItems().remove(existingLineItem.get());
119-
return orderRepository.save(order);
119+
orderRepository.save(order);
120120
}
121121
}

src/main/java/com/answerdigital/answerking/service/ProductService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ public ProductResponse updateProduct(final Long productId, final ProductRequest
7777
return productMapper.convertProductEntityToProductResponse(savedProduct);
7878
}
7979

80-
public ProductResponse retireProduct(final Long productId) {
80+
public void retireProduct(final Long productId) {
8181
final Product product = findById(productId);
8282
if (product.isRetired()) {
8383
throw new RetirementException(String.format("The product with ID %d is already retired", productId));
8484
}
8585
product.setRetired(true);
8686
final Product savedProduct = productRepository.save(product);
87-
return productMapper.convertProductEntityToProductResponse(savedProduct);
8887
}
8988

9089
public List<ProductResponse> findProductsByCategoryId(final Long categoryId) {

src/test/java/com/answerdigital/answerking/service/CategoryServiceTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ void testRetireCategory() {
281281
doReturn(expectedCategory).when(categoryRepository).save(any(Category.class));
282282

283283
// then
284-
assertEquals(categoryResponse.getClass(), categoryService.retireCategory(category.getId()).getClass());
285-
verify(categoryRepository).findById(anyLong());
286-
verify(categoryRepository).save(any(Category.class));
284+
categoryService.retireCategory(category.getId());
285+
286+
verify(categoryRepository).findById(category.getId());
287+
verify(categoryRepository).save(expectedCategory);
287288
}
288289

289290
@Test
@@ -299,7 +300,7 @@ void testRetireCategoryAlreadyRetiredThrowsRetirementException() {
299300

300301
// then
301302
assertThrows(RetirementException.class, () -> categoryService.retireCategory(retiredCategory.getId()));
302-
verify(categoryRepository).findById(anyLong());
303+
verify(categoryRepository).findById(retiredCategory.getId());
303304
}
304305

305306
@Test

src/test/java/com/answerdigital/answerking/service/OrderServiceTest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,17 @@ void testUpdateProductQuantityOrderNotFoundThrowsNotFoundException() {
369369
void testUpdateProductQuantityExistingOrderProductNotPresentThrowsNotFoundException() {
370370
// Given
371371
final Product product = Product.builder()
372-
.id(12L)
373-
.name("King Burger")
374-
.description("A burger fit for a king")
375-
.price(new BigDecimal("12.99"))
376-
.retired(false)
377-
.build();
378-
final Order order = Order.builder()
379-
.lineItems(new HashSet<>())
380-
.build();
372+
.id(12L)
373+
.name("King Burger")
374+
.description("A burger fit for a king")
375+
.price(new BigDecimal("12.99"))
376+
.retired(false)
377+
.build();
378+
final Order order = Order.builder().lineItems(new HashSet<>()).build();
381379

382380
// When
383-
when(orderRepository.findById(anyLong()))
384-
.thenReturn(Optional.of(order));
385-
when(productService.findById(anyLong()))
386-
.thenReturn(product);
381+
when(orderRepository.findById(anyLong())).thenReturn(Optional.of(order));
382+
when(productService.findById(anyLong())).thenReturn(product);
387383

388384
// Then
389385
assertThrows(NotFoundException.class,
@@ -395,9 +391,7 @@ void testUpdateProductQuantityExistingOrderProductNotPresentThrowsNotFoundExcept
395391
@Test
396392
void testDeleteProductInBasket() {
397393
// Given
398-
final Product product = Product.builder()
399-
.id(12L)
400-
.build();
394+
final Product product = Product.builder().id(12L).build();
401395

402396
final Order order = Order.builder()
403397
.id(12L)
@@ -411,22 +405,17 @@ void testDeleteProductInBasket() {
411405
.build();
412406
order.getLineItems().add(lineItem);
413407

414-
final Order expectedResponse = Order.builder()
415-
.id(12L)
416-
.build();
408+
final Order expectedResponse = Order.builder().id(12L).build();
417409

418410
// When
419411
when(orderRepository.findById(anyLong()))
420412
.thenReturn(Optional.of(order));
421-
when(productService.findById(anyLong()))
422-
.thenReturn(product);
423-
when(orderRepository.save(any(Order.class)))
424-
.thenReturn(expectedResponse);
413+
when(productService.findById(anyLong())).thenReturn(product);
414+
when(orderRepository.save(any(Order.class))).thenReturn(expectedResponse);
425415

426-
final Order response = orderService.deleteProductInBasket(ORDER_ID, PRODUCT_ID);
416+
orderService.deleteProductInBasket(ORDER_ID, PRODUCT_ID);
427417

428418
// Then
429-
assertEquals(expectedResponse, response);
430419
verify(orderRepository).findById(anyLong());
431420
verify(productService).findById(anyLong());
432421
verify(orderRepository).save(any(Order.class));

src/test/java/com/answerdigital/answerking/service/ProductServiceTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525
import static org.junit.jupiter.api.Assertions.assertFalse;
26-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2726
import static org.junit.jupiter.api.Assertions.assertThrows;
2827
import static org.mockito.Mockito.any;
2928
import static org.mockito.Mockito.anyLong;
@@ -174,15 +173,15 @@ void updateProductThrowsExceptionIfProductNameAlreadyExist() {
174173
@Test
175174
void testRetireProduct() {
176175
// when
177-
when(productRepository.findById(anyLong()))
178-
.thenReturn(Optional.of(product));
179-
when(productRepository.save(any(Product.class)))
180-
.thenReturn(product);
176+
when(productRepository.findById(anyLong())).thenReturn(Optional.of(product));
177+
when(productRepository.save(any(Product.class))).thenReturn(product);
181178

182179
// then
183-
assertNotEquals(product.isRetired(), productService.retireProduct(PRODUCT_ID).isRetired());
180+
productService.retireProduct(PRODUCT_ID);
181+
product.setRetired(true);
182+
184183
verify(productRepository).findById(anyLong());
185-
verify(productRepository).save(any(Product.class));
184+
verify(productRepository).save(product);
186185
}
187186

188187
@Test
@@ -192,8 +191,7 @@ void testRetireProductAlreadyRetiredThrowsRetirementException() {
192191
expectedProduct.setRetired(true);
193192

194193
// when
195-
when(productRepository.findById(anyLong()))
196-
.thenReturn(Optional.of(expectedProduct));
194+
when(productRepository.findById(anyLong())).thenReturn(Optional.of(expectedProduct));
197195

198196
// then
199197
assertThrows(RetirementException.class, () -> productService.retireProduct(PRODUCT_ID));
@@ -203,8 +201,7 @@ void testRetireProductAlreadyRetiredThrowsRetirementException() {
203201
@Test
204202
void testRetireProductDoesNotExistThrowsNotFoundException() {
205203
// when
206-
when(productRepository.findById(anyLong()))
207-
.thenReturn(Optional.empty());
204+
when(productRepository.findById(anyLong())).thenReturn(Optional.empty());
208205

209206
// then
210207
assertThrows(NotFoundException.class, () -> productService.retireProduct(PRODUCT_ID));

0 commit comments

Comments
 (0)