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 Mockito.doReturn(retVal1).when(dao).readName(trans, "test4");
96 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
97 Result<List<Data>> retVal = roleDaoObj.readName(trans, "test4");
98 // System.out.println(retVal.status);
100 assertEquals("1", Integer.toString(retVal.status));
104 public void testReadNameFailure() {
105 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
106 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]);
107 Mockito.doReturn(retVal1).when(dao).readName(trans, "test3");
108 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
109 Result<List<Data>> retVal = roleDaoObj.readName(trans, "test3");
110 // System.out.println(retVal.status);
111 assertEquals("22", Integer.toString(retVal.status));
114 public void testReadNSSuccess() {
115 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
116 Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]);
117 Mockito.doReturn(retVal1).when(dao).readNS(trans, "");
118 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
119 Result<List<Data>> retVal = roleDaoObj.readNS(trans, "");
120 // System.out.println(retVal.status);
121 assertEquals("1", Integer.toString(retVal.status));
124 public void testReadNSFailure() {
125 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
126 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
127 Mockito.doReturn(retVal1).when(dao).readNS(trans, "");
128 // Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
129 Result<List<Data>> retVal = roleDaoObj.readNS(trans, "");
130 // System.out.println(retVal.status);
131 assertEquals("22", Integer.toString(retVal.status));
135 public void testReadChildren() {
136 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
137 Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
138 Mockito.doReturn(retVal1).when(dao).readChildren(trans, "","");
139 Result<List<Data>> retVal = roleDaoObj.readChildren(trans, "", "");
140 //System.out.println(retVal.status);
141 assertEquals("0", Integer.toString(retVal.status));
145 public void testAddPerm() {
146 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);
147 Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]);
148 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
149 Mockito.doReturn(retVal1).when(dao).addPerm(trans, data,permData);
150 Result<Void> retVal = roleDaoObj.addPerm(trans, data, permData);
151 assertEquals("testAddPerm", retVal.toString());
155 public void testDelPerm() {
156 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);
157 Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]);
158 Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
159 Mockito.doReturn(retVal1).when(dao).delPerm(trans, data,permData);
160 Result<Void> retVal = roleDaoObj.delPerm(trans, data, permData);
161 System.out.println(retVal);
162 assertEquals("testAddPerm", retVal.toString());
166 public void testAddDescription() {
167 CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
168 Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]);
169 Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","");
170 Result<Void> retVal = roleDaoObj.addDescription(trans, "", "","");
171 //System.out.println(retVal.status);
172 assertEquals("0", Integer.toString(retVal.status));