diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java index abb43ba73..c5d1bd553 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreController.java @@ -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 getPolicyById(// + @PathVariable(required = true) // + @ValidPolicyId // + @Parameter(description = "Policy ID.") // + final String policyId // + ) { + + final Map parameterMap = this.httpServletRequest.getParameterMap(); + + final Map> 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 }, diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java index fa7d89a4d..2a9d756bd 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyPagingService.java @@ -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; @@ -67,20 +68,22 @@ public Page getPolicies(final Map> bpnToPoli final Comparator comparator = new PolicyComparatorBuilder(pageable).build(); final Predicate filter = new PolicyFilterBuilder(searchCriteria).build(); - final List policies = bpnToPoliciesMap.entrySet() - .stream() - .flatMap(bpnWithPolicies -> bpnWithPolicies.getValue() - .stream() - .map(policy -> new PolicyWithBpn( - bpnWithPolicies.getKey(), - policy))) - .filter(filter) - .sorted(comparator) - .toList(); + final List policies = getPolicyWithBpnStream(bpnToPoliciesMap).filter(filter) + .sorted(comparator) + .toList(); return applyPaging(pageable, policies); } + public Stream getPolicyWithBpnStream(final Map> bpnToPoliciesMap) { + return bpnToPoliciesMap.entrySet() + .stream() + .flatMap(bpnWithPolicies -> bpnWithPolicies.getValue() + .stream() + .map(policy -> new PolicyWithBpn( + bpnWithPolicies.getKey(), policy))); + } + private PageImpl applyPaging(final Pageable pageable, final List policies) { final int start = Math.min(pageable.getPageNumber() * pageable.getPageSize(), policies.size()); final int end = Math.min((pageable.getPageNumber() + 1) * pageable.getPageSize(), policies.size());