Skip to content

Commit

Permalink
MARP-760 Handle Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed Aug 30, 2024
1 parent ae3275b commit b170899
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ private ProductDetailModel createModel(Product product, String version, String r
ProductDetailModel model = instantiateModel(product);
productModelAssembler.createResource(model, product);
String productId = Optional.of(product).map(Product::getId).orElse(StringUtils.EMPTY);

if (requestPath.equals(RequestMappingConstants.BEST_MATCH_BY_ID_AND_VERSION)) {
String bestMatchVersion = VersionUtils.getBestMatchVersion(product.getReleasedVersions(), version);
Link link = linkTo(
methodOn(ProductDetailsController.class).findProductJsonContent(productId, bestMatchVersion)).withSelfRel();
model.setMetaProductJsonUrl(link.getHref());
}

selfLinkWithTag = switch (requestPath) {
case RequestMappingConstants.BEST_MATCH_BY_ID_AND_VERSION ->
methodOn(ProductDetailsController.class).findBestMatchProductDetailsByVersion(productId, version);
Expand All @@ -56,16 +64,6 @@ private ProductDetailModel createModel(Product product, String version, String r
default -> methodOn(ProductDetailsController.class).findProductDetails(productId);
};

try {
if (requestPath.equals(RequestMappingConstants.BEST_MATCH_BY_ID_AND_VERSION)) {
String bestMatchVersion = VersionUtils.getBestMatchVersion(product.getReleasedVersions(), version);
Link link = linkTo(
methodOn(ProductDetailsController.class).findProductJsonContent(productId, bestMatchVersion)).withSelfRel();
model.setMetaProductJsonUrl(link.getHref());
}
} catch (JsonProcessingException jsonProcessingException) {
log.error(jsonProcessingException.getMessage());
}

model.add(linkTo(selfLinkWithTag).withSelfRel());
createDetailResource(model, product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ResponseEntity<List<MavenArtifactVersionModel>> findProductVersionsById(
@GetMapping(PRODUCT_JSON_CONTENT_BY_PRODUCT_ID_AND_VERSION)
@Operation(summary = "Get product json content for designer to install", description = "When we click install in designer, this API will send content of product json for installing in Ivy designer")
public ResponseEntity<Map<String, Object>> findProductJsonContent(@PathVariable(PRODUCT_ID) String productId,
@PathVariable(VERSION) String version) throws JsonProcessingException {
@PathVariable(VERSION) String version) {
Map<String, Object> productJsonContent = versionService.getProductJsonContentByIdAndVersion(productId, version);
return new ResponseEntity<>(productJsonContent, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.axonivy.market.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -9,6 +10,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class VersionAndUrlModel {
String version;
String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public interface VersionService {
List<MavenArtifactVersionModel> getArtifactsAndVersionToDisplay(String productId, Boolean isShowDevVersion,
String designerVersion);

Map<String, Object> getProductJsonContentByIdAndVersion(String name , String version)
throws JsonProcessingException;
Map<String, Object> getProductJsonContentByIdAndVersion(String name , String version);

List<VersionAndUrlModel> getVersionsForDesigner(String productId) throws JsonProcessingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,19 @@ public List<MavenArtifactVersionModel> getArtifactsAndVersionToDisplay(String pr
}

@Override
public Map<String, Object> getProductJsonContentByIdAndVersion(String productId, String version)
throws JsonProcessingException {
ProductJsonContent productJsonContent = productJsonContentRepository.findByProductIdAndVersion(productId, version);
if (ObjectUtils.isEmpty(productJsonContent)) {
return new HashMap<>();
public Map<String, Object> getProductJsonContentByIdAndVersion(String productId, String version){
Map<String, Object> result = new HashMap<>();
try {
ProductJsonContent productJsonContent = productJsonContentRepository.findByProductIdAndVersion(productId, version);
if (ObjectUtils.isEmpty(productJsonContent)) {
return new HashMap<>();
}
result = mapper.readValue(productJsonContent.getContent(), Map.class);
result.computeIfAbsent(NAME, k -> productJsonContent.getName());

} catch (JsonProcessingException jsonProcessingException){
log.error(jsonProcessingException.getMessage());
}
Map<String, Object> result = mapper.readValue(productJsonContent.getContent(), Map.class);
result.computeIfAbsent(NAME, k -> productJsonContent.getName());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

@ExtendWith(MockitoExtension.class)
class ProductDetailModelAssemblerTest {
private static final String ID = "portal";
Expand All @@ -25,6 +27,7 @@ void setup() {
productDetailModelAssembler = new ProductDetailModelAssembler(new ProductModelAssembler());
mockProduct = new Product();
mockProduct.setId(ID);
mockProduct.setReleasedVersions(List.of("11.0.1", "10.0.8"));
}

@Test
Expand All @@ -47,9 +50,10 @@ void testToModelWithRequestPathAndVersion() {
Assertions.assertTrue(model.getLink(SELF_RELATION).get().getHref().endsWith("/api/product-details/portal/10.0.19"));
}

// @Test
// void testToModelWithRequestPathAndBestMatchVersion() {
// ProductDetailModel model = productDetailModelAssembler.toModel(mockProduct, VERSION, RequestMappingConstants.BEST_MATCH_BY_ID_AND_VERSION);
// Assertions.assertTrue(model.getLink(SELF_RELATION).get().getHref().endsWith("/api/product-details/portal/10.0.19/bestmatch"));
// }
@Test
void testToModelWithRequestPathAndBestMatchVersion() {
ProductDetailModel model = productDetailModelAssembler.toModel(mockProduct, VERSION, RequestMappingConstants.BEST_MATCH_BY_ID_AND_VERSION);
Assertions.assertEquals(model.getMetaProductJsonUrl(), "/api/product-details/productjsoncontent/portal/10.0.8");
Assertions.assertTrue(model.getLink(SELF_RELATION).get().getHref().endsWith("/api/product-details/portal/10.0.19/bestmatch"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Map;
import com.axonivy.market.constants.RequestMappingConstants;
import com.axonivy.market.entity.productjsonfilecontent.ProductJsonContent;
import com.axonivy.market.model.VersionAndUrlModel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -122,18 +124,34 @@ void testSyncInstallationCount() {
assertEquals(1, result.getBody());
}

// @Test
// void findProductVersionsById() {
// when(versionService.getVersionsForDesigner("google-maps-connector")).thenReturn(
// List.of("10.0.21", "10.0.22", "10.0.23"));
//
// var result = productDetailsController.findVersionsForDesigner("google-maps-connector");
//
// assertEquals(3, Objects.requireNonNull(result.getBody()).size());
// assertEquals("10.0.21", Objects.requireNonNull(result.getBody()).get(0));
// assertEquals("10.0.22", Objects.requireNonNull(result.getBody()).get(1));
// assertEquals("10.0.23", Objects.requireNonNull(result.getBody()).get(2));
// }
@Test
void findProductVersionsById() throws JsonProcessingException {
when(versionService.getVersionsForDesigner("google-maps-connector")).thenReturn(mockVersionAndUrlModels());

var result = productDetailsController.findVersionsForDesigner("google-maps-connector");

assertEquals(2, Objects.requireNonNull(result.getBody()).size());
assertEquals("10.0.21", Objects.requireNonNull(result.getBody()).get(0).getVersion());
assertEquals("/api/product-details/productjsoncontent/portal/10.0.21",
Objects.requireNonNull(result.getBody()).get(0).getUrl());
assertEquals("10.0.22", Objects.requireNonNull(result.getBody()).get(1).getVersion());
assertEquals("/api/product-details/productjsoncontent/portal/10.0.22",
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 findProductJsonContentByIdAndTag() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.axonivy.market.github.model.MavenArtifact;
import com.axonivy.market.github.service.GHAxonIvyProductRepoService;
import com.axonivy.market.model.MavenArtifactVersionModel;
import com.axonivy.market.model.VersionAndUrlModel;
import com.axonivy.market.repository.MavenArtifactVersionRepository;
import com.axonivy.market.repository.ProductJsonContentRepository;
import com.axonivy.market.repository.ProductRepository;
Expand Down Expand Up @@ -36,6 +37,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -417,15 +419,21 @@ void testGetRepoNameFromMarketRepo() {
Assertions.assertEquals(expectedRepoName, result);
}

// @Test
// void testGetVersionsForDesigner() {
// Mockito.when(productRepository.getReleasedVersionsById(anyString()))
// .thenReturn(List.of("11.3.0", "11.1.1", "11.1.0", "10.0.2"));
//
// List<String> result = versionService.getVersionsForDesigner("11.3.0");
//
// Assertions.assertEquals(result, List.of("11.3.0", "11.1.1", "11.1.0", "10.0.2"));
// }
@Test
void testGetVersionsForDesigner() throws JsonProcessingException {
Mockito.when(productRepository.getReleasedVersionsById(anyString()))
.thenReturn(List.of("11.3.0", "11.1.1", "11.1.0", "10.0.2"));

List<VersionAndUrlModel> result = versionService.getVersionsForDesigner("11.3.0");

Assertions.assertEquals(result.stream().map(VersionAndUrlModel::getVersion).collect(Collectors.toList()),
List.of("11.3.0", "11.1.1", "11.1.0", "10.0.2"));
Assertions.assertEquals("/api/product-details/productjsoncontent/11.3.0/11.3.0", result.get(0).getUrl());
Assertions.assertEquals("/api/product-details/productjsoncontent/11.3.0/11.1.1", result.get(1).getUrl());
Assertions.assertEquals("/api/product-details/productjsoncontent/11.3.0/11.1.0", result.get(2).getUrl());
Assertions.assertEquals("/api/product-details/productjsoncontent/11.3.0/10.0.2", result.get(3).getUrl());

}

@Test
void testGetProductJsonContentByIdAndVersion() throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
buttonClass="form-select form-select-sm versions-selector__dropdown border__dropdown h-100 text-primary install-designer-dropdown"
ariaLabel=".form-select-sm example"
class="flex-grow-1 col-8"
[meta_data_json_url] = "meta_data_json_url()"
[metaDataJsonUrl] = "metaDataJsonUrl()"
(itemSelected)="onSelectVersionInDesigner($event.value)">
</app-common-dropdown>

Expand All @@ -18,7 +18,7 @@
onClick="function installInDesigner() {
const selectedItemElement = document.querySelector('.install-designer-dropdown');
if (selectedItemElement) {
const metaDataJsonUrl = selectedItemElement.getAttribute('meta_data_json_url');
const metaDataJsonUrl = selectedItemElement.getAttribute('metaDataJsonUrl');
console.log(metaDataJsonUrl);
install(metaDataJsonUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ProductDetailVersionActionComponent implements AfterViewInit {
label: version,
}));
});
meta_data_json_url = model<string>('');
metaDataJsonUrl = model<string>('');
versionDropdownInDesigner: ItemDropdown[] = [];

artifacts: WritableSignal<ItemDropdown[]> = signal([]);
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ProductDetailVersionActionComponent implements AfterViewInit {
const versionMap = data.map(data => data.version).map(version => 'Version '.concat(version));
data.forEach(data => {
const currentVersion = 'Version '.concat(data.version);
const versionAndUrl: ItemDropdown = { value: currentVersion, label: currentVersion, meta_data_json_url: data.url };
const versionAndUrl: ItemDropdown = { value: currentVersion, label: currentVersion, metaDataJsonUrl: data.url };
this.versionDropdownInDesigner.push(versionAndUrl);
});
this.versions.set(versionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h4 class="analysis-title text-primary text-capitalize mb-0">
class="w-100 d-flex d-lg-block text-end"
(installationCount)="receiveInstallationCountData($event)"
[(selectedVersion)]="selectedVersion!"
[(meta_data_json_url)]="metaProductJsonUrl!"
[(metaDataJsonUrl)]="metaProductJsonUrl!"
[productId]="productDetail().id"
[product]="productDetail()"
(selectedVersionChange)="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<button (click)="toggleDropdown()"
[class]="buttonClass"
[ngClass]="{'indicator-arrow__up': isDropdownOpen}"
[attr.meta_data_json_url]="meta_data_json_url"
[attr.metaDataJsonUrl]="metaDataJsonUrl"
[attr.aria-label]="ariaLabel">
{{ selectedItem }}
</button>
<div class="dropdown-menu menu-bar" [class.show]="isDropdownOpen">
@for (item of items; track $index) {
<div class="dropdown-item item-bar text-primary" (click)="onSelect(item)"
[attr.meta_data_json_url]="item.meta_data_json_url"
[attr.metaDataJsonUrl]="item.metaDataJsonUrl"
[ngClass]="{'active': isActiveItem(item , selectedItem)}">
{{ item.label | translate }}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class CommonDropdownComponent<T extends string> {
@Output() itemSelected = new EventEmitter<ItemDropdown<T>>();
elementRef = inject(ElementRef);
isDropdownOpen = false;
@Input() meta_data_json_url: string | undefined = '';
@Input() metaDataJsonUrl: string | undefined = '';

toggleDropdown() {
this.isDropdownOpen = !this.isDropdownOpen;
Expand All @@ -33,7 +33,7 @@ export class CommonDropdownComponent<T extends string> {
onSelect(item: ItemDropdown<T>) {
this.itemSelected.emit(item);
this.isDropdownOpen = false;
this.meta_data_json_url = item.meta_data_json_url;
this.metaDataJsonUrl = item.metaDataJsonUrl;
}

isActiveItem(value: ItemDropdown, selectedItem: T | undefined): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface ItemDropdown<T extends string = string> {
downloadUrl?: string;
isProductArtifact?: boolean | null;

meta_data_json_url?: string;
metaDataJsonUrl?: string;
}

0 comments on commit b170899

Please sign in to comment.