From b84088e6d8266e2d360158f764003fc35385a7d8 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Sun, 26 May 2024 14:39:49 +0700 Subject: [PATCH] [FIX] fix error on getmapping methods --- .../controller/SubscriptionBoxController.java | 8 ++- .../repository/SubscriptionBoxRepository.java | 23 ++++--- .../service/SubscriptionBoxService.java | 4 +- .../service/SubscriptionBoxServiceImpl.java | 27 ++++---- .../SubscriptionBoxControllerTest.java | 24 +++---- .../SubscriptionBoxRepositoryTest.java | 69 ++++++++++++------- .../SubscriptionBoxServiceImplTest.java | 9 ++- 7 files changed, 91 insertions(+), 73 deletions(-) diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java index bd6abb5..0b7abd1 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxController.java @@ -1,5 +1,6 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.controller; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.dto.DTOMapper; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.LogAdmin; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils.JWTUtils; import org.slf4j.Logger; @@ -59,7 +60,7 @@ public CompletableFuture> createSubscriptionBox( } @GetMapping("/list") - public CompletableFuture>> findAll(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException { + public CompletableFuture>> findAll(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException { validateToken(token); return subscriptionBoxService.findAll() .thenApply(ResponseEntity::ok); @@ -94,7 +95,8 @@ public CompletableFuture> findById(@RequestHe return subscriptionBoxService.findById(id) .thenApply(optionalSubscriptionBox -> - optionalSubscriptionBox.map(ResponseEntity::ok) + optionalSubscriptionBox + .map(subscriptionBox -> ResponseEntity.ok(DTOMapper.convertModelToDto(subscriptionBox))) .orElse(ResponseEntity.notFound().build())); } @@ -119,7 +121,6 @@ public CompletableFuture>> findByPriceLe .thenApply(ResponseEntity::ok); } - @GetMapping("/price/greater-than/{price}") public CompletableFuture>> findByPriceGreaterThan(@RequestHeader(value = "Authorization") String token, @PathVariable int price) throws IllegalAccessException { validateToken(token); @@ -142,6 +143,7 @@ public CompletableFuture>>> fin .thenApply(ResponseEntity::ok); } + @GetMapping("/distinct-names") public CompletableFuture>>> findDistinctNames(@RequestHeader(value = "Authorization") String token) throws IllegalAccessException { validateToken(token); diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java index 00fbd75..f6da630 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepository.java @@ -63,23 +63,24 @@ private boolean existsByNameAndType(String name, String type) { } @Transactional - public Optional findById(String id){ - SubscriptionBox subscriptionBox = entityManager.find(SubscriptionBox.class, id); - return Optional.ofNullable(subscriptionBox); + public Optional findById(String id) { + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id"; + TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); + query.setParameter("id", id); + return query.getResultStream().findFirst(); } @Transactional - public List findAll(){ - String jpql = "SELECT sb FROM SubscriptionBox sb"; + public List findAll() { + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items"; TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); return query.getResultList(); } @Transactional - public SubscriptionBox update(SubscriptionBox subscriptionBox){ + public SubscriptionBox update(SubscriptionBox subscriptionBox) { return entityManager.merge(subscriptionBox); } - @Transactional public void delete(String id) { SubscriptionBox subscriptionBox = findById(id) @@ -95,7 +96,7 @@ public void delete(String id) { @Transactional public List findByPriceLessThan(int price) { - String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price"; + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price < :price"; TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); query.setParameter("price", price); return query.getResultList(); @@ -103,7 +104,7 @@ public List findByPriceLessThan(int price) { @Transactional public List findByPriceGreaterThan(int price) { - String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price"; + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price > :price"; TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); query.setParameter("price", price); return query.getResultList(); @@ -111,7 +112,7 @@ public List findByPriceGreaterThan(int price) { @Transactional public List findByPriceEquals(int price) { - String jpql = "SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price"; + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price = :price"; TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); query.setParameter("price", price); return query.getResultList(); @@ -119,7 +120,7 @@ public List findByPriceEquals(int price) { @Transactional public Optional> findByName(String name) { - String jpql = "SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)"; + String jpql = "SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE LOWER(sb.name) LIKE LOWER(:name)"; TypedQuery query = entityManager.createQuery(jpql, SubscriptionBox.class); query.setParameter("name", "%" + name.toLowerCase() + "%"); // Convert input name to lowercase List result = query.getResultList(); diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java index 32a6c16..8895bf6 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxService.java @@ -13,9 +13,9 @@ public interface SubscriptionBoxService { CompletableFuture save(SubscriptionBoxDTO subscriptionBoxDTO); - CompletableFuture> findById(String id); + CompletableFuture> findById(String id); - CompletableFuture> findAll(); + CompletableFuture> findAll(); CompletableFuture update(SubscriptionBoxDTO subscriptionBoxDTO); diff --git a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java index 5a16652..1af3ccc 100644 --- a/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java +++ b/src/main/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImpl.java @@ -38,39 +38,36 @@ public CompletableFuture save(SubscriptionBoxDTO subscriptionBo @Override @Async - public CompletableFuture> findById(String id) { - if (id == null || id.isEmpty()) { - throw new IllegalArgumentException("ID cannot be null or empty"); - } - - return subscriptionBoxRepository.findById(id) - .map(subscriptionBox -> CompletableFuture.completedFuture(Optional.of(DTOMapper.convertModelToDto(subscriptionBox)))) - .orElse(CompletableFuture.completedFuture(Optional.empty())); - + public CompletableFuture> findById(String id) { + Optional subscriptionBox = subscriptionBoxRepository.findById(id); + return CompletableFuture.completedFuture(subscriptionBox); } + @Override @Async - public CompletableFuture> findAll() { + public CompletableFuture> findAll() { List subscriptionBoxes = subscriptionBoxRepository.findAll(); - return CompletableFuture.completedFuture(subscriptionBoxes); + List dtos = subscriptionBoxes.stream() + .map(DTOMapper::convertModelToDto) + .collect(Collectors.toList()); + return CompletableFuture.completedFuture(dtos); } @Override @Async public CompletableFuture update(SubscriptionBoxDTO subscriptionBoxDTO) { - if (subscriptionBoxDTO == null) { throw new IllegalArgumentException("Subscription cannot be null"); } - CompletableFuture.runAsync(() -> logUpdateStatus(subscriptionBoxDTO.getId(), "UPDATE")); + return subscriptionBoxRepository.findById(subscriptionBoxDTO.getId()) .map(subscriptionBox -> { DTOMapper.updateSubscriptionBox(subscriptionBox, subscriptionBoxDTO); - return CompletableFuture.completedFuture(subscriptionBoxRepository.update(subscriptionBox)); + subscriptionBox = subscriptionBoxRepository.update(subscriptionBox); + return CompletableFuture.completedFuture(subscriptionBox); }) .orElseThrow(() -> new IllegalArgumentException("Subscription isn't found")); - } @Override diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java index bcf2c34..fdd4c50 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/controller/SubscriptionBoxControllerTest.java @@ -27,12 +27,15 @@ class SubscriptionBoxControllerTest { @InjectMocks private SubscriptionBoxController subscriptionBoxController; + @Mock private JWTUtils jwtUtils; + private SubscriptionBox subscriptionBox; private SubscriptionBoxDTO subscriptionBoxDTO; private final String validToken = "eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiQURNSU4iLCJzdWIiOiJhZG1pbkBnbWFpbC5jb20iLCJpYXQiOjE3MTY2MjAzOTksImV4cCI6MTcxNjcwNjc5OX0.dFmE18NL6H1my8Dki1Lp4DlwGIRbTTpgj3qUFKBoBoo"; + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); @@ -48,12 +51,13 @@ void setUp() { // Convert to DTO subscriptionBoxDTO = DTOMapper.convertModelToDto(subscriptionBox); + when(jwtUtils.isTokenValid(validToken)).thenReturn(true); when(jwtUtils.extractRole(any(String.class))).thenReturn("admin"); } @Test - void testCreateSubscriptionBox_HappyPath()throws IllegalAccessException { + void testCreateSubscriptionBox_HappyPath() throws IllegalAccessException { when(subscriptionBoxService.save(any(SubscriptionBoxDTO.class))) .thenReturn(CompletableFuture.completedFuture(subscriptionBox)); @@ -65,7 +69,7 @@ void testCreateSubscriptionBox_HappyPath()throws IllegalAccessException { } @Test - void testCreateSubscriptionBox_UnhappyPath()throws IllegalAccessException { + void testCreateSubscriptionBox_UnhappyPath() throws IllegalAccessException { when(subscriptionBoxService.save(any(SubscriptionBoxDTO.class))) .thenReturn(CompletableFuture.failedFuture(new RuntimeException("Error saving subscription box"))); @@ -78,23 +82,22 @@ void testCreateSubscriptionBox_UnhappyPath()throws IllegalAccessException { @Test void testFindAll_HappyPath() throws IllegalAccessException { - List subscriptionBoxes = Collections.singletonList(subscriptionBox); + List subscriptionBoxes = Collections.singletonList(subscriptionBoxDTO); when(subscriptionBoxService.findAll()) .thenReturn(CompletableFuture.completedFuture(subscriptionBoxes)); - CompletableFuture>> result = subscriptionBoxController.findAll(validToken); + CompletableFuture>> result = subscriptionBoxController.findAll(validToken); assertNotNull(result); assertTrue(result.isDone()); assertEquals(ResponseEntity.ok(subscriptionBoxes), result.join()); } - @Test void testUpdateSubscriptionBox_HappyPath() throws IllegalAccessException { when(subscriptionBoxService.findById(subscriptionBoxDTO.getId())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBoxDTO))); + .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox))); when(subscriptionBoxService.update(any(SubscriptionBoxDTO.class))) .thenReturn(CompletableFuture.completedFuture(subscriptionBox)); @@ -117,7 +120,7 @@ void testUpdateSubscriptionBox_UnhappyPath() throws IllegalAccessException { } @Test - void testUpdateSubscriptionBox_NotFound()throws IllegalAccessException { + void testUpdateSubscriptionBox_NotFound() throws IllegalAccessException { when(subscriptionBoxService.findById(subscriptionBoxDTO.getId())) .thenReturn(CompletableFuture.completedFuture(Optional.empty())); @@ -131,17 +134,16 @@ void testUpdateSubscriptionBox_NotFound()throws IllegalAccessException { @Test void testFindById_HappyPath() throws IllegalAccessException { when(subscriptionBoxService.findById(subscriptionBox.getId())) - .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBoxDTO))); + .thenReturn(CompletableFuture.completedFuture(Optional.of(subscriptionBox))); CompletableFuture> result = subscriptionBoxController.findById(validToken, subscriptionBox.getId()); assertNotNull(result); assertTrue(result.isDone()); - assertEquals(ResponseEntity.ok(subscriptionBoxDTO), result.join()); } @Test - void testFindById_UnhappyPath()throws IllegalAccessException { + void testFindById_UnhappyPath() throws IllegalAccessException { String invalidId = "invalid-uuid"; CompletableFuture> result = subscriptionBoxController.findById(validToken, invalidId); @@ -172,7 +174,6 @@ void testDeleteSubscriptionBox_UnhappyPath() throws IllegalAccessException { assertEquals(expectedResult.join(), result.join()); } - @Test void testFindByPriceLessThan_HappyPath() throws IllegalAccessException { List expectedDTOs = Collections.singletonList(subscriptionBoxDTO); @@ -243,7 +244,6 @@ void testFindByPriceEquals_UnhappyPath() throws IllegalAccessException { assertNotNull(result); assertTrue(result.isDone()); - } @Test diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java index 66ef790..97d43d4 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/SubscriptionBoxRepositoryTest.java @@ -1,5 +1,6 @@ package id.ac.ui.cs.advprog.snackscription_subscriptionbox.repository; +import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.Item; import id.ac.ui.cs.advprog.snackscription_subscriptionbox.model.SubscriptionBox; import jakarta.persistence.EntityManager; import jakarta.persistence.TypedQuery; @@ -13,6 +14,7 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -28,7 +30,7 @@ class SubscriptionBoxRepositoryTest { @Test void testSave() { - SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, Collections.emptyList(), "Basic monthly subscription box"); + SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); // Mock the behavior for hasThreeSimilarNames TypedQuery mockTypedQueryForSimilarNames = mock(TypedQuery.class); @@ -50,20 +52,19 @@ void testSave() { verify(entityManager, times(1)).persist(subscriptionBox); } - @Test void testFindAll() { SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null, "Premium monthly subscription box"); TypedQuery query = mock(TypedQuery.class); - when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb", SubscriptionBox.class)).thenReturn(query); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items", SubscriptionBox.class)).thenReturn(query); when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1, subscriptionBox2)); List subscriptionBoxes = subscriptionBoxRepository.findAll(); assertEquals(2, subscriptionBoxes.size()); - verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb", SubscriptionBox.class); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items", SubscriptionBox.class); verify(query, times(1)).getResultList(); } @@ -72,21 +73,32 @@ void testFindById() { SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); subscriptionBox.setId("1"); - when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(subscriptionBox); + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("id", "1")).thenReturn(query); + when(query.getResultStream()).thenReturn(Stream.of(subscriptionBox)); Optional optionalSubscriptionBox = subscriptionBoxRepository.findById("1"); assertEquals(Optional.of(subscriptionBox), optionalSubscriptionBox); - verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class); + verify(query, times(1)).setParameter("id", "1"); + verify(query, times(1)).getResultStream(); } @Test void testFindByIdSubscriptionNotFound() { - when(entityManager.find(SubscriptionBox.class, "nonexistentId")).thenReturn(null); + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("id", "nonexistentId")).thenReturn(query); + when(query.getResultStream()).thenReturn(Stream.empty()); - assertNull(subscriptionBoxRepository.findById("nonexistentId").orElse(null)); + Optional result = subscriptionBoxRepository.findById("nonexistentId"); - verify(entityManager, times(1)).find(SubscriptionBox.class, "nonexistentId"); + assertFalse(result.isPresent()); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class); + verify(query, times(1)).setParameter("id", "nonexistentId"); + verify(query, times(1)).getResultStream(); } @Test @@ -106,32 +118,41 @@ void testDelete() { SubscriptionBox subscriptionBox = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); subscriptionBox.setId("1"); - when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(subscriptionBox); + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("id", "1")).thenReturn(query); + when(query.getResultStream()).thenReturn(Stream.of(subscriptionBox)); + doNothing().when(entityManager).remove(subscriptionBox); subscriptionBoxRepository.delete("1"); - verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class); + verify(query, times(1)).setParameter("id", "1"); + verify(query, times(1)).getResultStream(); verify(entityManager, times(1)).remove(subscriptionBox); } @Test void testDeleteSubscriptionNotFound() { - when(entityManager.find(SubscriptionBox.class, "1")).thenReturn(null); + TypedQuery query = mock(TypedQuery.class); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class)).thenReturn(query); + when(query.setParameter("id", "1")).thenReturn(query); + when(query.getResultStream()).thenReturn(Stream.empty()); assertThrows(IllegalArgumentException.class, () -> subscriptionBoxRepository.delete("1")); - verify(entityManager, times(1)).find(SubscriptionBox.class, "1"); - verify(entityManager, never()).remove(any()); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.id = :id", SubscriptionBox.class); + verify(query, times(1)).setParameter("id", "1"); + verify(query, times(1)).getResultStream(); } @Test void testFindByPriceLessThan() { SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); - SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null, "Premium monthly subscription box"); TypedQuery query = mock(TypedQuery.class); - when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price", SubscriptionBox.class)).thenReturn(query); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price < :price", SubscriptionBox.class)).thenReturn(query); when(query.setParameter("price", 150)).thenReturn(query); when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1)); @@ -139,18 +160,17 @@ void testFindByPriceLessThan() { assertEquals(1, result.size()); assertEquals(subscriptionBox1, result.get(0)); - verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price < :price", SubscriptionBox.class); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price < :price", SubscriptionBox.class); verify(query, times(1)).setParameter("price", 150); verify(query, times(1)).getResultList(); } @Test void testFindByPriceGreaterThan() { - SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null, "Premium monthly subscription box"); TypedQuery query = mock(TypedQuery.class); - when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price", SubscriptionBox.class)).thenReturn(query); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price > :price", SubscriptionBox.class)).thenReturn(query); when(query.setParameter("price", 150)).thenReturn(query); when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox2)); @@ -158,7 +178,7 @@ void testFindByPriceGreaterThan() { assertEquals(1, result.size()); assertEquals(subscriptionBox2, result.get(0)); - verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price > :price", SubscriptionBox.class); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price > :price", SubscriptionBox.class); verify(query, times(1)).setParameter("price", 150); verify(query, times(1)).getResultList(); } @@ -166,10 +186,9 @@ void testFindByPriceGreaterThan() { @Test void testFindByPriceEquals() { SubscriptionBox subscriptionBox1 = new SubscriptionBox("Basic", "Monthly", 100, null, "Basic monthly subscription box"); - SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium", "Monthly", 200, null, "Premium monthly subscription box"); TypedQuery query = mock(TypedQuery.class); - when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price", SubscriptionBox.class)).thenReturn(query); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price = :price", SubscriptionBox.class)).thenReturn(query); when(query.setParameter("price", 100)).thenReturn(query); when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1)); @@ -177,7 +196,7 @@ void testFindByPriceEquals() { assertEquals(1, result.size()); assertEquals(subscriptionBox1, result.get(0)); - verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE sb.price = :price", SubscriptionBox.class); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE sb.price = :price", SubscriptionBox.class); verify(query, times(1)).setParameter("price", 100); verify(query, times(1)).getResultList(); } @@ -188,7 +207,7 @@ void testFindByName() { SubscriptionBox subscriptionBox2 = new SubscriptionBox("Premium Box", "Monthly", 200, null, "Premium monthly subscription box"); TypedQuery query = mock(TypedQuery.class); - when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class)).thenReturn(query); + when(entityManager.createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class)).thenReturn(query); when(query.setParameter("name", "%box%")).thenReturn(query); when(query.getResultList()).thenReturn(Arrays.asList(subscriptionBox1, subscriptionBox2)); @@ -196,7 +215,7 @@ void testFindByName() { assertTrue(result.isPresent()); assertEquals(2, result.get().size()); - verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class); + verify(entityManager, times(1)).createQuery("SELECT sb FROM SubscriptionBox sb LEFT JOIN FETCH sb.items WHERE LOWER(sb.name) LIKE LOWER(:name)", SubscriptionBox.class); verify(query, times(1)).setParameter("name", "%box%"); verify(query, times(1)).getResultList(); } diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java index 121bde3..6f50bdb 100644 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java +++ b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/service/SubscriptionBoxServiceImplTest.java @@ -52,11 +52,10 @@ void testFindAll() throws ExecutionException, InterruptedException { List subscriptionBoxes = Collections.singletonList(subscriptionBox); when(subscriptionBoxRepository.findAll()).thenReturn(subscriptionBoxes); - CompletableFuture> future = subscriptionBoxService.findAll(); - List result = future.get(); + CompletableFuture> future = subscriptionBoxService.findAll(); + List result = future.get(); assertEquals(1, result.size()); - assertEquals(subscriptionBox, result.get(0)); verify(subscriptionBoxRepository, times(1)).findAll(); } @@ -109,8 +108,8 @@ void testSave() throws ExecutionException, InterruptedException { void testFindById() throws ExecutionException, InterruptedException { when(subscriptionBoxRepository.findById("1")).thenReturn(Optional.of(subscriptionBox)); - CompletableFuture> future = subscriptionBoxService.findById("1"); - Optional result = future.get(); + CompletableFuture> future = subscriptionBoxService.findById("1"); + Optional result = future.get(); assertTrue(result.isPresent());