From 6b1ed0236dc1e6320d96a266e7b12050a7771934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=E1=BA=A7n=20V=C4=A9nh=20Thi=E1=BB=87n=20Ph=C3=BAc?= Date: Mon, 15 Jul 2024 16:53:23 +0700 Subject: [PATCH] MARP-473 Random new number for installation count --- .../ProductDetailModelAssembler.java | 1 + .../market/model/ProductDetailModel.java | 1 + .../service/impl/ProductServiceImpl.java | 19 ++++++++++++++----- .../ProductDetailsControllerTest.java | 2 +- .../service/ProductServiceImplTest.java | 1 + 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/marketplace-service/src/main/java/com/axonivy/market/assembler/ProductDetailModelAssembler.java b/marketplace-service/src/main/java/com/axonivy/market/assembler/ProductDetailModelAssembler.java index dbfa0be0b..ff9ade568 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/assembler/ProductDetailModelAssembler.java +++ b/marketplace-service/src/main/java/com/axonivy/market/assembler/ProductDetailModelAssembler.java @@ -58,6 +58,7 @@ private void createDetailResource(ProductDetailModel model, Product product, Str model.setCompatibility(product.getCompatibility()); model.setContactUs(product.getContactUs()); model.setCost(product.getCost()); + model.setInstallationCount(product.getInstallationCount()); if (StringUtils.isBlank(tag) && StringUtils.isNotBlank(product.getNewestReleaseVersion())) { tag = product.getNewestReleaseVersion(); diff --git a/marketplace-service/src/main/java/com/axonivy/market/model/ProductDetailModel.java b/marketplace-service/src/main/java/com/axonivy/market/model/ProductDetailModel.java index 2943ccc79..99a13922f 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/model/ProductDetailModel.java +++ b/marketplace-service/src/main/java/com/axonivy/market/model/ProductDetailModel.java @@ -22,6 +22,7 @@ public class ProductDetailModel extends ProductModel { private String compatibility; private Boolean contactUs; private ProductModuleContent productModuleContent; + private int installationCount; @Override public int hashCode() { 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 39b3b436e..8497c44db 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 @@ -60,6 +60,8 @@ import lombok.extern.log4j.Log4j2; +import javax.swing.text.html.Option; + @Log4j2 @Service public class ProductServiceImpl implements ProductService { @@ -143,14 +145,14 @@ public int updateInstallationCountForProduct(String key) { } private void syncInstallationCountWithProduct(Product product) { - log.info("synchronizing installation count for product"); + log.info("synchronizing installation count for product {}", product.getId()); try { String installationCounts = Files.readString(Paths.get(installationCountPath)); Map mapping = mapper.readValue(installationCounts, HashMap.class); List keyList = mapping.keySet().stream().toList(); - if (keyList.contains(product.getId())) { - product.setInstallationCount(mapping.get(product.getId())); - } + int currentInstallationCount = keyList.contains(product.getId()) ? + mapping.get(product.getId()) : new Random().nextInt(20, 50); + product.setInstallationCount(currentInstallationCount); product.setSynchronizedInstallationCount(true); log.info("synchronized installation count for products"); } catch (IOException ex) { @@ -349,6 +351,13 @@ public String getCompatibilityFromOldestTag(String oldestTag) { @Override public Product fetchProductDetail(String id) { - return productRepository.findById(id).orElse(null); + Product product = productRepository.findById(id).orElse(null); + return Optional.ofNullable(product).map(productItem -> { + if (!BooleanUtils.isTrue(productItem.getSynchronizedInstallationCount())) { + syncInstallationCountWithProduct(productItem); + return productRepository.save(productItem); + } + return productItem; + }).orElse(null); } } diff --git a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java index 8e4b796d3..23bad8527 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/controller/ProductDetailsControllerTest.java @@ -76,7 +76,7 @@ void testFindProductVersionsById() { } @Test - public void testSyncInstallationCount() throws Exception { + void testSyncInstallationCount() { when(productService.updateInstallationCountForProduct("google-maps-connector")).thenReturn(1); var result = productDetailsController.syncInstallationCount("google-maps-connector"); diff --git a/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java b/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java index 11e8bfb70..bcfbcedce 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/service/ProductServiceImplTest.java @@ -345,6 +345,7 @@ void testSearchProducts() { void testFetchProductDetail() { String id = "amazon-comprehend"; Product mockProduct = mockResultReturn.getContent().get(0); + mockProduct.setSynchronizedInstallationCount(true); when(productRepository.findById(id)).thenReturn(Optional.ofNullable(mockProduct)); Product result = productService.fetchProductDetail(id); assertEquals(mockProduct, result);