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