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 74bae5be3e..db5744402b 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 @@ -131,8 +131,6 @@ public List readAll(final String bpn) { public Map> readAll() { try { - - return policyStorePersistence.getAllBlobs().entrySet().stream().map(entry -> { try { return new AbstractMap.SimpleEntry<>(entry.getKey(), diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java index 66f1edf631..58b7713076 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java @@ -33,6 +33,7 @@ import java.time.OffsetDateTime; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import jakarta.json.Json; @@ -143,6 +144,21 @@ void getPolicies() { policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList())); } + @Test + void getAllPolicies() { + // arrange + final List policies = List.of( + new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions())); + when(service.getAllStoredPolicies()).thenReturn(Map.of("bpn", policies)); + + // act + final Map> returnedPolicies = testee.getAllPolicies(); + + // assert + assertThat(returnedPolicies.get("bpn")).isEqualTo( + policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList())); + } + @Test void deleteAllowedPolicy() { // act 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 7264c7b3f6..8e5945cb3e 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 @@ -36,6 +36,7 @@ import java.time.OffsetDateTime; import java.util.List; +import java.util.Map; import java.util.Optional; import com.fasterxml.jackson.core.JsonProcessingException; @@ -79,6 +80,18 @@ void save() throws BlobPersistenceException { verify(mockPersistence).putBlob(anyString(), any()); } + @Test + void saveWithoutBpn() throws BlobPersistenceException { + // arrange + final var policy = new Policy("test", OffsetDateTime.now(), OffsetDateTime.now(), emptyList()); + + // act + testee.save(null, policy); + + // assert + verify(mockPersistence).putBlob(anyString(), any()); + } + @Test void saveDuplicate() throws BlobPersistenceException, JsonProcessingException { // arrange @@ -151,6 +164,20 @@ void readAll() throws BlobPersistenceException, JsonProcessingException { assertThat(readPolicies).hasSize(1); } + @Test + void whenReadAllShouldReturnCorrect() throws BlobPersistenceException, JsonProcessingException { + // arrange + final var policy = new Policy("test", OffsetDateTime.now(), OffsetDateTime.now(), emptyList()); + final var policies = List.of(policy); + when(mockPersistence.getAllBlobs()).thenReturn(Map.of("bpn1", mapper.writeValueAsBytes(policies))); + + // act + final var readPolicies = testee.readAll(); + + // assert + assertThat(readPolicies).hasSize(1); + } + @Test void readAllWithError() throws BlobPersistenceException, JsonProcessingException { // arrange 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 03edeeed12..4741fb3b4e 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 @@ -47,6 +47,7 @@ import org.eclipse.tractusx.irs.edc.client.policy.PolicyType; import org.eclipse.tractusx.irs.policystore.config.DefaultAcceptedPoliciesConfig; import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException; +import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest; import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -223,4 +224,19 @@ void whenRegisterPolicyWithMissingConstraintShouldThrowException() { assertThrows(ResponseStatusException.class, () -> testee.registerPolicy(policy, null)); } + + @Test + void whenUpdate() { + // arrange + final OffsetDateTime now = OffsetDateTime.now(clock); + when(persistence.readAll()).thenReturn(Map.of("bpn2", List.of(new Policy("testId", now, now.plusMinutes(1), emptyList())))); + when(persistence.readAll(any())).thenReturn(List.of(new Policy("testId", now, now.plusMinutes(1), emptyList()))); + + // act + testee.updatePolicy("testId", new UpdatePolicyRequest(OffsetDateTime.now(), List.of("bpn1"), List.of("policyId"))); + + // assert + verify(persistence).delete("bpn2", "testId"); + verify(persistence).save(eq(List.of("bpn1")), any()); + } } \ No newline at end of file