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