Skip to content

Commit

Permalink
MARP-1099 Market website show nonexist version then fail in step inst…
Browse files Browse the repository at this point in the history
…allation market item
  • Loading branch information
ntqdinh-axonivy authored Oct 18, 2024
1 parent 36919be commit a391b04
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ public ResponseEntity<Message> syncProducts(@RequestHeader(value = AUTHORIZATION

@PutMapping(SYNC_PRODUCT_VERSION)
@Operation(hidden = true)
public ResponseEntity<Message> syncProductVersions(@RequestHeader(value = AUTHORIZATION) String authorizationHeader) {
public ResponseEntity<Message> syncProductVersions(@RequestHeader(value = AUTHORIZATION) String authorizationHeader
,@RequestParam(value = RESET_SYNC, required = false) Boolean resetSync) {
String token = AuthorizationUtils.getBearerToken(authorizationHeader);
gitHubService.validateUserInOrganizationAndTeam(token, GitHubConstants.AXONIVY_MARKET_ORGANIZATION_NAME,
GitHubConstants.AXONIVY_MARKET_TEAM_NAME);
if (Boolean.TRUE.equals(resetSync)) {
productService.clearAllProductVersion();
}
int nonSyncResult = metadataService.syncAllProductsMetadata();
var message = new Message();
HttpStatus statusCode = HttpStatus.OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public interface CustomProductRepository {

Product getProductById(String id);

Product getProductByIdWithNewestReleaseVersion(String id, Boolean isShowDevVersion);

List<String> getReleasedVersionsById(String id);

int updateInitialCount(String productId, int initialCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import com.axonivy.market.repository.CustomProductRepository;
import com.axonivy.market.repository.CustomRepository;
import com.axonivy.market.repository.ProductModuleContentRepository;
import com.axonivy.market.util.VersionUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.BsonRegularExpression;
import org.springframework.data.domain.Page;
Expand All @@ -42,7 +40,7 @@ public class CustomProductRepositoryImpl extends CustomRepository implements Cus
public static final String LOCALIZE_SEARCH_PATTERN = "%s.%s";

final MongoTemplate mongoTemplate;
final ProductModuleContentRepository contentRepository;
final ProductModuleContentRepository contentRepo;

public Product queryProductByAggregation(Aggregation aggregation) {
return Optional.of(mongoTemplate.aggregate(aggregation, EntityConstants.PRODUCT, Product.class))
Expand All @@ -64,19 +62,6 @@ public Product getProductByIdWithTagOrVersion(String id, String tag) {
return result;
}

@Override
public Product getProductByIdWithNewestReleaseVersion(String id, Boolean isShowDevVersion) {
Product result = findProductById(id);
if (ObjectUtils.isEmpty(result)) {
return null;
}
List<String> devVersions = VersionUtils.getVersionsToDisplay(result.getReleasedVersions(), isShowDevVersion, null);
String currentTag = VersionUtils.convertVersionToTag(result.getId(), devVersions.get(0));
ProductModuleContent content = contentRepository.findByTagAndProductId(currentTag, id);
result.setProductModuleContent(content);
return result;
}

@Override
public ProductModuleContent findByProductIdAndTagOrMavenVersion(String productId, String tag) {
Criteria productIdCriteria = Criteria.where(MongoDBConstants.PRODUCT_ID).is(productId);
Expand All @@ -97,7 +82,7 @@ private Product findProductById(String id) {
public Product getProductById(String id) {
Product result = findProductById(id);
if (!Objects.isNull(result)) {
ProductModuleContent content = contentRepository.findByTagAndProductId(
ProductModuleContent content = contentRepo.findByTagAndProductId(
result.getNewestReleaseVersion(), id);
result.setProductModuleContent(content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface ProductService {
Product fetchProductDetailByIdAndVersion(String id, String version);

boolean syncOneProduct(String productId, String marketItemPath, Boolean overrideMarketItemPath);

void clearAllProductVersion();
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.axonivy.market.service.impl;

import com.axonivy.market.bo.Artifact;
import com.axonivy.market.comparator.LatestVersionComparator;
import com.axonivy.market.constants.MavenConstants;
import com.axonivy.market.controller.ProductDetailsController;
import com.axonivy.market.entity.MavenArtifactVersion;
Expand Down Expand Up @@ -29,9 +30,12 @@
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.axonivy.market.constants.ProductJsonConstants.NAME;
Expand Down Expand Up @@ -93,10 +97,14 @@ public Map<String, Object> getProductJsonContentByIdAndTag(String productId, Str
@Override
public List<VersionAndUrlModel> getVersionsForDesigner(String productId) {
List<VersionAndUrlModel> versionAndUrlList = new ArrayList<>();
MavenArtifactVersion existingMavenArtifactVersion = mavenArtifactVersionRepo.findById(productId).orElse(
MavenArtifactVersion.builder().productId(productId).build());
List<String> versions = MavenUtils.getAllExistingVersions(existingMavenArtifactVersion, true,
null);
List<String> releasedVersions =
VersionUtils.getInstallableVersionsFromMetadataList(metadataRepo.findByProductId(productId));
if (CollectionUtils.isEmpty(releasedVersions)) {
return Collections.emptyList();
}
List<String> versions = releasedVersions.stream().filter(
version -> VersionUtils.isOfficialVersionOrUnReleasedDevVersion(releasedVersions, version)).sorted(
new LatestVersionComparator()).toList();
for (String version : versions) {
Link link = linkTo(
methodOn(ProductDetailsController.class).findProductJsonContent(productId, version)).withSelfRel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,9 @@ public static List<String> getAllExistingVersions(MavenArtifactVersion existingM
return VersionUtils.getVersionsToDisplay(new ArrayList<>(existingProductsArtifactByVersion), isShowDevVersion,
designerVersion);
}

public static boolean isProductMetadata(Metadata metadata) {
return StringUtils.endsWith(Objects.requireNonNullElse(metadata, new Metadata()).getArtifactId(),
MavenConstants.PRODUCT_ARTIFACT_POSTFIX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.axonivy.market.comparator.MavenVersionComparator;
import com.axonivy.market.constants.CommonConstants;
import com.axonivy.market.constants.GitHubConstants;
import com.axonivy.market.entity.Metadata;
import com.axonivy.market.entity.Product;
import com.axonivy.market.enums.NonStandardProduct;
import lombok.extern.log4j.Log4j2;
Expand All @@ -17,10 +18,12 @@
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.axonivy.market.constants.MavenConstants.*;
@Log4j2
Expand Down Expand Up @@ -71,7 +74,7 @@ public static String getBestMatchVersion(List<String> versions, String designerV
return bestMatchVersion;
}

public static boolean isOfficialVersionOrUnReleasedDevVersion(List<String> versions, String version) {
public static boolean isOfficialVersionOrUnReleasedDevVersion(Collection<String> versions, String version) {
if (isReleasedVersion(version)) {
return true;
}
Expand Down Expand Up @@ -150,7 +153,7 @@ public static String getOldestVersion(List<GHTag> tags) {
if (!CollectionUtils.isEmpty(tags)) {
List<String> releasedTags = tags.stream().map(tag -> tag.getName().replaceAll(NON_NUMERIC_CHAR, Strings.EMPTY))
.distinct().sorted(new LatestVersionComparator()).toList();
return CollectionUtils.lastElement(releasedTags);
result = CollectionUtils.lastElement(releasedTags);
}
return result;
}
Expand Down Expand Up @@ -182,4 +185,13 @@ public static boolean isMajorVersion(String version) {
public static boolean isMinorVersion(String version) {
return getNumbersOnly(version).split(MAIN_VERSION_REGEX).length == 2 && isReleasedVersion(version);
}

public static List<String> getInstallableVersionsFromMetadataList(List<Metadata> metadataList) {
if (CollectionUtils.isEmpty(metadataList)) {
return new ArrayList<>();
}
return metadataList.stream().filter(MavenUtils::isProductMetadata).findAny().map(
metadata -> metadata.getVersions().stream().sorted(new LatestVersionComparator()).collect(
Collectors.toList())).orElse(new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.axonivy.market.enums.Language;
import com.axonivy.market.enums.SortOption;
import com.axonivy.market.model.MavenArtifactModel;
import com.axonivy.market.util.MavenUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -40,6 +43,7 @@ public class BaseSetup {
protected static final Pageable PAGEABLE = PageRequest.of(0, 20,
Sort.by(SortOption.ALPHABETICALLY.getOption()).descending());
protected static final String MOCK_PRODUCT_ID = "bpmn-statistic";
protected static final String MOCK_PRODUCT_ID_WITH_TAG = "bpmn-statistic-v10.0.10";
protected static final String MOCK_ARTIFACT_ID = "bpmn-statistic";
protected static final String MOCK_PRODUCT_ARTIFACT_ID = "bpmn-statistic-product";
protected static final String MOCK_RELEASED_VERSION = "10.0.10";
Expand Down Expand Up @@ -68,6 +72,7 @@ public class BaseSetup {
".com/com/axonivy/util/bpmn-statistic/10.0.10-SNAPSHOT/bpmn-statistic-10.0.10-SNAPSHOT.zip";
protected static final String MOCK_ARTIFACT_NAME = "bpmn statistic (zip)";
protected static final String MOCK_ARTIFACT_DOWNLOAD_FILE = "bpmn-statistic.zip";
protected static final String LEGACY_INSTALLATION_COUNT_PATH_FIELD_NAME = "legacyInstallationCountPath";

protected Page<Product> createPageProductsMock() {
var mockProducts = new ArrayList<Product>();
Expand Down Expand Up @@ -165,11 +170,23 @@ protected MavenArtifactVersion getMockMavenArtifactVersion() {
new HashMap<>());
}

protected MavenArtifactVersion getMockMavenArtifactVersionWithData() {
MavenArtifactVersion mockMavenArtifactVersion = getMockMavenArtifactVersion();
Map<String, List<MavenArtifactModel>> mockArtifactModelsByVersion = new HashMap<>();
mockArtifactModelsByVersion.put(MOCK_SNAPSHOT_VERSION, new ArrayList<>());
mockMavenArtifactVersion.setProductArtifactsByVersion(mockArtifactModelsByVersion);
return mockMavenArtifactVersion;
}

protected Product getMockProduct() {
Product mockProduct = Product.builder().id(MOCK_PRODUCT_ID).releasedVersions(new ArrayList<>()).artifacts(
List.of(getMockArtifact())).build();
mockProduct.getReleasedVersions().add(MOCK_RELEASED_VERSION);
return mockProduct;
}

protected List<Product> getMockProducts() {
Product mockProduct =
Product.builder().id(MOCK_PRODUCT_ID).releasedVersions(List.of(MOCK_RELEASED_VERSION)).artifacts(
List.of(getMockArtifact())).build();
return List.of(mockProduct);
return List.of(getMockProduct());
}

protected Metadata getMockMetadata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ void testSyncProductsInvalidToken() {

@Test
void testSyncMavenVersionSuccess() {
var response = productController.syncProductVersions(AUTHORIZATION_HEADER);
var response = productController.syncProductVersions(AUTHORIZATION_HEADER, false);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.MAVEN_VERSION_SYNC_FAILED.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode());
when(metadataService.syncAllProductsMetadata()).thenReturn(1);
response = productController.syncProductVersions(AUTHORIZATION_HEADER);
response = productController.syncProductVersions(AUTHORIZATION_HEADER, false);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.hasBody());
assertEquals(ErrorCode.SUCCESSFUL.getCode(), Objects.requireNonNull(response.getBody()).getHelpCode());
Expand All @@ -169,7 +169,7 @@ void testSyncMavenVersionWithInvalidToken() {
.validateUserInOrganizationAndTeam(any(String.class), any(String.class), any(String.class));

UnauthorizedException exception = assertThrows(UnauthorizedException.class,
() -> productController.syncProductVersions(INVALID_AUTHORIZATION_HEADER));
() -> productController.syncProductVersions(INVALID_AUTHORIZATION_HEADER, false));

assertEquals(ErrorCode.GITHUB_USER_UNAUTHORIZED.getHelpText(), exception.getMessage());
}
Expand Down
Loading

0 comments on commit a391b04

Please sign in to comment.