Skip to content

Commit

Permalink
Add config-value to set comparators to lower case in client
Browse files Browse the repository at this point in the history
fixes #355
  • Loading branch information
Captain-P-Goldfish committed Nov 2, 2022
1 parent 3e2bb87 commit 6ebef57
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public class ScimClientConfig
*/
private ConfigManipulator configManipulator;

/**
* if the filter-expression-comparators should be sent in lowercase instead of uppercase e.g.: eq instead of
* EQ.
*/
private boolean useLowerCaseInFilterComparators;

@Builder
public ScimClientConfig(Integer requestTimeout,
Integer socketTimeout,
Expand All @@ -109,7 +115,8 @@ public ScimClientConfig(Integer requestTimeout,
Map<String, String> httpHeaders,
Map<String, String[]> httpMultiHeaders,
BasicAuth basicAuth,
ConfigManipulator configManipulator)
ConfigManipulator configManipulator,
boolean useLowerCaseInFilterComparators)
{
this.requestTimeout = requestTimeout == null ? DEFAULT_TIMEOUT : requestTimeout;
this.socketTimeout = socketTimeout == null ? DEFAULT_TIMEOUT : socketTimeout;
Expand All @@ -123,6 +130,7 @@ public ScimClientConfig(Integer requestTimeout,
setHeaders(httpHeaders, httpMultiHeaders);
this.basicAuth = basicAuth;
this.configManipulator = configManipulator;
this.useLowerCaseInFilterComparators = useLowerCaseInFilterComparators;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;

import de.captaingoldfish.scim.sdk.client.ScimClientConfig;
import de.captaingoldfish.scim.sdk.client.http.HttpResponse;
import de.captaingoldfish.scim.sdk.client.http.ScimHttpClient;
import de.captaingoldfish.scim.sdk.client.response.ServerResponse;
Expand Down Expand Up @@ -891,7 +892,10 @@ public FilterBuilder<T> or(boolean openParenthesis,
*/
private void setExpression(String attributeName, Comparator comparator, Object value)
{
filterString.append(attributeName).append(" ").append(comparator.name());
ScimClientConfig scimClientConfig = scimHttpClient.getScimClientConfig();
String comparatorString = scimClientConfig.isUseLowerCaseInFilterComparators() ? comparator.name().toLowerCase()
: comparator.name();
filterString.append(attributeName).append(" ").append(comparatorString);
if (value instanceof String)
{
filterString.append(value == null ? "" : " ").append("\"").append(value == null ? "" : value).append("\"");
Expand All @@ -911,7 +915,7 @@ public ListBuilder<T> build()
{
throw new IllegalStateException("error within filter expression\n\topened parentheses: " + openedParenthesis
+ "\n\tclosed parentheses: " + closedParenthesis + "\n\tfilter: "
+ filterString.toString());
+ filterString);
}
listBuilder.requestParameters.put(AttributeNames.RFC7643.FILTER, filterString.toString());
return listBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,57 @@ public void testBuildFilterWithErroneousOpenedParenthesis()
}
}

/**
* verifies that filter comparators are written in lower case if the {@link ScimClientConfig} has the property
* {@link ScimClientConfig#useLowerCaseInFilterComparators} set to true
*/
@Test
public void testFilterComparatorsAreWrittenInLowerCase()
{
ScimClientConfig scimClientConfig = ScimClientConfig.builder().useLowerCaseInFilterComparators(true).build();
ScimHttpClient scimHttpClient = new ScimHttpClient(scimClientConfig);
ListBuilder<User> listBuilder = new ListBuilder<>(getServerUrl(), EndpointPaths.USERS, User.class, scimHttpClient);
listBuilder.filter("username", Comparator.SW, "hello").and("displayName", Comparator.EW, "world").build();

AtomicBoolean wasCalled = new AtomicBoolean();
setVerifyRequestAttributes((httpExchange, requestBody) -> {
Map<String, String> requestParams = RequestUtils.getQueryParameters(httpExchange.getRequestURI().getQuery());

String filterExpression = requestParams.get(AttributeNames.RFC7643.FILTER);
Assertions.assertEquals("username sw \"hello\" and displayName ew \"world\"", filterExpression);
wasCalled.set(true);
});

ServerResponse<ListResponse<User>> response = listBuilder.get().sendRequest();
Assertions.assertEquals(HttpStatus.OK, response.getHttpStatus());
Assertions.assertTrue(wasCalled.get());
}

/**
* verifies that filter comparators are written in upper case if no configurations are adjusted.
*/
@Test
public void testFilterComparatorsAreWrittenInUpperCase()
{
ScimClientConfig scimClientConfig = new ScimClientConfig();
ScimHttpClient scimHttpClient = new ScimHttpClient(scimClientConfig);
ListBuilder<User> listBuilder = new ListBuilder<>(getServerUrl(), EndpointPaths.USERS, User.class, scimHttpClient);
listBuilder.filter("username", Comparator.SW, "hello").and("displayName", Comparator.EW, "world").build();

AtomicBoolean wasCalled = new AtomicBoolean();
setVerifyRequestAttributes((httpExchange, requestBody) -> {
Map<String, String> requestParams = RequestUtils.getQueryParameters(httpExchange.getRequestURI().getQuery());

String filterExpression = requestParams.get(AttributeNames.RFC7643.FILTER);
Assertions.assertEquals("username SW \"hello\" and displayName EW \"world\"", filterExpression);
wasCalled.set(true);
});

ServerResponse<ListResponse<User>> response = listBuilder.get().sendRequest();
Assertions.assertEquals(HttpStatus.OK, response.getHttpStatus());
Assertions.assertTrue(wasCalled.get());
}

private void parseFilterWithAntlr(String filter)
{
FilterRuleErrorListener filterRuleErrorListener = new FilterRuleErrorListener();
Expand Down

0 comments on commit 6ebef57

Please sign in to comment.