Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/533 cleanup bpnl without policies #615

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ _**For better traceability add the corresponding GitHub issue number in each cha

## [Unreleased]

### Fixed

- Cleaning up BPNLs without policies. #533


## [5.1.1] - 2024-05-08

### Fixed
Expand Down
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
Loading