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.verify;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
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.PermDAO.Data;
37 import org.onap.aaf.auth.dao.cass.RoleDAO;
38 import org.onap.aaf.auth.dao.cass.Status;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.env.AuthzTransImpl;
41 import org.onap.aaf.auth.layer.Result;
42 import org.onap.aaf.misc.env.LogTarget;
44 public class JU_CachedPermDAOTest {
47 private CIDAO<AuthzTrans> info;
55 private PermDAO.Data perm;
58 private AuthzTrans trans;
60 private Result<List<PermDAO.Data>> value;
63 public void setUp() throws Exception {
66 when(dao.readNS(trans, "ns")).thenReturn(value);
67 when(trans.debug()).thenReturn(new LogTarget() {
70 public void printf(String fmt, Object... vars) {}
73 public void log(Throwable e, Object... msgs) {
81 public void log(Object... msgs) {
85 public boolean isLoggable() {
93 public void testReadNS() {
94 when(value.isOKhasData()).thenReturn(true);
95 when(value.isOK()).thenReturn(false);
96 CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l);
98 Result<List<Data>> result = ccDao.readNS(trans, "ns");
100 assertEquals(result, value);
102 when(value.isOKhasData()).thenReturn(false);
104 result = ccDao.readNS(trans, "ns");
106 assertEquals(result.status, Status.ERR_PermissionNotFound);
108 ccDao.readChildren(trans, "ns", "type");
110 verify(dao).readChildren(trans, "ns", "type");
114 public void testReadByTypeSuccess() {
115 CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
116 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test4",new String[0]);
117 Mockito.doReturn(retVal1).when(dao).readByType(trans, "test4","");
118 Result<List<Data>> retVal = roleDaoObj.readByType(trans, "test4","");
119 // System.out.println(retVal.status);
121 assertEquals("1", Integer.toString(retVal.status));
125 public void testReadByTypeFailure() {
126 CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
127 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]);
128 Mockito.doReturn(retVal1).when(dao).readByType(trans, "test3","");
129 Result<List<Data>> retVal = roleDaoObj.readByType(trans, "test3","");
130 //System.out.println(retVal.status);
131 assertEquals("23", Integer.toString(retVal.status));
135 public void testAddRole() {
136 CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);
137 Result<Void> retVal1 = new Result<Void>(null,0,"testAddRole",new String[0]);
138 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
139 Mockito.doReturn(retVal1).when(dao).addRole(trans, perm,null);
140 Result<Void> retVal = roleDaoObj.addRole(trans, perm, role);
141 // System.out.println("ret value is::"+retVal);
142 assertEquals("testAddRole", retVal.toString());
146 public void testDelRole() {
147 CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);
148 Result<Void> retVal1 = new Result<Void>(null,0,"testAddRole",new String[0]);
149 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
150 Mockito.doReturn(retVal1).when(dao).delRole(trans, perm,null);
151 Result<Void> retVal = roleDaoObj.delRole(trans, perm, role);
152 // System.out.println(retVal);
153 assertEquals("testAddRole", retVal.toString());
157 public void testAddDescription() {
158 CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
159 Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]);
160 Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","","","");
161 Result<Void> retVal = roleDaoObj.addDescription(trans, "", "","","","");
162 //System.out.println(retVal.status);
163 assertEquals("0", Integer.toString(retVal.status));