Skip to content

Commit

Permalink
feat(impl): [#528] extract constraints to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed May 13, 2024
1 parent 2711640 commit 67ea850
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.policy;

/**
* Constants for {@link Constraint}s
*/
public final class ConstraintConstants {

private ConstraintConstants() {
// helper class
}

public static final Constraint ACTIVE_MEMBERSHIP = new Constraint("Membership", new Operator(OperatorType.EQ),
"active");

public static final Constraint FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE = new Constraint(
"FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active");

public static final Constraint PURPOSE_ID_3_1_TRACE = new Constraint("PURPOSE", new Operator(OperatorType.EQ),
"ID 3.1 Trace");

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.tractusx.irs.edc.client.policy.ConstraintConstants.ACTIVE_MEMBERSHIP;
import static org.eclipse.tractusx.irs.edc.client.policy.ConstraintConstants.FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE;
import static org.eclipse.tractusx.irs.edc.client.testutil.TestMother.createEdcTransformer;
import static org.eclipse.tractusx.irs.testing.wiremock.SubmodelFacadeWiremockSupport.DATAPLANE_HOST;
import static org.eclipse.tractusx.irs.testing.wiremock.SubmodelFacadeWiremockSupport.PATH_DATAPLANE_PUBLIC;
Expand Down Expand Up @@ -67,8 +69,6 @@
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
import org.eclipse.tractusx.irs.edc.client.policy.ConstraintCheckerService;
import org.eclipse.tractusx.irs.edc.client.policy.Constraints;
import org.eclipse.tractusx.irs.edc.client.policy.Operator;
import org.eclipse.tractusx.irs.edc.client.policy.OperatorType;
import org.eclipse.tractusx.irs.edc.client.policy.Permission;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyCheckerService;
Expand Down Expand Up @@ -134,11 +134,10 @@ void configureSystemUnderTest(WireMockRuntimeInfo wireMockRuntimeInfo) {
storage);

acceptedPoliciesProvider = mock(AcceptedPoliciesProvider.class);
when(acceptedPoliciesProvider.getAcceptedPolicies("BPN")).thenReturn(List.of(new AcceptedPolicy(policy("IRS Policy",
List.of(new Permission(PolicyType.USE, new Constraints(
List.of(new Constraint("Membership", new Operator(OperatorType.EQ), "active"),
new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ),
"active")), new ArrayList<>())))), OffsetDateTime.now().plusYears(1))));
when(acceptedPoliciesProvider.getAcceptedPolicies("BPN")).thenReturn(List.of(new AcceptedPolicy(
policy("IRS Policy", List.of(new Permission(PolicyType.USE,
new Constraints(List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE),
new ArrayList<>())))), OffsetDateTime.now().plusYears(1))));
final PolicyCheckerService policyCheckerService = new PolicyCheckerService(acceptedPoliciesProvider,
new ConstraintCheckerService());
final ContractNegotiationService contractNegotiationService = new ContractNegotiationService(controlPlaneClient,
Expand All @@ -157,11 +156,9 @@ void shouldReturnAssemblyPartRelationshipAsString()
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(
responseWithStatus(200).withBodyFile("singleLevelBomAsBuilt.json")));

final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"), new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE,
new Constraints(andConstraints, orConstraints));
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));
when(acceptedPoliciesProvider.getAcceptedPolicies(eq("bpn"))).thenReturn(List.of(acceptedPolicy));
Expand All @@ -182,11 +179,9 @@ void shouldReturnMaterialForRecyclingAsString()
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(
responseWithStatus(200).withBodyFile("materialForRecycling.json")));

final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"), new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE,
new Constraints(andConstraints, orConstraints));
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));
when(acceptedPoliciesProvider.getAcceptedPolicies(eq("bpn"))).thenReturn(List.of(acceptedPolicy));
Expand All @@ -206,11 +201,9 @@ void shouldReturnObjectAsStringWhenResponseNotJSON()
prepareNegotiation();
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(responseWithStatus(200).withBody("test")));

