Fix for Sonar tests in POMs
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_CadiWrapTest.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.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Matchers.isA;
30 import static org.mockito.Mockito.when;
31
32 import java.security.Principal;
33 import java.util.List;
34
35 import javax.servlet.http.HttpServletRequest;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aaf.cadi.Access;
42 import org.onap.aaf.cadi.CachingLur;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.CadiWrap;
45 import org.onap.aaf.cadi.Lur;
46 import org.onap.aaf.cadi.Permission;
47 import org.onap.aaf.cadi.PropAccess;
48 import org.onap.aaf.cadi.User;
49 import org.onap.aaf.cadi.CachedPrincipal.Resp;
50 import org.onap.aaf.cadi.filter.MapPermConverter;
51 import org.onap.aaf.cadi.lur.EpiLur;
52 import org.onap.aaf.cadi.principal.TaggedPrincipal;
53 import org.onap.aaf.cadi.taf.TafResp;
54
55 public class JU_CadiWrapTest {
56         
57         @Mock
58         private HttpServletRequest request;
59         
60         @Mock
61         private TafResp tafResp;
62         
63         @Mock
64         private TaggedPrincipal principle;
65
66         @Mock
67         private Lur lur;
68
69         @Before
70         public void setUp() throws Exception {
71                 MockitoAnnotations.initMocks(this);
72         }
73
74         @Test
75         public void testInstantiate() throws CadiException {
76                 Access a = new PropAccess();
77                 when(tafResp.getAccess()).thenReturn(a);
78                 
79                 lur.fishAll(isA(Principal.class), isA(List.class));
80                 
81                 EpiLur lur1 = new EpiLur(lur);
82                 
83                 CadiWrap wrap = new CadiWrap(request, tafResp, lur1);
84                 
85                 assertNull(wrap.getUserPrincipal());
86                 assertNull(wrap.getRemoteUser());
87                 assertNull(wrap.getUser());
88                 assertEquals(wrap.getPermissions(principle).size(), 0);
89                 assertTrue(wrap.access() instanceof PropAccess);
90                 
91                 byte[] arr = {'1','2'};
92                 wrap.setCred(arr);
93                 
94                 assertEquals(arr, wrap.getCred());
95                 
96                 wrap.setUser("User1");
97                 assertEquals("User1", wrap.getUser());
98                 
99                 wrap.invalidate("1");
100
101                 assertFalse(wrap.isUserInRole(null));
102                 
103                 wrap.set(tafResp, lur);
104                 
105                 wrap.invalidate("2");
106                 
107                 wrap.isUserInRole("User1");
108         }
109
110         @Test
111         public void testInstantiateWithPermConverter() throws CadiException {
112                 Access a = new PropAccess();
113                 when(tafResp.getAccess()).thenReturn(a);
114                 when(tafResp.getPrincipal()).thenReturn(principle);
115                 
116                 // Anonymous object for testing purposes
117                 CachingLur<Permission> lur1 = new CachingLur<Permission>() {
118                         @Override public Permission createPerm(String p) { return null; }
119                         @Override public boolean fish(Principal bait, Permission pond) { return true; }
120                         @Override public void fishAll(Principal bait, List<Permission> permissions) { }
121                         @Override public void destroy() { }
122                         @Override public boolean handlesExclusively(Permission pond) { return false; }
123                         @Override public boolean handles(Principal principal) { return false; }
124                         @Override public void remove(String user) { }
125                         @Override public Resp reload(User<Permission> user) { return null; }
126                         @Override public void setDebug(String commaDelimIDsOrNull) { }
127                         @Override public void clear(Principal p, StringBuilder sb) { }
128                 };
129                 
130                 MapPermConverter pc = new MapPermConverter();
131                 
132                 CadiWrap wrap = new CadiWrap(request, tafResp, lur1, pc);
133                 
134                 assertNotNull(wrap.getUserPrincipal());
135                 assertNull(wrap.getRemoteUser());
136                 assertNull(wrap.getUser());
137                 
138                 byte[] arr = {'1','2'};
139                 wrap.setCred(arr);
140                 
141                 assertEquals(arr, wrap.getCred());
142                 
143                 wrap.setUser("User1");
144                 assertEquals("User1", wrap.getUser());
145                 
146                 wrap.invalidate("1");
147                 wrap.setPermConverter(new MapPermConverter());
148                 
149                 assertTrue(wrap.getLur() instanceof CachingLur);
150                 assertTrue(wrap.isUserInRole("User1"));
151                 
152                 wrap.set(tafResp, lur);
153                 assertFalse(wrap.isUserInRole("Perm1"));
154         }
155 }