Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cached / 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         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);
100         //retVal.status = 0;
101         assertEquals("1", Integer.toString(retVal.status));
102     }    
103     
104     @Test
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));
113     }
114     @Test
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));
123     }    
124     @Test
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));
133     }
134     
135     @Test
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));
143     }
144     
145     @Test
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());
153     }
154     
155     @Test
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());
164     }
165
166     @Test
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));
174     }
175 }