From 26c18ffd0157f8285d5d74d2b10b2487321327e1 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: Thu, 19 Dec 2024 13:07:42 +0700 Subject: [PATCH] MARP-975 Update UT --- .../service/impl/ProductServiceImpl.java | 37 +++++++------ .../java/com/axonivy/market/BaseSetup.java | 53 +++++++++++++++++++ .../ProductDetailsControllerTest.java | 14 +---- .../service/impl/ProductServiceImplTest.java | 29 ++++++++++ 4 files changed, 105 insertions(+), 28 deletions(-) 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 9bb3cde9..d7040b2c 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 @@ -62,7 +62,7 @@ import java.time.format.DateTimeFormatter; import java.util.*; -import static com.axonivy.market.constants.CommonConstants.SLASH; +import static com.axonivy.market.constants.CommonConstants.*; import static com.axonivy.market.constants.MavenConstants.*; import static com.axonivy.market.constants.ProductJsonConstants.EN_LANGUAGE; import static com.axonivy.market.constants.ProductJsonConstants.LOGO_FILE; @@ -586,15 +586,15 @@ public String getCompatibilityFromOldestVersion(String oldestVersion) { if (StringUtils.isBlank(oldestVersion)) { return Strings.EMPTY; } - if (!oldestVersion.contains(CommonConstants.DOT_SEPARATOR)) { + if (!oldestVersion.contains(DOT_SEPARATOR)) { return oldestVersion + ".0+"; } - int firstDot = oldestVersion.indexOf(CommonConstants.DOT_SEPARATOR); - int secondDot = oldestVersion.indexOf(CommonConstants.DOT_SEPARATOR, firstDot + 1); + int firstDot = oldestVersion.indexOf(DOT_SEPARATOR); + int secondDot = oldestVersion.indexOf(DOT_SEPARATOR, firstDot + 1); if (secondDot == -1) { - return oldestVersion.concat(CommonConstants.PLUS); + return oldestVersion.concat(PLUS); } - return oldestVersion.substring(0, secondDot).concat(CommonConstants.PLUS); + return oldestVersion.substring(0, secondDot).concat(PLUS); } @Override @@ -773,20 +773,27 @@ private String getCompatibilityRange(String productId) { return splitVersion(versions.get(0)); } - boolean isMoreThanVersion = versions.stream() - .filter(version -> { - String currentVersion = versions.get(0); - return version.startsWith(currentVersion.substring(0, currentVersion.indexOf("."))); - }).toList().size() > 1; + String currentMaxVersion = versions.get(0); + boolean isMoreThan1InMaxVersion = versions.stream() + .filter(version -> version.startsWith( + currentMaxVersion.substring(0, currentMaxVersion.indexOf(DOT_SEPARATOR)))).toList().size() > 1; - String maxValue = isMoreThanVersion ? splitVersion(versions.get(0)).concat("+") : splitVersion(versions.get(0)); + String maxValue = isMoreThan1InMaxVersion ? splitVersion(currentMaxVersion).concat(PLUS) : splitVersion( + currentMaxVersion); String minValue = splitVersion(versions.get(versions.size() - 1)); - return minValue.concat("-").concat(maxValue); + + return getPrefixOfVersion(minValue).equals(getPrefixOfVersion(maxValue)) ? + minValue.concat(PLUS) : + minValue.concat(DASH_SEPARATOR).concat(maxValue); } private String splitVersion(String version) { - int firstDot = version.indexOf('.'); - int secondDot = version.indexOf('.', firstDot + 1); + int firstDot = version.indexOf(DOT_SEPARATOR); + int secondDot = version.indexOf(DOT_SEPARATOR, firstDot + 1); return version.substring(0, secondDot); } + + private String getPrefixOfVersion(String version) { + return version.substring(0, version.indexOf(DOT_SEPARATOR)); + } } \ No newline at end of file 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 971097a6..8526630e 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java +++ b/marketplace-service/src/test/java/com/axonivy/market/BaseSetup.java @@ -12,6 +12,7 @@ import com.axonivy.market.enums.Language; import com.axonivy.market.enums.SortOption; import com.axonivy.market.model.MavenArtifactModel; +import com.axonivy.market.model.VersionAndUrlModel; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; @@ -264,4 +265,56 @@ protected static ProductJsonContent getMockProductJsonContentContainMavenDropins protected ProductMarketplaceData getMockProductMarketplaceData() { return ProductMarketplaceData.builder().id(MOCK_PRODUCT_ID).installationCount(3).build(); } + + protected List mockVersionAndUrlModels() { + VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder() + .version("10.0.21") + .url("/api/product-details/productjsoncontent/portal/10.0.21") + .build(); + + VersionAndUrlModel versionAndUrlModel2 = VersionAndUrlModel.builder() + .version("10.0.22") + .url("/api/product-details/productjsoncontent/portal/10.0.22") + .build(); + + return List.of(versionAndUrlModel, versionAndUrlModel2); + } + + protected List mockVersionModels() { + VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder() + .version("11.3.1") + .build(); + + VersionAndUrlModel versionAndUrlModel2 = VersionAndUrlModel.builder() + .version("10.0.22") + .build(); + + return List.of(versionAndUrlModel, versionAndUrlModel2); + } + + protected List mockVersionModels2() { + VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder() + .version("11.3.2") + .build(); + + List versionAndUrlModels = new ArrayList<>(mockVersionModels()); + versionAndUrlModels.add(0,versionAndUrlModel); + + return versionAndUrlModels; + } + + protected List mockVersionModels3() { + VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder() + .version("11.3.2") + .build(); + + VersionAndUrlModel versionAndUrlModel2 = VersionAndUrlModel.builder() + .version("12.0.0") + .build(); + + List versionAndUrlModels = new ArrayList<>(mockVersionModels()); + versionAndUrlModels.add(0,versionAndUrlModel); + versionAndUrlModels.add(0,versionAndUrlModel2); + return versionAndUrlModels; + } } 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 249df32a..267187ea 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 @@ -177,19 +177,7 @@ void findProductVersionsById() { Objects.requireNonNull(result.getBody()).get(1).getUrl()); } - private List mockVersionAndUrlModels() { - VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder() - .version("10.0.21") - .url("/api/product-details/productjsoncontent/portal/10.0.21") - .build(); - - VersionAndUrlModel versionAndUrlModel2 = VersionAndUrlModel.builder() - .version("10.0.22") - .url("/api/product-details/productjsoncontent/portal/10.0.22") - .build(); - - return List.of(versionAndUrlModel, versionAndUrlModel2); - } + @Test void findProductJsonContentByIdAndVersion() throws IOException { 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 7896e389..b8849798 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 @@ -33,6 +33,7 @@ import com.axonivy.market.service.MetadataService; import com.axonivy.market.service.ProductContentService; import com.axonivy.market.service.ProductMarketplaceDataService; +import com.axonivy.market.service.VersionService; import com.axonivy.market.util.MavenUtils; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; @@ -129,6 +130,8 @@ class ProductServiceImplTest extends BaseSetup { private ProductMarketplaceDataService productMarketplaceDataService; @Mock private ProductMarketplaceDataRepository productMarketplaceDataRepo; + @Mock + private VersionService versionService; @InjectMocks private ProductServiceImpl productService; @@ -401,6 +404,32 @@ void testFetchProductDetail() { assertNull(result); } + @Test + void testGetCompatibilityRangeAfterFetchProductDetail() { + MavenArtifactVersion mockMavenArtifactVersion = getMockMavenArtifactVersionWithData(); + when(mavenArtifactVersionRepo.findById(MOCK_PRODUCT_ID)).thenReturn( + Optional.ofNullable(mockMavenArtifactVersion)); + + + when(productRepo.getProductByIdAndVersion(MOCK_PRODUCT_ID, MOCK_SNAPSHOT_VERSION)) + .thenReturn(getMockProduct()); + when(versionService.getVersionsForDesigner(MOCK_PRODUCT_ID)) + .thenReturn(mockVersionAndUrlModels(),mockVersionModels(),mockVersionModels2(),mockVersionModels3()); + + + Product result = productService.fetchProductDetail(MOCK_PRODUCT_ID, true); + assertEquals(result.getCompatibilityRange(),"10.0+"); + + result = productService.fetchProductDetail(MOCK_PRODUCT_ID, true); + assertEquals(result.getCompatibilityRange(),"10.0-11.3"); + + result = productService.fetchProductDetail(MOCK_PRODUCT_ID, true); + assertEquals(result.getCompatibilityRange(),"10.0-11.3+"); + + result = productService.fetchProductDetail(MOCK_PRODUCT_ID, true); + assertEquals(result.getCompatibilityRange(),"10.0-12.0"); + } + @Test void testGetProductByIdWithNewestReleaseVersion() { MavenArtifactVersion mockMavenArtifactVersion = getMockMavenArtifactVersionWithData();