1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .security .web .jackson2 ;
18
18
19
+ import static org .assertj .core .api .Assertions .assertThat ;
20
+
21
+ import com .fasterxml .jackson .databind .ObjectMapper ;
22
+ import java .util .stream .Stream ;
19
23
import org .junit .jupiter .api .BeforeEach ;
20
- import org .junit .jupiter .api .Test ;
24
+ import org .junit .jupiter .params .ParameterizedTest ;
25
+ import org .junit .jupiter .params .provider .Arguments ;
26
+ import org .junit .jupiter .params .provider .MethodSource ;
21
27
import org .skyscreamer .jsonassert .JSONAssert ;
22
-
23
28
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
24
29
import org .springframework .security .core .Authentication ;
25
30
import org .springframework .security .core .authority .AuthorityUtils ;
26
- import org .springframework .security .jackson2 .AbstractMixinTests ;
31
+ import org .springframework .security .jackson2 .CoreJackson2Module ;
32
+ import org .springframework .security .jackson2 .SecurityJackson2Modules ;
27
33
import org .springframework .security .jackson2 .SimpleGrantedAuthorityMixinTests ;
28
34
import org .springframework .security .web .authentication .switchuser .SwitchUserGrantedAuthority ;
29
35
30
- import static org .assertj .core .api .Assertions .assertThat ;
31
-
32
36
/**
33
37
* @author Markus Heiden
34
38
* @since 6.3
35
39
*/
36
- public class SwitchUserGrantedAuthorityMixInTests extends AbstractMixinTests {
40
+ public class SwitchUserGrantedAuthorityMixInTests {
37
41
38
42
// language=JSON
39
43
private static final String SWITCH_JSON = """
@@ -53,22 +57,42 @@ public class SwitchUserGrantedAuthorityMixInTests extends AbstractMixinTests {
53
57
54
58
private Authentication source ;
55
59
60
+ static Stream <Arguments > mappers () {
61
+ ObjectMapper securityJackson2ModulesMapper = new ObjectMapper ();
62
+ ClassLoader classLoader = SwitchUserGrantedAuthorityMixInTests .class .getClassLoader ();
63
+ securityJackson2ModulesMapper .registerModules (SecurityJackson2Modules .getModules (classLoader ));
64
+
65
+ ObjectMapper webJackson2ModuleMapper = new ObjectMapper ();
66
+ webJackson2ModuleMapper .registerModule (new CoreJackson2Module ());
67
+ webJackson2ModuleMapper .registerModule (new WebJackson2Module ());
68
+
69
+ ObjectMapper webServletJackson2ModuleMapper = new ObjectMapper ();
70
+ webServletJackson2ModuleMapper .registerModule (new CoreJackson2Module ());
71
+ webServletJackson2ModuleMapper .registerModule (new WebServletJackson2Module ());
72
+
73
+ return Stream .of (Arguments .of (securityJackson2ModulesMapper ), Arguments .of (webJackson2ModuleMapper ),
74
+ Arguments .of (webServletJackson2ModuleMapper ));
75
+ }
76
+
56
77
@ BeforeEach
57
78
public void setUp () {
58
79
this .source = new UsernamePasswordAuthenticationToken ("principal" , "credentials" ,
59
80
AuthorityUtils .createAuthorityList ("ROLE_USER" ));
60
81
}
61
82
62
- @ Test
63
- public void serializeWhenPrincipalCredentialsAuthoritiesThenSuccess () throws Exception {
83
+ @ ParameterizedTest
84
+ @ MethodSource ("mappers" )
85
+ public void serializeWhenPrincipalCredentialsAuthoritiesThenSuccess (ObjectMapper mapper ) throws Exception {
64
86
SwitchUserGrantedAuthority expected = new SwitchUserGrantedAuthority ("switched" , this .source );
65
- String serializedJson = this . mapper .writeValueAsString (expected );
87
+ String serializedJson = mapper .writeValueAsString (expected );
66
88
JSONAssert .assertEquals (SWITCH_JSON , serializedJson , true );
67
89
}
68
90
69
- @ Test
70
- public void deserializeWhenSourceIsUsernamePasswordAuthenticationTokenThenSuccess () throws Exception {
71
- SwitchUserGrantedAuthority deserialized = this .mapper .readValue (SWITCH_JSON , SwitchUserGrantedAuthority .class );
91
+ @ ParameterizedTest
92
+ @ MethodSource ("mappers" )
93
+ public void deserializeWhenSourceIsUsernamePasswordAuthenticationTokenThenSuccess (ObjectMapper mapper )
94
+ throws Exception {
95
+ SwitchUserGrantedAuthority deserialized = mapper .readValue (SWITCH_JSON , SwitchUserGrantedAuthority .class );
72
96
assertThat (deserialized ).isNotNull ();
73
97
assertThat (deserialized .getAuthority ()).isEqualTo ("switched" );
74
98
assertThat (deserialized .getSource ()).isEqualTo (this .source );
0 commit comments