Add more junits to batch module
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / helpers / test / JU_Approval.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.helpers.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.UUID;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.helpers.Approval;
39 import org.onap.aaf.auth.helpers.creators.RowCreator;
40 import org.onap.aaf.auth.layer.Result;
41 import org.onap.aaf.misc.env.LogTarget;
42
43 import junit.framework.Assert;
44
45 public class JU_Approval {
46
47         Approval approval;
48         UUID id;
49         UUID ticket;
50         Date date;
51
52         @Before
53         public void setUp() {
54                 id = new UUID(0, 0);
55                 ticket = new UUID(0, 0);
56                 date = new Date();
57
58                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
59         }
60
61         @Test
62         public void testRoleFromMemo() {
63                 Assert.assertNull(approval.roleFromMemo(null));
64                 Assert.assertEquals(".admin",
65                                 approval.roleFromMemo("Re-Validate as Administrator for AAF Namespace '\'test\'test"));
66                 Assert.assertEquals(".owner", approval.roleFromMemo("Re-Validate Ownership for AAF Namespace '\'test\'test"));
67                 Assert.assertEquals("", approval.roleFromMemo("Re-Approval in Role '\'test\'test"));
68         }
69
70         @Test
71         public void testExpunge() {
72                 approval.expunge();
73         }
74
75         @Test
76         public void testGetLast_notified() {
77                 Assert.assertTrue(approval.getLast_notified() instanceof Date);
78         }
79
80         @Test
81         public void testSetLastNotified() {
82                 approval.setLastNotified(date);
83         }
84
85         @Test
86         public void testGetStatus() {
87                 Assert.assertEquals("status", approval.getStatus());
88         }
89
90         @Test
91         public void testSetStatus() {
92                 approval.setStatus("status");
93         }
94
95         @Test
96         public void testGetId() {
97                 Assert.assertTrue(approval.getId() instanceof UUID);
98         }
99
100         @Test
101         public void testGetTicket() {
102                 Assert.assertTrue(approval.getTicket() instanceof UUID);
103         }
104
105         @Test
106         public void testGetMemo() {
107                 Assert.assertEquals("memo", approval.getMemo());
108         }
109
110         @Test
111         public void testGetOperation() {
112                 Assert.assertEquals("operation", approval.getOperation());
113         }
114
115         @Test
116         public void testGetType() {
117                 Assert.assertEquals("type", approval.getType());
118         }
119
120         @Test
121         public void testLapsed() {
122                 approval.lapsed();
123         }
124
125         @Test
126         public void testGetRole() {
127                 Assert.assertNull(approval.getRole());
128         }
129
130         @Test
131         public void testToString() {
132                 Assert.assertEquals("user memo", approval.toString());
133         }
134
135         @Test
136         public void testResetLocalData() {
137                 approval.resetLocalData();
138         }
139
140         @Test
141         public void testSizeForDeletion() {
142                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
143                 Assert.assertEquals(0, approval.sizeForDeletion());
144         }
145
146         @Test
147         public void testPendingDelete() {
148                 Assert.assertFalse(approval.pendingDelete(approval));
149         }
150
151         @Test
152         public void testUpdateNonDryRun() {
153                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
154                 AuthzTrans trans = mock(AuthzTrans.class);
155                 ApprovalDAO dao = mock(ApprovalDAO.class);
156                 LogTarget target = mock(LogTarget.class);
157
158                 when(trans.info()).thenReturn(target);
159
160                 approval.update(trans, dao, false);
161         }
162
163         @Test
164         public void testUpdateDryRun() {
165                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
166                 AuthzTrans trans = mock(AuthzTrans.class);
167                 ApprovalDAO dao = mock(ApprovalDAO.class);
168                 LogTarget target = mock(LogTarget.class);
169
170                 when(trans.info()).thenReturn(target);
171
172                 approval.update(trans, dao, true);
173         }
174
175         @Test
176         public void testDelayDeleteDryRun() {
177                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
178                 AuthzTrans trans = mock(AuthzTrans.class);
179                 ApprovalDAO dao = mock(ApprovalDAO.class);
180                 LogTarget target = mock(LogTarget.class);
181
182                 when(trans.info()).thenReturn(target);
183
184                 List<Approval> list = new ArrayList<Approval>();
185                 list.add(approval);
186                 Approval.delayDelete(trans, dao, true, list, "text");
187         }
188
189         @Test
190         public void testDelayDeleteNonDryRun() {
191                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
192                 AuthzTrans trans = mock(AuthzTrans.class);
193                 ApprovalDAO dao = mock(ApprovalDAO.class);
194                 LogTarget target = mock(LogTarget.class);
195
196                 when(trans.info()).thenReturn(target);
197                 Result<Void> rv = Result.ok();
198                 when(dao.delete(any(AuthzTrans.class), any(ApprovalDAO.Data.class), any(Boolean.class))).thenReturn(rv);
199
200                 List<Approval> list = new ArrayList<Approval>();
201                 list.add(approval);
202                 Approval.delayDelete(trans, dao, false, list, "text");
203         }
204
205         @Test
206         public void testDelayDeleteResultNotOk() {
207                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
208                 AuthzTrans trans = mock(AuthzTrans.class);
209                 ApprovalDAO dao = mock(ApprovalDAO.class);
210                 LogTarget target = mock(LogTarget.class);
211
212                 when(trans.info()).thenReturn(target);
213                 Result<Void> rv = Result.err(new Exception());
214                 when(dao.delete(any(AuthzTrans.class), any(ApprovalDAO.Data.class), any(Boolean.class))).thenReturn(rv);
215
216                 List<Approval> list = new ArrayList<Approval>();
217                 list.add(approval);
218                 Approval.delayDelete(trans, dao, false, list, "text");
219         }
220
221         @Test
222         public void testv2() {
223                 Approval.v2_0_17.create(RowCreator.getRow());
224
225                 assertEquals(
226                                 "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval",
227                                 Approval.v2_0_17.select());
228
229         }
230
231 }