20e1d4d9e263cf0bb658f01002933b454db67b0b
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / principal / test / JU_CachedBasicPrincipal.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22
23 package org.onap.aaf.cadi.principal.test;
24
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;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36
37 import java.io.IOException;
38 import java.lang.reflect.Field;
39
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;
44
45 public class JU_CachedBasicPrincipal {
46         private Field creatorField;
47         private Field timeToLiveField;
48
49         @Mock
50         private HttpTaf creator;
51
52         private CachedPrincipal.Resp resp;
53
54         @Before
55         public void setup() throws NoSuchFieldException, SecurityException {
56                 MockitoAnnotations.initMocks(this);
57
58                 creatorField = CachedBasicPrincipal.class.getDeclaredField("creator");
59                 timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive");
60
61                 creatorField.setAccessible(true);
62                 timeToLiveField.setAccessible(true);
63         }
64
65         @Test
66         public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException {
67                 String name = "User";
68                 String password = "password";
69                 BasicCred bc = mock(BasicCred.class);
70                 when(bc.getUser()).thenReturn(name);
71                 when(bc.getCred()).thenReturn(password.getBytes());
72
73                 long timeToLive = 10000L;
74                 long expires = System.currentTimeMillis() + timeToLive;
75                 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive);
76
77                 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
78                 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
79                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
80         }
81
82         @Test
83         public void Constructor2Test() throws Exception {
84                 String name = "User";
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);
90
91                 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
92                 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
93                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
94         }
95
96         @Test
97         public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException {
98                 resp = CachedPrincipal.Resp.REVALIDATED;
99                 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
100
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);
107
108                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
109
110                 Thread.sleep(1);
111                 expires = System.currentTimeMillis() + timeToLive;
112                 assertThat(cbp.revalidate(new Object()), is(resp));
113                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
114
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);
119
120                 assertThat(cbp.revalidate(new Object()), is(resp));
121                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
122         }
123
124 }