2e27bfd9c241642892dd4d1000130eb51310bbdb
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / JU_CachedRoleDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21 package org.onap.aaf.auth.dao.cached;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.util.List;
28
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;
43
44
45 @RunWith(MockitoJUnitRunner.class) 
46 public class JU_CachedRoleDAO {
47
48         @Mock
49         RoleDAO dao;
50         
51         @Mock
52         CIDAO<AuthzTrans> info;
53         
54         @Mock
55         AuthzTransImpl trans;
56         
57         @Mock
58         RoleDAO.Data data;
59         
60         @Mock
61         PermDAO.Data permData;
62         
63         @Before
64         public void setUp() throws Exception {
65                 initMocks(this);
66                 when(trans.debug()).thenReturn(new LogTarget() {
67             
68             @Override
69             public void printf(String fmt, Object... vars) {}
70             
71             @Override
72             public void log(Throwable e, Object... msgs) {
73                 e.getMessage();
74                 e.printStackTrace();
75                 msgs.toString();
76                 
77             }
78             
79             @Override
80             public void log(Object... msgs) {
81             }
82             
83             @Override
84             public boolean isLoggable() {
85                 
86                 return true;
87             }
88         });
89         }
90         
91         @Test
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);
99                 //retVal.status = 0;
100                 assertEquals("1", Integer.toString(retVal.status));
101         }       
102         
103         @Test
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));
112         }
113         @Test
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));
122         }       
123         @Test
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));
132         }
133         
134         @Test
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));
142         }
143         
144         @Test
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());
152         }
153         
154         @Test
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());
163         }
164
165         @Test
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));
173         }
174 }