diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java index 2b44cec707cf6676ddc34ec650f7c6797db7437f..6646026e7704c060b85549eb1530d7a00da8b8ae 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java @@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; +import ntnu.idatt2016.v233.SmartMat.util.CategoryUtil; +import ntnu.idatt2016.v233.SmartMat.util.ProductUtil; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -38,6 +40,11 @@ public class ProductController { .url(productRequest.image()) .build(); + + + + CategoryUtil.defineCategory(product.getName(),product.getDescription()); + if(productService.getProductById(productRequest.ean()).isPresent()) return ResponseEntity.status(409).build(); diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeProductAssoService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeProductAssoService.java index 6dccc0791a15e71b29e22999fd7db1b9260ec0be..59f7b17d0f9a32e61caf9f21fa3721a692b99386 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeProductAssoService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeProductAssoService.java @@ -51,7 +51,7 @@ public class FridgeProductAssoService { * Deletes a fridge product association * @param fridgeProductAsso the fridge product association to delete */ - public void deleteFridgeProductAsso(FridgeProductAsso fridgeProductAsso) { + public boolean deleteFridgeProductAsso(FridgeProductAsso fridgeProductAsso) { fridgeProductAsso.getFridgeId().getProducts().remove(fridgeProductAsso); fridgeProductAsso.getEan().getFridges().remove(fridgeProductAsso); fridgeProductAssoRepository.delete(fridgeProductAsso); @@ -59,5 +59,7 @@ public class FridgeProductAssoService { fridgeRepository.save(fridgeProductAsso.getFridgeId()); productRepository.save(fridgeProductAsso.getEan()); + + return true; } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java index 0f036e2b17f4f708b249286a05e076f47861b259..6406aedf256a272b5b20103bb125a9bbba99dda0 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java @@ -91,7 +91,6 @@ public class FridgeService { Fridge fridge = fridgeRepository.findByGroupGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); - if (product.isPresent()) { Product productToRemove = product.get(); if (!fridge.getProducts().contains( diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeServiceTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..edd061c1ddc1ac2f60b1d01640aa8b360114ffb6 --- /dev/null +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeServiceTest.java @@ -0,0 +1,102 @@ +package ntnu.idatt2016.v233.SmartMat.service.group; + +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 java.sql.Date; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Optional; + +import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; +import ntnu.idatt2016.v233.SmartMat.service.group.FridgeProductAssoService; +import ntnu.idatt2016.v233.SmartMat.service.group.FridgeService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso; +import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; +import ntnu.idatt2016.v233.SmartMat.entity.group.Group; +import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository; +import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; + + +public class FridgeServiceTest { + + @Mock + private FridgeRepository fridgeRepository; + + @Mock + private ProductService productService; + + @Mock + private FridgeProductAssoService fridgeProductAssoService; + + @Mock + private GroupRepository groupRepository; + + @InjectMocks + private FridgeService fridgeService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testAddProductToFridge() { + // Arrange + long groupId = 1L; + long ean = 12345L; + Optional<Product> product = Optional.of(Product.builder() + .ean(ean) + .name("Test Product") + .build()); + Fridge fridge = new Fridge(); + fridge.setProducts(new ArrayList<>()); + Group group = new Group(); + fridge.setGroup(group); + group.setFridge(fridge); + + when(productService.getProductById(ean)).thenReturn(product); + when(fridgeRepository.findByGroupGroupId(groupId)).thenReturn(Optional.of(fridge)); + when(fridgeProductAssoService.createFridgeProductAsso(any(Fridge.class), any(Product.class), any(Date.class))).thenReturn(new FridgeProductAsso()); + + // Act + boolean result = fridgeService.addProductToFridge(groupId, ean); + + + verify(fridgeProductAssoService).createFridgeProductAsso(any(Fridge.class), any(Product.class), any(Date.class)); + + + // Assert + assertTrue(result); + } + + + + @Test + public void testAddGroupToFridge() { + // Arrange + long fridgeId = 1L; + long groupId = 1L; + Optional<Fridge> fridge = Optional.of(new Fridge()); + Optional<Group> group = Optional.of(new Group()); + + when(fridgeRepository.findById(fridgeId)).thenReturn(fridge); + when(groupRepository.findByGroupId(groupId)).thenReturn(group); + + // Act + fridgeService.addGroupToFridge(fridgeId, groupId); + + // Assert + assertEquals(fridge.get().getGroup(), group.get()); + assertEquals(group.get().getFridge(), fridge.get()); + } +} diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupServiceTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupServiceTest.java index 11aab39a219c2fb2da8dbcaafe14b3cf2533c5f2..bb980bfa66ca08c7cc88269fece031a42ab7b909 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupServiceTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupServiceTest.java @@ -1,29 +1,32 @@ package ntnu.idatt2016.v233.SmartMat.service.group; +import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; +import ntnu.idatt2016.v233.SmartMat.entity.group.Group; +import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository; +import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.util.Collections; +import java.util.List; import java.util.Optional; -import ntnu.idatt2016.v233.SmartMat.entity.group.Group; -import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; -import ntnu.idatt2016.v233.SmartMat.util.GroupUtil; - +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; -import static org.junit.jupiter.api.Assertions.*; -class GroupServiceTest { - - @InjectMocks - private GroupService groupService; +public class GroupServiceTest { @Mock private GroupRepository groupRepository; + @Mock + private FridgeRepository fridgeRepository; + + @InjectMocks + private GroupService groupService; + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); @@ -31,62 +34,61 @@ class GroupServiceTest { @Test void testGetGroupByName() { + // Arrange String groupName = "Test Group"; Group group = new Group(); group.setGroupName(groupName); - when(groupRepository.findByGroupName(groupName)).thenReturn(Optional.of(group)); + // Act Optional<Group> result = groupService.getGroupByName(groupName); - assertTrue(result.isPresent()); - assertEquals(groupName, result.get().getGroupName()); - } - - @Test - void testGetGroupById() { - long groupId = 1L; - Group group = new Group(); - group.setGroupId(groupId); - - when(groupRepository.findById(groupId)).thenReturn(Optional.of(group)); - - Optional<Group> result = groupService.getGroupById(groupId); - - assertTrue(result.isPresent()); - assertEquals(groupId, result.get().getGroupId()); + // Assert + assertEquals(group, result.get()); + verify(groupRepository).findByGroupName(groupName); } @Test void testCreateGroup() { - String groupName = "New Group"; + // Arrange + String groupName = "Test Group"; Group group = new Group(); group.setGroupName(groupName); - when(groupRepository.findByGroupName(groupName)).thenReturn(Optional.empty()); - when(groupRepository.findAllLinkCode()).thenReturn(Collections.emptyList()); - when(groupRepository.save(any(Group.class))).thenAnswer(invocation -> invocation.getArgument(0)); + when(groupRepository.findAllLinkCode()).thenReturn(List.of()); + when(groupRepository.save(group)).thenReturn(group); - Group createdGroup = groupService.createGroup(group); + // Act + Group result = groupService.createGroup(group); - assertEquals(groupName, createdGroup.getGroupName()); - assertNotNull(createdGroup.getLinkCode()); + // Assert + assertEquals(group, result); + verify(groupRepository).findByGroupName(groupName); + verify(groupRepository).findAllLinkCode(); + verify(groupRepository).save(group); } @Test - void testGetLevelByGroupId() { - long groupId = 1L; - long level = 3L; + void testAddFridgeToGroup() { + // Arrange + long fridgeId = 1L; + long groupId = 2L; + Fridge fridge = new Fridge(); Group group = new Group(); - group.setGroupId(groupId); - group.setLevel(level); - - when(groupRepository.findById(groupId)).thenReturn(Optional.of(group)); - when(groupRepository.getLevelByGroupId(groupId)).thenReturn(Optional.of(level)); - - Optional<Long> result = groupService.getLevelByGroupId(groupId); - - assertTrue(result.isPresent()); - assertEquals(level, result.get()); + when(fridgeRepository.findById(fridgeId)).thenReturn(Optional.of(fridge)); + when(groupRepository.findByGroupId(groupId)).thenReturn(Optional.of(group)); + when(groupRepository.save(group)).thenReturn(group); + when(fridgeRepository.save(fridge)).thenReturn(fridge); + + // Act + groupService.addFridgeToGroup(fridgeId, groupId); + + // Assert + assertEquals(group, fridge.getGroup()); + assertEquals(fridge, group.getFridge()); + verify(fridgeRepository).findById(fridgeId); + verify(groupRepository).findByGroupId(groupId); + verify(groupRepository).save(group); + verify(fridgeRepository).save(fridge); } }