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 064de544d..e9277ccf5 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 @@ -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; @@ -177,12 +178,16 @@ public List getStoredPolicies(final List bpnls) { public Map> getAllStoredPolicies() { final Map> 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> 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 bpnsContainingPolicyId = PolicyHelper.findBpnsByPolicyId(getAllStoredPolicies(), policyId); 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 ff40670cf..9d473c9e0 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 @@ -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; @@ -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; @@ -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); @@ -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); @@ -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 @@ -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( @@ -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)); @@ -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 @@ -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)