Skip to content

Commit

Permalink
feat(impl):[eclipse-tractusx#750] get policy by id
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jul 9, 2024
1 parent 2a2ae02 commit 8f0cb4d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,52 @@ 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.")
@ApiResponses(
value = { @ApiResponse(responseCode = "200", description = "Returns the policies as list of policies.",
content = { @Content(mediaType = APPLICATION_JSON_VALUE,
// TODO examples = @ExampleObject(PolicyResponse.BPN_TO_POLICY_MAP_EXAMPLE),
schema = @Schema(description = "List of policies"))
}),
@ApiResponse(responseCode = "401", description = UNAUTHORIZED_DESC,
content = { @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponse.class),
examples = @ExampleObject(name = "error",
ref = "#/components/examples/error-response-401"))
}),
@ApiResponse(responseCode = "403", description = FORBIDDEN_DESC,
content = { @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponse.class),
examples = @ExampleObject(name = "error",
ref = "#/components/examples/error-response-403"))
}),
})
@GetMapping("/policies/{policyId}")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')")
public List<PolicyResponse> getPolicyById(//
@PathVariable(required = true) //
@ValidPolicyId //
@Parameter(description = "Policy ID.") //
final String policyId //
) {

final Map<String, String[]> parameterMap = this.httpServletRequest.getParameterMap();

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

// TODO (mfischer): #750: return only one policy and List of BPN as attribute?!
// TODO (mfischer): #750: update insomnia
// TODO (mfischer): #750: update swagger
// TODO (mfischer): #750: add test
return policyPagingService.getPolicyWithBpnStream(policies)
.filter(p -> p.policy().getPolicyId().equals(policyId))
.map(p -> PolicyResponse.from(p.policy(), p.bpn()))
.toList();
}

@Operation(operationId = "getAllowedPoliciesByBpn",
summary = "Lists the registered policies that should be accepted in EDC negotiation.",
security = @SecurityRequirement(name = API_KEY), tags = { POLICY_API_TAG },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -67,20 +68,22 @@ public Page<PolicyWithBpn> getPolicies(final Map<String, List<Policy>> bpnToPoli
final Comparator<PolicyWithBpn> comparator = new PolicyComparatorBuilder(pageable).build();
final Predicate<PolicyWithBpn> filter = new PolicyFilterBuilder(searchCriteria).build();

final List<PolicyWithBpn> policies = bpnToPoliciesMap.entrySet()
.stream()
.flatMap(bpnWithPolicies -> bpnWithPolicies.getValue()
.stream()
.map(policy -> new PolicyWithBpn(
bpnWithPolicies.getKey(),
policy)))
.filter(filter)
.sorted(comparator)
.toList();
final List<PolicyWithBpn> policies = getPolicyWithBpnStream(bpnToPoliciesMap).filter(filter)
.sorted(comparator)
.toList();

return applyPaging(pageable, policies);
}

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

private PageImpl<PolicyWithBpn> applyPaging(final Pageable pageable, final List<PolicyWithBpn> policies) {
final int start = Math.min(pageable.getPageNumber() * pageable.getPageSize(), policies.size());
final int end = Math.min((pageable.getPageNumber() + 1) * pageable.getPageSize(), policies.size());
Expand Down

0 comments on commit 8f0cb4d

Please sign in to comment.