55import com .auth0 .jwt .interfaces .Claim ;
66import com .fasterxml .jackson .databind .JsonNode ;
77import com .fasterxml .jackson .databind .ObjectMapper ;
8+ import com .fasterxml .jackson .databind .node .JsonNodeType ;
89import com .fasterxml .jackson .databind .node .MissingNode ;
910import com .fasterxml .jackson .databind .node .NullNode ;
11+ import com .fasterxml .jackson .databind .node .ObjectNode ;
12+ import org .hamcrest .collection .IsMapContaining ;
1013import org .junit .Before ;
1114import org .junit .Rule ;
1215import org .junit .Test ;
1316import org .junit .rules .ExpectedException ;
1417
15- import java .util .Arrays ;
16- import java .util .Collections ;
17- import java .util .Date ;
18- import java .util .Map ;
18+ import java .io .IOException ;
19+ import java .util .*;
1920
2021import static com .auth0 .jwt .impl .JWTParser .getDefaultObjectMapper ;
2122import static com .auth0 .jwt .impl .JsonNodeClaim .claimFromNode ;
2223import static org .hamcrest .Matchers .*;
2324import static org .hamcrest .core .IsNull .notNullValue ;
2425import static org .hamcrest .core .IsNull .nullValue ;
2526import static org .junit .Assert .assertThat ;
27+ import static org .mockito .Mockito .mock ;
28+ import static org .mockito .Mockito .when ;
2629
2730public class JsonNodeClaimTest {
2831
@@ -206,6 +209,54 @@ public void shouldThrowIfListClassMismatch() throws Exception {
206209 claim .asList (UserPojo .class );
207210 }
208211
212+ @ Test
213+ public void shouldGetNullMapIfNullValue () throws Exception {
214+ JsonNode value = mapper .valueToTree (null );
215+ Claim claim = claimFromNode (value );
216+
217+ assertThat (claim .asMap (), is (nullValue ()));
218+ }
219+
220+ @ Test
221+ public void shouldGetNullMapIfNonArrayValue () throws Exception {
222+ JsonNode value = mapper .valueToTree (1 );
223+ Claim claim = claimFromNode (value );
224+
225+ assertThat (claim .asMap (), is (nullValue ()));
226+ }
227+
228+ @ Test
229+ public void shouldGetMapValue () throws Exception {
230+ Map <String , Object > map = new HashMap <>();
231+ map .put ("text" , "extraValue" );
232+ map .put ("number" , 12 );
233+ map .put ("boolean" , true );
234+ map .put ("object" , Collections .singletonMap ("something" , "else" ));
235+
236+ JsonNode value = mapper .valueToTree (map );
237+ Claim claim = claimFromNode (value );
238+
239+ assertThat (claim , is (notNullValue ()));
240+ Map <String , Object > backMap = claim .asMap ();
241+ assertThat (backMap , is (notNullValue ()));
242+ assertThat (backMap , hasEntry ("text" , (Object ) "extraValue" ));
243+ assertThat (backMap , hasEntry ("number" , (Object ) 12 ));
244+ assertThat (backMap , hasEntry ("boolean" , (Object ) true ));
245+ assertThat (backMap , hasKey ("object" ));
246+ assertThat ((Map <String , Object >) backMap .get ("object" ), IsMapContaining .hasEntry ("something" , (Object ) "else" ));
247+ }
248+
249+ @ Test
250+ public void shouldThrowIfAnExtraordinaryExceptionHappensWhenParsingAsGenericMap () throws Exception {
251+ JsonNode value = mock (ObjectNode .class );
252+ when (value .getNodeType ()).thenReturn (JsonNodeType .OBJECT );
253+ when (value .fields ()).thenThrow (IOException .class );
254+ Claim claim = claimFromNode (value );
255+
256+ exception .expect (JWTDecodeException .class );
257+ claim .asMap ();
258+ }
259+
209260 @ Test
210261 public void shouldGetCustomClassValue () throws Exception {
211262 JsonNode value = mapper .valueToTree (new UserPojo ("john" , 123 ));
@@ -268,12 +319,82 @@ public void shouldReturnBaseClaimWhenParsingNullValue() throws Exception {
268319 }
269320
270321 @ Test
271- public void shouldReturnValidButNullClaimIfTreeIsEmpty () throws Exception {
322+ public void shouldReturnNonNullClaimWhenParsingObject () throws Exception {
272323 JsonNode value = mapper .valueToTree (new Object ());
273324 Claim claim = claimFromNode (value );
274325
275326 assertThat (claim , is (notNullValue ()));
276327 assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
277- assertThat (claim .isNull (), is (true ));
328+ assertThat (claim .isNull (), is (false ));
329+ }
330+
331+ @ Test
332+ public void shouldReturnNonNullClaimWhenParsingArray () throws Exception {
333+ JsonNode value = mapper .valueToTree (new String []{});
334+ Claim claim = claimFromNode (value );
335+
336+ assertThat (claim , is (notNullValue ()));
337+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
338+ assertThat (claim .isNull (), is (false ));
339+ }
340+
341+ @ Test
342+ public void shouldReturnNonNullClaimWhenParsingList () throws Exception {
343+ JsonNode value = mapper .valueToTree (new ArrayList <String >());
344+ Claim claim = claimFromNode (value );
345+
346+ assertThat (claim , is (notNullValue ()));
347+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
348+ assertThat (claim .isNull (), is (false ));
349+ }
350+
351+ @ Test
352+ public void shouldReturnNonNullClaimWhenParsingStringValue () throws Exception {
353+ JsonNode value = mapper .valueToTree ("" );
354+ Claim claim = claimFromNode (value );
355+
356+ assertThat (claim , is (notNullValue ()));
357+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
358+ assertThat (claim .isNull (), is (false ));
359+ }
360+
361+ @ Test
362+ public void shouldReturnNonNullClaimWhenParsingIntValue () throws Exception {
363+ JsonNode value = mapper .valueToTree (Integer .MAX_VALUE );
364+ Claim claim = claimFromNode (value );
365+
366+ assertThat (claim , is (notNullValue ()));
367+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
368+ assertThat (claim .isNull (), is (false ));
369+ }
370+
371+ @ Test
372+ public void shouldReturnNonNullClaimWhenParsingDoubleValue () throws Exception {
373+ JsonNode value = mapper .valueToTree (Double .MAX_VALUE );
374+ Claim claim = claimFromNode (value );
375+
376+ assertThat (claim , is (notNullValue ()));
377+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
378+ assertThat (claim .isNull (), is (false ));
379+ }
380+
381+ @ Test
382+ public void shouldReturnNonNullClaimWhenParsingDateValue () throws Exception {
383+ JsonNode value = mapper .valueToTree (new Date ());
384+ Claim claim = claimFromNode (value );
385+
386+ assertThat (claim , is (notNullValue ()));
387+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
388+ assertThat (claim .isNull (), is (false ));
389+ }
390+
391+ @ Test
392+ public void shouldReturnNonNullClaimWhenParsingBooleanValue () throws Exception {
393+ JsonNode value = mapper .valueToTree (Boolean .TRUE );
394+ Claim claim = claimFromNode (value );
395+
396+ assertThat (claim , is (notNullValue ()));
397+ assertThat (claim , is (instanceOf (JsonNodeClaim .class )));
398+ assertThat (claim .isNull (), is (false ));
278399 }
279400}
0 commit comments