Skip to content

Commit

Permalink
MARP-473 Random new number for installation count
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed Jul 15, 2024
1 parent d16dccc commit 6b1ed02
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@

import lombok.extern.log4j.Log4j2;

import javax.swing.text.html.Option;

@Log4j2
@Service
public class ProductServiceImpl implements ProductService {
Expand Down Expand Up @@ -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<String, Integer> mapping = mapper.readValue(installationCounts, HashMap.class);
List<String> 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) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6b1ed02

Please sign in to comment.