66c32a7b4e54a68dac1c4943021a46bae75b66d5
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cached / JU_CachedPermDAOTest.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.verify;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import java.util.List;
29
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;
43
44 public class JU_CachedPermDAOTest {
45
46         @Mock
47         private CIDAO<AuthzTrans> info;
48         @Mock
49         private PermDAO dao;
50         
51         @Mock
52         RoleDAO.Data role;
53         
54         @Mock
55         private PermDAO.Data perm;
56         
57         @Mock
58         private AuthzTrans trans;
59         @Mock
60         private Result<List<PermDAO.Data>> value;
61
62         @Before
63         public void setUp() throws Exception {
64                 initMocks(this);
65
66                 when(dao.readNS(trans, "ns")).thenReturn(value);
67                 when(trans.debug()).thenReturn(new LogTarget() {
68             
69             @Override
70             public void printf(String fmt, Object... vars) {}
71             
72             @Override
73             public void log(Throwable e, Object... msgs) {
74                 e.getMessage();
75                 e.printStackTrace();
76                 msgs.toString();
77                 
78             }
79             
80             @Override
81             public void log(Object... msgs) {
82             }
83             
84             @Override
85             public boolean isLoggable() {
86                 
87                 return true;
88             }
89         });
90         }
91
92         @Test
93         public void testReadNS() {
94                 when(value.isOKhasData()).thenReturn(true);
95                 when(value.isOK()).thenReturn(false);
96                 CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l);
97
98                 Result<List<Data>> result = ccDao.readNS(trans, "ns");
99
100                 assertEquals(result, value);
101
102                 when(value.isOKhasData()).thenReturn(false);
103
104                 result = ccDao.readNS(trans, "ns");
105
106                 assertEquals(result.status, Status.ERR_PermissionNotFound);
107
108                 ccDao.readChildren(trans, "ns", "type");
109
110                 verify(dao).readChildren(trans, "ns", "type");
111         }
112         
113         @Test
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);
120                 //retVal.status = 0;
121                 assertEquals("1", Integer.toString(retVal.status));
122         }       
123         
124         @Test
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));
132         }
133         
134         @Test
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());
143         }
144         
145         @Test
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());
154         }
155
156         @Test
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));
164         }
165
166 }