Skip to content

Commit

Permalink
feat(policy-api): [eclipse-tractusx#803] fix list of associated BPN
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jul 17, 2024
1 parent 3e27bc2 commit 68a5eab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.time.OffsetDateTime;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -56,6 +55,7 @@
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.PolicyWithBpn;
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 @@ -146,12 +146,31 @@ public Policy registerPolicy(final CreatePolicyRequest request) {
* @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()));
final Map<String, List<Policy>> policiesMap = getPolicies(null);
final List<PolicyWithBpn> policiesWithBpn = getPolicyWithBpnStream(policiesMap);
final List<PolicyWithBpn> matchingPolicies = policiesWithBpn.stream()
.filter(p -> p.policy() != null)
.filter(p -> p.policy()
.getPolicyId()
.equals(policyId))
.toList();
final List<String> associatedBusinessPartnerNumbers = matchingPolicies.stream()
.map(PolicyWithBpn::bpn)
.toList();
return matchingPolicies.stream()
// In MinIO we store the policy per BPN.
.findFirst() //
.map(p -> GetPolicyByIdResponse.from(p.policy(), associatedBusinessPartnerNumbers));
}

private List<PolicyWithBpn> getPolicyWithBpnStream(final Map<String, List<Policy>> bpnToPoliciesMap) {
return bpnToPoliciesMap.entrySet()
.stream()
.flatMap(bpnWithPolicies -> bpnWithPolicies.getValue()
.stream()
.map(policy -> new PolicyWithBpn(
bpnWithPolicies.getKey(), policy)))
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,35 @@ 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()));
final Map<String, List<Policy>> policies = new HashMap<>();

policies.put("BPNL1234567890CD", List.of( //
Policy.builder()
.policyId("policy1")
.createdOn(OffsetDateTime.now())
.validUntil(OffsetDateTime.now())
.permissions(createPermissions())
.build()));
policies.put("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()));
policies.put("BPNL1234567890GH", List.of( //
Policy.builder()
.policyId("policy3")
.createdOn(OffsetDateTime.now())
.validUntil(OffsetDateTime.now())
.permissions(createPermissions())
.build()));

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

Expand Down

0 comments on commit 68a5eab

Please sign in to comment.