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.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
32 import java.io.IOException;
33 import java.lang.reflect.Field;
34 import java.util.Date;
36 import org.onap.aaf.cadi.BasicCred;
37 import org.onap.aaf.cadi.CachedPrincipal;
38 import org.onap.aaf.cadi.CachedPrincipal.Resp;
39 import org.onap.aaf.cadi.Symm;
40 import org.onap.aaf.cadi.principal.CachedBasicPrincipal;
41 import org.onap.aaf.cadi.principal.StringTagLookup;
42 import org.onap.aaf.cadi.principal.TaggedPrincipal;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup;
44 import org.onap.aaf.cadi.taf.HttpTaf;
46 public class JU_CachedBasicPrincipal {
47 private Field creatorField;
48 private Field timeToLiveField;
51 private HttpTaf creator;
53 private CachedPrincipal.Resp resp;
56 public void setup() throws NoSuchFieldException, SecurityException {
57 MockitoAnnotations.initMocks(this);
59 creatorField = CachedBasicPrincipal.class.getDeclaredField("creator");
60 timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive");
62 creatorField.setAccessible(true);
63 timeToLiveField.setAccessible(true);
67 public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException {
69 String password = "password";
70 BasicCred bc = mock(BasicCred.class);
71 when(bc.getUser()).thenReturn(name);
72 when(bc.getCred()).thenReturn(password.getBytes());
74 long timeToLive = 10000L;
75 long expires = System.currentTimeMillis() + timeToLive;
76 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive);
78 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
79 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
80 assertTrue(Math.abs(cbp.expires() - expires) < 10);
84 public void Constructor2Test() throws Exception {
86 String password = "password";
87 String content = name + ":" + password;
88 long timeToLive = 10000L;
89 long expires = System.currentTimeMillis() + timeToLive;
90 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
92 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
93 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
94 assertTrue(Math.abs(cbp.expires() - expires) < 10);
98 public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException {
99 resp = CachedPrincipal.Resp.REVALIDATED;
100 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
102 String name = "User";
103 String password = "password";
104 String content = name + ":" + password;
105 long timeToLive = 10000L;
106 long expires = System.currentTimeMillis() + timeToLive;
107 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
109 assertTrue(Math.abs(cbp.expires() - expires) < 10);
112 expires = System.currentTimeMillis() + timeToLive;
113 assertThat(cbp.revalidate(new Object()), is(resp));
114 assertTrue(Math.abs(cbp.expires() - expires) < 10);
116 resp = CachedPrincipal.Resp.UNVALIDATED;
117 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
118 expires = System.currentTimeMillis() + timeToLive;
119 cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive);
121 assertThat(cbp.revalidate(new Object()), is(resp));
122 assertTrue(Math.abs(cbp.expires() - expires) < 10);