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.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;
32 import java.security.Principal;
33 import java.util.List;
35 import javax.servlet.http.HttpServletRequest;
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;
55 public class JU_CadiWrapTest {
58 private HttpServletRequest request;
61 private TafResp tafResp;
64 private TaggedPrincipal principle;
70 public void setUp() throws Exception {
71 MockitoAnnotations.initMocks(this);
75 public void testInstantiate() throws CadiException {
76 Access a = new PropAccess();
77 when(tafResp.getAccess()).thenReturn(a);
79 lur.fishAll(isA(Principal.class), isA(List.class));
81 EpiLur lur1 = new EpiLur(lur);
83 CadiWrap wrap = new CadiWrap(request, tafResp, lur1);
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);
91 byte[] arr = {'1','2'};
94 assertEquals(arr, wrap.getCred());
96 wrap.setUser("User1");
97 assertEquals("User1", wrap.getUser());
101 assertFalse(wrap.isUserInRole(null));
103 wrap.set(tafResp, lur);
105 wrap.invalidate("2");
107 wrap.isUserInRole("User1");
111 public void testInstantiateWithPermConverter() throws CadiException {
112 Access a = new PropAccess();
113 when(tafResp.getAccess()).thenReturn(a);
114 when(tafResp.getPrincipal()).thenReturn(principle);
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) { }
130 MapPermConverter pc = new MapPermConverter();
132 CadiWrap wrap = new CadiWrap(request, tafResp, lur1, pc);
134 assertNotNull(wrap.getUserPrincipal());
135 assertNull(wrap.getRemoteUser());
136 assertNull(wrap.getUser());
138 byte[] arr = {'1','2'};
141 assertEquals(arr, wrap.getCred());
143 wrap.setUser("User1");
144 assertEquals("User1", wrap.getUser());
146 wrap.invalidate("1");
147 wrap.setPermConverter(new MapPermConverter());
149 assertTrue(wrap.getLur() instanceof CachingLur);
150 assertTrue(wrap.isUserInRole("User1"));
152 wrap.set(tafResp, lur);
153 assertFalse(wrap.isUserInRole("Perm1"));