Cleanup JUnits
[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.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.mockito.Mockito.*;
28 import org.junit.*;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31
32 import java.io.IOException;
33 import java.lang.reflect.Field;
34 import java.util.Date;
35
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;
45
46 public class JU_CachedBasicPrincipal {
47         private Field creatorField;
48         private Field timeToLiveField;
49
50         @Mock
51         private HttpTaf creator;
52
53         private CachedPrincipal.Resp resp;
54
55         @Before
56         public void setup() throws NoSuchFieldException, SecurityException {
57                 MockitoAnnotations.initMocks(this);
58
59                 creatorField = CachedBasicPrincipal.class.getDeclaredField("creator");
60                 timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive");
61
62                 creatorField.setAccessible(true);
63                 timeToLiveField.setAccessible(true);
64         }
65
66         @Test
67         public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException {
68                 String name = "User";
69                 String password = "password";
70                 BasicCred bc = mock(BasicCred.class);
71                 when(bc.getUser()).thenReturn(name);
72                 when(bc.getCred()).thenReturn(password.getBytes());
73
74                 long timeToLive = 10000L;
75                 long expires = System.currentTimeMillis() + timeToLive;
76                 CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive);
77
78                 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
79                 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
80                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
81         }
82
83         @Test
84         public void Constructor2Test() throws Exception {
85                 String name = "User";
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);
91
92                 assertThat((HttpTaf)creatorField.get(cbp), is(creator));
93                 assertThat((Long)timeToLiveField.get(cbp), is(timeToLive));
94                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
95         }
96
97         @Test
98         public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException {
99                 resp = CachedPrincipal.Resp.REVALIDATED;
100                 when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp);
101
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);
108
109                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
110
111                 Thread.sleep(1);
112                 expires = System.currentTimeMillis() + timeToLive;
113                 assertThat(cbp.revalidate(new Object()), is(resp));
114                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
115
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);
120
121                 assertThat(cbp.revalidate(new Object()), is(resp));
122                 assertTrue(Math.abs(cbp.expires() - expires) < 10);
123         }
124
125 }