Skip to content

Commit

Permalink
feat(irs):[#249] changed return type of get all policies to match new…
Browse files Browse the repository at this point in the history
… format
  • Loading branch information
ds-psosnowski committed Feb 1, 2024
1 parent 7a8b34d commit a72d875
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -133,8 +135,10 @@ public void registerAllowedPolicy(final @RequestBody CreatePolicyRequest request
@GetMapping("/policies")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')")
public List<Policy> getPolicies() {
return service.getStoredPolicies();
public List<PolicyResponse> getPolicies() {
return service.getStoredPolicies().stream()
.map(PolicyResponse::fromPolicy)
.collect(Collectors.toList());
}

@Operation(operationId = "deleteAllowedPolicy",
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
) {

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -126,14 +130,16 @@ void registerAllowedPolicy() {
@Test
void getPolicies() {
// arrange
final List<Policy> policies = List.of(new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions()));
final List<Policy> policies = List.of(
new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions()));
when(service.getStoredPolicies()).thenReturn(policies);

// act
final var returnedPolicies = testee.getPolicies();
final List<PolicyResponse> returnedPolicies = testee.getPolicies();

// assert
assertThat(returnedPolicies).isEqualTo(policies);
assertThat(returnedPolicies).isEqualTo(
policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList()));
}

@Test
Expand All @@ -159,19 +165,14 @@ void updateAllowedPolicy() {
}

private List<Permission> 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")));
}
}

0 comments on commit a72d875

Please sign in to comment.