Skip to content

Commit

Permalink
fix: review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eschrewe committed Jul 24, 2024
1 parent 148704a commit dcab4b7
Showing 1 changed file with 92 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.eclipse.tractusx.puris.backend.common.util.VariablesService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -59,6 +61,8 @@ public class EdcAdapterServiceTest {

private final Pattern urlPattern = PatternStore.URL_PATTERN;

private final JsonLdUtils jsonLdUtils = new JsonLdUtils();

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -120,7 +124,6 @@ public void correctConstraints_testContractPolicyConstraints_succeed() throws Js
"}";

JsonNode validJsonNode = objectMapper.readTree(validJson);
JsonLdUtils jsonLdUtils = new JsonLdUtils();
validJsonNode = jsonLdUtils.expand(validJsonNode);
System.out.println(validJsonNode.toPrettyString());

Expand Down Expand Up @@ -190,7 +193,6 @@ public void wrongConstraints_testContractPolicyConstraints_fails() throws JsonPr
"}";

JsonNode invalidJsonNode = objectMapper.readTree(invalidJson);
JsonLdUtils jsonLdUtils = new JsonLdUtils();
invalidJsonNode = jsonLdUtils.expand(invalidJsonNode);

// when
Expand Down Expand Up @@ -249,7 +251,6 @@ public void oneConstraint_testContractPolicyConstraints_fails() throws JsonProce
"}";

JsonNode invalidJsonNode = objectMapper.readTree(invalidJson);
JsonLdUtils jsonLdUtils = new JsonLdUtils();
invalidJsonNode = jsonLdUtils.expand(invalidJsonNode);

// when
Expand All @@ -263,135 +264,110 @@ public void oneConstraint_testContractPolicyConstraints_fails() throws JsonProce
}

