From 90c12e6f68e493fe6035e6b25a3b3039845ebb15 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Thu, 12 Sep 2024 13:27:03 +0700 Subject: [PATCH] refactor code --- .../ProductModuleContentRepository.java | 6 +- .../service/impl/ProductServiceImpl.java | 11 ++- .../service/impl/ProductServiceImplTest.java | 96 ++++++++----------- 3 files changed, 52 insertions(+), 61 deletions(-) diff --git a/marketplace-service/src/main/java/com/axonivy/market/repository/ProductModuleContentRepository.java b/marketplace-service/src/main/java/com/axonivy/market/repository/ProductModuleContentRepository.java index 0383b4ac8..5b5074127 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/repository/ProductModuleContentRepository.java +++ b/marketplace-service/src/main/java/com/axonivy/market/repository/ProductModuleContentRepository.java @@ -4,9 +4,11 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface ProductModuleContentRepository extends MongoRepository { ProductModuleContent findByTagAndProductId(String tag, String productId); - - boolean existsByProductIdAndTag(String productId, String tag); + + List findByProductId(String productId); } diff --git a/marketplace-service/src/main/java/com/axonivy/market/service/impl/ProductServiceImpl.java b/marketplace-service/src/main/java/com/axonivy/market/service/impl/ProductServiceImpl.java index d1c2ed3fc..90f9e51e5 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/service/impl/ProductServiceImpl.java +++ b/marketplace-service/src/main/java/com/axonivy/market/service/impl/ProductServiceImpl.java @@ -381,7 +381,12 @@ private void updateProductFromReleaseTags(Product product, GHRepository productR ghTags = ghTags.stream().filter(t -> !currentTags.contains(t.getName())).toList(); } + List existedTag = productModuleContentRepository.findByProductId(product.getId()).stream() + .map(ProductModuleContent::getTag).toList(); for (GHTag ghTag : ghTags) { + if (existedTag.contains(ghTag.getName())) { + continue; + } ProductModuleContent productModuleContent = axonIvyProductRepoService.getReadmeAndProductContentsFromTag(product, productRepo, ghTag.getName()); if (productModuleContent != null) { @@ -393,9 +398,9 @@ private void updateProductFromReleaseTags(Product product, GHRepository productR } product.getReleasedVersions().add(versionFromTag); } - productModuleContents.stream() - .filter(content -> !productModuleContentRepository.existsByProductIdAndTag(content.getProductId(), content.getTag())) - .forEach(productModuleContentRepository::save); + if (!CollectionUtils.isEmpty(productModuleContents)) { + productModuleContentRepository.saveAll(productModuleContents); + } } private Date getPublishedDateFromLatestTag(GHTag lastTag) { 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 5e036feb3..ab28230d0 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 @@ -12,10 +12,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -334,16 +336,44 @@ void testSyncProductsFirstTime() throws IOException { Map> mockGHContentMap = new HashMap<>(); mockGHContentMap.put(SAMPLE_PRODUCT_ID, List.of(mockContent)); when(marketRepoService.fetchAllMarketItems()).thenReturn(mockGHContentMap); - when(productModuleContentRepository.existsByProductIdAndTag("amazon", RELEASE_TAG)).thenReturn(false); - when(productModuleContentRepository.save(any())).thenReturn(mockReadmeProductContent()); - + when(productModuleContentRepository.saveAll(anyList())).thenReturn(List.of(mockReadmeProductContent())); // Executes productService.syncLatestDataFromMarketRepo(); - verify(productModuleContentRepository).save(argumentCaptorProductModuleContent.capture()); + verify(productModuleContentRepository).saveAll(argumentCaptorProductModuleContents.capture()); verify(productRepository).save(argumentCaptor.capture()); - assertThat(argumentCaptorProductModuleContent.getValue()).usingRecursiveComparison() - .isEqualTo(mockReadmeProductContent()); + assertThat(argumentCaptorProductModuleContents.getValue()).usingRecursiveComparison() + .isEqualTo(List.of(mockReadmeProductContent())); + } + + @Test + void testSyncProductsFirstTimeWithExistedData() throws IOException { + var mockCommit = mockGHCommitHasSHA1(SHA1_SAMPLE); + when(marketRepoService.getLastCommit(anyLong())).thenReturn(mockCommit); + when(repoMetaRepository.findByRepoName(anyString())).thenReturn(null); + when(gitHubService.getRepository(any())).thenReturn(ghRepository); + + GHTag mockTag = mock(GHTag.class); + GHCommit mockGHCommit = mock(GHCommit.class); + + when(mockTag.getName()).thenReturn(RELEASE_TAG); + when(mockTag.getCommit()).thenReturn(mockGHCommit); + when(mockGHCommit.getCommitDate()).thenReturn(new Date()); + + when(gitHubService.getRepositoryTags(anyString())).thenReturn(List.of(mockTag)); + var mockContent = mockGHContentAsMetaJSON(); + InputStream inputStream = this.getClass().getResourceAsStream(SLASH.concat(META_FILE)); + when(mockContent.read()).thenReturn(inputStream); + Map> mockGHContentMap = new HashMap<>(); + mockGHContentMap.put(SAMPLE_PRODUCT_ID, List.of(mockContent)); + when(marketRepoService.fetchAllMarketItems()).thenReturn(mockGHContentMap); + + ProductModuleContent mockReturnProductContent = mockReadmeProductContent(); + mockReturnProductContent.setTag(RELEASE_TAG); + when(productModuleContentRepository.findByProductId("amazon-comprehend")).thenReturn(List.of(mockReturnProductContent)); + // Executes + productService.syncLatestDataFromMarketRepo(); + verify(productModuleContentRepository, never()).saveAll(anyList()); } @Test @@ -390,65 +420,19 @@ void testSyncProductsSecondTime() throws IOException { ProductModuleContent mockReturnProductContent = mockReadmeProductContent(); mockReturnProductContent.setTag("v10.0.3"); - when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), anyString())) .thenReturn(mockReturnProductContent); - + when(productModuleContentRepository.saveAll(anyList())) + .thenReturn(List.of(mockReadmeProductContent(), mockReturnProductContent)); // Executes productService.syncLatestDataFromMarketRepo(); - verify(productModuleContentRepository, times(2)).save(argumentCaptorProductModuleContent.capture()); + verify(productModuleContentRepository).saveAll(argumentCaptorProductModuleContents.capture()); verify(productRepository).save(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getProductModuleContent()).usingRecursiveComparison() .isEqualTo(mockReadmeProductContent()); } - @Test - void testSyncProductsSecondTimeWithDuplicatedContent() throws IOException { - String firstTag = "v10.0.2"; - String secondTag = "v10.0.3"; - var gitHubRepoMeta = mock(GitHubRepoMeta.class); - when(gitHubRepoMeta.getLastSHA1()).thenReturn(SHA1_SAMPLE); - var mockCommit = mockGHCommitHasSHA1(SHA1_SAMPLE); - when(marketRepoService.getLastCommit(anyLong())).thenReturn(mockCommit); - when(repoMetaRepository.findByRepoName(anyString())).thenReturn(gitHubRepoMeta); - when(productRepository.findAll()).thenReturn(mockProducts()); - - - GHCommit mockGHCommit = mock(GHCommit.class); - - GHTag mockTag = mock(GHTag.class); - when(mockTag.getName()).thenReturn(firstTag); - - GHTag mockTag2 = mock(GHTag.class); - when(mockTag2.getName()).thenReturn(secondTag); - when(mockTag2.getCommit()).thenReturn(mockGHCommit); - - when(mockGHCommit.getCommitDate()).thenReturn(new Date()); - when(gitHubService.getRepositoryTags(anyString())).thenReturn(Arrays.asList(mockTag, mockTag2)); - - ProductModuleContent mockReturnProductContent = mockReadmeProductContent(); - mockReturnProductContent.setTag(firstTag); - - ProductModuleContent mockReturnProductContent2 = mockReadmeProductContent(); - mockReturnProductContent.setTag(secondTag); - - when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), eq(firstTag))) - .thenReturn(mockReturnProductContent); - when(ghAxonIvyProductRepoService.getReadmeAndProductContentsFromTag(any(), any(), eq(secondTag))) - .thenReturn(mockReturnProductContent2); - - when(productModuleContentRepository.existsByProductIdAndTag("amazon", firstTag)).thenReturn(true); - when(productModuleContentRepository.existsByProductIdAndTag("amazon", secondTag)).thenReturn(false); - - // Executes - productService.syncLatestDataFromMarketRepo(); - - verify(productModuleContentRepository).save(argumentCaptorProductModuleContent.capture()); - assertThat(argumentCaptorProductModuleContent.getValue()).usingRecursiveComparison() - .isEqualTo(mockReturnProductContent); - } - @Test void testNothingToSync() { var gitHubRepoMeta = mock(GitHubRepoMeta.class); @@ -648,7 +632,7 @@ private GHContent mockGHContentAsMetaJSON() { private ProductModuleContent mockReadmeProductContent() { ProductModuleContent productModuleContent = new ProductModuleContent(); productModuleContent.setId("123"); - productModuleContent.setProductId("amazon"); + productModuleContent.setProductId("amazon-comprehend"); productModuleContent.setTag("v10.0.2"); productModuleContent.setName("Amazon Comprehend"); Map description = new HashMap<>();