Skip to content

Commit

Permalink
fix(impl): [#533] cleanup empty BPNL
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed May 11, 2024
1 parent a4fcfb8 commit 002e44b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,7 +97,11 @@ public void delete(final String bpn, final String policyId) {
private void save(final String bpn, final List<Policy> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down

0 comments on commit 002e44b

Please sign in to comment.