Skip to content

Commit

Permalink
feat(irs-policy-store): [TRI-1593] Policy Store API Extension - add d…
Browse files Browse the repository at this point in the history
…efault values for policy creation
  • Loading branch information
ds-ext-abugajewski committed Sep 21, 2023
1 parent c96e040 commit 3382989
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Permission> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Permission> 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());
}
Expand Down

0 comments on commit 3382989

Please sign in to comment.