2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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 package org.onap.aaf.auth.dao.cached;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.onap.aaf.auth.dao.CIDAO;
35 import org.onap.aaf.auth.dao.cass.PermDAO;
36 import org.onap.aaf.auth.dao.cass.RoleDAO;
37 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
38 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.env.AuthzTransImpl;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal;
44 import org.onap.aaf.misc.env.LogTarget;
45 import org.onap.aaf.misc.env.Slot;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 @RunWith(PowerMockRunner.class)
49 public class JU_CachedUserRoleDAO {
55 CIDAO<AuthzTrans> info;
64 PermDAO.Data permData;
67 public void setUp() throws Exception {
69 when(trans.debug()).thenReturn(new LogTarget() {
72 public void printf(String fmt, Object... vars) {}
75 public void log(Throwable e, Object... msgs) {
83 public void log(Object... msgs) {
87 public boolean isLoggable() {
94 private class TaggedPrincipalStub extends TaggedPrincipal {
95 String name="TaggedPrincipalStub";
96 public TaggedPrincipalStub() { super(); }
97 public TaggedPrincipalStub(final TagLookup tl) { super(tl); }
98 @Override public String getName() { return name; }
99 @Override public String tag() { return null; }
103 public void testReadName() {
104 CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L);
105 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test4",new String[0]);
106 Mockito.doReturn(retVal1).when(dao).readByUser(trans, "test4");
107 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
108 Result<List<Data>> retVal = roleDaoObj.readByUser(trans, "test4");
109 //System.out.println(retVal.status);
111 assertEquals("25", Integer.toString(retVal.status));
115 public void testReadNameUser() {
116 CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L);
117 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"TaggedPrincipalStub",new String[0]);
118 AuthzEnv env = Mockito.mock(AuthzEnv.class);
119 AuthzTransImpl transTemp = new AuthzTransImpl(env) {
121 public<T> T get(Slot slot, T deflt) {
127 transTemp.setUser(new TaggedPrincipalStub());
128 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
129 Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub");
130 roleDaoObj.invalidate("TaggedPrincipalStub");
131 Result<List<Data>> retVal = roleDaoObj.readByUser(transTemp, "TaggedPrincipalStub");
132 // System.out.println(retVal.status);
133 assertEquals("1", Integer.toString(retVal.status));
137 public void testReadByRoleSuccess() {
138 CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);//
139 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]);
140 Mockito.doReturn(retVal1).when(dao).readByRole(trans, "");
141 roleDaoObj.invalidate("");
142 Result<List<Data>> retVal = roleDaoObj.readByRole(trans, "");
143 //System.out.println(retVal.status);
144 assertEquals("1", Integer.toString(retVal.status));
147 public void testReadByRoleFailure() {
148 CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);//
149 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
150 Mockito.doReturn(retVal1).when(dao).readByRole(trans, "");
151 roleDaoObj.invalidate("");
152 Result<List<Data>> retVal = roleDaoObj.readByRole(trans, "");
153 //System.out.println(retVal.status);
154 assertEquals("25", Integer.toString(retVal.status));
158 public void testReadUserInRole() {
159 CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
160 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"TaggedPrincipalStub",new String[0]);
161 AuthzEnv env = Mockito.mock(AuthzEnv.class);
162 AuthzTransImpl transTemp = new AuthzTransImpl(env) {
164 public<T> T get(Slot slot, T deflt) {
170 transTemp.setUser(new TaggedPrincipalStub());
171 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
172 Mockito.doReturn(retVal1).when(dao).readByUserRole(transTemp, "","");
173 Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub");
174 Result<List<Data>> retVal = roleDaoObj.readUserInRole(transTemp, "TaggedPrincipalStub","");
175 //System.out.println(retVal.status);
176 assertEquals("25", Integer.toString(retVal.status));