forked from eclipse-tractusx/tractusx-edc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(edrs): add EDR api schema and example (eclipse-tractusx#705)
- Loading branch information
Showing
5 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...tensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/schema/EdrSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.api.edr.schema; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import org.eclipse.edc.connector.api.management.configuration.ManagementApiSchema; | ||
import org.eclipse.edc.connector.api.management.contractnegotiation.ContractNegotiationApi; | ||
import org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry; | ||
|
||
import java.util.List; | ||
|
||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE; | ||
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.EndpointDataReferenceEntrySchema.ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE; | ||
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_EXAMPLE; | ||
|
||
public class EdrSchema { | ||
|
||
@Schema(name = "NegotiateEdrRequest", example = NEGOTIATE_EDR_REQUEST_EXAMPLE) | ||
public record NegotiateEdrRequestSchema( | ||
@Schema(name = TYPE, example = EDR_REQUEST_DTO_TYPE) | ||
String type, | ||
String protocol, | ||
String connectorAddress, | ||
@Deprecated(since = "0.1.3") | ||
@Schema(deprecated = true, description = "please use providerId instead") | ||
String connectorId, | ||
String providerId, | ||
ContractNegotiationApi.ContractOfferDescriptionSchema offer, | ||
List<ManagementApiSchema.CallbackAddressSchema> callbackAddresses) { | ||
|
||
public static final String NEGOTIATE_EDR_REQUEST_EXAMPLE = """ | ||
{ | ||
"@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" }, | ||
"@type": "NegotiateEdrRequestDto", | ||
"connectorAddress": "http://provider-address", | ||
"protocol": "dataspace-protocol-http", | ||
"providerId": "provider-id", | ||
"offer": { | ||
"offerId": "offer-id", | ||
"assetId": "asset-id", | ||
"policy": { | ||
"@context": "http://www.w3.org/ns/odrl.jsonld", | ||
"@type": "Set", | ||
"@id": "offer-id", | ||
"permission": [{ | ||
"target": "asset-id", | ||
"action": "display" | ||
}] | ||
} | ||
}, | ||
"callbackAddresses": [{ | ||
"transactional": false, | ||
"uri": "http://callback/url", | ||
"events": ["contract.negotiation", "transfer.process"] | ||
}] | ||
} | ||
"""; | ||
} | ||
|
||
@Schema(name = "EndpointDataReferenceEntry", example = ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE) | ||
public record EndpointDataReferenceEntrySchema( | ||
@Schema(name = TYPE, example = EndpointDataReferenceEntry.SIMPLE_TYPE) | ||
String type, | ||
String agreementId, | ||
String assetId, | ||
String providerId, | ||
String edrState, | ||
Long expirationDate | ||
) { | ||
public static final String ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE = """ | ||
{ | ||
"@type": "tx:EndpointDataReferenceEntry", | ||
"edc:agreementId": "MQ==:MQ==:ZTY3MzQ4YWEtNTdmZC00YzA0LTg2ZmQtMGMxNzk0MWM3OTkw", | ||
"edc:transferProcessId": "78a66945-d638-4c0a-be71-b35a0318a410", | ||
"edc:assetId": "1", | ||
"edc:providerId": "BPNL00DATAP00001", | ||
"tx:edrState": "NEGOTIATED", | ||
"tx:expirationDate": 1690811364000, | ||
"@context": { | ||
"dct": "https://purl.org/dc/terms/", | ||
"tx": "https://w3id.org/tractusx/v0.0.1/ns/", | ||
"edc": "https://w3id.org/edc/v0.0.1/ns/", | ||
"dcat": "https://www.w3.org/ns/dcat/", | ||
"odrl": "http://www.w3.org/ns/odrl/2/", | ||
"dspace": "https://w3id.org/dspace/v0.8/" | ||
} | ||
} | ||
"""; | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/EdrApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.api.edr; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.api.transformer.JsonObjectToCallbackAddressTransformer; | ||
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferDescriptionTransformer; | ||
import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractRequestTransformer; | ||
import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl; | ||
import org.eclipse.edc.core.transform.transformer.OdrlTransformersFactory; | ||
import org.eclipse.edc.jsonld.JsonLdExtension; | ||
import org.eclipse.edc.jsonld.spi.JsonLd; | ||
import org.eclipse.edc.jsonld.util.JacksonJsonLd; | ||
import org.eclipse.edc.junit.assertions.AbstractResultAssert; | ||
import org.eclipse.edc.transform.spi.TypeTransformerRegistry; | ||
import org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto; | ||
import org.eclipse.tractusx.edc.api.edr.transform.JsonObjectToNegotiateEdrRequestDtoTransformer; | ||
import org.eclipse.tractusx.edc.api.edr.validation.NegotiateEdrRequestDtoValidator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; | ||
import static org.eclipse.edc.junit.extensions.TestServiceExtensionContext.testServiceExtensionContext; | ||
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.EndpointDataReferenceEntrySchema.ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE; | ||
import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_EXAMPLE; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_EXPIRATION_DATE; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_PROVIDER_ID; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_STATE; | ||
import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_TRANSFER_PROCESS_ID; | ||
|
||
public class EdrApiTest { | ||
|
||
private final ObjectMapper objectMapper = JacksonJsonLd.createObjectMapper(); | ||
private final JsonLd jsonLd = new JsonLdExtension().createJsonLdService(testServiceExtensionContext()); | ||
|
||
private final TypeTransformerRegistry transformer = new TypeTransformerRegistryImpl(); | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer.register(new JsonObjectToContractRequestTransformer()); | ||
transformer.register(new JsonObjectToContractOfferDescriptionTransformer()); | ||
transformer.register(new JsonObjectToCallbackAddressTransformer()); | ||
transformer.register(new JsonObjectToNegotiateEdrRequestDtoTransformer()); | ||
OdrlTransformersFactory.jsonObjectToOdrlTransformers().forEach(transformer::register); | ||
} | ||
|
||
@Test | ||
void edrRequestExample() throws JsonProcessingException { | ||
var validator = NegotiateEdrRequestDtoValidator.instance(); | ||
|
||
var jsonObject = objectMapper.readValue(NEGOTIATE_EDR_REQUEST_EXAMPLE, JsonObject.class); | ||
assertThat(jsonObject).isNotNull(); | ||
|
||
var expanded = jsonLd.expand(jsonObject); | ||
AbstractResultAssert.assertThat(expanded).isSucceeded() | ||
.satisfies(exp -> AbstractResultAssert.assertThat(validator.validate(exp)).isSucceeded()) | ||
.extracting(e -> transformer.transform(e, NegotiateEdrRequestDto.class)) | ||
.satisfies(transformResult -> AbstractResultAssert.assertThat(transformResult).isSucceeded() | ||
.satisfies(transformed -> { | ||
assertThat(transformed.getOffer()).isNotNull(); | ||
assertThat(transformed.getCallbackAddresses()).asList().hasSize(1); | ||
assertThat(transformed.getProviderId()).isNotBlank(); | ||
})); | ||
} | ||
|
||
@Test | ||
void edrEntryExample() throws JsonProcessingException { | ||
|
||
var jsonObject = objectMapper.readValue(ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE, JsonObject.class); | ||
assertThat(jsonObject).isNotNull(); | ||
|
||
var expanded = jsonLd.expand(jsonObject); | ||
|
||
AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(content -> { | ||
|
||
assertThat(first(content, EDR_ENTRY_STATE).getJsonString(VALUE).getString()) | ||
.isEqualTo(jsonObject.getString("tx:edrState")); | ||
|
||
assertThat(first(content, EDR_ENTRY_ASSET_ID).getJsonString(VALUE).getString()) | ||
.isEqualTo(jsonObject.getString("edc:assetId")); | ||
|
||
assertThat(first(content, EDR_ENTRY_AGREEMENT_ID).getJsonString(VALUE).getString()) | ||
.isEqualTo(jsonObject.getString("edc:agreementId")); | ||
|
||
assertThat(first(content, EDR_ENTRY_TRANSFER_PROCESS_ID).getJsonString(VALUE).getString()) | ||
.isEqualTo(jsonObject.getString("edc:transferProcessId")); | ||
|
||
assertThat(first(content, EDR_ENTRY_PROVIDER_ID).getJsonString(VALUE).getString()) | ||
.isEqualTo(jsonObject.getString("edc:providerId")); | ||
|
||
assertThat(first(content, EDR_ENTRY_EXPIRATION_DATE).getJsonNumber(VALUE).longValue()) | ||
.isEqualTo(jsonObject.getJsonNumber("tx:expirationDate").longValue()); | ||
}); | ||
} | ||
|
||
private JsonObject first(JsonObject content, String name) { | ||
return content.getJsonArray(name).getJsonObject(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters