Skip to content

Commit

Permalink
feat: migrate transfer history page to UiAsset (UI API Wrapper, #535)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI authored Sep 21, 2023
1 parent 118ea19 commit 5e87f14
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void testProviderTransferProcess(ContractNegotiationStore negotiationStore,

//assert
assertThat(providerAssetResult.getAssetId()).isEqualTo(TransferProcessTestUtils.VALID_ASSET_ID);
assertThat(providerAssetResult.getProperties().get(Asset.PROPERTY_NAME)).isEqualTo(TransferProcessTestUtils.ASSET_NAME);
assertThat(providerAssetResult.getName()).isEqualTo(TransferProcessTestUtils.ASSET_NAME);
}

@Test
Expand All @@ -71,7 +71,7 @@ void testConsumerTransferProcess(ContractNegotiationStore negotiationStore,

//assert
assertThat(consumerAssetResult.getAssetId()).isEqualTo(TransferProcessTestUtils.UNKNOWN_ASSET_ID);
assertThat(consumerAssetResult.getProperties().get("asset:prop:name")).isNull();
assertThat(consumerAssetResult.getName()).isEqualTo(TransferProcessTestUtils.UNKNOWN_ASSET_ID);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package de.sovity.edc.client;

import de.sovity.edc.utils.jsonld.vocab.Prop;
import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
Expand Down Expand Up @@ -88,7 +89,7 @@ private static DataAddress getDataAddress() {
private static void createAsset(AssetService assetStore, DataAddress dataAddress, String assetId, String assetName) throws ParseException {
var asset = Asset.Builder.newInstance()
.id(assetId)
.property(Asset.PROPERTY_NAME, assetName)
.property(Prop.Dcterms.TITLE, assetName)
.createdAt(dateFormatterToLong("2023-06-01"))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class UiAsset {
@Schema(description = "Asset Id", requiredMode = Schema.RequiredMode.REQUIRED)
private String assetId;

@Schema(description = "Asset Name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@Schema(description = "Asset Name", requiredMode = Schema.RequiredMode.REQUIRED)
private String name;

@Schema(description = "Asset Language", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.json.JsonObjectBuilder;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.jetbrains.annotations.Nullable;

Expand All @@ -30,9 +31,12 @@ public UiAsset buildUiAsset(JsonObject assetJsonLd) {
var uiAsset = new UiAsset();
uiAsset.setAssetJsonLd(buildCompactAssetJsonLd(assetJsonLd));

uiAsset.setAssetId(JsonLdUtils.string(assetJsonLd, Prop.ID));
String id = JsonLdUtils.string(assetJsonLd, Prop.ID);
String title = JsonLdUtils.string(properties, Prop.Dcterms.TITLE);

uiAsset.setAssetId(id);
uiAsset.setName(StringUtils.isBlank(title) ? id : title);
uiAsset.setLicenseUrl(JsonLdUtils.string(properties, Prop.Dcterms.LICENSE));
uiAsset.setName(JsonLdUtils.string(properties, Prop.Dcterms.NAME));
uiAsset.setDescription(JsonLdUtils.string(properties, Prop.Dcterms.DESCRIPTION));
uiAsset.setLanguage(JsonLdUtils.string(properties, Prop.Dcterms.LANGUAGE));
uiAsset.setVersion(JsonLdUtils.string(properties, Prop.Dcat.VERSION));
Expand Down Expand Up @@ -83,7 +87,7 @@ private JsonObjectBuilder getAssetProperties(UiAssetCreateRequest uiAssetCreateR

addNonNull(properties, Prop.Edc.ID, uiAssetCreateRequest.getId());
addNonNull(properties, Prop.Dcterms.LICENSE, uiAssetCreateRequest.getLicenseUrl());
addNonNull(properties, Prop.Dcterms.NAME, uiAssetCreateRequest.getName());
addNonNull(properties, Prop.Dcterms.TITLE, uiAssetCreateRequest.getName());
addNonNull(properties, Prop.Dcterms.DESCRIPTION, uiAssetCreateRequest.getDescription());
addNonNull(properties, Prop.Dcterms.LANGUAGE, uiAssetCreateRequest.getLanguage());
addNonNull(properties, Prop.Dcat.VERSION, uiAssetCreateRequest.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,22 @@ void test_buildAssetDto() {
}

@Test
@SneakyThrows
void test_empty() {

// Arrange
var assetJsonLd = createObjectBuilder()
.add(Prop.ID, "my-asset-1")
.build();
// Act
var uiAsset = assetMapper.buildUiAsset(assetJsonLd);

// Assert
assertThat(uiAsset).isNotNull();
assertThat(uiAsset.getAssetId()).isEqualTo("my-asset-1");
assertThat(uiAsset.getName()).isEqualTo("my-asset-1");
}

@Test
void test_KeywordsAsSingleString() {

// Arrange
Expand All @@ -84,14 +99,13 @@ void test_KeywordsAsSingleString() {
}

@Test
@SneakyThrows
void test_StringValueWrappedInAtValue() {

// Arrange
var assetJsonLd = createObjectBuilder()
.add(Prop.ID, "my-asset-1")
.add(Prop.Edc.PROPERTIES, createObjectBuilder()
.add(Prop.Dcterms.NAME, createObjectBuilder()
.add(Prop.Dcterms.TITLE, createObjectBuilder()
.add(Prop.VALUE, "AssetName")
.add(Prop.LANGUAGE, "en")))
.build();
Expand All @@ -105,12 +119,11 @@ void test_StringValueWrappedInAtValue() {
}

@Test
@SneakyThrows
void test_StringsAsMap() {

// Arrange
var properties = createObjectBuilder()
.add(Prop.Dcterms.NAME, createArrayBuilder()
.add(Prop.Dcterms.TITLE, createArrayBuilder()
.add(createObjectBuilder()
.add(Prop.TYPE, "SomeType")
.add(Prop.VALUE, "AssetName")
Expand All @@ -131,7 +144,6 @@ void test_StringsAsMap() {
}

@Test
@SneakyThrows
void test_badBooleanValue() {
// Arrange
var assetJsonLd = createObjectBuilder()
Expand All @@ -150,7 +162,6 @@ void test_badBooleanValue() {
}

@Test
@SneakyThrows
void test_noBooleanValue() {
// Arrange
var assetJsonLd = createObjectBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public static WrapperExtensionContext buildContext(
transferProcessStateService);
var transferHistoryPageAssetFetcherService = new TransferHistoryPageAssetFetcherService(
assetService,
transferProcessService);
transferProcessService,
assetMapper);
var contractNegotiationUtils = new ContractNegotiationUtils(contractNegotiationService);
var contractAgreementUtils = new ContractAgreementUtils(contractAgreementService);
var assetApiService = new AssetApiService(assetService, assetMapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

package de.sovity.edc.ext.wrapper.api.ui;

import de.sovity.edc.ext.wrapper.api.common.model.AssetDto;
import de.sovity.edc.ext.wrapper.api.common.model.PolicyDefinitionCreateRequest;
import de.sovity.edc.ext.wrapper.api.common.model.UiAsset;
import de.sovity.edc.ext.wrapper.api.common.model.UiAssetCreateRequest;
import de.sovity.edc.ext.wrapper.api.ui.model.AssetPage;
import de.sovity.edc.ext.wrapper.api.ui.model.ContractAgreementPage;
Expand Down Expand Up @@ -99,7 +99,7 @@ public TransferHistoryPage transferHistoryPageEndpoint() {
@GET
@Path("pages/transfer-history-page/transfer-processes/{transferProcessId}/asset")
@Produces(MediaType.APPLICATION_JSON)
public AssetDto getTransferProcessAsset(@PathParam("transferProcessId") String transferProcessId) {
public UiAsset getTransferProcessAsset(@PathParam("transferProcessId") String transferProcessId) {
return transferHistoryPageAssetFetcherService.getAssetForTransferHistoryPage(transferProcessId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import de.sovity.edc.ext.wrapper.api.ui.model.ContractAgreementDirection;
import de.sovity.edc.ext.wrapper.api.ui.model.TransferHistoryEntry;
import de.sovity.edc.utils.jsonld.vocab.Prop;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
Expand Down Expand Up @@ -84,7 +86,11 @@ public List<TransferHistoryEntry> getTransferHistoryEntries() {
if (direction == ContractAgreementDirection.CONSUMING) {
transferHistoryEntry.setAssetName(asset.getId());
} else {
transferHistoryEntry.setAssetName(asset.getName() == null ? asset.getId() : asset.getName());
transferHistoryEntry.setAssetName(
StringUtils.isBlank((String) asset.getProperties().get(Prop.Dcterms.TITLE))
? asset.getId()
: asset.getProperties().get(Prop.Dcterms.TITLE).toString()
);
}
transferHistoryEntry.setContractAgreementId(agreement.getId());
transferHistoryEntry.setCounterPartyConnectorEndpoint(negotiation.getCounterPartyAddress());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

package de.sovity.edc.ext.wrapper.api.ui.pages.transferhistory;

import de.sovity.edc.ext.wrapper.api.common.model.AssetDto;
import lombok.NonNull;
import de.sovity.edc.ext.wrapper.api.common.mappers.AssetMapper;
import de.sovity.edc.ext.wrapper.api.common.model.UiAsset;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.spi.transferprocess.TransferProcessService;
Expand All @@ -23,15 +23,14 @@
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.jetbrains.annotations.NotNull;

import static de.sovity.edc.ext.wrapper.utils.EdcDateUtils.utcMillisToOffsetDateTime;
import static de.sovity.edc.ext.wrapper.utils.MapUtils.mapValues;

@RequiredArgsConstructor
public class TransferHistoryPageAssetFetcherService {
private final AssetService assetService;
private final TransferProcessService transferProcessService;
private final AssetMapper assetMapper;


public AssetDto getAssetForTransferHistoryPage(String transferProcessId) {
public UiAsset getAssetForTransferHistoryPage(String transferProcessId) {

var transferProcessById = transferProcessService.findById(transferProcessId);
if (transferProcessById == null) {
Expand All @@ -41,20 +40,12 @@ public AssetDto getAssetForTransferHistoryPage(String transferProcessId) {
}

@NotNull
private AssetDto getAssetFromTransferProcess(TransferProcess process) {
private UiAsset getAssetFromTransferProcess(TransferProcess process) {
var assetId = process.getDataRequest().getAssetId();
var asset = assetService.findById(process.getDataRequest().getAssetId());
if (asset == null) {
asset = Asset.Builder.newInstance().id(assetId).build();
}
return buildAssetDto(asset);
}

@NotNull
private AssetDto buildAssetDto(@NonNull Asset asset) {
var createdAt = utcMillisToOffsetDateTime(asset.getCreatedAt());
var properties = mapValues(asset.getProperties(), Object::toString);
return new AssetDto(asset.getId(), createdAt, properties);
return assetMapper.buildUiAsset(asset);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Odrl {
public class Dcterms {
public final String CTX = "http://purl.org/dc/terms/";
public final String IDENTIFIER = CTX + "identifier";
public final String NAME = CTX + "title";
public final String TITLE = CTX + "title";
public final String DESCRIPTION = CTX + "description";
public final String LANGUAGE = CTX + "language";
public final String CREATOR = CTX + "creator";
Expand Down

0 comments on commit 5e87f14

Please sign in to comment.