Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / actions / test / JU_RoleCreate.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
22 package org.onap.aaf.auth.batch.actions.test;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
30 import java.text.DateFormat;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Properties;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.onap.aaf.auth.batch.actions.ActionDAO;
44 import org.onap.aaf.auth.batch.actions.RoleCreate;
45 import org.onap.aaf.auth.batch.helpers.Role;
46 import org.onap.aaf.auth.batch.helpers.UserRole;
47 import org.onap.aaf.auth.common.Define;
48 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
49 import org.onap.aaf.auth.dao.cass.RoleDAO;
50 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
51 import org.onap.aaf.auth.dao.hl.Question;
52 import org.onap.aaf.auth.env.AuthzTrans;
53 import org.onap.aaf.auth.layer.Result;
54 import org.onap.aaf.cadi.CadiException;
55 import org.onap.aaf.cadi.PropAccess;
56 import org.onap.aaf.cadi.config.Config;
57 import org.onap.aaf.misc.env.APIException;
58 import org.onap.aaf.misc.env.LogTarget;
59 import org.onap.aaf.misc.env.TimeTaken;
60
61 import com.datastax.driver.core.Cluster;
62 import com.datastax.driver.core.ColumnDefinitions;
63 import com.datastax.driver.core.PreparedId;
64 import com.datastax.driver.core.PreparedStatement;
65 import com.datastax.driver.core.Session;
66
67 public class JU_RoleCreate {
68     
69         @Mock
70     AuthzTrans trans;
71         @Mock
72         Cluster cluster;
73         @Mock
74         PropAccess access;
75         
76         @Mock
77         RoleCreate createObj;
78
79     
80     @Before
81     public void setUp() throws APIException, IOException {
82         initMocks(this);
83         Session sessionObj=Mockito.mock(Session.class);
84         PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
85                 try {
86                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
87                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
88                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
89                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
90                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
91                         Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
92                         Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
93                         Mockito.doReturn(sessionObj).when(cluster).connect("authz");
94                         Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
95                         
96                         Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
97                         Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
98                         Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
99                         Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
100                         Define.set(access);
101                         createObj = new RoleCreate(trans, cluster, true);
102                 } catch (APIException e) {
103                         // TODO Auto-generated catch block
104                         e.printStackTrace();
105                 } catch (IOException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 } catch (CadiException e) {
109                         // TODO Auto-generated catch block
110                         e.printStackTrace();
111                 }
112     }
113     
114     @Test
115         public void testExec() {
116                 Result<RoleDAO.Data> retVal = createObj.exec(trans,Mockito.mock(Role.class),"test");
117                 assertTrue(retVal.toString().contains("Success"));
118                 
119         }
120     @Test
121         public void testExecElse() {
122         Question ques = Mockito.mock(Question.class);
123                 try {
124                         Role roleObj = new Role("test","test","test",new HashSet());
125                 
126                         CachedRoleDAO roleDaoObj = Mockito.mock(CachedRoleDAO.class);
127                         
128                         List<Data> dataAL = new ArrayList<Data>();
129                         Data data = new Data();
130                         data.expires = new Date();
131                         dataAL.add(data);
132                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
133
134                         Mockito.doReturn(retVal1).when(roleDaoObj).create(Mockito.any(), Mockito.any());
135                         
136                         createObj = new RoleCreateImpl(trans,  cluster, false, ques, roleDaoObj);
137                 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
138                 assertTrue(0 == session.status);
139                 } catch (APIException | IOException e) {
140                         // TODO Auto-generated catch block
141                         e.printStackTrace();
142                 } catch (IllegalArgumentException e) {
143                         // TODO Auto-generated catch block
144                         e.printStackTrace();
145                 }
146                 
147         }
148     
149     @Test
150         public void testExecElseDateLess() {
151         Question ques = Mockito.mock(Question.class);
152                 try {
153                         Role roleObj = new Role("test","test","test",new HashSet());
154                 
155                         CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class);
156                         
157                         List<Data> dataAL = new ArrayList<Data>();
158                         Data data = new Data();
159                         DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
160                         try {
161                                 data.expires = sdf.parse("01/01/2100");
162                         } catch (ParseException e) {
163                                 // TODO Auto-generated catch block
164                                 e.printStackTrace();
165                         }
166                         dataAL.add(data);
167                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
168
169                         Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any());
170                         
171                         createObj = new RoleCreateImpl(trans,  cluster, false, ques, userRoleDaoObj);
172                 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
173                 assertTrue(0 == session.status);
174                 } catch (APIException | IOException e) {
175                         // TODO Auto-generated catch block
176                         e.printStackTrace();
177                 } catch (IllegalArgumentException e) {
178                         // TODO Auto-generated catch block
179                         e.printStackTrace();
180                 }
181                 
182         }
183     
184     @Test
185         public void testExecElseNok() {
186         Question ques = Mockito.mock(Question.class);
187                 try {
188                         Role roleObj = new Role("test","test","test",new HashSet());
189                 
190                         CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class);
191                         
192                         Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]);
193
194                         Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any());
195                         
196                         createObj = new RoleCreateImpl(trans,  cluster, false, ques, userRoleDaoObj);
197                 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
198                 assertTrue(session.toString().contains("test"));
199                 } catch (APIException | IOException e) {
200                         // TODO Auto-generated catch block
201                         e.printStackTrace();
202                 } catch (IllegalArgumentException e) {
203                         // TODO Auto-generated catch block
204                         e.printStackTrace();
205                 }
206                 
207         }
208
209     @Test
210         public void test2Argonstructor() {
211                 createObj = new RoleCreate(trans, Mockito.mock(ActionDAO.class));
212         }
213    
214     class RoleCreateImpl extends RoleCreate{
215
216                 public RoleCreateImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedRoleDAO userRoleDaoObj) throws APIException, IOException {
217                         super(trans, cluster, dryRun);
218                         setQuestion(ques, userRoleDaoObj);
219                 }
220                 
221                 public void setQuestion(Question ques, CachedRoleDAO userRoleDaoObj) {
222                         Field field, nsDaoField;
223                         try {
224                                 field = RoleCreateImpl.class.getSuperclass().getSuperclass().getDeclaredField("q");
225                                 nsDaoField = Question.class.getDeclaredField("roleDAO");
226                                 
227                                 field.setAccessible(true);
228                                 nsDaoField.setAccessible(true);
229                         // remove final modifier from field
230                         Field modifiersField = Field.class.getDeclaredField("modifiers");
231                         modifiersField.setAccessible(true);
232                         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
233                         modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL);
234                         
235                         field.set(this, ques);
236                         nsDaoField.set(ques, userRoleDaoObj);
237                         } catch (NoSuchFieldException | SecurityException e) {
238                                 // TODO Auto-generated catch block
239                                 e.printStackTrace();
240                         } catch (IllegalArgumentException e) {
241                                 // TODO Auto-generated catch block
242                                 e.printStackTrace();
243                         } catch (IllegalAccessException e) {
244                                 // TODO Auto-generated catch block
245                                 e.printStackTrace();
246                         }
247                 }
248     }
249 }