JSON tests ObjectMapper Cleanup
* Move to @Setup * Consistently extend from AbstractMixinTests and reuse ObjectMapper Issue gh-3736
This commit is contained in:
@@ -17,9 +17,9 @@
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Jitenra Singh
|
||||
@@ -27,14 +27,12 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public abstract class AbstractMixinTests {
|
||||
|
||||
ObjectMapper mapper;
|
||||
protected ObjectMapper mapper;
|
||||
|
||||
protected ObjectMapper buildObjectMapper() {
|
||||
if (ObjectUtils.isEmpty(mapper)) {
|
||||
mapper = new ObjectMapper();
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
||||
}
|
||||
return mapper;
|
||||
@Before
|
||||
public void setup() {
|
||||
mapper = new ObjectMapper();
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,15 +16,14 @@
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -32,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
public class CookieMixinTests {
|
||||
public class CookieMixinTests extends AbstractMixinTests {
|
||||
|
||||
// @formatter:off
|
||||
private static final String COOKIE_JSON = "{"
|
||||
@@ -49,23 +48,16 @@ public class CookieMixinTests {
|
||||
+ "}";
|
||||
// @formatter:on
|
||||
|
||||
ObjectMapper buildObjectMapper() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
||||
return mapper;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeCookie() throws JsonProcessingException, JSONException {
|
||||
Cookie cookie = new Cookie("demo", "cookie1");
|
||||
String actualString = buildObjectMapper().writeValueAsString(cookie);
|
||||
String actualString = mapper.writeValueAsString(cookie);
|
||||
JSONAssert.assertEquals(COOKIE_JSON, actualString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeCookie() throws IOException {
|
||||
Cookie cookie = buildObjectMapper().readValue(COOKIE_JSON, Cookie.class);
|
||||
Cookie cookie = mapper.readValue(COOKIE_JSON, Cookie.class);
|
||||
assertThat(cookie).isNotNull();
|
||||
assertThat(cookie.getName()).isEqualTo("demo");
|
||||
assertThat(cookie.getDomain()).isEqualTo("");
|
||||
|
||||
@@ -20,13 +20,10 @@ import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.security.web.csrf.DefaultCsrfToken;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -35,11 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
public class DefaultCsrfTokenMixinTests {
|
||||
public class DefaultCsrfTokenMixinTests extends AbstractMixinTests {
|
||||
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
|
||||
// @formatter:off
|
||||
public static final String CSRF_JSON = "{"
|
||||
+ "\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", "
|
||||
@@ -49,23 +43,16 @@ public class DefaultCsrfTokenMixinTests {
|
||||
+ "}";
|
||||
// @formatter:on
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
objectMapper = new ObjectMapper();
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
objectMapper.registerModules(SecurityJacksonModules.getModules(loader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCsrfTokenSerializedTest() throws JsonProcessingException, JSONException {
|
||||
DefaultCsrfToken token = new DefaultCsrfToken("csrf-header", "_csrf", "1");
|
||||
String serializedJson = objectMapper.writeValueAsString(token);
|
||||
String serializedJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(CSRF_JSON, serializedJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCsrfTokenDeserializeTest() throws IOException {
|
||||
DefaultCsrfToken token = objectMapper.readValue(CSRF_JSON, DefaultCsrfToken.class);
|
||||
DefaultCsrfToken token = mapper.readValue(CSRF_JSON, DefaultCsrfToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.getHeaderName()).isEqualTo("csrf-header");
|
||||
assertThat(token.getParameterName()).isEqualTo("_csrf");
|
||||
@@ -75,12 +62,12 @@ public class DefaultCsrfTokenMixinTests {
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void defaultCsrfTokenDeserializeWithoutClassTest() throws IOException {
|
||||
String tokenJson = "{\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
|
||||
objectMapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
mapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void defaultCsrfTokenDeserializeNullValuesTest() throws IOException {
|
||||
String tokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", \"headerName\": \"\", \"parameterName\": null, \"token\": \"1\"}";
|
||||
objectMapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
mapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setCookies(new Cookie("SESSION", "123456789"));
|
||||
request.addHeader("x-auth-token", "12");
|
||||
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new DefaultSavedRequest(request, new PortResolverImpl()));
|
||||
String actualString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new DefaultSavedRequest(request, new PortResolverImpl()));
|
||||
JSONAssert.assertEquals(REQUEST_JSON, actualString, true);
|
||||
}
|
||||
|
||||
@@ -106,13 +106,13 @@ public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
|
||||
.setScheme("http").setRequestURL("http://localhost").setServerName("localhost").setRequestURI("")
|
||||
.setLocales(Collections.singletonList(new Locale("en"))).setContextPath("").setMethod("")
|
||||
.setServletPath("").build();
|
||||
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(request);
|
||||
String actualString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);
|
||||
JSONAssert.assertEquals(REQUEST_JSON, actualString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeDefaultSavedRequest() throws IOException {
|
||||
DefaultSavedRequest request = (DefaultSavedRequest) buildObjectMapper().readValue(REQUEST_JSON, Object.class);
|
||||
DefaultSavedRequest request = (DefaultSavedRequest) mapper.readValue(REQUEST_JSON, Object.class);
|
||||
assertThat(request).isNotNull();
|
||||
assertThat(request.getCookies()).hasSize(1);
|
||||
assertThat(request.getLocales()).hasSize(1).contains(new Locale("en"));
|
||||
|
||||
@@ -25,7 +25,6 @@ import javax.servlet.http.Cookie;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
@@ -61,14 +60,13 @@ public class SavedCookieMixinTests extends AbstractMixinTests {
|
||||
@Test
|
||||
public void serializeWithDefaultConfigurationTest() throws JsonProcessingException, JSONException {
|
||||
SavedCookie savedCookie = new SavedCookie(new Cookie("SESSION", "123456789"));
|
||||
String actualJson = buildObjectMapper().writeValueAsString(savedCookie);
|
||||
String actualJson = mapper.writeValueAsString(savedCookie);
|
||||
JSONAssert.assertEquals(COOKIE_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeWithOverrideConfigurationTest() throws JsonProcessingException, JSONException {
|
||||
SavedCookie savedCookie = new SavedCookie(new Cookie("SESSION", "123456789"));
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PUBLIC_ONLY)
|
||||
.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
|
||||
String actualJson = mapper.writeValueAsString(savedCookie);
|
||||
@@ -79,14 +77,14 @@ public class SavedCookieMixinTests extends AbstractMixinTests {
|
||||
public void serializeSavedCookieWithList() throws JsonProcessingException, JSONException {
|
||||
List<SavedCookie> savedCookies = new ArrayList<SavedCookie>();
|
||||
savedCookies.add(new SavedCookie(new Cookie("SESSION", "123456789")));
|
||||
String actualJson = buildObjectMapper().writeValueAsString(savedCookies);
|
||||
String actualJson = mapper.writeValueAsString(savedCookies);
|
||||
JSONAssert.assertEquals(COOKIES_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void deserializeSavedCookieWithList() throws IOException, JSONException {
|
||||
List<SavedCookie> savedCookies = (List<SavedCookie>)buildObjectMapper().readValue(COOKIES_JSON, Object.class);
|
||||
List<SavedCookie> savedCookies = (List<SavedCookie>)mapper.readValue(COOKIES_JSON, Object.class);
|
||||
assertThat(savedCookies).isNotNull().hasSize(1);
|
||||
assertThat(savedCookies.get(0).getName()).isEqualTo("SESSION");
|
||||
assertThat(savedCookies.get(0).getValue()).isEqualTo("123456789");
|
||||
@@ -94,7 +92,7 @@ public class SavedCookieMixinTests extends AbstractMixinTests {
|
||||
|
||||
@Test
|
||||
public void deserializeSavedCookieJsonTest() throws IOException {
|
||||
SavedCookie savedCookie = (SavedCookie) buildObjectMapper().readValue(COOKIE_JSON, Object.class);
|
||||
SavedCookie savedCookie = (SavedCookie) mapper.readValue(COOKIE_JSON, Object.class);
|
||||
assertThat(savedCookie).isNotNull();
|
||||
assertThat(savedCookie.getName()).isEqualTo("SESSION");
|
||||
assertThat(savedCookie.getValue()).isEqualTo("123456789");
|
||||
|
||||
@@ -19,15 +19,12 @@ package org.springframework.security.web.jackson2;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -36,10 +33,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
public class WebAuthenticationDetailsMixinTests {
|
||||
public class WebAuthenticationDetailsMixinTests extends AbstractMixinTests {
|
||||
|
||||
ObjectMapper mapper;
|
||||
|
||||
// @formatter:off
|
||||
private static final String AUTHENTICATION_DETAILS_JSON = "{"
|
||||
+ "\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\","
|
||||
@@ -49,13 +44,6 @@ public class WebAuthenticationDetailsMixinTests {
|
||||
+ "}";
|
||||
// @formatter:on
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mapper = new ObjectMapper();
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
this.mapper.registerModules(SecurityJacksonModules.getModules(loader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildWebAuthenticationDetailsUsingDifferentConstructors()
|
||||
throws IOException {
|
||||
@@ -65,7 +53,7 @@ public class WebAuthenticationDetailsMixinTests {
|
||||
|
||||
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
|
||||
|
||||
WebAuthenticationDetails authenticationDetails = this.mapper.readValue(AUTHENTICATION_DETAILS_JSON,
|
||||
WebAuthenticationDetails authenticationDetails = mapper.readValue(AUTHENTICATION_DETAILS_JSON,
|
||||
WebAuthenticationDetails.class);
|
||||
assertThat(details.equals(authenticationDetails));
|
||||
}
|
||||
@@ -77,14 +65,14 @@ public class WebAuthenticationDetailsMixinTests {
|
||||
request.setRemoteAddr("/localhost");
|
||||
request.setSession(new MockHttpSession(null, "1"));
|
||||
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
|
||||
String actualJson = this.mapper.writeValueAsString(details);
|
||||
String actualJson = mapper.writeValueAsString(details);
|
||||
JSONAssert.assertEquals(AUTHENTICATION_DETAILS_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webAuthenticationDetailsDeserializeTest()
|
||||
throws IOException, JSONException {
|
||||
WebAuthenticationDetails details = this.mapper.readValue(AUTHENTICATION_DETAILS_JSON,
|
||||
WebAuthenticationDetails details = mapper.readValue(AUTHENTICATION_DETAILS_JSON,
|
||||
WebAuthenticationDetails.class);
|
||||
assertThat(details).isNotNull();
|
||||
assertThat(details.getRemoteAddress()).isEqualTo("/localhost");
|
||||
|
||||
Reference in New Issue
Block a user