Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / approvalsets / JU_ApprovalSetTest.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.approvalsets;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import java.io.IOException;
29 import java.util.Date;
30 import java.util.GregorianCalendar;
31 import java.util.Properties;
32 import java.util.Set;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.onap.aaf.auth.common.Define;
39 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
40 import org.onap.aaf.auth.dao.cass.FutureDAO;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.PropAccess;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.LogTarget;
48 import org.onap.aaf.misc.env.TimeTaken;
49
50 import com.datastax.driver.core.Cluster;
51 import com.datastax.driver.core.ColumnDefinitions;
52 import com.datastax.driver.core.PreparedId;
53 import com.datastax.driver.core.PreparedStatement;
54 import com.datastax.driver.core.ResultSet;
55 import com.datastax.driver.core.Session;
56
57 public class JU_ApprovalSetTest {
58     
59     @Mock
60     AuthzTrans trans;
61     @Mock
62     Cluster cluster;
63     @Mock
64     PropAccess access;
65     
66     @Mock
67     ApprovalSet actionObj;
68
69     @Mock
70     DataView dv;
71     
72     @Before
73     public void setUp() throws APIException, IOException {
74         initMocks(this);
75         Session sessionObj=Mockito.mock(Session.class);
76         PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
77         try {
78             Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
79             Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
80             Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
81             Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
82             Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
83             Mockito.doReturn(sessionObj).when(cluster).connect("authz");
84             Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
85             
86             Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
87             Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
88             Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
89             Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
90             Define.set(access);
91             GregorianCalendar start= new GregorianCalendar();
92             actionObj = new ApprovalSet(start, "test", dv);
93         } catch (CadiException e) {
94             // TODO Auto-generated catch block
95             e.printStackTrace();
96         }
97    }
98     
99     @Test
100     public void testPuntDate() {
101         actionObj.write(trans);
102         ApprovalDAO.Data dataObj = new ApprovalDAO.Data();
103         
104         Result<FutureDAO.Data> rs1 = new Result<FutureDAO.Data>(null,0,"test",new Object[0]);
105         Mockito.doReturn(rs1).when(dv).insert(Mockito.any(AuthzTrans.class), Mockito.any(FutureDAO.Data.class));
106         Mockito.doReturn(rs1).when(dv).insert(Mockito.any(AuthzTrans.class), Mockito.any(ApprovalDAO.Data.class));
107         actionObj.ladd.add(dataObj);
108         Result<Void> retVal = actionObj.write(trans);
109         
110         rs1 = new Result<FutureDAO.Data>(null,1,"test",new Object[0]);
111         Mockito.doReturn(rs1).when(dv).insert(Mockito.any(AuthzTrans.class), Mockito.any(ApprovalDAO.Data.class));
112         retVal = actionObj.write(trans);
113         assertTrue("Security - test".equals(retVal.details));
114         
115         actionObj.ladd.add(dataObj);
116         retVal = actionObj.write(trans);
117         assertTrue(retVal.details.contains("Security - test"));
118
119         Mockito.doReturn(rs1).when(dv).insert(Mockito.any(AuthzTrans.class), Mockito.any(FutureDAO.Data.class));
120         retVal = actionObj.write(trans);
121         assertTrue(retVal.details.contains("Security - test"));
122         
123         actionObj.setConstruct(null);
124         actionObj.setExpires(new GregorianCalendar());
125         actionObj.setMemo("");
126         actionObj.ladd = null;
127         actionObj.write(trans);
128     }
129     
130     @Test
131     public void testHasApprovals() {
132         assertFalse(actionObj.hasApprovals());
133         
134         ApprovalDAO.Data dataObj = new ApprovalDAO.Data();
135         actionObj.ladd.add(dataObj);
136         assertTrue(actionObj.hasApprovals());
137     }
138     
139     @Test
140     public void testApprovers() {
141         Set<String> retVal = actionObj.approvers();
142         assertTrue(retVal.size() == 0);
143         
144         ApprovalDAO.Data dataObj = new ApprovalDAO.Data();
145         actionObj.ladd.add(dataObj);
146         retVal = actionObj.approvers();
147         assertTrue(retVal.size() == 1);
148         
149     }
150 }