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 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static org.junit.Assert.*;
26 import org.junit.Test;
29 import static org.hamcrest.CoreMatchers.*;
30 import static org.mockito.Mockito.when;
32 import java.lang.reflect.Field;
33 import java.security.Principal;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
39 import org.junit.Before;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.aaf.cadi.Permission;
43 import org.onap.aaf.cadi.User;
44 import org.onap.aaf.cadi.lur.LocalPermission;
46 public class JU_User {
48 private final Long SECOND = 1000L;
49 private final String name = "Fakey McFake";
50 private final String cred = "Fake credentials";
52 private Field perms_field;
53 private Field count_field;
56 private Principal principal;
59 private LocalPermission permission;
61 private LocalPermission permission2;
64 public void setup() throws NoSuchFieldException, SecurityException {
65 MockitoAnnotations.initMocks(this);
67 when(principal.getName()).thenReturn("Principal");
69 when(permission.getKey()).thenReturn("NewKey");
70 when(permission.match(permission)).thenReturn(true);
72 when(permission2.getKey()).thenReturn("NewKey2");
73 when(permission2.match(permission)).thenReturn(false);
75 perms_field = User.class.getDeclaredField("perms");
76 perms_field.setAccessible(true);
78 count_field = User.class.getDeclaredField("count");
79 count_field.setAccessible(true);
83 public void constructorPrincipalTest() throws IllegalArgumentException, IllegalAccessException {
84 User<Permission> user = new User<Permission>(principal);
85 assertThat(user.name, is(principal.getName()));
86 assertThat(user.principal, is(principal));
87 assertThat(user.permExpires(), is(Long.MAX_VALUE));
88 assertThat((int)count_field.get(user), is(0));
92 public void constructorNameCredTest() throws IllegalArgumentException, IllegalAccessException {
93 User<Permission> user = new User<Permission>(name, cred.getBytes());
94 assertThat(user.name, is(name));
95 assertThat(user.principal, is(nullValue()));
96 assertThat(user.permExpires(), is(Long.MAX_VALUE));
97 assertThat((int)count_field.get(user), is(0));
98 assertThat(user.getCred(), is(cred.getBytes()));
102 public void constructorPrincipalIntervalTest() throws IllegalArgumentException, IllegalAccessException {
103 User<Permission> user = new User<Permission>(principal, 61 * SECOND);
104 Long approxExpiration = System.currentTimeMillis() + 61 * SECOND;
105 assertThat(user.name, is(principal.getName()));
106 assertThat(user.principal, is(principal));
107 assertTrue(Math.abs(user.permExpires() - approxExpiration) < 10L);
108 assertThat((int)count_field.get(user), is(0));
112 public void constructorNameCredIntervalTest() throws IllegalArgumentException, IllegalAccessException {
113 String name = "Fakey McFake";
114 User<Permission> user = new User<Permission>(name, cred.getBytes(), 61 * SECOND);
115 Long approxExpiration = System.currentTimeMillis() + 61 * SECOND;
116 assertThat(user.name, is(name));
117 assertThat(user.principal, is(nullValue()));
118 assertTrue(Math.abs(user.permExpires() - approxExpiration) < 10L);
119 assertThat((int)count_field.get(user), is(0));
120 assertThat(user.getCred(), is(cred.getBytes()));
124 public void countCheckTest() throws IllegalArgumentException, IllegalAccessException {
125 User<Permission> user = new User<Permission>(principal);
127 assertThat((int)count_field.get(user), is(0));
129 assertThat((int)count_field.get(user), is(1));
131 assertThat((int)count_field.get(user), is(2));
133 assertThat((int)count_field.get(user), is(0));
137 public void permTest() throws InterruptedException, IllegalArgumentException, IllegalAccessException {
138 User<Permission> user = new User<Permission>(principal);
139 assertThat(user.permExpires(), is(Long.MAX_VALUE));
141 Thread.sleep(1); // Let it expire
142 assertThat(user.permExpired(), is(true));
144 user = new User<Permission>(principal,100);
145 assertTrue(user.noPerms());
146 user.add(permission);
147 assertFalse(user.permsUnloaded());
148 assertFalse(user.noPerms());
150 assertThat(user.permExpired(), is(false));
151 assertTrue(user.permsUnloaded());
152 assertTrue(user.noPerms());
153 perms_field.set(user, null);
154 assertTrue(user.permsUnloaded());
155 assertTrue(user.noPerms());
159 public void addValuesToNewMapTest() {
160 User<Permission> user = new User<Permission>(principal);
161 Map<String, Permission> newMap = new HashMap<String,Permission>();
163 assertFalse(user.contains(permission));
165 user.add(newMap, permission);
168 assertTrue(user.contains(permission));
170 List<Permission> sink = new ArrayList<Permission>();
171 user.copyPermsTo(sink);
173 assertThat(sink.size(), is(1));
174 assertTrue(sink.contains(permission));
176 assertThat(user.toString(), is("Principal|:NewKey"));
178 user.add(newMap, permission2);
180 assertFalse(user.contains(permission2));
182 assertThat(user.toString(), is("Principal|:NewKey2,NewKey"));