Skip to content

Commit

Permalink
MARP-975 Update UT
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed Dec 19, 2024
1 parent 5f7014e commit 26c18ff
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -264,4 +265,56 @@ protected static ProductJsonContent getMockProductJsonContentContainMavenDropins
protected ProductMarketplaceData getMockProductMarketplaceData() {
return ProductMarketplaceData.builder().id(MOCK_PRODUCT_ID).installationCount(3).build();
}

protected List<VersionAndUrlModel> 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<VersionAndUrlModel> 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<VersionAndUrlModel> mockVersionModels2() {
VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder()
.version("11.3.2")
.build();

List<VersionAndUrlModel> versionAndUrlModels = new ArrayList<>(mockVersionModels());
versionAndUrlModels.add(0,versionAndUrlModel);

return versionAndUrlModels;
}

protected List<VersionAndUrlModel> mockVersionModels3() {
VersionAndUrlModel versionAndUrlModel = VersionAndUrlModel.builder()
.version("11.3.2")
.build();

VersionAndUrlModel versionAndUrlModel2 = VersionAndUrlModel.builder()
.version("12.0.0")
.build();

List<VersionAndUrlModel> versionAndUrlModels = new ArrayList<>(mockVersionModels());
versionAndUrlModels.add(0,versionAndUrlModel);
versionAndUrlModels.add(0,versionAndUrlModel2);
return versionAndUrlModels;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,7 @@ void findProductVersionsById() {
Objects.requireNonNull(result.getBody()).get(1).getUrl());
}

private List<VersionAndUrlModel> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +130,8 @@ class ProductServiceImplTest extends BaseSetup {
private ProductMarketplaceDataService productMarketplaceDataService;
@Mock
private ProductMarketplaceDataRepository productMarketplaceDataRepo;
@Mock
private VersionService versionService;
@InjectMocks
private ProductServiceImpl productService;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 26c18ff

Please sign in to comment.