diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java index efa8dcb846..16a1e89caf 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java @@ -32,8 +32,13 @@ import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPoliciesProvider; import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPolicy; import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException; +import org.eclipse.tractusx.irs.policystore.models.Constraint; +import org.eclipse.tractusx.irs.policystore.models.Constraints; import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest; +import org.eclipse.tractusx.irs.policystore.models.OperatorType; +import org.eclipse.tractusx.irs.policystore.models.Permission; import org.eclipse.tractusx.irs.policystore.models.Policy; +import org.eclipse.tractusx.irs.policystore.models.PolicyType; import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest; import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence; import org.springframework.beans.factory.annotation.Value; @@ -62,12 +67,18 @@ public PolicyStoreService(@Value("${apiAllowedBpn:}") final String apiAllowedBpn .map(p -> new Policy(p, OffsetDateTime.now(), OffsetDateTime.now().plusYears( DEFAULT_POLICY_LIFETIME_YEARS), - Collections.emptyList())) + createPermissionFrom(p))) .toList(); this.persistence = persistence; this.clock = clock; } + private List createPermissionFrom(final String name) { + return List.of(new Permission(PolicyType.USE, + List.of(new Constraints(List.of(new Constraint("PURPOSE", OperatorType.EQ, List.of(name))), + Collections.emptyList())))); + } + public void registerPolicy(final CreatePolicyRequest request) { log.info("Registering new policy with id {}, valid until {}", request.policyId(), request.validUntil()); try { diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java index e3c64a117d..5ea951bafe 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java @@ -60,6 +60,7 @@ class PolicyStoreServiceTest { private static final String BPN = "testBpn"; + private static final String EXAMPLE_ALLOWED_NAME = "ID 3.0 Trace"; private PolicyStoreService testee; @Mock @@ -72,7 +73,7 @@ class PolicyStoreServiceTest { @BeforeEach void setUp() { - testee = new PolicyStoreService(BPN, List.of(), persistence, clock); + testee = new PolicyStoreService(BPN, List.of(EXAMPLE_ALLOWED_NAME), persistence, clock); } @Test @@ -129,6 +130,17 @@ void getStoredPolicies() { assertThat(storedPolicies).hasSize(3); } + @Test + void getStoredPoliciesWhenEmpty() { + // act + final var defaultPolicies = testee.getStoredPolicies(); + + // assert + assertThat(defaultPolicies).hasSize(1); + List permissionList = defaultPolicies.get(0).getPermissions(); + assertThat(permissionList).hasSize(1); + } + private Policy createPolicy(final String policyId) { return new Policy(policyId, OffsetDateTime.now(clock), OffsetDateTime.now(clock).plusDays(1), emptyList()); }