Skip to content

Commit

Permalink
feat(policy-api): [eclipse-tractusx#750] move logic to service, add t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
dsmf committed Jul 15, 2024
1 parent 8f06f29 commit 094b7b0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public CreatePoliciesResponse registerAllowedPolicy(@Valid @RequestBody final Cr
return CreatePoliciesResponse.fromPolicy(registeredPolicy);
}


@Operation(operationId = "getPolicyById", summary = "Gets policy by ID.",
security = @SecurityRequirement(name = API_KEY), tags = { POLICY_API_TAG },
description = "Gets policy by ID.")
Expand Down Expand Up @@ -192,24 +191,13 @@ public CreatePoliciesResponse registerAllowedPolicy(@Valid @RequestBody final Cr
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')")
public GetPolicyByIdResponse getPolicyById(//
@PathVariable(required = true) //
@PathVariable //
@ValidPolicyId //
@Parameter(description = "Policy ID.") //
final String policyId //
) {

final Map<String, List<Policy>> policies = service.getPolicies(null);

// TODO (mfischer): #750: update insomnia
// TODO (mfischer): #750: add test
return policyPagingService.getPolicyWithBpnStream(policies)
.filter(p -> p.policy().getPolicyId().equals(policyId))
// In MinIO we store the policy per BPN.
// Therefore, we can simply take the first one and collect the keys to get all the
// business partner numbers to which the policy is assigned.
.findFirst()
.map(p -> GetPolicyByIdResponse.from(p.policy(), policies.keySet().stream().toList()))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Policy not found"));
return service.getPolicyById(policyId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Policy not found"));
}

@Operation(operationId = "getAllowedPoliciesByBpn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.tractusx.irs.policystore.config.DefaultAcceptedPoliciesConfig;
import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException;
import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.models.GetPolicyByIdResponse;
import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence;
import org.eclipse.tractusx.irs.policystore.validators.PolicyValidator;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class PolicyStoreService implements AcceptedPoliciesProvider {

private static final String DEFAULT = "default";


/**
* Constants for the configured default policy.
*/
Expand Down Expand Up @@ -137,6 +139,24 @@ public Policy registerPolicy(final CreatePolicyRequest request) {
}
}

/**
* Find a policy by policy ID.
* @param policyId the policy ID
* @return Returns the policy including a list of associated business partner numbers.
*/
public Optional<GetPolicyByIdResponse> getPolicyById(final String policyId) {
final Map<String, List<Policy>> policies = getPolicies(null);
return policies.values()
.stream()
.flatMap(Collection::stream)
.filter(p -> p.getPolicyId().equals(policyId))
// In MinIO we store the policy per BPN.
// Therefore, we can simply take the first one and collect the keys to get all the
// business partner numbers to which the policy is assigned.
.findFirst()
.map(p -> GetPolicyByIdResponse.from(p, policies.keySet().stream().toList()));
}

/**
* Finds policies by list of BPN.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand All @@ -59,6 +60,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.server.ResponseStatusException;

@ExtendWith(MockitoExtension.class)
public class PolicyStoreControllerTest {
Expand Down Expand Up @@ -153,6 +155,14 @@ void pageSizeTooLow() {
@Nested
class GetPoliciesTests {

@Test
void getPolicyById() {
when(policyStoreServiceMock.getPolicyById("policyThatDoesNotExist")).thenReturn(Optional.empty());

assertThatThrownBy(() -> testee.getPolicyById("policyThatDoesNotExist")).isInstanceOf(
ResponseStatusException.class);
}

@Test
void getPolicies() {
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import jakarta.json.JsonObject;
import lombok.val;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.ThrowableAssert;
import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl;
Expand All @@ -58,6 +60,7 @@
import org.eclipse.tractusx.irs.policystore.controllers.PolicyStoreControllerTest;
import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException;
import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.models.GetPolicyByIdResponse;
import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence;
import org.eclipse.tractusx.irs.policystore.testutil.PolicyStoreTestUtil;
Expand Down Expand Up @@ -306,6 +309,49 @@ void getStoredPolicies_whenNoPoliciesForBpn_shouldReturnTheConfiguredDefaultPoli
}
}

@Nested
class GetPolicyByIdTests {

@Test
void getPolicyById() {
final Map<String, List<Policy>> policies = Map.of( //
"BPNL1234567890CD", List.of( //
Policy.builder()
.policyId("policy1")
.createdOn(OffsetDateTime.now())
.validUntil(OffsetDateTime.now())
.permissions(createPermissions())
.build()), //
"BPNL1234567890EF", List.of( //
Policy.builder()
.policyId("policy1")
.createdOn(OffsetDateTime.now())
.validUntil(OffsetDateTime.now())
.permissions(createPermissions())
.build(), //
Policy.builder()
.policyId("policy2")
.createdOn(OffsetDateTime.now())
.validUntil(OffsetDateTime.now())
.permissions(createPermissions())
.build()));

when(persistenceMock.readAll()).thenReturn(policies);

final Optional<GetPolicyByIdResponse> optResult = testee.getPolicyById("policy1");
assertThat(optResult).isPresent();
final GetPolicyByIdResponse result = optResult.get();
assertThat(result.bpn()).containsExactlyInAnyOrder("BPNL1234567890CD", "BPNL1234567890EF");
assertThat(result.payload().policyId()).isEqualTo("policy1");
}

@Test
void notFound() {
assertThat( testee.getPolicyById("policyThatDoesNotExist")).isNotPresent();
}

}

@Nested
class GetAcceptedPoliciesTests {

Expand Down

0 comments on commit 094b7b0

Please sign in to comment.