Merge "Sonar Fix: Owner.java"
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / 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.batch.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.batch.helpers.Approval;
37 import org.onap.aaf.auth.batch.helpers.creators.RowCreator;
38 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
39 import org.onap.aaf.auth.env.AuthzTrans;
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 testPendingDelete() {
142                 Assert.assertFalse(approval.pendingDelete(approval));
143         }
144
145         @Test
146         public void testUpdateNonDryRun() {
147                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
148                 AuthzTrans trans = mock(AuthzTrans.class);
149                 ApprovalDAO dao = mock(ApprovalDAO.class);
150                 LogTarget target = mock(LogTarget.class);
151
152                 when(trans.info()).thenReturn(target);
153
154                 approval.update(trans, dao, false);
155         }
156
157         @Test
158         public void testUpdateDryRun() {
159                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
160                 AuthzTrans trans = mock(AuthzTrans.class);
161                 ApprovalDAO dao = mock(ApprovalDAO.class);
162                 LogTarget target = mock(LogTarget.class);
163
164                 when(trans.info()).thenReturn(target);
165
166                 approval.update(trans, dao, true);
167         }
168
169         @Test
170         public void testDelayDeleteDryRun() {
171                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
172                 AuthzTrans trans = mock(AuthzTrans.class);
173                 ApprovalDAO dao = mock(ApprovalDAO.class);
174                 LogTarget target = mock(LogTarget.class);
175
176                 when(trans.info()).thenReturn(target);
177
178                 List<Approval> list = new ArrayList<Approval>();
179                 list.add(approval);
180                 Approval.delayDelete(trans, dao, true, list, "text");
181         }
182
183         @Test
184         public void testDelayDeleteNonDryRun() {
185                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
186                 AuthzTrans trans = mock(AuthzTrans.class);
187                 ApprovalDAO dao = mock(ApprovalDAO.class);
188                 LogTarget target = mock(LogTarget.class);
189
190                 when(trans.info()).thenReturn(target);
191                 Result<Void> rv = Result.ok();
192                 when(dao.delete(any(AuthzTrans.class), any(ApprovalDAO.Data.class), any(Boolean.class))).thenReturn(rv);
193
194                 List<Approval> list = new ArrayList<Approval>();
195                 list.add(approval);
196                 Approval.delayDelete(trans, dao, false, list, "text");
197         }
198
199         @Test
200         public void testDelayDeleteResultNotOk() {
201                 approval = new Approval(id, ticket, "approver", date, "user", "memo", "operation", "status", "type", 100l);
202                 AuthzTrans trans = mock(AuthzTrans.class);
203                 ApprovalDAO dao = mock(ApprovalDAO.class);
204                 LogTarget target = mock(LogTarget.class);
205
206                 when(trans.info()).thenReturn(target);
207                 Result<Void> rv = Result.err(new Exception());
208                 when(dao.delete(any(AuthzTrans.class), any(ApprovalDAO.Data.class), any(Boolean.class))).thenReturn(rv);
209
210                 List<Approval> list = new ArrayList<Approval>();
211                 list.add(approval);
212                 Approval.delayDelete(trans, dao, false, list, "text");
213         }
214
215         @Test
216         public void testv2() {
217                 Approval.v2_0_17.create(RowCreator.getRow());
218
219                 assertEquals(
220                                 "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval",
221                                 Approval.v2_0_17.select());
222
223         }
224
225 }