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.test;
25 import static org.junit.Assert.*;
27 import org.junit.Test;
30 import static org.hamcrest.CoreMatchers.*;
31 import static org.mockito.Mockito.when;
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;
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;
47 public class JU_User {
49 private final Long SECOND = 1000L;
50 private final String name = "Fakey McFake";
51 private final String cred = "Fake credentials";
53 private Field perms_field;
54 private Field count_field;
57 private Principal principal;
60 private LocalPermission permission;
62 private LocalPermission permission2;
65 public void setup() throws NoSuchFieldException, SecurityException {
66 MockitoAnnotations.initMocks(this);
68 when(principal.getName()).thenReturn("Principal");
70 when(permission.getKey()).thenReturn("NewKey");
71 when(permission.match(permission)).thenReturn(true);
73 when(permission2.getKey()).thenReturn("NewKey2");
74 when(permission2.match(permission)).thenReturn(false);
76 perms_field = User.class.getDeclaredField("perms");
77 perms_field.setAccessible(true);
79 count_field = User.class.getDeclaredField("count");
80 count_field.setAccessible(true);
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));
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()));
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));
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()));
125 public void countCheckTest() throws IllegalArgumentException, IllegalAccessException {
126 User<Permission> user = new User<Permission>(principal);
128 assertThat((int)count_field.get(user), is(0));
130 assertThat((int)count_field.get(user), is(1));
132 assertThat((int)count_field.get(user), is(2));
134 assertThat((int)count_field.get(user), is(0));
138 public void permTest() throws InterruptedException, IllegalArgumentException, IllegalAccessException {
139 User<Permission> user = new User<Permission>(principal);
140 assertThat(user.permExpires(), is(Long.MAX_VALUE));
142 Thread.sleep(1); // Let it expire
143 assertThat(user.permExpired(), is(true));
145 user = new User<Permission>(principal,100);
146 assertTrue(user.noPerms());
147 user.add(permission);
148 assertFalse(user.permsUnloaded());
149 assertFalse(user.noPerms());
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());
160 public void addValuesToNewMapTest() {
161 User<Permission> user = new User<Permission>(principal);
162 Map<String, Permission> newMap = new HashMap<>();
164 assertFalse(user.contains(permission));
166 user.add(newMap, permission);
169 assertTrue(user.contains(permission));
171 List<Permission> sink = new ArrayList<>();
172 user.copyPermsTo(sink);
174 assertThat(sink.size(), is(1));
175 assertTrue(sink.contains(permission));
177 assertThat(user.toString(), is("Principal|:NewKey"));
179 user.add(newMap, permission2);
181 assertFalse(user.contains(permission2));
183 assertThat(user.toString(), is("Principal|:NewKey2,NewKey"));