Skip to content

Commit

Permalink
Merge pull request #75 from neshanjo/removed-unsupported-enconding-ex…
Browse files Browse the repository at this point in the history
…ception

removed unmotivated throwing of UnsupportedEncodingException
  • Loading branch information
lbalmaceda authored Sep 8, 2017
2 parents 8e4e563 + 1269728 commit faf4adf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.auth0.client.mgmt.filter;

import java.io.UnsupportedEncodingException;

/**
* Class used to filter the results received when calling the Logs endpoint. Related to the {@link com.auth0.client.mgmt.LogEventsEntity} entity.
*/
Expand All @@ -27,7 +25,7 @@ public LogEventFilter withTotals(boolean includeTotals) {
}

@Override
public LogEventFilter withQuery(String query) throws UnsupportedEncodingException {
public LogEventFilter withQuery(String query) {
super.withQuery(query);
return this;
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public class QueryFilter extends FieldsFilter {
* @param query the query expression using the following syntax https://auth0.com/docs/api/management/v2/query-string-syntax.
* @return this filter instance
*/
public QueryFilter withQuery(String query) throws UnsupportedEncodingException {
String encodedQuery = URLEncoder.encode(query, "UTF-8");
parameters.put(KEY_QUERY, encodedQuery);
return this;
public QueryFilter withQuery(String query) {
try {
String encodedQuery = urlEncode(query);
parameters.put(KEY_QUERY, encodedQuery);
return this;
} catch (UnsupportedEncodingException ex) {
//"Every implementation of the Java platform is required to support the following standard charsets [...]: UTF-8"
//cf. https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html
throw new IllegalStateException("UTF-8 encoding not supported by current Java platform implementation.", ex);
}
}

/**
Expand Down Expand Up @@ -59,5 +65,10 @@ public QueryFilter withFields(String fields, boolean includeFields) {
super.withFields(fields, includeFields);
return this;
}

//Visible for testing
String urlEncode(String query) throws UnsupportedEncodingException {
return URLEncoder.encode(query, "UTF-8");
}

}
4 changes: 1 addition & 3 deletions src/main/java/com/auth0/client/mgmt/filter/UserFilter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.auth0.client.mgmt.filter;

import java.io.UnsupportedEncodingException;

/**
* Class used to filter the results received when calling the Users endpoint. Related to the {@link com.auth0.client.mgmt.UsersEntity} entity.
*/
Expand All @@ -21,7 +19,7 @@ public UserFilter withTotals(boolean includeTotals) {
}

@Override
public UserFilter withQuery(String query) throws UnsupportedEncodingException {
public UserFilter withQuery(String query) {
super.withQuery(query);
return this;
}
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/auth0/client/mgmt/filter/QueryFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.isA;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;

import java.io.UnsupportedEncodingException;

import org.junit.Rule;
import org.junit.rules.ExpectedException;

public class QueryFilterTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private QueryFilter filter;

@Before
Expand Down Expand Up @@ -73,4 +84,16 @@ public void shouldFilterWithoutFields() throws Exception {
assertThat(filter.getAsMap(), Matchers.hasEntry("fields", (Object) "a,b,c"));
assertThat(filter.getAsMap(), Matchers.hasEntry("include_fields", (Object) false));
}
}

@Test
public void shouldThrowIllegalStateExceptionWhenUtf8IsNotSupported() throws Exception {
exception.expect(IllegalStateException.class);
exception.expectMessage("UTF-8 encoding not supported by current Java platform implementation.");
exception.expectCause(isA(UnsupportedEncodingException.class));
String value = "my test value";
QueryFilter filter = spy(new QueryFilter());
doThrow(UnsupportedEncodingException.class).when(filter).urlEncode(value);
filter.withQuery(value);
}

}

0 comments on commit faf4adf

Please sign in to comment.