/**
* Tests with unexpected (non-empty) prohibition, which must be rejected
* Tests policy with unexpected (non-empty) obligation or prohibition, which must be rejected
*
* @throws JsonProcessingException if json is invalid
*/
@Test
public void wrongProhibition_testContractPolicyConstraints_fails() throws JsonProcessingException {
@ParameterizedTest
@ValueSource(strings = {unexpectedObligation, unexpectedProhibition})
public void unexpectedRule_testContractPolicyConstraints_fails(String input) throws JsonProcessingException {
// given
String validJson = "{\n" +
" \"@id\" : \"PartTypeInformationSubmodelApi@BPNL00000007RXRX\",\n" +
" \"@type\" : \"dcat:Dataset\",\n" +
" \"odrl:hasPolicy\" : {\n" +
" \"@id\" : \"QlBOTDAwMDAwMDA3UlRVUF9jb250cmFjdGRlZmluaXRpb25fZm9yX1BhcnRUeXBlSW5mb3JtYXRpb25TdWJtb2RlbEFwaUBCUE5MMDAwMDAwMDdSWFJY:UGFydFR5cGVJbmZvcm1hdGlvblN1Ym1vZGVsQXBpQEJQTkwwMDAwMDAwN1JYUlg=:NzE3MGJmZDMtYTg5NS00YmU2LWI5Y2EtMDVhYTUwY2VjMDk2\",\n" +
" \"@type\" : \"odrl:Offer\",\n" +
" \"odrl:permission\" : {\n" +
" \"odrl:action\" : {\n" +
" \"@id\" : \"odrl:use\"\n" +
" },\n" +
" \"odrl:constraint\" : {\n" +
" \"odrl:and\" : [ {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:FrameworkAgreement\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"Puris:1.0\"\n" +
" }, {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:UsagePurpose\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"cx.puris.base:1\"\n" +
" } ]\n" +
" }\n" +
" },\n" +
" \"odrl:prohibition\" : [ {\"foo\": \"bar\"} ],\n" +
" \"odrl:obligation\" : [ ]\n" +
" }," +
" \"@context\": {\n" +
" \"@vocab\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"edc\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"tx\": \"https://w3id.org/tractusx/v0.0.1/ns/\",\n" +
" \"tx-auth\": \"https://w3id.org/tractusx/auth/\",\n" +
" \"cx-policy\": \"https://w3id.org/catenax/policy/\",\n" +
" \"dcat\": \"http://www.w3.org/ns/dcat#\",\n" +
" \"dct\": \"http://purl.org/dc/terms/\",\n" +
" \"odrl\": \"http://www.w3.org/ns/odrl/2/\",\n" +
" \"dspace\": \"https://w3id.org/dspace/v0.8/\"\n" +
" }" +
"}";

JsonNode validJsonNode = objectMapper.readTree(validJson);
JsonLdUtils jsonLdUtils = new JsonLdUtils();
JsonNode validJsonNode = objectMapper.readTree(input);
validJsonNode = jsonLdUtils.expand(validJsonNode);
System.out.println(validJsonNode.toPrettyString());


// when
when(variablesService.getPurisFrameworkAgreementWithVersion()).thenReturn("Puris:1.0");
when(variablesService.getPurisPurposeWithVersion()).thenReturn("cx.puris.base:1");

// then
boolean result = edcAdapterService.testContractPolicyConstraints(validJsonNode);

assertFalse(result);
}

/**
* Tests policy with unexpected (non-empty) obligation, which must be rejected
*
* @throws JsonProcessingException if json is invalid
*/
@Test
public void wrongObligation_testContractPolicyConstraints_fails() throws JsonProcessingException {
// given
String validJson = "{\n" +
" \"@id\" : \"PartTypeInformationSubmodelApi@BPNL00000007RXRX\",\n" +
" \"@type\" : \"dcat:Dataset\",\n" +
" \"odrl:hasPolicy\" : {\n" +
" \"@id\" : \"QlBOTDAwMDAwMDA3UlRVUF9jb250cmFjdGRlZmluaXRpb25fZm9yX1BhcnRUeXBlSW5mb3JtYXRpb25TdWJtb2RlbEFwaUBCUE5MMDAwMDAwMDdSWFJY:UGFydFR5cGVJbmZvcm1hdGlvblN1Ym1vZGVsQXBpQEJQTkwwMDAwMDAwN1JYUlg=:NzE3MGJmZDMtYTg5NS00YmU2LWI5Y2EtMDVhYTUwY2VjMDk2\",\n" +
" \"@type\" : \"odrl:Offer\",\n" +
" \"odrl:permission\" : {\n" +
" \"odrl:action\" : {\n" +
" \"@id\" : \"odrl:use\"\n" +
" },\n" +
" \"odrl:constraint\" : {\n" +
" \"odrl:and\" : [ {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:FrameworkAgreement\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"Puris:1.0\"\n" +
" }, {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:UsagePurpose\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"cx.puris.base:1\"\n" +
" } ]\n" +
" }\n" +
" },\n" +
" \"odrl:prohibition\" : [ ],\n" +
" \"odrl:obligation\" : [ {\"foo\": \"bar\"} ]\n" +
" }," +
" \"@context\": {\n" +
" \"@vocab\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"edc\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"tx\": \"https://w3id.org/tractusx/v0.0.1/ns/\",\n" +
" \"tx-auth\": \"https://w3id.org/tractusx/auth/\",\n" +
" \"cx-policy\": \"https://w3id.org/catenax/policy/\",\n" +
" \"dcat\": \"http://www.w3.org/ns/dcat#\",\n" +
" \"dct\": \"http://purl.org/dc/terms/\",\n" +
" \"odrl\": \"http://www.w3.org/ns/odrl/2/\",\n" +
" \"dspace\": \"https://w3id.org/dspace/v0.8/\"\n" +
" }" +
"}";

JsonNode validJsonNode = objectMapper.readTree(validJson);
JsonLdUtils jsonLdUtils = new JsonLdUtils();
validJsonNode = jsonLdUtils.expand(validJsonNode);
System.out.println(validJsonNode.toPrettyString());


// when
when(variablesService.getPurisFrameworkAgreementWithVersion()).thenReturn("Puris:1.0");
when(variablesService.getPurisPurposeWithVersion()).thenReturn("cx.puris.base:1");

// then
boolean result = edcAdapterService.testContractPolicyConstraints(validJsonNode);

assertFalse(result);
}
private final static String unexpectedProhibition = "{\n" +
" \"@id\" : \"PartTypeInformationSubmodelApi@BPNL00000007RXRX\",\n" +
" \"@type\" : \"dcat:Dataset\",\n" +
" \"odrl:hasPolicy\" : {\n" +
" \"@id\" : \"QlBOTDAwMDAwMDA3UlRVUF9jb250cmFjdGRlZmluaXRpb25fZm9yX1BhcnRUeXBlSW5mb3JtYXRpb25TdWJtb2RlbEFwaUBCUE5MMDAwMDAwMDdSWFJY:UGFydFR5cGVJbmZvcm1hdGlvblN1Ym1vZGVsQXBpQEJQTkwwMDAwMDAwN1JYUlg=:NzE3MGJmZDMtYTg5NS00YmU2LWI5Y2EtMDVhYTUwY2VjMDk2\",\n" +
" \"@type\" : \"odrl:Offer\",\n" +
" \"odrl:permission\" : {\n" +
" \"odrl:action\" : {\n" +
" \"@id\" : \"odrl:use\"\n" +
" },\n" +
" \"odrl:constraint\" : {\n" +
" \"odrl:and\" : [ {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:FrameworkAgreement\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"Puris:1.0\"\n" +
" }, {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:UsagePurpose\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"cx.puris.base:1\"\n" +
" } ]\n" +
" }\n" +
" },\n" +
" \"odrl:prohibition\" : [ {\"foo\": \"bar\"} ],\n" +
" \"odrl:obligation\" : [ ]\n" +
" }," +
" \"@context\": {\n" +
" \"@vocab\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"edc\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"tx\": \"https://w3id.org/tractusx/v0.0.1/ns/\",\n" +
" \"tx-auth\": \"https://w3id.org/tractusx/auth/\",\n" +
" \"cx-policy\": \"https://w3id.org/catenax/policy/\",\n" +
" \"dcat\": \"http://www.w3.org/ns/dcat#\",\n" +
" \"dct\": \"http://purl.org/dc/terms/\",\n" +
" \"odrl\": \"http://www.w3.org/ns/odrl/2/\",\n" +
" \"dspace\": \"https://w3id.org/dspace/v0.8/\"\n" +
" }" +
"}";

private final static String unexpectedObligation = "{\n" +
" \"@id\" : \"PartTypeInformationSubmodelApi@BPNL00000007RXRX\",\n" +
" \"@type\" : \"dcat:Dataset\",\n" +
" \"odrl:hasPolicy\" : {\n" +
" \"@id\" : \"QlBOTDAwMDAwMDA3UlRVUF9jb250cmFjdGRlZmluaXRpb25fZm9yX1BhcnRUeXBlSW5mb3JtYXRpb25TdWJtb2RlbEFwaUBCUE5MMDAwMDAwMDdSWFJY:UGFydFR5cGVJbmZvcm1hdGlvblN1Ym1vZGVsQXBpQEJQTkwwMDAwMDAwN1JYUlg=:NzE3MGJmZDMtYTg5NS00YmU2LWI5Y2EtMDVhYTUwY2VjMDk2\",\n" +
" \"@type\" : \"odrl:Offer\",\n" +
" \"odrl:permission\" : {\n" +
" \"odrl:action\" : {\n" +
" \"@id\" : \"odrl:use\"\n" +
" },\n" +
" \"odrl:constraint\" : {\n" +
" \"odrl:and\" : [ {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:FrameworkAgreement\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"Puris:1.0\"\n" +
" }, {\n" +
" \"odrl:leftOperand\" : { \"@id\": \"cx-policy:UsagePurpose\"},\n" +
" \"odrl:operator\" : {\n" +
" \"@id\" : \"odrl:eq\"\n" +
" },\n" +
" \"odrl:rightOperand\" : \"cx.puris.base:1\"\n" +
" } ]\n" +
" }\n" +
" },\n" +
" \"odrl:prohibition\" : [ ],\n" +
" \"odrl:obligation\" : [ {\"foo\": \"bar\"} ]\n" +
" }," +
" \"@context\": {\n" +
" \"@vocab\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"edc\": \"https://w3id.org/edc/v0.0.1/ns/\",\n" +
" \"tx\": \"https://w3id.org/tractusx/v0.0.1/ns/\",\n" +
" \"tx-auth\": \"https://w3id.org/tractusx/auth/\",\n" +
" \"cx-policy\": \"https://w3id.org/catenax/policy/\",\n" +
" \"dcat\": \"http://www.w3.org/ns/dcat#\",\n" +
" \"dct\": \"http://purl.org/dc/terms/\",\n" +
" \"odrl\": \"http://www.w3.org/ns/odrl/2/\",\n" +
" \"dspace\": \"https://w3id.org/dspace/v0.8/\"\n" +
" }" +
"}";

}

0 comments on commit dcab4b7

Please sign in to comment.