Skip to content

Commit

Permalink
Clean up and Polish of Testing Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
duanemay committed Dec 19, 2024
1 parent 7233c38 commit dc5fda7
Show file tree
Hide file tree
Showing 237 changed files with 2,622 additions and 3,530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
public class JsonUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();

private JsonUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static String writeValueAsString(Object object) throws JsonUtilException {
try {
return objectMapper.writeValueAsString(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class JsonUtilsTest {
@Test
void writeValueAsString() {
String testObjectString = JsonUtils.writeValueAsString(getTestObject());
assertThat(testObjectString).isNotNull();
assertThat(testObjectString).isEqualTo(JSON_TEST_OBJECT_STRING);
}

Expand All @@ -45,13 +44,15 @@ void readValueByteClass() {
void readValueAsMap() {
final String jsonInput = "{\"prop1\":\"abc\",\"prop2\":{\"prop2a\":\"def\",\"prop2b\":\"ghi\"},\"prop3\":[\"jkl\",\"mno\"]}";
final Map<String, Object> map = JsonUtils.readValueAsMap(jsonInput);
assertThat(map).isNotNull();
assertThat(map.get("prop1")).isNotNull().isEqualTo("abc");
assertThat(map.get("prop2")).isNotNull().isInstanceOf(Map.class);
assertThat(((Map<String, Object>) map.get("prop2")).get("prop2a")).isNotNull().isEqualTo("def");
assertThat(((Map<String, Object>) map.get("prop2")).get("prop2b")).isNotNull().isEqualTo("ghi");
assertThat(map.get("prop3")).isNotNull().isInstanceOf(List.class);
assertThat((List<String>) map.get("prop3")).containsExactly("jkl", "mno");
assertThat(map).containsEntry("prop1", "abc")
.containsKey("prop3");
assertThat(((Map<String, Object>) map.get("prop2")))
.isInstanceOf(Map.class)
.containsEntry("prop2a", "def")
.containsEntry("prop2b", "ghi");
assertThat((List<String>) map.get("prop3"))
.isInstanceOf(List.class)
.containsExactly("jkl", "mno");
}

@ParameterizedTest
Expand Down Expand Up @@ -113,4 +114,4 @@ void hasText() {
private Object getTestObject() {
return new UrlGroup().setCategory("category").setGroup("group").setPattern("/pattern").setLimit(1_000L);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.util.UriTemplate;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Collections;
Expand Down Expand Up @@ -99,7 +98,7 @@ public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientCo
* tests appendQueryParameter
*/
@Test
void appendQueryParameter() throws Exception {
void appendQueryParameter() {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search?type=checkin"),
token);
Expand All @@ -110,7 +109,7 @@ void appendQueryParameter() throws Exception {
* tests appendQueryParameter
*/
@Test
void appendQueryParameterWithNoExistingParameters() throws Exception {
void appendQueryParameterWithNoExistingParameters() {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search"), token);
assertThat(appended).hasToString("https://graph.facebook.com/search?bearer_token=12345");
Expand All @@ -120,7 +119,7 @@ void appendQueryParameterWithNoExistingParameters() throws Exception {
* tests encoding of access token value
*/
@Test
void doubleEncodingOfParameterValue() throws Exception {
void doubleEncodingOfParameterValue() {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("1/qIxxx");
URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search"), token);
assertThat(appended).hasToString("https://graph.facebook.com/search?bearer_token=1%2FqIxxx");
Expand All @@ -130,7 +129,7 @@ void doubleEncodingOfParameterValue() throws Exception {
* tests no double encoding of existing query parameter
*/
@Test
void nonEncodingOfUriTemplate() throws Exception {
void nonEncodingOfUriTemplate() {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
UriTemplate uriTemplate = new UriTemplate("https://graph.facebook.com/fql?q={q}");
URI expanded = uriTemplate.expand("[q: fql]");
Expand All @@ -142,7 +141,7 @@ void nonEncodingOfUriTemplate() throws Exception {
* tests URI with fragment value
*/
@Test
void fragmentUri() throws Exception {
void fragmentUri() {
OAuth2AccessToken token = new DefaultOAuth2AccessToken("1234");
URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search#foo"), token);
assertThat(appended).hasToString("https://graph.facebook.com/search?bearer_token=1234#foo");
Expand All @@ -152,21 +151,18 @@ void fragmentUri() throws Exception {
* tests encoding of access token value passed in protected requests ref: SECOAUTH-90
*/
@Test
void doubleEncodingOfAccessTokenValue() throws Exception {
void doubleEncodingOfAccessTokenValue() {
// try with fictitious token value with many characters to encode
OAuth2AccessToken token = new DefaultOAuth2AccessToken("1 qI+x:y=z");
// System.err.println(UriUtils.encodeQueryParam(token.getValue(), "UTF-8"));
URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search"), token);
assertThat(appended).hasToString("https://graph.facebook.com/search?bearer_token=1+qI%2Bx%3Ay%3Dz");
}

@Test
void noRetryAccessDeniedExceptionForNoExistingToken() {
restTemplate.setAccessTokenProvider(new StubAccessTokenProvider());
restTemplate.setRequestFactory(new ClientHttpRequestFactory() {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
throw new AccessTokenRequiredException(resource);
}
restTemplate.setRequestFactory((uri, httpMethod) -> {
throw new AccessTokenRequiredException(resource);
});
assertThatExceptionOfType(AccessTokenRequiredException.class).isThrownBy(() ->
restTemplate.doExecute(new URI("https://foo"), HttpMethod.GET, new NullRequestCallback(),
Expand All @@ -179,7 +175,7 @@ void retryAccessDeniedException() throws Exception {
restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken("TEST"));
restTemplate.setAccessTokenProvider(new StubAccessTokenProvider());
restTemplate.setRequestFactory(new ClientHttpRequestFactory() {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
if (!failed.get()) {
failed.set(true);
throw new AccessTokenRequiredException(resource);
Expand Down Expand Up @@ -307,18 +303,19 @@ public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails detail
} catch (UserRedirectRequiredException e) {
// planned
}
// context token should be reset as it clearly is invalid at this point
// context token should be reset as it is invalid at this point
assertThat(restTemplate.getOAuth2ClientContext().getAccessToken()).isNull();
}

private final class SimpleResponseExtractor implements ResponseExtractor<Boolean> {
public Boolean extractData(ClientHttpResponse response) throws IOException {
public Boolean extractData(ClientHttpResponse response) {
return true;
}
}

private static class NullRequestCallback implements RequestCallback {
public void doWithRequest(ClientHttpRequest request) throws IOException {
public void doWithRequest(ClientHttpRequest request) {
// do nothing
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ public int getRawStatusCode() throws IOException {
}

public void close() {
// do nothing
}
}

private OAuth2ErrorHandler handler;

@BeforeEach
void setUp() throws Exception {
void setUp() {
handler = new OAuth2ErrorHandler(resource);
}

Expand Down Expand Up @@ -255,13 +256,13 @@ public List<MediaType> getSupportedMediaTypes() {
}

@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
public Object read(Class clazz, HttpInputMessage inputMessage) throws HttpMessageNotReadableException {
throw new HttpMessageConversionException("error");
}

@Override
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {

public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException {
// do nothing
}
};
ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ class FormOAuth2ExceptionHttpMessageTest {
FormOAuth2AccessTokenMessageConverter auth2AccessTokenMessageConverter;

@BeforeEach
void setUp() throws Exception {
void setUp() {
converter = new FormOAuth2ExceptionHttpMessageConverter();
auth2AccessTokenMessageConverter = new FormOAuth2AccessTokenMessageConverter();
}

@Test
void canRead() {
assertThat(converter.canRead(new OAuth2Exception("").getClass(), MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
assertThat(auth2AccessTokenMessageConverter.canRead(new BadClientCredentialsException().getClass(), MediaType.APPLICATION_FORM_URLENCODED)).isFalse();
assertThat(converter.canRead(OAuth2Exception.class, MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
assertThat(auth2AccessTokenMessageConverter.canRead(BadClientCredentialsException.class, MediaType.APPLICATION_FORM_URLENCODED)).isFalse();
}

@Test
void canWrite() {
assertThat(converter.canWrite(new OAuth2Exception("").getClass(), MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
assertThat(auth2AccessTokenMessageConverter.canWrite(new BadClientCredentialsException().getClass(), MediaType.APPLICATION_FORM_URLENCODED)).isFalse();
assertThat(converter.canWrite(OAuth2Exception.class, MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
assertThat(auth2AccessTokenMessageConverter.canWrite(BadClientCredentialsException.class, MediaType.APPLICATION_FORM_URLENCODED)).isFalse();
}

@Test
Expand All @@ -46,7 +46,7 @@ void getSupportedMediaTypes() {

@Test
void read() throws IOException {
assertThat(converter.read(new OAuth2Exception("").getClass(), new MockHttpInputMessage("".getBytes()))).isNotNull();
assertThat(converter.read(OAuth2Exception.class, new MockHttpInputMessage("".getBytes()))).isNotNull();
}

@Test
Expand All @@ -63,4 +63,4 @@ void write() throws IOException {
converter.write(e, MediaType.APPLICATION_FORM_URLENCODED, outputMessage);
assertThat(outputMessage.getBody()).hasToString("error=invalid_client&error_description=Bad+client+credentials&key=value");
}
}
}
Loading

0 comments on commit dc5fda7

Please sign in to comment.