Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cached / JU_CachedUserRoleDAO.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.onap.aaf.auth.dao.CIDAO;
35 import org.onap.aaf.auth.dao.cass.PermDAO;
36 import org.onap.aaf.auth.dao.cass.RoleDAO;
37 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
38 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.env.AuthzTransImpl;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal;
44 import org.onap.aaf.misc.env.LogTarget;
45 import org.onap.aaf.misc.env.Slot;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @RunWith(PowerMockRunner.class)
49 public class JU_CachedUserRoleDAO {
50
51     @Mock
52     UserRoleDAO dao;
53     
54     @Mock
55     CIDAO<AuthzTrans> info;
56     
57     @Mock
58     AuthzTransImpl trans;
59     
60     @Mock
61     RoleDAO.Data data;
62     
63     @Mock
64     PermDAO.Data permData;
65     
66     @Before
67     public void setUp() throws Exception {
68         initMocks(this);
69         when(trans.debug()).thenReturn(new LogTarget() {
70             
71             @Override
72             public void printf(String fmt, Object... vars) {}
73             
74             @Override
75             public void log(Throwable e, Object... msgs) {
76                 e.getMessage();
77                 e.printStackTrace();
78                 msgs.toString();
79                 
80             }
81             
82             @Override
83             public void log(Object... msgs) {
84             }
85             
86             @Override
87             public boolean isLoggable() {
88                 
89                 return true;
90             }
91         });
92     }
93     
94     private class TaggedPrincipalStub extends TaggedPrincipal {
95         String name="TaggedPrincipalStub";
96         public TaggedPrincipalStub() { super(); }
97         public TaggedPrincipalStub(final TagLookup tl) { super(tl); }
98         @Override public String getName() { return name; }
99         @Override public String tag() { return null; }
100     }
101     
102     @Test
103     public void testReadName() {
104         CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L);
105         Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test4",new String[0]);
106         Mockito.doReturn(retVal1).when(dao).readByUser(trans, "test4");
107 //        Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1);
108         Result<List<Data>> retVal = roleDaoObj.readByUser(trans, "test4");
109         //System.out.println(retVal.status);
110         //retVal.status = 0;
111         assertEquals("25", Integer.toString(retVal.status));
112     }    
113     
114     @Test
115     public void testReadNameUser() {
116         CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L);
117         Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"TaggedPrincipalStub",new String[0]);
118         AuthzEnv env = Mockito.mock(AuthzEnv.class);
119         AuthzTransImpl transTemp = new AuthzTransImpl(env) {
120             @Override
121             public<T> T get(Slot slot, T deflt) {
122                 Object o=null;
123                 return (T)o;
124             }
125             
126         };
127         transTemp.setUser(new TaggedPrincipalStub());
128         Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
129         Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub");
130         roleDaoObj.invalidate("TaggedPrincipalStub");
131         Result<List<Data>> retVal = roleDaoObj.readByUser(transTemp, "TaggedPrincipalStub");
132 //        System.out.println(retVal.status);
133         assertEquals("1", Integer.toString(retVal.status));
134     }
135     
136     @Test
137     public void testReadByRoleSuccess() {
138         CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);//
139         Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]);
140         Mockito.doReturn(retVal1).when(dao).readByRole(trans, "");
141         roleDaoObj.invalidate("");
142         Result<List<Data>> retVal = roleDaoObj.readByRole(trans, "");
143         //System.out.println(retVal.status);
144         assertEquals("1", Integer.toString(retVal.status));
145     }    
146     @Test
147     public void testReadByRoleFailure() {
148         CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);//
149         Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]);
150         Mockito.doReturn(retVal1).when(dao).readByRole(trans, "");
151         roleDaoObj.invalidate("");
152         Result<List<Data>> retVal = roleDaoObj.readByRole(trans, "");
153         //System.out.println(retVal.status);
154         assertEquals("25", Integer.toString(retVal.status));
155     }
156     
157     @Test
158     public void testReadUserInRole() {
159         CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);//
160         Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"TaggedPrincipalStub",new String[0]);
161         AuthzEnv env = Mockito.mock(AuthzEnv.class);
162         AuthzTransImpl transTemp = new AuthzTransImpl(env) {
163             @Override
164             public<T> T get(Slot slot, T deflt) {
165                 Object o=null;
166                 return (T)o;
167             }
168             
169         };
170         transTemp.setUser(new TaggedPrincipalStub());
171         Mockito.doReturn(retVal1).when(info).touch(trans, null,null);
172         Mockito.doReturn(retVal1).when(dao).readByUserRole(transTemp, "","");
173         Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub");
174         Result<List<Data>> retVal = roleDaoObj.readUserInRole(transTemp, "TaggedPrincipalStub","");
175         //System.out.println(retVal.status);
176         assertEquals("25", Integer.toString(retVal.status));
177     }
178     
179
180 }