From a72d8756ab31be977d024aecd2aeea36c1c320b3 Mon Sep 17 00:00:00 2001 From: Pawel Sosnowski Date: Thu, 1 Feb 2024 13:25:15 +0100 Subject: [PATCH] feat(irs):[#249] changed return type of get all policies to match new format --- .../controllers/PolicyStoreController.java | 8 +- .../irs/policystore/models/Context.java | 31 ++++++ .../irs/policystore/models/Payload.java | 36 +++++++ .../policystore/models/PolicyResponse.java | 43 ++++++++ .../models/UpdatePolicyRequest.java | 5 +- .../PolicyStoreControllerTest.java | 101 +++++++++--------- 6 files changed, 169 insertions(+), 55 deletions(-) create mode 100644 irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Context.java create mode 100644 irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Payload.java create mode 100644 irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/PolicyResponse.java diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java index 7d280e0f95..97cb6cacd3 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java @@ -28,6 +28,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import java.util.List; +import java.util.stream.Collectors; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.ArraySchema; @@ -45,6 +46,7 @@ import org.eclipse.tractusx.irs.edc.client.policy.Policy; import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer; import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest; +import org.eclipse.tractusx.irs.policystore.models.PolicyResponse; import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest; import org.eclipse.tractusx.irs.policystore.services.PolicyStoreService; import org.springframework.http.HttpStatus; @@ -133,8 +135,10 @@ public void registerAllowedPolicy(final @RequestBody CreatePolicyRequest request @GetMapping("/policies") @ResponseStatus(HttpStatus.OK) @PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')") - public List getPolicies() { - return service.getStoredPolicies(); + public List getPolicies() { + return service.getStoredPolicies().stream() + .map(PolicyResponse::fromPolicy) + .collect(Collectors.toList()); } @Operation(operationId = "deleteAllowedPolicy", diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Context.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Context.java new file mode 100644 index 0000000000..8e8c7c3a45 --- /dev/null +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Context.java @@ -0,0 +1,31 @@ +/******************************************************************************** + * 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.policystore.models; + +/** + * Context representation for get all policies response + */ +public record Context(String odrl) { + private static final String ODRL_VALUE = "http://www.w3.org/ns/odrl/2/"; + + public static Context getDefault() { + return new Context(ODRL_VALUE); + } +} diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Payload.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Payload.java new file mode 100644 index 0000000000..42a29dfde1 --- /dev/null +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/Payload.java @@ -0,0 +1,36 @@ +/******************************************************************************** + * 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.policystore.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import org.eclipse.tractusx.irs.edc.client.policy.Policy; + +/** + * Payload representation for get all policies response + */ +@Builder +public record Payload( + @JsonProperty("@context") Context context, + @JsonProperty("@id") String policyId, + Policy policy +) { + +} diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/PolicyResponse.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/PolicyResponse.java new file mode 100644 index 0000000000..36d7d5fe8d --- /dev/null +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/PolicyResponse.java @@ -0,0 +1,43 @@ +/******************************************************************************** + * 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.policystore.models; + +import java.time.OffsetDateTime; + +import lombok.Builder; +import org.eclipse.tractusx.irs.edc.client.policy.Policy; + +/** + * Policy representation for get all policies response + */ +@Builder +public record PolicyResponse(OffsetDateTime validUntil, Payload payload) { + + public static PolicyResponse fromPolicy(final Policy policy) { + return PolicyResponse.builder() + .validUntil(policy.getValidUntil()) + .payload(Payload.builder() + .policyId(policy.getPolicyId()) + .context(Context.getDefault()) + .policy(policy) + .build()) + .build(); + } +} diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/UpdatePolicyRequest.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/UpdatePolicyRequest.java index af6749ff85..4b4c455bd3 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/UpdatePolicyRequest.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/UpdatePolicyRequest.java @@ -30,9 +30,8 @@ /** * Request object for policy update - * */ @Schema(description = "Request to add a policy") -public record UpdatePolicyRequest(@Schema(description = "Timestamp after which the policy will no longer be accepted in negotiations") @NotNull OffsetDateTime validUntil) { - +public record UpdatePolicyRequest( + @Schema(description = "Timestamp after which the policy will no longer be accepted in negotiations") @NotNull OffsetDateTime validUntil) { } diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java index fcccff5225..f685cd33ab 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java @@ -32,6 +32,7 @@ import java.time.OffsetDateTime; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import jakarta.json.Json; import jakarta.json.JsonObject; @@ -48,6 +49,7 @@ import org.eclipse.tractusx.irs.edc.client.policy.PolicyType; import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer; import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest; +import org.eclipse.tractusx.irs.policystore.models.PolicyResponse; import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest; import org.eclipse.tractusx.irs.policystore.services.PolicyStoreService; import org.junit.jupiter.api.BeforeEach; @@ -60,45 +62,46 @@ class PolicyStoreControllerTest { public static final String EXAMPLE_PAYLOAD = """ - { - "validUntil": "2025-12-12T23:59:59.999Z", - "payload": { - "@context": { - "odrl": "http://www.w3.org/ns/odrl/2/" - }, - "@id": "policy-id", - "policy": { - "odrl:permission": [ - { - "odrl:action": "USE", - "odrl:constraint": { - "odrl:and": [ - { - "odrl:leftOperand": "Membership", - "odrl:operator": { - "@id": "odrl:eq" - }, - "odrl:rightOperand": "active" - }, - { - "odrl:leftOperand": "PURPOSE", - "odrl:operator": { - "@id": "odrl:eq" - }, - "odrl:rightOperand": "ID 3.1 Trace" - } - ] - } - } - ] - } - } - } - """; + { + "validUntil": "2025-12-12T23:59:59.999Z", + "payload": { + "@context": { + "odrl": "http://www.w3.org/ns/odrl/2/" + }, + "@id": "policy-id", + "policy": { + "odrl:permission": [ + { + "odrl:action": "USE", + "odrl:constraint": { + "odrl:and": [ + { + "odrl:leftOperand": "Membership", + "odrl:operator": { + "@id": "odrl:eq" + }, + "odrl:rightOperand": "active" + }, + { + "odrl:leftOperand": "PURPOSE", + "odrl:operator": { + "@id": "odrl:eq" + }, + "odrl:rightOperand": "ID 3.1 Trace" + } + ] + } + } + ] + } + } + } + """; private PolicyStoreController testee; private final TitaniumJsonLd titaniumJsonLd = new TitaniumJsonLd(new ConsoleMonitor()); - private final EdcTransformer edcTransformer = new EdcTransformer(new com.fasterxml.jackson.databind.ObjectMapper(), titaniumJsonLd, new TypeTransformerRegistryImpl()); + private final EdcTransformer edcTransformer = new EdcTransformer(new com.fasterxml.jackson.databind.ObjectMapper(), + titaniumJsonLd, new TypeTransformerRegistryImpl()); @Mock private PolicyStoreService service; @@ -117,7 +120,8 @@ void registerAllowedPolicy() { jsonReader.close(); // act - testee.registerAllowedPolicy(new CreatePolicyRequest(now.plusMinutes(1), jsonObject.get("payload").asJsonObject())); + testee.registerAllowedPolicy( + new CreatePolicyRequest(now.plusMinutes(1), jsonObject.get("payload").asJsonObject())); // assert verify(service).registerPolicy(any()); @@ -126,14 +130,16 @@ void registerAllowedPolicy() { @Test void getPolicies() { // arrange - final List policies = List.of(new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions())); + final List policies = List.of( + new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions())); when(service.getStoredPolicies()).thenReturn(policies); // act - final var returnedPolicies = testee.getPolicies(); + final List returnedPolicies = testee.getPolicies(); // assert - assertThat(returnedPolicies).isEqualTo(policies); + assertThat(returnedPolicies).isEqualTo( + policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList())); } @Test @@ -159,19 +165,14 @@ void updateAllowedPolicy() { } private List createPermissions() { - return List.of( - new Permission(PolicyType.USE, createConstraints()), - new Permission(PolicyType.ACCESS, createConstraints()) - ); + return List.of(new Permission(PolicyType.USE, createConstraints()), + new Permission(PolicyType.ACCESS, createConstraints())); } private Constraints createConstraints() { - return new Constraints( - Collections.emptyList(), - List.of( - new Constraint("Membership", new Operator(OperatorType.EQ), "active"), + 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")) - ); + new Constraint("PURPOSE", new Operator(OperatorType.EQ), "ID 3.1 Trace"))); } } \ No newline at end of file