JSON tests ObjectMapper Cleanup
* Move to @Setup * Consistently extend from AbstractMixinTests and reuse ObjectMapper Issue gh-3736
This commit is contained in:
@@ -17,26 +17,23 @@
|
||||
package org.springframework.security.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Jitenra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
public abstract class AbstractMixinTests {
|
||||
protected ObjectMapper mapper;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
User createDefaultUser() {
|
||||
|
||||
+4
-4
@@ -56,13 +56,13 @@ public class AnonymousAuthenticationTokenMixinTests extends AbstractMixinTests {
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken(
|
||||
HASH_KEY, user, user.getAuthorities()
|
||||
);
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(ANONYMOUS_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeAnonymousAuthenticationTokenTest() throws IOException {
|
||||
AnonymousAuthenticationToken token = buildObjectMapper()
|
||||
AnonymousAuthenticationToken token = mapper
|
||||
.readValue(ANONYMOUS_JSON, AnonymousAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.getKeyHash()).isEqualTo(HASH_KEY.hashCode());
|
||||
@@ -74,7 +74,7 @@ public class AnonymousAuthenticationTokenMixinTests extends AbstractMixinTests {
|
||||
String jsonString = "{\"@class\": \"org.springframework.security.authentication.AnonymousAuthenticationToken\", \"details\": null," +
|
||||
"\"principal\": \"user\", \"authenticated\": true, \"keyHash\": " + HASH_KEY.hashCode() + "," +
|
||||
"\"authorities\": [\"java.util.ArrayList\", []]}";
|
||||
buildObjectMapper().readValue(jsonString, AnonymousAuthenticationToken.class);
|
||||
mapper.readValue(jsonString, AnonymousAuthenticationToken.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,7 +84,7 @@ public class AnonymousAuthenticationTokenMixinTests extends AbstractMixinTests {
|
||||
HASH_KEY, user, user.getAuthorities()
|
||||
);
|
||||
token.eraseCredentials();
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(ANONYMOUS_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -72,7 +72,7 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
|
||||
@Test
|
||||
public void serializeRememberMeAuthenticationToken() throws JsonProcessingException, JSONException {
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, "admin", Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")));
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
|
||||
public void serializeRememberMeAuthenticationWithUserToken() throws JsonProcessingException, JSONException {
|
||||
User user = createDefaultUser();
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, user, user.getAuthorities());
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(String.format(REMEMBERME_AUTH_JSON, "\"password\""), actualJson, true);
|
||||
}
|
||||
|
||||
@@ -89,13 +89,13 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
|
||||
User user = createDefaultUser();
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, user, user.getAuthorities());
|
||||
token.eraseCredentials();
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(REMEMBERME_AUTH_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeRememberMeAuthenticationToken() throws IOException {
|
||||
RememberMeAuthenticationToken token = buildObjectMapper().readValue(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, RememberMeAuthenticationToken.class);
|
||||
RememberMeAuthenticationToken token = mapper.readValue(REMEMBERME_AUTH_STRINGPRINCIPAL_JSON, RememberMeAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.getPrincipal()).isNotNull().isEqualTo("admin").isEqualTo(token.getName());
|
||||
assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
@@ -103,7 +103,7 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
|
||||
|
||||
@Test
|
||||
public void deserializeRememberMeAuthenticationTokenWithUserTest() throws IOException {
|
||||
RememberMeAuthenticationToken token = buildObjectMapper()
|
||||
RememberMeAuthenticationToken token = mapper
|
||||
.readValue(String.format(REMEMBERME_AUTH_JSON, "\"password\""), RememberMeAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
|
||||
|
||||
+2
-2
@@ -47,13 +47,13 @@ public class SecurityContextMixinTests extends AbstractMixinTests {
|
||||
public void securityContextSerializeTest() throws JsonProcessingException, JSONException {
|
||||
SecurityContext context = new SecurityContextImpl();
|
||||
context.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "1234", Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"))));
|
||||
String actualJson = buildObjectMapper().writeValueAsString(context);
|
||||
String actualJson = mapper.writeValueAsString(context);
|
||||
JSONAssert.assertEquals(SECURITY_CONTEXT_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void securityContextDeserializeTest() throws IOException {
|
||||
SecurityContext context = buildObjectMapper().readValue(SECURITY_CONTEXT_JSON, SecurityContextImpl.class);
|
||||
SecurityContext context = mapper.readValue(SECURITY_CONTEXT_JSON, SecurityContextImpl.class);
|
||||
assertThat(context).isNotNull();
|
||||
assertThat(context.getAuthentication()).isNotNull().isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo("admin");
|
||||
|
||||
+3
-3
@@ -48,13 +48,13 @@ public class SimpleGrantedAuthorityMixinTests extends AbstractMixinTests {
|
||||
@Test
|
||||
public void serializeSimpleGrantedAuthorityTest() throws JsonProcessingException, JSONException {
|
||||
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
|
||||
String serializeJson = buildObjectMapper().writeValueAsString(authority);
|
||||
String serializeJson = mapper.writeValueAsString(authority);
|
||||
JSONAssert.assertEquals(AUTHORITY_JSON, serializeJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeGrantedAuthorityTest() throws IOException {
|
||||
SimpleGrantedAuthority authority = buildObjectMapper().readValue(AUTHORITY_JSON, SimpleGrantedAuthority.class);
|
||||
SimpleGrantedAuthority authority = mapper.readValue(AUTHORITY_JSON, SimpleGrantedAuthority.class);
|
||||
assertThat(authority).isNotNull();
|
||||
assertThat(authority.getAuthority()).isNotNull().isEqualTo("ROLE_USER");
|
||||
}
|
||||
@@ -62,6 +62,6 @@ public class SimpleGrantedAuthorityMixinTests extends AbstractMixinTests {
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void deserializeGrantedAuthorityWithoutRoleTest() throws IOException {
|
||||
String json = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\"}";
|
||||
buildObjectMapper().readValue(json, SimpleGrantedAuthority.class);
|
||||
mapper.readValue(json, SimpleGrantedAuthority.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ public class UserDeserializerTests extends AbstractMixinTests {
|
||||
|
||||
@Test
|
||||
public void serializeUserTest() throws JsonProcessingException, JSONException {
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
User user = createDefaultUser();
|
||||
String userJson = mapper.writeValueAsString(user);
|
||||
JSONAssert.assertEquals(userWithPasswordJson(user.getPassword()), userJson, true);
|
||||
@@ -63,7 +62,6 @@ public class UserDeserializerTests extends AbstractMixinTests {
|
||||
|
||||
@Test
|
||||
public void serializeUserWithoutAuthority() throws JsonProcessingException, JSONException {
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
User user = new User("admin", "1234", Collections.<GrantedAuthority>emptyList());
|
||||
String userJson = mapper.writeValueAsString(user);
|
||||
JSONAssert.assertEquals(userWithNoAuthoritiesJson(), userJson, true);
|
||||
@@ -73,14 +71,11 @@ public class UserDeserializerTests extends AbstractMixinTests {
|
||||
public void deserializeUserWithNullPasswordEmptyAuthorityTest() throws IOException {
|
||||
String userJsonWithoutPasswordString = USER_JSON.replace(SimpleGrantedAuthorityMixinTests.AUTHORITIES_SET_JSON, "[]");
|
||||
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
mapper.readValue(userJsonWithoutPasswordString, User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeUserWithNullPasswordNoAuthorityTest() throws Exception {
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
|
||||
String userJsonWithoutPasswordString = removeNode(userWithNoAuthoritiesJson(), mapper, "password");
|
||||
|
||||
User user = mapper.readValue(userJsonWithoutPasswordString, User.class);
|
||||
@@ -93,14 +88,13 @@ public class UserDeserializerTests extends AbstractMixinTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void deserializeUserWithNoClassIdInAuthoritiesTest() throws Exception {
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
String userJson = USER_JSON.replace(SimpleGrantedAuthorityMixinTests.AUTHORITIES_SET_JSON, "[{\"authority\": \"ROLE_USER\"}]");
|
||||
mapper.readValue(userJson, User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeUserWithClassIdInAuthoritiesTest() throws IOException {
|
||||
User user = buildObjectMapper().readValue(userJson(), User.class);
|
||||
User user = mapper.readValue(userJson(), User.class);
|
||||
assertThat(user).isNotNull();
|
||||
assertThat(user.getUsername()).isEqualTo("admin");
|
||||
assertThat(user.getPassword()).isEqualTo("1234");
|
||||
|
||||
+6
-8
@@ -19,7 +19,6 @@ package org.springframework.security.jackson2;
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
@@ -59,7 +58,7 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
@Test
|
||||
public void serializeUnauthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws JsonProcessingException, JSONException {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", "1234");
|
||||
String serializedJson = buildObjectMapper().writeValueAsString(token);
|
||||
String serializedJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(UNAUTHENTICATED_STRINGPRINCIPAL_JSON, serializedJson, true);
|
||||
}
|
||||
|
||||
@@ -67,13 +66,13 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
public void serializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws JsonProcessingException, JSONException {
|
||||
User user = createDefaultUser();
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), user.getAuthorities());
|
||||
String serializedJson = buildObjectMapper().writeValueAsString(token);
|
||||
String serializedJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(AUTHENTICATED_STRINGPRINCIPAL_JSON, serializedJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeUnauthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws IOException, JSONException {
|
||||
UsernamePasswordAuthenticationToken token = buildObjectMapper()
|
||||
UsernamePasswordAuthenticationToken token = mapper
|
||||
.readValue(UNAUTHENTICATED_STRINGPRINCIPAL_JSON, UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.isAuthenticated()).isEqualTo(false);
|
||||
@@ -83,7 +82,7 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
@Test
|
||||
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws IOException {
|
||||
UsernamePasswordAuthenticationToken expectedToken = createToken();
|
||||
UsernamePasswordAuthenticationToken token = buildObjectMapper()
|
||||
UsernamePasswordAuthenticationToken token = mapper
|
||||
.readValue(AUTHENTICATED_STRINGPRINCIPAL_JSON, UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.isAuthenticated()).isTrue();
|
||||
@@ -93,13 +92,12 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
@Test
|
||||
public void serializeAuthenticatedUsernamePasswordAuthenticationTokenMixinWithUserTest() throws JsonProcessingException, JSONException {
|
||||
UsernamePasswordAuthenticationToken token = createToken();
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(AUTHENTICATED_JSON, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest() throws IOException {
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
UsernamePasswordAuthenticationToken token = mapper
|
||||
.readValue(AUTHENTICATED_JSON, UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
@@ -113,7 +111,7 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
public void serializeAuthenticatedUsernamePasswordAuthenticationTokenMixinAfterEraseCredentialInvoked() throws JsonProcessingException, JSONException {
|
||||
UsernamePasswordAuthenticationToken token = createToken();
|
||||
token.eraseCredentials();
|
||||
String actualJson = buildObjectMapper().writeValueAsString(token);
|
||||
String actualJson = mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(AUTHENTICATED_JSON.replaceAll(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user