3df12f80e8c52b81885d89f48abb9b8c9c51bfa3
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_User.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.test;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.Test;
28
29
30 import static org.hamcrest.CoreMatchers.*;
31 import static org.mockito.Mockito.when;
32
33 import java.lang.reflect.Field;
34 import java.security.Principal;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import org.junit.Before;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.aaf.cadi.Permission;
44 import org.onap.aaf.cadi.User;
45 import org.onap.aaf.cadi.lur.LocalPermission;
46
47 public class JU_User {
48
49     private final Long SECOND = 1000L;
50     private final String name = "Fakey McFake";
51     private final String cred = "Fake credentials";
52
53     private Field perms_field;
54     private Field count_field;
55
56     @Mock
57     private Principal principal;
58     
59     @Mock
60     private LocalPermission permission;
61     @Mock
62     private LocalPermission permission2;
63     
64     @Before
65     public void setup() throws NoSuchFieldException, SecurityException {
66         MockitoAnnotations.initMocks(this);
67         
68         when(principal.getName()).thenReturn("Principal");
69
70         when(permission.getKey()).thenReturn("NewKey");
71         when(permission.match(permission)).thenReturn(true);
72
73         when(permission2.getKey()).thenReturn("NewKey2");
74         when(permission2.match(permission)).thenReturn(false);
75
76         perms_field = User.class.getDeclaredField("perms");
77         perms_field.setAccessible(true);
78
79         count_field = User.class.getDeclaredField("count");
80         count_field.setAccessible(true);
81     }
82
83     @Test
84     public void constructorPrincipalTest() throws IllegalArgumentException, IllegalAccessException {
85         User<Permission> user = new User<Permission>(principal);
86         assertThat(user.name, is(principal.getName()));
87         assertThat(user.principal, is(principal));
88         assertThat(user.permExpires(), is(Long.MAX_VALUE));
89         assertThat((int)count_field.get(user), is(0));
90     }
91
92     @Test
93     public void constructorNameCredTest() throws IllegalArgumentException, IllegalAccessException {
94         User<Permission> user = new User<Permission>(name, cred.getBytes());
95         assertThat(user.name, is(name));
96         assertThat(user.principal, is(nullValue()));
97         assertThat(user.permExpires(), is(Long.MAX_VALUE));
98         assertThat((int)count_field.get(user), is(0));
99         assertThat(user.getCred(), is(cred.getBytes()));
100     }
101     
102     @Test
103     public void constructorPrincipalIntervalTest() throws IllegalArgumentException, IllegalAccessException {
104         User<Permission> user = new User<Permission>(principal, 61 * SECOND);
105         Long approxExpiration = System.currentTimeMillis() + 61 * SECOND;
106         assertThat(user.name, is(principal.getName()));
107         assertThat(user.principal, is(principal));
108         assertTrue(Math.abs(user.permExpires() - approxExpiration) < 10L);
109         assertThat((int)count_field.get(user), is(0));
110     }
111
112     @Test
113     public void constructorNameCredIntervalTest() throws IllegalArgumentException, IllegalAccessException {
114         String name = "Fakey McFake";
115         User<Permission> user = new User<Permission>(name, cred.getBytes(), 61 * SECOND);
116         Long approxExpiration = System.currentTimeMillis() + 61 * SECOND;
117         assertThat(user.name, is(name));
118         assertThat(user.principal, is(nullValue()));
119         assertTrue(Math.abs(user.permExpires() - approxExpiration) < 10L);
120         assertThat((int)count_field.get(user), is(0));
121         assertThat(user.getCred(), is(cred.getBytes()));
122     }
123
124     @Test
125     public void countCheckTest() throws IllegalArgumentException, IllegalAccessException {
126         User<Permission> user = new User<Permission>(principal);
127         user.resetCount();
128         assertThat((int)count_field.get(user), is(0));
129         user.incCount();
130         assertThat((int)count_field.get(user), is(1));
131         user.incCount();
132         assertThat((int)count_field.get(user), is(2));
133         user.resetCount();
134         assertThat((int)count_field.get(user), is(0));
135     }
136     
137     @Test
138     public void permTest() throws InterruptedException, IllegalArgumentException, IllegalAccessException {
139         User<Permission> user = new User<Permission>(principal);
140         assertThat(user.permExpires(), is(Long.MAX_VALUE));
141         user.renewPerm();
142         Thread.sleep(1);  // Let it expire
143         assertThat(user.permExpired(), is(true));
144
145         user = new User<Permission>(principal,100);
146         assertTrue(user.noPerms());
147         user.add(permission);
148         assertFalse(user.permsUnloaded());
149         assertFalse(user.noPerms());
150         user.setNoPerms();
151         assertThat(user.permExpired(), is(false));
152         assertTrue(user.permsUnloaded());
153         assertTrue(user.noPerms());
154         perms_field.set(user, null);
155         assertTrue(user.permsUnloaded());
156         assertTrue(user.noPerms());
157     }
158     
159     @Test
160     public void addValuesToNewMapTest() {
161         User<Permission> user = new User<Permission>(principal);
162         Map<String, Permission> newMap = new HashMap<>();
163         
164         assertFalse(user.contains(permission));
165         
166         user.add(newMap, permission);
167         user.setMap(newMap);
168         
169         assertTrue(user.contains(permission));
170         
171         List<Permission> sink = new ArrayList<>();
172         user.copyPermsTo(sink);
173         
174         assertThat(sink.size(), is(1));
175         assertTrue(sink.contains(permission));
176         
177         assertThat(user.toString(), is("Principal|:NewKey"));
178
179         user.add(newMap, permission2);
180         user.setMap(newMap);
181         assertFalse(user.contains(permission2));
182         
183         assertThat(user.toString(), is("Principal|:NewKey2,NewKey"));
184     }
185     
186 }