1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.principal.test;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.Mockito.mock;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
37 import java.io.IOException;
38 import java.lang.reflect.Field;
40 import org.onap.aaf.cadi.BasicCred;
41 import org.onap.aaf.cadi.CachedPrincipal;
42 import org.onap.aaf.cadi.principal.CachedBasicPrincipal;
43 import org.onap.aaf.cadi.taf.HttpTaf;
45 public class JU_CachedBasicPrincipal {
46 private Field creatorField;
47 private Field timeToLiveField;
50 private HttpTaf creator;
52 private CachedPrincipal.Resp resp;
55 public void setup() throws NoSuchFieldException, SecurityException {
56 MockitoAnnotations.initMocks(this);
58 creatorField = CachedBasicPrincipal.class.getDeclaredField("creator");
59 timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive");
61 creatorField.setAccessible(true);
62 timeToLiveField.setAccessible(true);
66 public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException {
68 String password = "password";
69 BasicCred bc = mock(BasicCred.class);
70 when(bc.getUser()).thenReturn(name);
71 when(bc.getCred()).thenReturn(password.getBytes());
73 long timeToLive = 10000L;
74 long expires = System.currentTimeMillis() + timeToLive;
75 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive);
77 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
78 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
79 assertTrue(Math.abs(cbp.expires() - expires) < 10);
83 public void Constructor2Test() throws Exception {
85 String password = "password";
86 String content = name + ":" + password;
87 long timeToLive = 10000L;
88 long expires = System.currentTimeMillis() + timeToLive;
89 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
91 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
92 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
93 assertTrue(Math.abs(cbp.expires() - expires) < 10);
97 public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException {
98 resp = CachedPrincipal.Resp.REVALIDATED;
99 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
101 String name = "User";
102 String password = "password";
103 String content = name + ":" + password;
104 long timeToLive = 10000L;
105 long expires = System.currentTimeMillis() + timeToLive;
106 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
108 assertTrue(Math.abs(cbp.expires() - expires) < 10);
111 expires = System.currentTimeMillis() + timeToLive;
112 assertThat(cbp.revalidate(new Object()), is(resp));
113 assertTrue(Math.abs(cbp.expires() - expires) < 10);
115 resp = CachedPrincipal.Resp.UNVALIDATED;
116 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
117 expires = System.currentTimeMillis() + timeToLive;
118 cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
120 assertThat(cbp.revalidate(new Object()), is(resp));
121 assertTrue(Math.abs(cbp.expires() - expires) < 10);