Add Notice of aaf/cadi source moving to aaf/authz
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / JU_CadiWrapTest.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi;\r
24 \r
25 import static org.junit.Assert.*;\r
26 \r
27 import org.junit.Test;\r
28 \r
29 import static org.junit.Assert.*;\r
30 import static org.mockito.Matchers.isA;\r
31 import static org.mockito.Mockito.when;\r
32 \r
33 import java.security.Principal;\r
34 import java.util.List;\r
35 \r
36 import javax.servlet.http.HttpServletRequest;\r
37 \r
38 import org.junit.Before;\r
39 import org.junit.Test;\r
40 import org.mockito.Mock;\r
41 import org.mockito.Mockito;\r
42 import org.mockito.MockitoAnnotations;\r
43 import org.onap.aaf.cadi.CachedPrincipal.Resp;\r
44 import org.onap.aaf.cadi.filter.MapPermConverter;\r
45 import org.onap.aaf.cadi.lur.EpiLur;\r
46 import org.onap.aaf.cadi.taf.TafResp;\r
47 \r
48 public class JU_CadiWrapTest {\r
49         \r
50         @Mock\r
51         private HttpServletRequest request;\r
52         \r
53         @Mock\r
54         private TafResp tafResp;\r
55         \r
56         @Mock\r
57         private Principal principle;\r
58 \r
59         @Mock\r
60         private Lur lur;\r
61 \r
62         @Before\r
63         public void setUp() throws Exception {\r
64                 MockitoAnnotations.initMocks(this);\r
65         }\r
66 \r
67         @Test\r
68         public void testInstantiate() throws CadiException {\r
69                 Access a = new PropAccess();\r
70                 when(tafResp.getAccess()).thenReturn(a);\r
71                 \r
72                 lur.fishAll(isA(Principal.class), isA(List.class));\r
73                 \r
74                 EpiLur lur1 = new EpiLur(lur);\r
75                 \r
76                 CadiWrap wrap = new CadiWrap(request, tafResp, lur1);\r
77                 \r
78                 assertNull(wrap.getUserPrincipal());\r
79                 assertNull(wrap.getRemoteUser());\r
80                 assertNull(wrap.getUser());\r
81                 assertEquals(wrap.getPermissions(principle).size(), 0);\r
82                 \r
83                 byte[] arr = {'1','2'};\r
84                 wrap.setCred(arr);\r
85                 \r
86                 assertEquals(arr, wrap.getCred());\r
87                 \r
88                 wrap.setUser("User1");\r
89                 assertEquals("User1", wrap.getUser());\r
90                 \r
91                 wrap.invalidate("1");\r
92 \r
93                 assertFalse(wrap.isUserInRole(null));\r
94                 \r
95                 wrap.set(tafResp, lur);\r
96                 \r
97                 wrap.invalidate("2");\r
98                 \r
99                 wrap.isUserInRole("User1");\r
100         }\r
101 \r
102         @Test\r
103         public void testInstantiateWithPermConverter() throws CadiException {\r
104                 Access a = new PropAccess();\r
105                 when(tafResp.getAccess()).thenReturn(a);\r
106                 when(tafResp.getPrincipal()).thenReturn(principle);\r
107                 \r
108                 \r
109                 CachingLur<Permission> lur1 = new CachingLur<Permission>() {\r
110 \r
111                         @Override\r
112                         public Permission createPerm(String p) {\r
113                                 // TODO Auto-generated method stub\r
114                                 return null;\r
115                         }\r
116 \r
117                         @Override\r
118                         public boolean fish(Principal bait, Permission pond) {\r
119                                 // TODO Auto-generated method stub\r
120                                 return true;\r
121                         }\r
122 \r
123                         @Override\r
124                         public void fishAll(Principal bait, List<Permission> permissions) {\r
125                                 // TODO Auto-generated method stub\r
126                                 \r
127                         }\r
128 \r
129                         @Override\r
130                         public void destroy() {\r
131                                 // TODO Auto-generated method stub\r
132                                 \r
133                         }\r
134 \r
135                         @Override\r
136                         public boolean handlesExclusively(Permission pond) {\r
137                                 // TODO Auto-generated method stub\r
138                                 return false;\r
139                         }\r
140 \r
141                         @Override\r
142                         public boolean supports(String userName) {\r
143                                 // TODO Auto-generated method stub\r
144                                 return false;\r
145                         }\r
146 \r
147                         @Override\r
148                         public void remove(String user) {\r
149                                 // TODO Auto-generated method stub\r
150                                 \r
151                         }\r
152 \r
153                         @Override\r
154                         public Resp reload(User<Permission> user) {\r
155                                 // TODO Auto-generated method stub\r
156                                 return null;\r
157                         }\r
158 \r
159                         @Override\r
160                         public void setDebug(String commaDelimIDsOrNull) {\r
161                                 // TODO Auto-generated method stub\r
162                                 \r
163                         }\r
164 \r
165                         @Override\r
166                         public void clear(Principal p, StringBuilder sb) {\r
167                                 // TODO Auto-generated method stub\r
168                                 \r
169                         }\r
170                 };\r
171                 \r
172                 MapPermConverter pc = new MapPermConverter();\r
173                 \r
174                 CadiWrap wrap = new CadiWrap(request, tafResp, lur1, pc);\r
175                 \r
176                 assertNotNull(wrap.getUserPrincipal());\r
177                 assertNull(wrap.getRemoteUser());\r
178                 assertNull(wrap.getUser());\r
179                 \r
180                 byte[] arr = {'1','2'};\r
181                 wrap.setCred(arr);\r
182                 \r
183                 assertEquals(arr, wrap.getCred());\r
184                 \r
185                 wrap.setUser("User1");\r
186                 assertEquals("User1", wrap.getUser());\r
187                 \r
188                 wrap.invalidate("1");\r
189                 wrap.setPermConverter(new MapPermConverter());\r
190                 \r
191                 assertTrue(wrap.getLur() instanceof CachingLur);\r
192                 assertTrue(wrap.isUserInRole("User1"));\r
193                 \r
194                 wrap.set(tafResp, lur);\r
195                 assertFalse(wrap.isUserInRole("Perm1"));\r
196         }\r
197 }\r