cb46d2de8a9067429fc8f2d5313dced4e373508b
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / common / aai / tasks / AAIFlagTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.so.bpmn.common.aai.tasks;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.ArgumentMatchers.isA;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import java.io.IOException;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.bpmn.BaseTaskTest;
41 import org.onap.so.bpmn.common.BuildingBlockExecution;
42 import org.onap.so.bpmn.common.InjectionHelper;
43 import org.onap.so.bpmn.common.data.TestDataSetup;
44 import org.onap.so.bpmn.infrastructure.aai.tasks.AAIFlagTasks;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
46 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
47 import org.onap.aaiclient.client.aai.AAIResourcesClient;
48 import org.onap.so.client.aai.mapper.AAIObjectMapper;
49 import org.onap.so.client.exception.BBObjectNotFoundException;
50 import org.onap.so.client.exception.ExceptionBuilder;
51 import org.onap.so.client.orchestration.AAIVnfResources;
52 import org.springframework.beans.factory.annotation.Autowired;
53
54
55 public class AAIFlagTasksTest extends BaseTaskTest {
56
57     @InjectMocks
58     private AAIFlagTasks aaiFlagTasks = new AAIFlagTasks();
59
60     private GenericVnf genericVnf;
61
62     @Before
63     public void before() throws BBObjectNotFoundException {
64         genericVnf = setGenericVnf();
65         doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
66         when(extractPojosForBB.extractByKey(any(), any())).thenReturn(genericVnf);
67     }
68
69     @Test
70     public void checkVnfInMaintTestTrue() throws Exception {
71         doThrow(new BpmnError("VNF is in maintenance in A&AI")).when(exceptionUtil)
72                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
73         doReturn(false).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
74         doReturn(true).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
75         try {
76             aaiFlagTasks.checkVnfInMaintFlag(execution);
77         } catch (Exception e) {
78             verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
79             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
80                     eq("VNF is in maintenance in A&AI"));
81         }
82     }
83
84     @Test
85     public void checkVnfInMaintTestFalse() throws Exception {
86         doReturn(false).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
87         aaiFlagTasks.checkVnfInMaintFlag(execution);
88         verify(exceptionUtil, times(0)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
89                 any(int.class), any(String.class));
90     }
91
92     @Test
93     public void checkVnfInMaintFlagExceptionTest() {
94
95         doThrow(new BpmnError("Unknown Error")).when(exceptionUtil)
96                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
97         doThrow(RuntimeException.class).when(aaiVnfResources).checkInMaintFlag(isA(String.class));
98         try {
99             aaiFlagTasks.checkVnfInMaintFlag(execution);
100         } catch (Exception e) {
101             verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
102             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
103                     any(String.class));
104         }
105
106     }
107
108     @Test
109     public void modifyVnfInMaintFlagTest() throws Exception {
110         doNothing().when(aaiVnfResources).updateObjectVnf(ArgumentMatchers.any(GenericVnf.class));
111         aaiFlagTasks.modifyVnfInMaintFlag(execution, true);
112         verify(aaiVnfResources, times(1)).updateObjectVnf(ArgumentMatchers.any(GenericVnf.class));
113     }
114
115     @Test
116     public void modifyVnfInMaintFlagExceptionTest() {
117
118         doThrow(new BpmnError("Unknown Error")).when(exceptionUtil)
119                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
120         doThrow(RuntimeException.class).when(aaiVnfResources).updateObjectVnf(isA(GenericVnf.class));
121         try {
122             aaiFlagTasks.modifyVnfInMaintFlag(execution, true);
123         } catch (Exception e) {
124             verify(aaiVnfResources, times(1)).checkInMaintFlag(any(String.class));
125             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
126                     any(String.class));
127         }
128     }
129
130     @Test
131     public void checkVnfClosedLoopDisabledTestTrue() throws Exception {
132         doThrow(new BpmnError("VNF Closed Loop Disabled in A&AI")).when(exceptionUtil)
133                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
134         doReturn(false).when(aaiVnfResources).checkVnfClosedLoopDisabledFlag(isA(String.class));
135         try {
136             aaiFlagTasks.checkVnfClosedLoopDisabledFlag(execution);
137         } catch (Exception e) {
138             verify(aaiVnfResources, times(1)).checkVnfClosedLoopDisabledFlag(any(String.class));
139             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
140                     eq("VNF Closed Loop Disabled in A&AI"));
141         }
142     }
143
144     @Test
145     public void checkVnfClosedLoopDisabledTestFalse() throws Exception {
146         doReturn(false).when(aaiVnfResources).checkVnfClosedLoopDisabledFlag(isA(String.class));
147         aaiFlagTasks.checkVnfClosedLoopDisabledFlag(execution);
148         verify(exceptionUtil, times(0)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
149                 any(int.class), any(String.class));
150     }
151
152     @Test
153     public void checkVnfClosedLoopDisabledFlagExceptionTest() {
154
155         doThrow(new BpmnError("Unknown Error")).when(exceptionUtil)
156                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
157         doThrow(RuntimeException.class).when(aaiVnfResources).checkVnfClosedLoopDisabledFlag(isA(String.class));
158         try {
159             aaiFlagTasks.checkVnfClosedLoopDisabledFlag(execution);
160         } catch (Exception e) {
161             verify(aaiVnfResources, times(1)).checkVnfClosedLoopDisabledFlag(any(String.class));
162             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
163                     any(String.class));
164         }
165     }
166
167     @Test
168     public void modifyVnfClosedLoopDisabledFlagTest() throws Exception {
169         doNothing().when(aaiVnfResources).updateObjectVnf(ArgumentMatchers.any(GenericVnf.class));
170         aaiFlagTasks.modifyVnfClosedLoopDisabledFlag(execution, true);
171         verify(aaiVnfResources, times(1)).updateObjectVnf(ArgumentMatchers.any(GenericVnf.class));
172     }
173
174     @Test
175     public void modifyVnfClosedLoopDisabledFlagExceptionTest() {
176
177         doThrow(new BpmnError("Unknown Error")).when(exceptionUtil)
178                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
179         doThrow(RuntimeException.class).when(aaiVnfResources).updateObjectVnf(isA(GenericVnf.class));
180         try {
181             aaiFlagTasks.modifyVnfClosedLoopDisabledFlag(execution, true);
182         } catch (Exception e) {
183             verify(aaiVnfResources, times(1)).checkVnfClosedLoopDisabledFlag(any(String.class));
184             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
185                     any(String.class));
186         }
187     }
188
189
190     @Test
191     public void checkVnfPserversLockedFlagTestTrue() throws Exception {
192         doThrow(new BpmnError("VNF PServers in Locked in A&AI")).when(exceptionUtil)
193                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
194         doReturn(true).when(aaiVnfResources).checkVnfPserversLockedFlag(isA(String.class));
195         try {
196             aaiFlagTasks.checkVnfClosedLoopDisabledFlag(execution);
197         } catch (Exception e) {
198             verify(aaiVnfResources, times(1)).checkVnfPserversLockedFlag(any(String.class));
199             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
200                     eq("VNF PServers in Locked in A&AI"));
201         }
202     }
203
204     @Test
205     public void checkVnfPserversLockedFlagTestFalse() throws Exception {
206         doReturn(false).when(aaiVnfResources).checkVnfPserversLockedFlag(isA(String.class));
207         aaiFlagTasks.checkVnfPserversLockedFlag(execution);
208         verify(exceptionUtil, times(0)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class),
209                 any(int.class), any(String.class));
210     }
211
212     @Test
213     public void checkVnfPserversLockedFlagExceptionTest() throws IOException {
214
215         doThrow(new BpmnError("Unknown Error")).when(exceptionUtil)
216                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
217         doThrow(RuntimeException.class).when(aaiVnfResources).checkVnfClosedLoopDisabledFlag(isA(String.class));
218         try {
219             aaiFlagTasks.checkVnfPserversLockedFlag(execution);
220         } catch (Exception e) {
221             verify(aaiVnfResources, times(1)).checkVnfPserversLockedFlag(any(String.class));
222             verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
223                     any(String.class));
224         }
225     }
226 }