final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"), new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE,
new Constraints(andConstraints, orConstraints));
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));
when(acceptedPoliciesProvider.getAcceptedPolicies(eq("bpn"))).thenReturn(List.of(acceptedPolicy));
Expand All @@ -226,8 +219,7 @@ void shouldReturnObjectAsStringWhenResponseNotJSON()
@Test
void shouldThrowExceptionWhenPoliciesAreNotAccepted() {
// Arrange
final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
Expand All @@ -241,8 +233,8 @@ void shouldThrowExceptionWhenPoliciesAreNotAccepted() {
// Act & Assert
final String errorMessage = "Consumption of asset '5a7ab616-989f-46ae-bdf2-32027b9f6ee6-31b614f5-ec14-4ed2-a509-e7b7780083e7' is not permitted as the required catalog offer policies do not comply with defined IRS policies.";
assertThatExceptionOfType(UsagePolicyException.class).isThrownBy(
() -> edcSubmodelClient.getSubmodelPayload(CONNECTOR_ENDPOINT_URL, SUBMODEL_DATAPLANE_URL, ASSET_ID, "bpn")
.get()).withMessageEndingWith(errorMessage);
() -> edcSubmodelClient.getSubmodelPayload(CONNECTOR_ENDPOINT_URL, SUBMODEL_DATAPLANE_URL, ASSET_ID,
"bpn").get()).withMessageEndingWith(errorMessage);
}

@Test
Expand All @@ -252,11 +244,9 @@ void shouldThrowExceptionWhenResponse_400() {
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(
responseWithStatus(400).withBody("{ error: '400'}")));

final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"), new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE,
new Constraints(andConstraints, orConstraints));
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));
when(acceptedPoliciesProvider.getAcceptedPolicies(eq("bpn"))).thenReturn(List.of(acceptedPolicy));
Expand All @@ -277,11 +267,9 @@ void shouldThrowExceptionWhenResponse_500() {
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(
responseWithStatus(500).withBody("{ error: '500'}")));

final List<Constraint> andConstraints = List.of(
new Constraint("Membership", new Operator(OperatorType.EQ), "active"), new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"));
final List<Constraint> andConstraints = List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE);
final ArrayList<Constraint> orConstraints = new ArrayList<>();
final Permission permission = new Permission(PolicyType.USE,
new Constraints(andConstraints, orConstraints));
final Permission permission = new Permission(PolicyType.USE, new Constraints(andConstraints, orConstraints));
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));
when(acceptedPoliciesProvider.getAcceptedPolicies(eq("bpn"))).thenReturn(List.of(acceptedPolicy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
package org.eclipse.tractusx.irs.policystore.controllers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.tractusx.irs.edc.client.policy.ConstraintConstants.ACTIVE_MEMBERSHIP;
import static org.eclipse.tractusx.irs.edc.client.policy.ConstraintConstants.FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE;
import static org.eclipse.tractusx.irs.edc.client.policy.ConstraintConstants.PURPOSE_ID_3_1_TRACE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -37,10 +40,7 @@

import jakarta.json.JsonObject;
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
import org.eclipse.tractusx.irs.edc.client.policy.Constraints;
import org.eclipse.tractusx.irs.edc.client.policy.Operator;
import org.eclipse.tractusx.irs.edc.client.policy.OperatorType;
import org.eclipse.tractusx.irs.edc.client.policy.Permission;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyType;
Expand Down Expand Up @@ -215,9 +215,7 @@ private List<Permission> createPermissions() {

private Constraints createConstraints() {
return new Constraints(Collections.emptyList(),
List.of(new Constraint("Membership", new Operator(OperatorType.EQ), "active"),
new Constraint("FrameworkAgreement.traceability", new Operator(OperatorType.EQ), "active"),
new Constraint("PURPOSE", new Operator(OperatorType.EQ), "ID 3.1 Trace")));
List.of(ACTIVE_MEMBERSHIP, FRAMEWORK_AGREEMENT_TRACEABILITY_ACTIVE, PURPOSE_ID_3_1_TRACE));
}

private static String randomPolicyId() {
Expand Down

0 comments on commit 67ea850

Please sign in to comment.