1010
1111
1212public class KeyParserTests {
13+ private static final int DEFAULT_TOKEN_EXPIRY_SECONDS = 2592000 ;
14+
1315 @ Test
14- public void parseKeyListSharingEndpoint () throws Exception
15- {
16+ public void parseKeyListSharingEndpoint () {
1617 String s = "{ \" body\" : { " +
1718 "\" caller_site_id\" : 11, " +
1819 "\" master_keyset_id\" : 1, " +
@@ -42,32 +43,32 @@ public void parseKeyListSharingEndpoint() throws Exception
4243 KeyContainer keyContainer = parse (s );
4344
4445 assertEquals (11 , keyContainer .getCallerSiteId ());
46+ assertEquals (1728000 , keyContainer .getTokenExpirySeconds ());
47+
4548 Key masterKey = keyContainer .getMasterKey (Instant .now ());
4649 assertNotNull (masterKey );
4750 assertEquals (2 , masterKey .getId ());
4851
4952 Key defaultKey = keyContainer .getDefaultKey (Instant .now ());
5053 assertNotNull (defaultKey );
5154 assertEquals (3 , defaultKey .getId ());
52- assertEquals (1728000 , keyContainer .getTokenExpirySeconds ());
5355
54- Key key = keyContainer .getKey (3 );
55- assertNotNull (key );
56- assertEquals (99999 , key .getKeysetId ());
57- assertEquals (Instant .ofEpochSecond (1609459200 ), key .getCreated ());
58- assertEquals (Instant .ofEpochSecond (1609459210 ), key .getActivates ());
59- assertEquals (Instant .ofEpochSecond (1893456000 ), key .getExpires ());
60- assertEquals ("o8HsvkwJ5Ulnrd0uui3GpukpwDapj+JLqb7qfN/GJKo=" , InputUtil .byteArrayToBase64 (key .getSecret ()));
61-
62- key = keyContainer .getKey (2 );
63- assertNotNull (key );
64- assertEquals (1 , key .getKeysetId ());
65- assertEquals (Instant .ofEpochSecond (1609458200 ), key .getCreated ());
66- assertEquals (Instant .ofEpochSecond (1609459220 ), key .getActivates ());
67- assertEquals (Instant .ofEpochSecond (1893457000 ), key .getExpires ());
68- assertEquals ("DD67xF8OFmbJ1/lMPQ6fGRDbJOT4kXErrYWcKdFfCUE=" , InputUtil .byteArrayToBase64 (key .getSecret ()));
69- }
56+ Key key3 = keyContainer .getKey (3 );
57+ assertNotNull (key3 );
58+ assertEquals (99999 , key3 .getKeysetId ());
59+ assertEquals (Instant .ofEpochSecond (1609459200 ), key3 .getCreated ());
60+ assertEquals (Instant .ofEpochSecond (1609459210 ), key3 .getActivates ());
61+ assertEquals (Instant .ofEpochSecond (1893456000 ), key3 .getExpires ());
62+ assertEquals ("o8HsvkwJ5Ulnrd0uui3GpukpwDapj+JLqb7qfN/GJKo=" , InputUtil .byteArrayToBase64 (key3 .getSecret ()));
7063
64+ Key key2 = keyContainer .getKey (2 );
65+ assertNotNull (key2 );
66+ assertEquals (1 , key2 .getKeysetId ());
67+ assertEquals (Instant .ofEpochSecond (1609458200 ), key2 .getCreated ());
68+ assertEquals (Instant .ofEpochSecond (1609459220 ), key2 .getActivates ());
69+ assertEquals (Instant .ofEpochSecond (1893457000 ), key2 .getExpires ());
70+ assertEquals ("DD67xF8OFmbJ1/lMPQ6fGRDbJOT4kXErrYWcKdFfCUE=" , InputUtil .byteArrayToBase64 (key2 .getSecret ()));
71+ }
7172
7273 @ Test
7374 public void parseErrorKeyList () {
@@ -79,6 +80,33 @@ public void parseErrorKeyList() {
7980 assertThrows (Exception .class , () -> parse ("{\" body\" : [{\" id\" : 5}]}" ));
8081 }
8182
83+ @ Test
84+ public void parseWithNullTokenExpirySecondField () {
85+ String s = "{ \" body\" : { " +
86+ "\" caller_site_id\" : 11, " +
87+ "\" token_expiry_seconds\" : null " +
88+ "}, " +
89+ "\" status\" : \" success\" }" ;
90+
91+ KeyContainer keyContainer = parse (s );
92+
93+ assertEquals (11 , keyContainer .getCallerSiteId ());
94+ assertEquals (DEFAULT_TOKEN_EXPIRY_SECONDS , keyContainer .getTokenExpirySeconds ());
95+ }
96+
97+ @ Test
98+ public void parseWithMissingTokenExpirySecondField () {
99+ String s = "{ \" body\" : { " +
100+ "\" caller_site_id\" : 11 " +
101+ "}, " +
102+ "\" status\" : \" success\" }" ;
103+
104+ KeyContainer keyContainer = parse (s );
105+
106+ assertEquals (11 , keyContainer .getCallerSiteId ());
107+ assertEquals (DEFAULT_TOKEN_EXPIRY_SECONDS , keyContainer .getTokenExpirySeconds ());
108+ }
109+
82110 private KeyContainer parse (String str ) {
83111 InputStream inputStream = new ByteArrayInputStream (str .getBytes (StandardCharsets .UTF_8 ));
84112 return KeyParser .parse (inputStream );
0 commit comments