Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ntqdinh-axonivy committed Sep 12, 2024
1 parent dfb4bb8 commit 90c12e6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, String> {
ProductModuleContent findByTagAndProductId(String tag, String productId);

boolean existsByProductIdAndTag(String productId, String tag);
List<ProductModuleContent> findByProductId(String productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,12 @@ private void updateProductFromReleaseTags(Product product, GHRepository productR
ghTags = ghTags.stream().filter(t -> !currentTags.contains(t.getName())).toList();
}

List<String> 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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -334,16 +336,44 @@ void testSyncProductsFirstTime() throws IOException {
Map<String, List<GHContent>> 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<String, List<GHContent>> 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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String, String> description = new HashMap<>();
Expand Down

0 comments on commit 90c12e6

Please sign in to comment.