diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeControllerTest.java index 240354d70184ff12fc80dc36a4e6b214e8537704..cb449e2fb28229ad92e252504c1d88208c68094e 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeControllerTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeControllerTest.java @@ -7,7 +7,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.service.group.FridgeService; +import ntnu.idatt2016.v233.SmartMat.service.group.GroupService; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -27,8 +29,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -43,6 +44,9 @@ public class FridgeControllerTest { @Mock private FridgeService fridgeService; + @Mock + private GroupService groupService; + private Fridge fridge; private Product product; @@ -220,87 +224,250 @@ public class FridgeControllerTest { } - @Test - public void addProductToFridgeAsUser() throws Exception { - when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + @Nested + class addProduct{ + @Test + public void addProductToFridgeAsUserNotAutorized() throws Exception { + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(false); - ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser); - verify(fridgeService).isUserInFridge("test", 0L); + ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser); - assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); + verify(fridgeService).isUserInFridge("test", 0L); - } + assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); - @Test - public void addProductToFridge_notFound() throws Exception { - when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty( )); + } - when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); - when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); + @Test + public void addProductToFridge_notFound() throws Exception { + when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty( )); + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); - ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser); - verify(fridgeService).isUserInFridge("test", 0L); + ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser); - assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); - } + verify(fridgeService).isUserInFridge("test", 0L); - @Test - public void updateProductInFridge() throws Exception { - when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(fridgeProductAsso)); + assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); + } - when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); - ResponseEntity<FridgeProductAsso> responseEntity = - fridgeController.updateProductInFridge(fridgeProductRequest, adminUser); + @Test + public void addProductTOFridgeAsAdmin(){ + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(product)); - verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class)); + ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, adminUser); - assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); - } - @Test - public void updateProductInFridge_notFound() throws Exception { - when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty()); + assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); - when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); - ResponseEntity<FridgeProductAsso> responseEntity = - fridgeController.updateProductInFridge(fridgeProductRequest, adminUser); + verify(fridgeService).addProductToFridge(any(FridgeProductRequest.class)); + + + } + + @Test + public void addProductTOFridgeAsRegUserAllowed(){ + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); + when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(product)); + + ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser); + + + assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); + + verify(fridgeService).addProductToFridge(any(FridgeProductRequest.class)); + + } - verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class)); - assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); } - @Test - public void removeProductFromFridge_success() throws Exception { - when(fridgeService.removeProductFromFridge(1L)).thenReturn(true); + @Nested + class updateProduct{ + @Test + public void updateProductInFridge() throws Exception { + when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(fridgeProductAsso)); - ResponseEntity<String> responseEntity = - fridgeController.removeProductFromFridge(1L, adminUser); + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, adminUser); - verify(fridgeService).removeProductFromFridge(1L); - assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); + verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); + } + + @Test + public void updateProductInFridge_notFound() throws Exception { + when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty()); + + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, adminUser); + + + verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); + } + + @Test + public void updateNotAuthorized() throws Exception { + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(false); + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, regularUser); + + verify(fridgeService, times(0)).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); + + } + @Test + public void updateNotAuthorizedRestricted() throws Exception { + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); + when(groupService.getUserGroupAssoAuthority("test", 1L)) + .thenReturn("restricted"); + + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, regularUser); + + verify(fridgeService, times(0)).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); + + } + @Test + public void updateAuthorizedWhenNotRestricted(){ + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); + when(groupService.getUserGroupAssoAuthority("test", 1L)) + .thenReturn("USER"); + when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))) + .thenReturn(Optional.of(fridgeProductAsso)); + + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, regularUser); + + verify(fridgeService, times(1)).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } + @Test + public void updateAuthorizedWhenAdmin(){ + when(fridgeService.isUserInFridge("test", 0L)).thenReturn(true); + when(groupService.getUserGroupAssoAuthority("test", 1L)) + .thenReturn("ADMIN"); + when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))) + .thenReturn(Optional.of(fridgeProductAsso)); + + when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge)); + ResponseEntity<FridgeProductAsso> responseEntity = + fridgeController.updateProductInFridge(fridgeProductRequest, regularUser); + + verify(fridgeService, times(1)).updateProductInFridge(any(FridgeProductRequest.class)); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } } - @Test - public void removeProductFromFridge_notFound() throws Exception { - when(fridgeService.removeProductFromFridge(1L)).thenReturn(false); - ResponseEntity<String> responseEntity = - fridgeController.removeProductFromFridge(1L, adminUser); + @Nested + class removeProductFromFridge { + @Test + public void removeProductFromFridge_success() throws Exception { + when(fridgeService.removeProductFromFridge(1L)).thenReturn(true); - verify(fridgeService).removeProductFromFridge(1L); - assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); + ResponseEntity<String> responseEntity = + fridgeController.removeProductFromFridge(1L, adminUser); + + verify(fridgeService).removeProductFromFridge(1L); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); + + + } + + @Test + public void removeProductFromFridge_notFound() throws Exception { + when(fridgeService.removeProductFromFridge(1L)).thenReturn(false); + + ResponseEntity<String> responseEntity = + fridgeController.removeProductFromFridge(1L, adminUser); + + verify(fridgeService).removeProductFromFridge(1L); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND); + } + + @Test + public void removeProductNotAuthorizedWhenRestricted(){ + + when(fridgeService.isUserInGroupWithFridgeProduct("test", 1L)) + .thenReturn(true); + when(groupService.getUserGroupAssoAuthority(eq("test"), eq(1L))) + .thenReturn("restricted"); + + when(fridgeService.getGroupIdFromFridgeProuctId(1L)).thenReturn(1L); + + ResponseEntity<String> responseEntity = + fridgeController.removeProductFromFridge(1L, regularUser); + + verify(fridgeService, times(0)).removeProductFromFridge(1L); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); + } + + @Test + public void removeProductAuthorizedWhenNotRestricted(){ + + when(fridgeService.removeProductFromFridge(1L)).thenReturn(true ); + + when(fridgeService.isUserInGroupWithFridgeProduct("test", 1L)) + .thenReturn(true); + when(groupService.getUserGroupAssoAuthority(eq("test"), eq(1L))) + .thenReturn("USER"); + + when(fridgeService.getGroupIdFromFridgeProuctId(1L)).thenReturn(1L); + + ResponseEntity<String> responseEntity = + fridgeController.removeProductFromFridge(1L, regularUser); + + verify(fridgeService, times(1)).removeProductFromFridge(1L); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); + } + + @Test + public void notRemoveProductWhenNotInGroup(){ + + when(fridgeService.isUserInGroupWithFridgeProduct("test", 1L)) + .thenReturn(false); + + ResponseEntity<String> responseEntity = + fridgeController.removeProductFromFridge(1L, regularUser); + + verify(fridgeService, times(0)).removeProductFromFridge(1L); + + assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN); + } } + + }