-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
271 additions
and
67 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
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
44 changes: 44 additions & 0 deletions
44
...or/api/management/contractnegotiation/transform/JsonObjectToContractOfferTransformer.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,44 @@ | ||
/* | ||
* 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.edc.connector.api.management.contractnegotiation.transform; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.policy.model.Policy; | ||
import org.eclipse.edc.spi.types.domain.offer.ContractOffer; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class JsonObjectToContractOfferTransformer extends AbstractJsonLdTransformer<JsonObject, ContractOffer> { | ||
|
||
public JsonObjectToContractOfferTransformer() { | ||
super(JsonObject.class, ContractOffer.class); | ||
} | ||
|
||
@Override | ||
public @Nullable ContractOffer transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) { | ||
var policy = context.transform(jsonObject, Policy.class); | ||
if (policy == null) { | ||
return null; | ||
} | ||
var id = nodeId(jsonObject); | ||
return ContractOffer.Builder.newInstance() | ||
.id(id) | ||
.assetId(policy.getTarget()) | ||
.policy(policy) | ||
.build(); | ||
} | ||
} |
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
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
87 changes: 87 additions & 0 deletions
87
...pi/management/contractnegotiation/transform/JsonObjectToContractOfferTransformerTest.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,87 @@ | ||
/* | ||
* 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.edc.connector.api.management.contractnegotiation.transform; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.jsonld.TitaniumJsonLd; | ||
import org.eclipse.edc.jsonld.spi.JsonLd; | ||
import org.eclipse.edc.policy.model.Policy; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static jakarta.json.Json.createObjectBuilder; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OBLIGATION_ATTRIBUTE; | ||
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE; | ||
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; | ||
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE; | ||
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class JsonObjectToContractOfferTransformerTest { | ||
|
||
private final JsonLd jsonLd = new TitaniumJsonLd(mock(Monitor.class)); | ||
private final TransformerContext context = mock(); | ||
private JsonObjectToContractOfferTransformer transformer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer = new JsonObjectToContractOfferTransformer(); | ||
} | ||
|
||
@Test | ||
void transform() { | ||
var offerPolicy = createObjectBuilder() | ||
.add(TYPE, ODRL_POLICY_TYPE_SET) | ||
.add(ID, "test-offer-id") | ||
.add(ODRL_TARGET_ATTRIBUTE, "test-asset") | ||
.add(ODRL_PERMISSION_ATTRIBUTE, getJsonObject("permission")) | ||
.add(ODRL_PROHIBITION_ATTRIBUTE, getJsonObject("prohibition")) | ||
.add(ODRL_OBLIGATION_ATTRIBUTE, getJsonObject("duty")) | ||
.build(); | ||
|
||
var policy = Policy.Builder.newInstance().target("test-asset").build(); | ||
when(context.transform(any(JsonValue.class), eq(Policy.class))).thenReturn(policy); | ||
|
||
var result = transformer.transform(jsonLd.expand(offerPolicy).getContent(), context); | ||
|
||
assertThat(result).isNotNull(); | ||
assertThat(result.getId()).isEqualTo("test-offer-id"); | ||
assertThat(result.getAssetId()).isEqualTo("test-asset"); | ||
} | ||
|
||
@Test | ||
void shouldReturnNull_whenPolicyIsNull() { | ||
when(context.transform(any(JsonValue.class), eq(Policy.class))).thenReturn(null); | ||
|
||
var result = transformer.transform(createObjectBuilder().build(), context); | ||
|
||
assertThat(result).isNull(); | ||
} | ||
|
||
private JsonObject getJsonObject(String type) { | ||
return createObjectBuilder() | ||
.add(TYPE, type) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.