Repository: ita-social-projects/greencity
Issue: [GreenCity] Fix bug in CustomShoppingListItemController

**Fix bug in CustomShoppingListItemController:**

`/custom/shopping-list-items/{userId}/custom-shopping-list-items`

Bug occurs when user tries to update items' status.


Changed files:
service/src/test/java/greencity/service/CustomShoppingListItemServiceImplTest.java
service/src/main/java/greencity/service/CustomShoppingListItemServiceImpl.java
service-api/src/main/java/greencity/constant/ErrorMessage.java
core/src/main/java/greencity/controller/CustomShoppingListItemController.java

diff --git a/core/src/main/java/greencity/controller/CustomShoppingListItemController.java b/core/src/main/java/greencity/controller/CustomShoppingListItemController.java
index fade95927..549d72cdf 100644
--- a/core/src/main/java/greencity/controller/CustomShoppingListItemController.java
+++ b/core/src/main/java/greencity/controller/CustomShoppingListItemController.java
@@ -105,7 +105,8 @@ public class CustomShoppingListItemController {
         @ApiResponse(code = 200, message = HttpStatuses.OK),
         @ApiResponse(code = 303, message = HttpStatuses.SEE_OTHER),
         @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
-        @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED)
+        @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
+        @ApiResponse(code = 404, message = HttpStatuses.BAD_REQUEST)
     })
     @PatchMapping("/{userId}/custom-shopping-list-items")
     public ResponseEntity<CustomShoppingListItemResponseDto> updateItemStatus(@PathVariable @CurrentUserId Long userId,
diff --git a/service-api/src/main/java/greencity/constant/ErrorMessage.java b/service-api/src/main/java/greencity/constant/ErrorMessage.java
index 6ddfcfb5b..d3a948d14 100644
--- a/service-api/src/main/java/greencity/constant/ErrorMessage.java
+++ b/service-api/src/main/java/greencity/constant/ErrorMessage.java
@@ -145,6 +145,7 @@ public final class ErrorMessage {
     public static final String ACHIEVEMENT_NOT_FOUND_BY_ID = "The name does not exist by this id: ";
     public static final String PAGE_INDEX_IS_MORE_THAN_TOTAL_PAGES = "Page index is more than total pages: ";
     public static final String MULTIPART_FILE_BAD_REQUEST = "Bad inputed image string : ";
+    public static final String INCORRECT_INPUT_ITEM_STATUS = "Incorrect input status to update item.";
 
     private ErrorMessage() {
     }
diff --git a/service/src/main/java/greencity/service/CustomShoppingListItemServiceImpl.java b/service/src/main/java/greencity/service/CustomShoppingListItemServiceImpl.java
index 7a9bb02ee..0dc1e1e00 100644
--- a/service/src/main/java/greencity/service/CustomShoppingListItemServiceImpl.java
+++ b/service/src/main/java/greencity/service/CustomShoppingListItemServiceImpl.java
@@ -12,6 +12,7 @@ import greencity.entity.Habit;
 import greencity.entity.User;
 import greencity.entity.UserShoppingListItem;
 import greencity.enums.ShoppingListItemStatus;
+import greencity.exception.exceptions.BadRequestException;
 import greencity.exception.exceptions.CustomShoppingListItemNotSavedException;
 import greencity.exception.exceptions.NotFoundException;
 import greencity.repository.CustomShoppingListItemRepo;
@@ -139,22 +140,22 @@ public class CustomShoppingListItemServiceImpl implements CustomShoppingListItem
     @Transactional
     @Override
     public CustomShoppingListItemResponseDto updateItemStatus(Long userId, Long itemId, String itemStatus) {
-        CustomShoppingListItemResponseDto customShoppingListItemResponseDto = null;
         CustomShoppingListItem customShoppingListItem =
             customShoppingListItemRepo.findByUserIdAndItemId(userId, itemId);
-        if (itemStatus.equals(ShoppingListItemStatus.DONE.toString())) {
+        if (customShoppingListItem == null) {
+            throw new NotFoundException(CUSTOM_SHOPPING_LIST_ITEM_NOT_FOUND_BY_ID);
+        }
+        if (itemStatus.equalsIgnoreCase(ShoppingListItemStatus.DONE.name())) {
             customShoppingListItem.setStatus(ShoppingListItemStatus.DONE);
-            customShoppingListItemRepo.save(customShoppingListItem);
-            customShoppingListItemResponseDto =
-                modelMapper.map(customShoppingListItem, CustomShoppingListItemResponseDto.class);
+            return modelMapper.map(customShoppingListItemRepo.save(customShoppingListItem),
+                CustomShoppingListItemResponseDto.class);
         }
-        if (itemStatus.equals(ShoppingListItemStatus.ACTIVE.toString())) {
+        if (itemStatus.equalsIgnoreCase(ShoppingListItemStatus.ACTIVE.name())) {
             customShoppingListItem.setStatus(ShoppingListItemStatus.ACTIVE);
-            customShoppingListItemRepo.save(customShoppingListItem);
-            customShoppingListItemResponseDto =
-                modelMapper.map(customShoppingListItem, CustomShoppingListItemResponseDto.class);
+            return modelMapper.map(customShoppingListItemRepo.save(customShoppingListItem),
+                CustomShoppingListItemResponseDto.class);
         }
-        return customShoppingListItemResponseDto;
+        throw new BadRequestException(ErrorMessage.INCORRECT_INPUT_ITEM_STATUS);
     }
 
     /**
diff --git a/service/src/test/java/greencity/service/CustomShoppingListItemServiceImplTest.java b/service/src/test/java/greencity/service/CustomShoppingListItemServiceImplTest.java
index 3b151fb93..a0e9e12cb 100644
--- a/service/src/test/java/greencity/service/CustomShoppingListItemServiceImplTest.java
+++ b/service/src/test/java/greencity/service/CustomShoppingListItemServiceImplTest.java
@@ -2,6 +2,7 @@ package greencity.service;
 
 import greencity.ModelUtils;
 import greencity.client.RestClient;
+import greencity.constant.ErrorMessage;
 import greencity.dto.shoppinglistitem.BulkSaveCustomShoppingListItemDto;
 import greencity.dto.shoppinglistitem.CustomShoppingListItemResponseDto;
 import greencity.dto.shoppinglistitem.CustomShoppingListItemSaveRequestDto;
@@ -13,6 +14,7 @@ import greencity.enums.EmailNotification;
 import greencity.enums.Role;
 import greencity.enums.ShoppingListItemStatus;
 import greencity.enums.UserStatus;
+import greencity.exception.exceptions.BadRequestException;
 import greencity.exception.exceptions.CustomShoppingListItemNotSavedException;
 import greencity.exception.exceptions.NotFoundException;
 import greencity.repository.CustomShoppingListItemRepo;
@@ -243,7 +245,14 @@ class CustomShoppingListItemServiceImplTest {
         when(customShoppingListItemRepo.save(customShoppingListItem1)).thenReturn(customShoppingListItem1);
         when(modelMapper.map(customShoppingListItem1, CustomShoppingListItemResponseDto.class)).thenReturn(test1);
         assertEquals(test1, customShoppingListItemService.updateItemStatus(12L, 2L, "ACTIVE"));
-
+        when(customShoppingListItemRepo.findByUserIdAndItemId(any(), anyLong())).thenReturn(null);
+        Exception thrown1 = assertThrows(NotFoundException.class,
+            () -> customShoppingListItemService.updateItemStatus(64L, 1L, "DONE"));
+        assertEquals(thrown1.getMessage(), ErrorMessage.CUSTOM_SHOPPING_LIST_ITEM_NOT_FOUND_BY_ID);
+        when(customShoppingListItemRepo.findByUserIdAndItemId(12L, 2L)).thenReturn(customShoppingListItem1);
+        Exception thrown2 = assertThrows(BadRequestException.class,
+            () -> customShoppingListItemService.updateItemStatus(12L, 2L, "NOTDONE"));
+        assertEquals(thrown2.getMessage(), ErrorMessage.INCORRECT_INPUT_ITEM_STATUS);
     }
 
     @Test