Skip to content

Commit

Permalink
feat(policy-api):[#639] harmonized with old endpoint, simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jul 3, 2024
1 parent 439ead5 commit 3f207f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ public Map<String, List<PolicyResponse>> getPolicies(//
) {

final Map<String, String[]> parameterMap = this.httpServletRequest.getParameterMap();
if (CollectionUtils.containsAny(parameterMap.keySet(), List.of("bpn", "bpns", "bpnls"))) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Please use parameter 'businessPartnerNumbers' instead");
}

ensureParamBusinessPartnerNumberCorrectlyNamed(parameterMap);

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

Expand All @@ -221,30 +219,40 @@ public Page<PolicyResponse> getPoliciesPaged(//
@RequestParam(required = false) //
@ValidListOfBusinessPartnerNumbers //
@Parameter(description = "List of business partner numbers.") //
final List<String> businessPartnerNumbers, //
// There seems to be a bug concerning interpretation of delimiters in Spring.
// If we pass only one search filter `?search=policyId,EQUALS,policy1`,
// the parts of this search filter are split up into several search filters even if we encode the comma.
// It only works if multiple search filters are passed `?search=policyId,EQUALS,policy1&search=...`.
// The solution described on stackoverflow
// (https://stackoverflow.com/questions/37058691/encoded-comma-in-url-is-read-as-list-in-spring)
// using the annotation Delimiter did not work either.
// Therefore, we read the search parameters from the request manually.
final HttpServletRequest request) {
final List<String> businessPartnerNumbers) {

if (pageable.getPageSize() > MAX_PAGE_SIZE) {
throw new IllegalArgumentException("Page size too large");
}

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

ensureParamBusinessPartnerNumberCorrectlyNamed(parameterMap);

// TODO (mfischer): #639: add test coverage for this block
final List<String> searchParameters = Arrays.asList(request.getParameterMap().get("search"));
// There seems to be a bug concerning interpretation of delimiters in Spring.
// If we pass only one search filter `?search=policyId,EQUALS,policy1`,
// the parts of this search filter are split up into several search filters even if we encode the comma.
// It only works if multiple search filters are passed `?search=policyId,EQUALS,policy1&search=...`.
// The solution described on stackoverflow
// (https://stackoverflow.com/questions/37058691/encoded-comma-in-url-is-read-as-list-in-spring)
// using the annotation Delimiter did not work either.
// Therefore, we read the search parameters from the request manually.
final List<String> searchParameters = Arrays.asList(parameterMap.get("search"));
final List<SearchCriteria<?>> searchCriteria = new SearchParameterParser(searchParameters).getSearchCriteria();
final Map<String, List<Policy>> bpnToPoliciesMap = service.getPolicies(businessPartnerNumbers);
final Page<PolicyWithBpn> policies = policyPagingService.getPolicies(bpnToPoliciesMap, pageable,
searchCriteria);
return policies.map(PolicyResponse::fromPolicyWithBpn);
}

private static void ensureParamBusinessPartnerNumberCorrectlyNamed(final Map<String, String[]> parameterMap) {
if (CollectionUtils.containsAny(parameterMap.keySet(), List.of("bpn", "bpns", "bpnls"))) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Please use parameter 'businessPartnerNumbers' instead");
}
}

@Operation(operationId = "deleteAllowedPolicy",
summary = "Removes a policy that should no longer be accepted in EDC negotiation.",
security = @SecurityRequirement(name = "api_key"), tags = { "Item Relationship Service" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private Predicate<PolicyWithBpn> getActionFilter(final SearchCriteria<?> searchC
if (permissions == null || permissions.isEmpty()) {
return false;
}
// TODO (mfischer) #639: clarify which to use
// TODO (mfischer) #639: clarify which to use, add test
// option 1: filter on first action
//return permissions.get(0).getAction().getValue().equalsIgnoreCase((String) searchCriteria.getValue());
// option 2: filter on all actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ class GetPoliciesPagedTests {
@Test
void pageSizeTooLarge() {
assertThatThrownBy(() -> testee.getPoliciesPaged(PageRequest.of(0, PolicyStoreController.MAX_PAGE_SIZE + 1),
Collections.emptyList(), mock(HttpServletRequest.class))).isInstanceOf(
IllegalArgumentException.class).hasMessageContaining("Page size too large");
Collections.emptyList())).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Page size too large");
}

@Test
void pageSizeTooLow() {
assertThatThrownBy(() -> testee.getPoliciesPaged(PageRequest.of(0, 0), Collections.emptyList(),
mock(HttpServletRequest.class))).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Page size must not be less than one");
assertThatThrownBy(
() -> testee.getPoliciesPaged(PageRequest.of(0, 0), Collections.emptyList())).isInstanceOf(
IllegalArgumentException.class).hasMessageContaining("Page size must not be less than one");
}
}

Expand Down

0 comments on commit 3f207f5

Please sign in to comment.