Collection syntax change because of Sonar
[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 package org.onap.aaf.cadi.test;
23
24 import static org.junit.Assert.*;
25
26 import org.junit.Test;
27
28
29 import static org.hamcrest.CoreMatchers.*;
30 import static org.mockito.Mockito.when;
31
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;
37 import java.util.Map;
38
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;
45
46 public class JU_User {
47
48         private final Long SECOND = 1000L;
49         private final String name = "Fakey McFake";
50         private final String cred = "Fake credentials";
51
52         private Field perms_field;
53         private Field count_field;
54
55         @Mock
56         private Principal principal;
57         
58         @Mock
59         private LocalPermission permission;
60         @Mock
61         private LocalPermission permission2;
62         
63         @Before
64         public void setup() throws NoSuchFieldException, SecurityException {
65                 MockitoAnnotations.initMocks(this);
66                 
67                 when(principal.getName()).thenReturn("Principal");
68
69                 when(permission.getKey()).thenReturn("NewKey");
70                 when(permission.match(permission)).thenReturn(true);
71
72                 when(permission2.getKey()).thenReturn("NewKey2");
73                 when(permission2.match(permission)).thenReturn(false);
74
75                 perms_field = User.class.getDeclaredField("perms");
76                 perms_field.setAccessible(true);
77
78                 count_field = User.class.getDeclaredField("count");
79                 count_field.setAccessible(true);
80         }
81
82         @Test
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));
89         }
90
91         @Test
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()));
99         }
100         
101         @Test
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));
109         }
110
111         @Test
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()));
121         }
122
123         @Test
124         public void countCheckTest() throws IllegalArgumentException, IllegalAccessException {
125                 User<Permission> user = new User<Permission>(principal);
126                 user.resetCount();
127                 assertThat((int)count_field.get(user), is(0));
128                 user.incCount();
129                 assertThat((int)count_field.get(user), is(1));
130                 user.incCount();
131                 assertThat((int)count_field.get(user), is(2));
132                 user.resetCount();
133                 assertThat((int)count_field.get(user), is(0));
134         }
135         
136         @Test
137         public void permTest() throws InterruptedException, IllegalArgumentException, IllegalAccessException {
138                 User<Permission> user = new User<Permission>(principal);
139                 assertThat(user.permExpires(), is(Long.MAX_VALUE));
140                 user.renewPerm();
141                 Thread.sleep(1);  // Let it expire
142                 assertThat(user.permExpired(), is(true));
143
144                 user = new User<Permission>(principal,100);
145                 assertTrue(user.noPerms());
146                 user.add(permission);
147                 assertFalse(user.permsUnloaded());
148                 assertFalse(user.noPerms());
149                 user.setNoPerms();
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());
156         }
157         
158         @Test
159         public void addValuesToNewMapTest() {
160                 User<Permission> user = new User<Permission>(principal);
161                 Map<String, Permission> newMap = new HashMap<>();
162                 
163                 assertFalse(user.contains(permission));
164                 
165                 user.add(newMap, permission);
166                 user.setMap(newMap);
167                 
168                 assertTrue(user.contains(permission));
169                 
170                 List<Permission> sink = new ArrayList<>();
171                 user.copyPermsTo(sink);
172                 
173                 assertThat(sink.size(), is(1));
174                 assertTrue(sink.contains(permission));
175                 
176                 assertThat(user.toString(), is("Principal|:NewKey"));
177
178                 user.add(newMap, permission2);
179                 user.setMap(newMap);
180                 assertFalse(user.contains(permission2));
181                 
182                 assertThat(user.toString(), is("Principal|:NewKey2,NewKey"));
183         }
184         
185 }