Skip to content

Commit

Permalink
fix(policy-api):[eclipse-tractusx#734] return configured default poli…
Browse files Browse the repository at this point in the history
…cies if no custom default policy is defined
  • Loading branch information
dsmf committed Jul 11, 2024
1 parent da3bc67 commit 8c523e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import jakarta.json.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPoliciesProvider;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPolicy;
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
Expand Down Expand Up @@ -177,12 +178,16 @@ public List<Policy> getStoredPolicies(final List<String> bpnls) {

public Map<String, List<Policy>> getAllStoredPolicies() {
final Map<String, List<Policy>> bpnToPolicies = persistence.readAll();
if (bpnToPolicies.isEmpty()) {
return Map.of("", allowedPoliciesFromConfig);
if (containsNoDefaultPolicyAvailable(bpnToPolicies)) {
bpnToPolicies.put("default", allowedPoliciesFromConfig);
}
return bpnToPolicies;
}

private static boolean containsNoDefaultPolicyAvailable(final Map<String, List<Policy>> bpnToPolicies) {
return bpnToPolicies.keySet().stream().noneMatch(key -> StringUtils.isEmpty(key) || DEFAULT.equals(key));
}

public void deletePolicy(final String policyId) {
log.info("Getting all policies to find correct BPN");
final List<String> bpnsContainingPolicyId = PolicyHelper.findBpnsByPolicyId(getAllStoredPolicies(), policyId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eclipse.tractusx.irs.policystore.services;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -37,6 +36,7 @@
import java.time.Clock;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -313,7 +313,7 @@ class GetAcceptedPoliciesTests {
void getAcceptedPolicies_whenParameterBpnIsNull_shouldReturnTheConfiguredDefaultPolicy() {

// ARRANGE
when(persistenceMock.readAll()).thenReturn(emptyMap());
when(persistenceMock.readAll()).thenReturn(new HashMap<>());

// ACT
final var acceptedPolicies = testee.getAcceptedPolicies(null);
Expand Down Expand Up @@ -389,12 +389,12 @@ void deletePolicyForEachBpn_success() {
void deletePolicy_deleteSuccessful() {
// ARRANGE
final String policyId = randomPolicyId();
when(persistenceMock.readAll()).thenReturn(Map.of(BPN, List.of(Policy.builder()
.policyId(policyId)
.createdOn(null)
.validUntil(null)
.permissions(null)
.build())));
when(persistenceMock.readAll()).thenReturn(new HashMap<>(Map.of(BPN, List.of(Policy.builder()
.policyId(policyId)
.createdOn(null)
.validUntil(null)
.permissions(null)
.build()))));

// ACT
testee.deletePolicy(policyId);
Expand All @@ -408,12 +408,12 @@ void deletePolicy_exceptionFromPolicyPersistence_shouldReturnHttpStatus500() {

// ACT
final String policyId = randomPolicyId();
when(persistenceMock.readAll()).thenReturn(Map.of(BPN, List.of(Policy.builder()
.policyId(policyId)
.createdOn(null)
.validUntil(null)
.permissions(null)
.build())));
when(persistenceMock.readAll()).thenReturn(new HashMap<>(Map.of(BPN, List.of(Policy.builder()
.policyId(policyId)
.createdOn(null)
.validUntil(null)
.permissions(null)
.build()))));
doThrow(new PolicyStoreException("")).when(persistenceMock).delete(BPN, policyId);

// ASSERT
Expand All @@ -428,7 +428,7 @@ void deletePolicy_whenPolicyNotFound_shouldReturnHttpStatus404() {
final String notExistingPolicyId = randomPolicyId();
final String policyId = randomPolicyId();
when(persistenceMock.readAll()).thenReturn(
Map.of(BPN, List.of(Policy.builder().policyId(policyId).build())));
new HashMap<>(Map.of(BPN, List.of(Policy.builder().policyId(policyId).build()))));

// ASSERT
assertThatThrownBy(() -> testee.deletePolicy(notExistingPolicyId)).isInstanceOf(
Expand Down Expand Up @@ -463,7 +463,7 @@ void updatePolicies_shouldUpdateBpnAndValidUntil() {
.validUntil(originalValidUntil)
.permissions(permissions)
.build();
when(persistenceMock.readAll()).thenReturn(Map.of(originalBpn, List.of(testPolicy)));
when(persistenceMock.readAll()).thenReturn(new HashMap<>(Map.of(originalBpn, List.of(testPolicy))));
// get policies for bpn
when(persistenceMock.readAll(originalBpn)).thenReturn(List.of(testPolicy));

Expand Down Expand Up @@ -498,7 +498,7 @@ void updatePolicies_shouldAddPolicyToEachBpn() {
.validUntil(validUntil)
.permissions(permissions)
.build();
when(persistenceMock.readAll()).thenReturn(Map.of("bpn2", List.of(testPolicy)));
when(persistenceMock.readAll()).thenReturn(new HashMap<>(Map.of("bpn2", List.of(testPolicy))));
when(persistenceMock.readAll("bpn2")).thenReturn(List.of(testPolicy));

// ACT
Expand Down Expand Up @@ -547,7 +547,7 @@ void updatePolicies_shouldAssociateEachGivenPolicyWithEachGivenBpn() {
// BPN1 without any policies

// BPN2 with testPolicy1 and testPolicy2
when(persistenceMock.readAll()).thenReturn(Map.of(bpn2, List.of(testPolicy1, testPolicy2)));
when(persistenceMock.readAll()).thenReturn(new HashMap<>(Map.of(bpn2, List.of(testPolicy1, testPolicy2))));
when(persistenceMock.readAll(bpn2)).thenReturn(List.of(Policy.builder()
.policyId(policyId1)
.createdOn(createdOn)
Expand Down

0 comments on commit 8c523e7

Please sign in to comment.