1717package org .rrajesh1979 .utils ;
1818
1919import org .javatuples .Pair ;
20+ import org .junit .jupiter .api .DisplayName ;
2021import org .junit .jupiter .api .Test ;
2122
2223import static org .junit .jupiter .api .Assertions .*;
2324
2425class JWTUtilTest {
2526
2627 @ Test
28+ @ DisplayName ("Test JWT payload generation" )
2729 void encodeDecodeJWTTest () {
2830 String userInput = "{\" name\" : \" Joe\" , \" picture\" : \" https://example.com/image.png\" }" ;
2931
@@ -34,11 +36,29 @@ void encodeDecodeJWTTest() {
3436 String key = jwtAndKey .getValue1 ();
3537
3638 Pair <String , String > decodedJwtAndKey = JWTUtil .decodeJWT (jwt , key );
37- assert (decodedJwtAndKey .getValue0 (). equals ( "{typ=JWT, alg=HS512}" ) );
38- assert (decodedJwtAndKey .getValue1 (). equals ( "{sub=JWT Encoder, aud=Hello JWT, name=Joe, iss=rrajesh1979, picture=https://example.com/image.png}" ) );
39+ assertEquals (decodedJwtAndKey .getValue0 (), "{typ=JWT, alg=HS512}" );
40+ assertEquals (decodedJwtAndKey .getValue1 (), "{sub=JWT Encoder, aud=Hello JWT, name=Joe, iss=rrajesh1979, picture=https://example.com/image.png}" );
3941 }
4042
4143 @ Test
44+ @ DisplayName ("Test JWT payload generation with dynamic ttl" )
45+ void encodeDecodeJWTTtlTest () {
46+ String userInput = "{\" name\" : \" Joe\" , \" picture\" : \" https://example.com/image.png\" }" ;
47+
48+ Pair <String , String > jwtAndKey = JWTUtil .createJWT ("JWT" , "HS512" , userInput ,
49+ "rrajesh1979" , "JWT Encoder" , "Hello JWT" , true , 100 );
50+
51+ String jwt = jwtAndKey .getValue0 ();
52+ String key = jwtAndKey .getValue1 ();
53+
54+ Pair <String , String > decodedJwtAndKey = JWTUtil .decodeJWT (jwt , key );
55+ assertEquals (decodedJwtAndKey .getValue0 (), "{typ=JWT, alg=HS512}" );
56+ assertNotEquals (decodedJwtAndKey .getValue1 (), "{sub=JWT Encoder, aud=Hello JWT, name=Joe, iss=rrajesh1979, picture=https://example.com/image.png}" );
57+ }
58+
59+
60+ @ Test
61+ @ DisplayName ("Test JWT decode with valid key" )
4262 void encodeDecodeJWTEmptyPayloadTest () {
4363 String userInput = "{}" ;
4464
@@ -49,7 +69,23 @@ void encodeDecodeJWTEmptyPayloadTest() {
4969 String key = jwtAndKey .getValue1 ();
5070
5171 Pair <String , String > decodedJwtAndKey = JWTUtil .decodeJWT (jwt , key );
52- assert (decodedJwtAndKey .getValue0 ().equals ("{typ=JWT, alg=HS512}" ));
53- assert (decodedJwtAndKey .getValue1 ().equals ("{sub=JWT Encoder, aud=Hello JWT, iss=rrajesh1979}" ));
72+ assertEquals (decodedJwtAndKey .getValue0 (), "{typ=JWT, alg=HS512}" );
73+ assertEquals (decodedJwtAndKey .getValue1 (), "{sub=JWT Encoder, aud=Hello JWT, iss=rrajesh1979}" );
74+ }
75+
76+ @ Test
77+ @ DisplayName ("Test JWT decode with invalid key" )
78+ void encodeDecodeJWTInvalidKeyTest () {
79+ String userInput = "{}" ;
80+
81+ Pair <String , String > jwtAndKey = JWTUtil .createJWT ("JWT" , "HS512" , userInput ,
82+ "rrajesh1979" , "JWT Encoder" , "Hello JWT" , true , 0 );
83+
84+ String jwt = jwtAndKey .getValue0 ();
85+ String key = jwtAndKey .getValue1 ();
86+
87+ Pair <String , String > decodedJwtAndKey = JWTUtil .decodeJWT (jwt , key );
88+ assertEquals (decodedJwtAndKey .getValue0 (), "{typ=JWT, alg=HS512}" );
89+ assertNotEquals (decodedJwtAndKey .getValue1 (), "{sub=JWT Encoder, aud=Hello JWT, iss=rrajesh1979}" );
5490 }
5591}
0 commit comments