From 2a6b9fab7dca8620af7929696275ee2ad36c5dd0 Mon Sep 17 00:00:00 2001 From: PHAM HOANG HUNG Date: Wed, 11 Dec 2024 17:41:34 +0700 Subject: [PATCH] Update UT --- .../java/com/axonivy/market/BaseSetup.java | 1 + .../controller/ProductControllerTest.java | 1 - .../service/impl/ProductServiceImplTest.java | 25 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java b/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java index 0e494e82..971097a6 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java +++ b/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java @@ -38,6 +38,7 @@ public class BaseSetup { protected static final String SAMPLE_PRODUCT_ID = "amazon-comprehend"; protected static final String SAMPLE_PRODUCT_PATH = "/market/connector/amazon-comprehend"; protected static final String SAMPLE_PRODUCT_NAME = "prody Comprehend"; + protected static final String SAMPLE_PRODUCT_REPOSITORY_NAME = "axonivy-market/amazon-comprehend"; protected static final Pageable PAGEABLE = PageRequest.of(0, 20, Sort.by(SortOption.ALPHABETICALLY.getOption()).descending()); protected static final String MOCK_PRODUCT_ID = "bpmn-statistic"; diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java index d92c6ad1..d84b22ea 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductControllerTest.java @@ -243,5 +243,4 @@ void testSyncFirstPublishedDateOfAllProductsSuccess() { assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(ErrorCode.SUCCESSFUL.getCode(), response.getBody().getHelpCode()); } - } diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/impl/ProductServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/impl/ProductServiceImplTest.java index eb87febb..6b384c9c 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/impl/ProductServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/impl/ProductServiceImplTest.java @@ -40,12 +40,14 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.kohsuke.github.GHCommit; import org.kohsuke.github.GHContent; +import org.kohsuke.github.GHTag; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; +import org.mockito.exceptions.base.MockitoException; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -56,6 +58,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -613,4 +616,26 @@ void testSyncProductsAsUpdateMetaJSONFromGitHub_AddVendorLogo() throws IOExcepti assertNotNull(result); assertTrue(result.isEmpty()); } + + @Test + void testSyncFirstPublishedDateOfAllProducts() throws IOException { + Product mockProduct = new Product(); + mockProduct.setId(SAMPLE_PRODUCT_ID); + mockProduct.setMarketDirectory(SAMPLE_PRODUCT_PATH); + mockProduct.setRepositoryName(SAMPLE_PRODUCT_REPOSITORY_NAME); + List products = Arrays.asList(mockProduct); + when(productRepo.findAll()).thenReturn(products); + when(productRepo.save(any(Product.class))).thenReturn(mockProduct); + GHTag ghTagVersionOne = new GHTag(); + GHTag ghTagVersionTwo = new GHTag(); + List tags = Arrays.asList(ghTagVersionOne, ghTagVersionTwo); + when(gitHubService.getRepositoryTags(SAMPLE_PRODUCT_REPOSITORY_NAME)).thenReturn(tags); + assertTrue(productService.syncFirstPublishedDateOfAllProducts()); + } + + @Test + void testSyncFirstPublishedDateOfAllProductsFailed() { + when(productRepo.findAll()).thenThrow(new MockitoException("Sync FirstPublishedDate of all products failed!")); + assertFalse(productService.syncFirstPublishedDateOfAllProducts()); + } }