1+ /*
2+ * Copyright 2011 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+ * the License. You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+ * specific language governing permissions and limitations under the License.
12+ */
13+ package org .springframework .security .oauth2 .common ;
14+
15+ import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertNull ;
17+
18+ import java .io .IOException ;
19+ import java .util .Date ;
20+ import java .util .HashSet ;
21+
22+ import org .codehaus .jackson .JsonGenerationException ;
23+ import org .codehaus .jackson .map .JsonMappingException ;
24+ import org .junit .Test ;
25+ import org .powermock .core .classloader .annotations .PrepareForTest ;
26+
27+ /**
28+ * Tests deserialization of an {@link OAuth2AccessToken} using jackson.
29+ *
30+ * @author Rob Winch
31+ */
32+ @ PrepareForTest (OAuth2AccessTokenDeserializer .class )
33+ public class TestOAuth2AccessTokenDeserializer extends BaseOAuth2AccessTokenJacksonTest {
34+
35+ @ Test
36+ public void readValueNoRefresh () throws JsonGenerationException , JsonMappingException , IOException {
37+ accessToken .setRefreshToken (null );
38+ accessToken .setScope (null );
39+ OAuth2AccessToken actual = mapper .readValue (ACCESS_TOKEN_NOREFRESH , OAuth2AccessToken .class );
40+ assertTokenEquals (accessToken ,actual );
41+ }
42+
43+ @ Test
44+ public void readValueWithRefresh () throws JsonGenerationException , JsonMappingException , IOException {
45+ accessToken .setScope (null );
46+ OAuth2AccessToken actual = mapper .readValue (ACCESS_TOKEN_NOSCOPE , OAuth2AccessToken .class );
47+ assertTokenEquals (accessToken ,actual );
48+ }
49+
50+ @ Test
51+ public void readValueWithSingleScopes () throws JsonGenerationException , JsonMappingException , IOException {
52+ accessToken .getScope ().remove (accessToken .getScope ().iterator ().next ());
53+ OAuth2AccessToken actual = mapper .readValue (ACCESS_TOKEN_SINGLESCOPE , OAuth2AccessToken .class );
54+ assertTokenEquals (accessToken ,actual );
55+ }
56+
57+ @ Test
58+ public void readValueWithEmptyStringScope () throws JsonGenerationException , JsonMappingException , IOException {
59+ accessToken .setScope (new HashSet <String >());
60+ OAuth2AccessToken actual = mapper .readValue (ACCESS_TOKEN_EMPTYSCOPE , OAuth2AccessToken .class );
61+ assertTokenEquals (accessToken ,actual );
62+ }
63+
64+ @ Test
65+ public void readValueWithMultiScopes () throws Exception {
66+ OAuth2AccessToken actual = mapper .readValue (ACCESS_TOKEN_MULTISCOPE , OAuth2AccessToken .class );
67+ assertTokenEquals (accessToken ,actual );
68+ }
69+
70+ @ Test
71+ public void readValueWithMac () throws Exception {
72+ accessToken .setTokenType ("mac" );
73+ String encodedToken = ACCESS_TOKEN_MULTISCOPE .replace ("bearer" , accessToken .getTokenType ());
74+ OAuth2AccessToken actual = mapper .readValue (encodedToken , OAuth2AccessToken .class );
75+ assertTokenEquals (accessToken ,actual );
76+ }
77+
78+ private static void assertTokenEquals (OAuth2AccessToken expected , OAuth2AccessToken actual ) {
79+ assertEquals (expected .getTokenType (), actual .getTokenType ());
80+ assertEquals (expected .getValue (), actual .getValue ());
81+
82+ OAuth2RefreshToken expectedRefreshToken = expected .getRefreshToken ();
83+ if (expectedRefreshToken == null ) {
84+ assertNull (actual .getRefreshToken ());
85+ }
86+ else {
87+ assertEquals (expectedRefreshToken .getValue (), actual .getRefreshToken ().getValue ());
88+ }
89+ assertEquals (expected .getScope (), actual .getScope ());
90+ Date expectedExpiration = expected .getExpiration ();
91+ if (expectedExpiration == null ) {
92+ assertNull (actual .getExpiration ());
93+ }
94+ else {
95+ assertEquals (expectedExpiration .getTime (), actual .getExpiration ().getTime ());
96+ }
97+ }
98+ }
0 commit comments