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.mockito.runners.MockitoJUnitRunner;
35 import org.onap.aaf.auth.dao.CIDAO;
36 import org.onap.aaf.auth.dao.cass.PermDAO;
37 import org.onap.aaf.auth.dao.cass.RoleDAO;
38 import org.onap.aaf.auth.dao.cass.RoleDAO.Data;
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;
45 @RunWith(MockitoJUnitRunner.class)
46 public class JU_CachedRoleDAO {
52 CIDAO<AuthzTrans> info;
61 PermDAO.Data permData;
64 public void setUp() throws Exception {
66 when(trans.debug()).thenReturn(new LogTarget() {
69 public void printf(String fmt, Object... vars) {}
72 public void log(Throwable e, Object... msgs) {
80 public void log(Object... msgs) {
84 public boolean isLoggable() {
92 public void testReadNameSuccess() {
93 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
94 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test4",new String[0]);
95 roleDaoObj.invalidate("");
96 Mockito.doReturn(retVal1).when(dao).readName(trans, "test4");
97 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
98 Result<List<Data>> retVal = roleDaoObj.readName(trans, "test4");
99 // System.out.println(retVal.status);
101 assertEquals("1", Integer.toString(retVal.status));
105 public void testReadNameFailure() {
106 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
107 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]);
108 Mockito.doReturn(retVal1).when(dao).readName(trans, "test3");
109 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
110 Result<List<Data>> retVal = roleDaoObj.readName(trans, "test3");
111 // System.out.println(retVal.status);
112 assertEquals("22", Integer.toString(retVal.status));
115 public void testReadNSSuccess() {
116 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
117 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]);
118 Mockito.doReturn(retVal1).when(dao).readNS(trans, "");
119 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
120 Result<List<Data>> retVal = roleDaoObj.readNS(trans, "");
121 // System.out.println(retVal.status);
122 assertEquals("1", Integer.toString(retVal.status));
125 public void testReadNSFailure() {
126 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
127 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
128 Mockito.doReturn(retVal1).when(dao).readNS(trans, "");
129 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
130 Result<List<Data>> retVal = roleDaoObj.readNS(trans, "");
131 // System.out.println(retVal.status);
132 assertEquals("22", Integer.toString(retVal.status));
136 public void testReadChildren() {
137 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
138 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
139 Mockito.doReturn(retVal1).when(dao).readChildren(trans, "","");
140 Result<List<Data>> retVal = roleDaoObj.readChildren(trans, "", "");
141 //System.out.println(retVal.status);
142 assertEquals("0", Integer.toString(retVal.status));
146 public void testAddPerm() {
147 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);
148 Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]);
149 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
150 Mockito.doReturn(retVal1).when(dao).addPerm(trans, data,permData);
151 Result<Void> retVal = roleDaoObj.addPerm(trans, data, permData);
152 assertEquals("testAddPerm", retVal.toString());
156 public void testDelPerm() {
157 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);
158 Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]);
159 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
160 Mockito.doReturn(retVal1).when(dao).delPerm(trans, data,permData);
161 Result<Void> retVal = roleDaoObj.delPerm(trans, data, permData);
162 System.out.println(retVal);
163 assertEquals("testAddPerm", retVal.toString());
167 public void testAddDescription() {
168 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
169 Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]);
170 Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","");
171 Result<Void> retVal = roleDaoObj.addDescription(trans, "", "","");
172 //System.out.println(retVal.status);
173 assertEquals("0", Integer.toString(retVal.status));