Skip to content

Commit

Permalink
feat(irs-api):[#199] added test
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-psosnowski committed Mar 12, 2024
1 parent c65f58e commit f9a886b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ public List<Policy> readAll(final String bpn) {

public Map<String, List<Policy>> readAll() {
try {


return policyStorePersistence.getAllBlobs().entrySet().stream().map(entry -> {
try {
return new AbstractMap.SimpleEntry<>(entry.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,6 +144,21 @@ void getPolicies() {
policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList()));
}

@Test
void getAllPolicies() {
// arrange
final List<Policy> policies = List.of(
new Policy("testId", OffsetDateTime.now(), OffsetDateTime.now(), createPermissions()));
when(service.getAllStoredPolicies()).thenReturn(Map.of("bpn", policies));

// act
final Map<String, List<PolicyResponse>> returnedPolicies = testee.getAllPolicies();

// assert
assertThat(returnedPolicies.get("bpn")).isEqualTo(
policies.stream().map(PolicyResponse::fromPolicy).collect(Collectors.toList()));
}

@Test
void deleteAllowedPolicy() {
// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f9a886b

Please sign in to comment.