From 002e44b129db395db6167ab020cd08f3a78f85a9 Mon Sep 17 00:00:00 2001 From: Matthias Fischer Date: Sat, 11 May 2024 03:27:40 +0200 Subject: [PATCH] fix(impl): [#533] cleanup empty BPNL --- .../persistence/PolicyPersistence.java | 7 +++++- .../persistence/PolicyPersistenceTest.java | 23 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java index c240987eed..610fdc3d9f 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.util.AbstractMap; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -96,7 +97,11 @@ public void delete(final String bpn, final String policyId) { private void save(final String bpn, final List modifiedPolicies) { writeLock(() -> { try { - policyStorePersistence.putBlob(bpn, mapper.writeValueAsBytes(modifiedPolicies)); + if (modifiedPolicies.isEmpty()) { + policyStorePersistence.delete(bpn, Collections.emptyList()); + } else { + policyStorePersistence.putBlob(bpn, mapper.writeValueAsBytes(modifiedPolicies)); + } } catch (BlobPersistenceException | JsonProcessingException e) { throw new PolicyStoreException("Unable to store policy data", e); } diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistenceTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistenceTest.java index ee193a9ea2..f183ee3f52 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistenceTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistenceTest.java @@ -135,10 +135,12 @@ class DeletePolicyTests { @Test void delete_success() throws BlobPersistenceException, JsonProcessingException { // ARRANGE - final String policyId = "test"; + final String policyId = "policy1"; + final String policyId2 = "policy2"; final var policy = new Policy(policyId, OffsetDateTime.now(), OffsetDateTime.now(), emptyList()); - final var policies = List.of(policy); - when(mockPersistence.getBlob(anyString())).thenReturn(Optional.of(mapper.writeValueAsBytes(policies))); + final var policy2 = new Policy(policyId2, OffsetDateTime.now(), OffsetDateTime.now(), emptyList()); + when(mockPersistence.getBlob(anyString())).thenReturn( + Optional.of(mapper.writeValueAsBytes(List.of(policy, policy2)))); // ACT testee.delete("testBpn", policyId); @@ -147,6 +149,21 @@ void delete_success() throws BlobPersistenceException, JsonProcessingException { verify(mockPersistence).putBlob(anyString(), any()); } + @Test + void delete_whenNoOtherPolicyLeftForBpn() throws BlobPersistenceException, JsonProcessingException { + // ARRANGE + final String policyId = "policy1"; + final var policy = new Policy(policyId, OffsetDateTime.now(), OffsetDateTime.now(), emptyList()); + when(mockPersistence.getBlob(anyString())).thenReturn( + Optional.of(mapper.writeValueAsBytes(List.of(policy)))); + + // ACT + testee.delete("testBpn", policyId); + + // ASSERT + verify(mockPersistence).delete(anyString(), any()); + } + @Test void delete_shouldThrowExceptionIfPolicyWithIdDoesntExists() throws BlobPersistenceException, JsonProcessingException {