Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / actions / test / JU_ActionDAO.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.util.Properties;
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.batch.actions.ActionDAO;
35 import org.onap.aaf.auth.common.Define;
36 import org.onap.aaf.auth.dao.hl.Function;
37 import org.onap.aaf.auth.dao.hl.Question;
38 import org.onap.aaf.auth.env.AuthzTrans;
39 import org.onap.aaf.auth.layer.Result;
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.LogTarget;
45 import org.onap.aaf.misc.env.TimeTaken;
46
47 import com.datastax.driver.core.Cluster;
48 import com.datastax.driver.core.ColumnDefinitions;
49 import com.datastax.driver.core.PreparedId;
50 import com.datastax.driver.core.PreparedStatement;
51 import com.datastax.driver.core.Session;
52
53 public class JU_ActionDAO {
54     
55         @Mock
56     AuthzTrans trans;
57         @Mock
58         Cluster cluster;
59         @Mock
60         PropAccess access;
61         
62         @Mock
63         ActionDAO actionObj;
64
65     private class ActionDAOStub extends ActionDAO {
66
67         public ActionDAOStub(AuthzTrans trans, ActionDAO predecessor) {
68             super(trans, predecessor);
69             // TODO Auto-generated constructor stub
70         }
71
72         public ActionDAOStub(AuthzTrans trans, Cluster cluster, boolean dryRun) throws APIException, IOException {
73             super(trans, cluster, dryRun);
74             // TODO Auto-generated constructor stub
75         }
76
77         @Override
78         public Result exec(AuthzTrans trans, Object data, Object t) {
79             // TODO Auto-generated method stub
80             return null;
81         }
82         
83     }
84     
85     @Before
86     public void setUp() throws APIException, IOException {
87         initMocks(this);
88         Session sessionObj=Mockito.mock(Session.class);
89         PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
90                 try {
91                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
92                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
93                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
94                         Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
95                         Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
96                         Mockito.doReturn(sessionObj).when(cluster).connect("authz");
97                         Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
98                         
99                         Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
100                         Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
101                         Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
102                         Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
103                         Define.set(access);
104                         actionObj = new ActionDAOStub(trans, cluster, true);
105                 } catch (APIException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 } catch (IOException e) {
109                         // TODO Auto-generated catch block
110                         e.printStackTrace();
111                 } catch (CadiException e) {
112                         // TODO Auto-generated catch block
113                         e.printStackTrace();
114                 }
115 //        Cluster.Initializer cInit = mock(Cluster.Initializer.class);
116 //        Cluster.Builder cBuild = new Cluster.Builder();
117 //        cBuild.addContactPoint("test");
118 //        cBuild.build();
119 //        cluster.buildFrom(cBuild);
120 //        cluster.builder();
121 //        cluster.init();
122 //        cluster.builder().getContactPoints();
123         
124
125         
126 //        aTrans = mock(AuthzTrans.class);
127 //        cluster = mock(Cluster.class);
128 //        actionDAOStub = new ActionDAOStub(aTrans,cluster,true);
129 //        actionDAOStub1 = new ActionDAOStub(aTrans, actionDAOStub);
130     }
131     
132     @Test
133         public void testGetSession() {
134         try {
135                 Session session = actionObj.getSession(trans);
136                 assertTrue(session.toString().contains("Mock for Session"));
137                 } catch (APIException e) {
138                         // TODO Auto-generated catch block
139                         e.printStackTrace();
140                 } catch (IOException e) {
141                         // TODO Auto-generated catch block
142                         e.printStackTrace();
143                 } 
144         }
145     
146     @Test
147         public void testQuestion() {
148                 Question retVal = actionObj.question();
149                 assertTrue(retVal.toString().contains("org.onap.aaf.auth.dao.hl.Question"));
150         }
151     
152     @Test
153         public void testFunction() {
154         Function retVal = actionObj.function();
155                 assertTrue(retVal.toString().contains("org.onap.aaf.auth.dao.hl.Function"));
156         }
157     
158     @Test
159         public void testClose() {
160                 actionObj.close(trans);
161 //              assertTrue(session.toString().contains("Mock for Session"));
162         }
163     
164     @Test
165         public void testCloseFalse() {
166         actionObj = new ActionDAOStub(trans, Mockito.mock(ActionDAO.class));
167                 actionObj.close(trans);
168 //                      assertTrue(session.toString().contains("Mock for Session"));
169         }
170
171 }