0189d065df8bd94be9c57475b638d95c83bfe0d2
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoUpdateNetworkInstanceRollbackTest.groovy
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
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import static org.mockito.Mockito.*
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.ProcessEngineServices
27 import org.camunda.bpm.engine.RepositoryService
28 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
29 import org.camunda.bpm.engine.repository.ProcessDefinition
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.mockito.MockitoAnnotations
32 import org.mockito.runners.MockitoJUnitRunner
33 import org.onap.so.bpmn.common.scripts.MsoUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.junit.Before
36 import org.junit.Rule;
37 import org.junit.Test
38 import org.junit.Ignore
39 import org.junit.runner.RunWith
40
41 import static org.junit.Assert.*;
42
43 import com.github.tomakehurst.wiremock.client.WireMock;
44 import com.github.tomakehurst.wiremock.junit.WireMockRule;
45
46 import java.util.HashMap;
47 import java.util.Map;
48 import java.util.UUID;
49 import org.apache.commons.lang3.*
50
51
52 @RunWith(MockitoJUnitRunner.class)
53 class DoUpdateNetworkInstanceRollbackTest  {
54
55         @Rule
56         public WireMockRule wireMockRule = new WireMockRule(28090);
57
58                 def utils = new MsoUtils()
59                 String Prefix="UPDNETIR_"
60
61
62                 String rollbackNetworkRequest =
63                 """<NetworkAdapter:rollbackNetwork xmlns:NetworkAdapter="http://org.onap.so/network">
64    <rollback>
65       <networkId>MNS-25180-L-01-dmz_direct_net_1/2c88a3a9-69b9-43a7-ada6-1aca577c3641</networkId>
66       <neutronNetworkId>c4f4e878-cde0-4b15-ae9a-bda857759cea</neutronNetworkId>
67       <networkStackId/>
68       <networkType>CONTRAIL_EXTERNAL</networkType>
69       <networkCreated>true</networkCreated>
70       <tenantId>7dd5365547234ee8937416c65507d266</tenantId>
71       <cloudSiteId>RDM2WAGPLCP</cloudSiteId>
72       <msoRequest>
73          <requestId>1ef47428-cade-45bd-a103-0751e8b2deb0</requestId>
74          <serviceInstanceId/>
75       </msoRequest>
76    </rollback>
77 </NetworkAdapter:rollbackNetwork>"""
78
79                                         String rollbackActivateSDNCRequest =
80                 """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
81                                   xmlns:ns5="http://org.onap/so/request/types/v1"
82                                   xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1">
83    <sdncadapter:RequestHeader>
84       <sdncadapter:RequestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</sdncadapter:RequestId>
85       <sdncadapter:SvcInstanceId>f70e927b-6087-4974-9ef8-c5e4d5847ca4</sdncadapter:SvcInstanceId>
86       <sdncadapter:SvcAction>rollback</sdncadapter:SvcAction>
87       <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
88       <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
89       <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
90    </sdncadapter:RequestHeader>
91    <aetgt:SDNCRequestData>
92       <request-information>
93          <request-id>88f65519-9a38-4c4b-8445-9eb4a5a5af56</request-id>
94          <request-action>CreateNetworkInstance</request-action>
95          <source>VID</source>
96          <notification-url/>
97          <order-number/>
98          <order-version/>
99       </request-information>
100       <service-information>
101          <service-id>a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb</service-id>
102          <service-type>MSO-dev-service-type</service-type>
103          <service-instance-id>f70e927b-6087-4974-9ef8-c5e4d5847ca4</service-instance-id>
104          <subscriber-name>MSO_1610_dev</subscriber-name>
105       </service-information>
106       <network-information>
107          <network-id>8abc633a-810b-4ca5-8b3a-09511d13a2ce</network-id>
108          <network-type>CONTRAIL_EXTERNAL</network-type>
109          <onap-model-information>
110             <model-invariant-uuid>invariant-uuid</model-invariant-uuid>
111             <model-customization-uuid>customization-uuid</model-customization-uuid>
112             <model-uuid>uuid</model-uuid>
113             <model-version>version</model-version>
114             <model-name>CONTRAIL_EXTERNAL</model-name>
115          </onap-model-information>
116       </network-information>
117       <network-request-information>
118          <network-id>8abc633a-810b-4ca5-8b3a-09511d13a2ce</network-id>
119          <network-type>CONTRAIL_EXTERNAL</network-type>
120          <network-name>MNS-25180-L-01-dmz_direct_net_1</network-name>
121          <tenant>7dd5365547234ee8937416c65507d266</tenant>
122          <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>
123       </network-request-information>
124    </aetgt:SDNCRequestData>
125 </aetgt:SDNCAdapterWorkflowRequest>"""
126
127                                         String rollbackSDNCRequest =
128                 """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
129                                   xmlns:ns5="http://org.onap/so/request/types/v1"
130                                   xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1">
131    <sdncadapter:RequestHeader>
132       <sdncadapter:RequestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</sdncadapter:RequestId>
133       <sdncadapter:SvcInstanceId>f70e927b-6087-4974-9ef8-c5e4d5847ca4</sdncadapter:SvcInstanceId>
134       <sdncadapter:SvcAction>rollback</sdncadapter:SvcAction>
135       <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
136       <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
137       <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
138    </sdncadapter:RequestHeader>
139    <aetgt:SDNCRequestData>
140       <request-information>
141          <request-id>88f65519-9a38-4c4b-8445-9eb4a5a5af56</request-id>
142          <request-action>CreateNetworkInstance</request-action>
143          <source>VID</source>
144          <notification-url/>
145          <order-number/>
146          <order-version/>
147       </request-information>
148       <service-information>
149          <service-id>a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb</service-id>
150          <service-type>MSO-dev-service-type</service-type>
151          <service-instance-id>f70e927b-6087-4974-9ef8-c5e4d5847ca4</service-instance-id>
152          <subscriber-name>MSO_1610_dev</subscriber-name>
153       </service-information>
154       <network-information>
155          <network-id>8abc633a-810b-4ca5-8b3a-09511d13a2ce</network-id>
156          <network-type>CONTRAIL_EXTERNAL</network-type>
157          <onap-model-information>
158             <model-invariant-uuid>invariant-uuid</model-invariant-uuid>
159             <model-customization-uuid>customization-uuid</model-customization-uuid>
160             <model-uuid>uuid</model-uuid>
161             <model-version>version</model-version>
162             <model-name>CONTRAIL_EXTERNAL</model-name>
163          </onap-model-information>
164       </network-information>
165       <network-request-information>
166          <network-id>8abc633a-810b-4ca5-8b3a-09511d13a2ce</network-id>
167          <network-type>CONTRAIL_EXTERNAL</network-type>
168          <network-name>MNS-25180-L-01-dmz_direct_net_1</network-name>
169          <tenant>7dd5365547234ee8937416c65507d266</tenant>
170          <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>
171       </network-request-information>
172    </aetgt:SDNCRequestData>
173 </aetgt:SDNCAdapterWorkflowRequest>"""
174
175 // - - - - - - - -
176
177
178             @Before
179                 public void init()
180                 {
181                         MockitoAnnotations.initMocks(this)
182                 }
183
184                 public void initializeVariables (DelegateExecution mockExecution) {
185
186                         verify(mockExecution).setVariable(Prefix + "rollbackNetworkRequest", null)
187                         verify(mockExecution).setVariable(Prefix + "rollbackSDNCRequest", null)
188                         verify(mockExecution).setVariable(Prefix + "WorkflowException", null)
189
190                         verify(mockExecution).setVariable(Prefix + "rollbackNetworkRequest", "")
191                         verify(mockExecution).setVariable(Prefix + "rollbackNetworkResponse", "")
192                         verify(mockExecution).setVariable(Prefix + "rollbackNetworkReturnCode", "")
193
194                         verify(mockExecution).setVariable(Prefix + "rollbackSDNCRequest", "")
195                         verify(mockExecution).setVariable(Prefix + "rollbackSDNCResponse", "")
196                         verify(mockExecution).setVariable(Prefix + "rollbackSDNCReturnCode", "")
197
198                         verify(mockExecution).setVariable(Prefix + "Success", false)
199                         verify(mockExecution).setVariable(Prefix + "fullRollback", false)
200
201
202                 }
203
204                 @Test
205                 //@Ignore
206                 public void preProcessRequest() {
207
208                         println "************ preProcessRequest ************* "
209
210                         WorkflowException workflowException = new WorkflowException("DoUpdateNetworkInstanceRollback", 2500, "Received error from Network Adapter: JBWEB000065: HTTP Status 500.")
211                         Map<String, String> rollbackData = new HashMap<String, String>();
212                         rollbackData.put("rollbackSDNCRequest", rollbackSDNCRequest)
213                         rollbackData.put("rollbackNetworkRequest", rollbackNetworkRequest)
214
215                         ExecutionEntity mockExecution = setupMock()
216                         // Initialize prerequisite variables
217                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
218                         when(mockExecution.getVariable("prefix")).thenReturn(Prefix + "")
219                         when(mockExecution.getVariable("WorkflowException")).thenReturn(workflowException)
220                         when(mockExecution.getVariable("rollbackData")).thenReturn(rollbackData)
221                         when(mockExecution.getVariable("sdncVersion")).thenReturn("1610")
222                         when(mockExecution.getVariable("mso.adapters.po.auth")).thenReturn("3141634BF7E070AA289CF2892C986C0B")
223                         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
224
225                         when(mockExecution.getVariable("mso.adapters.sdnc.endpoint")).thenReturn("http://localhost:28090/SDNCAdapter")
226                         when(mockExecution.getVariable("mso.adapters.network.rest.endpoint")).thenReturn("http://localhost:28090/networks/NetworkAdapter")
227                         when(mockExecution.getVariable("mso.adapters.sdnc.resource.endpoint")).thenReturn("http://localhost:28090/SDNCAdapterRpc")
228
229
230                         // preProcessRequest(DelegateExecution execution)
231                         DoUpdateNetworkInstanceRollback DoUpdateNetworkInstanceRollback = new DoUpdateNetworkInstanceRollback()
232                         DoUpdateNetworkInstanceRollback.preProcessRequest(mockExecution)
233
234 //                      verify(mockExecution).getVariable("isDebugLogEnabled")
235                         verify(mockExecution).setVariable("prefix", Prefix)
236
237                         //verify variable initialization
238                         initializeVariables(mockExecution)
239
240                 }
241
242
243                 @Test
244                 @Ignore
245                 public void validateRollbackResponses_Good() {
246
247                         WorkflowException workflowException = new WorkflowException("DoUpdateNetworkInstanceRollback", 2500, "AAI Update Contrail Failed.  Error 404.")
248                         WorkflowException expectedWorkflowException = new WorkflowException("DoUpdateNetworkInstanceRollback", 2500, "AAI Update Contrail Failed.  Error 404. + SNDC activate rollback completed. + PO Network rollback completed. + SNDC assign rollback completed.")
249
250                         println "************ validateRollbackResponses_Good() ************* "
251                         ExecutionEntity mockExecution = setupMock()
252                         // Initialize prerequisite variables
253                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
254                         when(mockExecution.getVariable("prefix")).thenReturn(Prefix + "")
255                         when(mockExecution.getVariable(Prefix + "rollbackNetworkRequest")).thenReturn("Good")
256                         when(mockExecution.getVariable(Prefix + "rollbackSDNCRequest")).thenReturn("Good")
257                         when(mockExecution.getVariable(Prefix + "rollbackNetworkReturnCode")).thenReturn("200")
258                         when(mockExecution.getVariable(Prefix + "rollbackNetworkResponse")).thenReturn("GoodResponse")
259                         when(mockExecution.getVariable(Prefix + "rollbackSDNCReturnCode")).thenReturn("200")
260                         when(mockExecution.getVariable(Prefix + "rollbackSDNCResponse")).thenReturn("GoodResponse")
261                         when(mockExecution.getVariable(Prefix + "WorkflowException")).thenReturn(workflowException)
262                         when(mockExecution.getVariable(Prefix + "fullRollback")).thenReturn(false)
263
264                         DoUpdateNetworkInstanceRollback DoUpdateNetworkInstanceRollback = new DoUpdateNetworkInstanceRollback()
265                         DoUpdateNetworkInstanceRollback.validateRollbackResponses(mockExecution)
266
267                         // verify set prefix = Prefix + ""
268                         verify(mockExecution, atLeast(1)).setVariable("prefix", Prefix)
269                         verify(mockExecution, atLeast(1)).setVariable("rolledBack", true)
270                         verify(mockExecution, atLeast(1)).setVariable("wasDeleted", true)
271                         verify(mockExecution).setVariable("WorkflowException", refEq(expectedWorkflowException, any(WorkflowException.class)))
272                         //verify(mockExecution).setVariable("WorkflowException", expectedWorkflowException)
273                 }
274
275                 @Test
276                 //@Ignore
277                 public void validateRollbackResponses_FullRollback() {
278
279                         Map<String, String> rollbackData = new HashMap<String, String>();
280                         rollbackData.put("rollbackSDNCRequest", rollbackSDNCRequest)
281                         rollbackData.put("rollbackNetworkRequest", rollbackNetworkRequest)
282
283                         println "************ validateRollbackResponses_FullRollback() ************* "
284                         ExecutionEntity mockExecution = setupMock()
285                         // Initialize prerequisite variables
286                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
287                         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
288                         when(mockExecution.getVariable(Prefix + "rollbackNetworkRequest")).thenReturn("Good")
289                         when(mockExecution.getVariable(Prefix + "rollbackSDNCRequest")).thenReturn("Good")
290                         when(mockExecution.getVariable(Prefix + "rollbackNetworkReturnCode")).thenReturn("200")
291                         when(mockExecution.getVariable(Prefix + "rollbackNetworkResponse")).thenReturn("GoodResponse")
292                         when(mockExecution.getVariable(Prefix + "rollbackSDNCReturnCode")).thenReturn("200")
293                         when(mockExecution.getVariable(Prefix + "rollbackSDNCResponse")).thenReturn("GoodResponse")
294                         when(mockExecution.getVariable(Prefix + "WorkflowException")).thenReturn(null)
295                         when(mockExecution.getVariable(Prefix + "fullRollback")).thenReturn(true)
296                         when(mockExecution.getVariable("rollbackData")).thenReturn(rollbackData)
297
298                         DoUpdateNetworkInstanceRollback DoUpdateNetworkInstanceRollback = new DoUpdateNetworkInstanceRollback()
299                         DoUpdateNetworkInstanceRollback.validateRollbackResponses(mockExecution)
300
301                         // verify set prefix = Prefix + ""
302                         verify(mockExecution, atLeast(1)).setVariable("prefix", Prefix)
303                         verify(mockExecution, atLeast(1)).setVariable("rollbackSuccessful", true)
304                         verify(mockExecution, atLeast(1)).setVariable("rollbackError", false)
305
306                 }
307
308
309                 private ExecutionEntity setupMock() {
310
311                         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
312                         when(mockProcessDefinition.getKey()).thenReturn("DoUpdateNetworkInstanceRollback")
313                         RepositoryService mockRepositoryService = mock(RepositoryService.class)
314                         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
315                         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoUpdateNetworkInstanceRollback")
316                         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
317                         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
318                         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
319
320                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
321                         // Initialize prerequisite variables
322
323                         when(mockExecution.getId()).thenReturn("100")
324                         when(mockExecution.getProcessDefinitionId()).thenReturn("DoUpdateNetworkInstanceRollback")
325                         when(mockExecution.getProcessInstanceId()).thenReturn("DoUpdateNetworkInstanceRollback")
326                         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
327                         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
328
329                         return mockExecution
330                 }
331
332 }