Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / UpdateNetworkInstanceTest.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
24 import static org.mockito.Mockito.*
25
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.junit.Before
32 import org.junit.Ignore
33 import org.junit.Rule
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 import org.mockito.MockitoAnnotations
37 import org.mockito.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.common.scripts.MsoUtils
39 import org.onap.so.bpmn.core.WorkflowException
40
41 import com.github.tomakehurst.wiremock.junit.WireMockRule
42 import org.apache.commons.lang3.*
43
44
45 @RunWith(MockitoJUnitRunner.class)
46 class UpdateNetworkInstanceTest  {
47
48         @Rule
49         public WireMockRule wireMockRule = new WireMockRule(8090);
50
51                 String Prefix="UPDNI_"
52                 def utils = new MsoUtils()
53
54                 String createDBRequestError =
55 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
56                                                 <soapenv:Header/>
57                                                 <soapenv:Body>
58                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
59                                                                 <requestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</requestId>
60                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
61                                                                 <statusMessage>Received error from SDN-C: No availability zone available</statusMessage>
62                                                                 <responseBody></responseBody>
63                                                                 <requestStatus>FAILED</requestStatus>
64                                                                 <vnfOutputs>&lt;network-id&gt;&lt;/network-id&gt;&lt;network-name&gt;&lt;/network-names&gt;</vnfOutputs>
65                                                         </ns:updateInfraRequest>
66                                                 </soapenv:Body>
67                                            </soapenv:Envelope>"""
68
69           String falloutHandlerRequest =
70                                            """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
71                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
72                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
73                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
74                                               <request-id>b69c9054-da09-4a2c-adf5-51042b62bfac</request-id>
75                                               <action>UPDATE</action>
76                                               <source>VID</source>
77                                            </request-info>
78                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
79                                                         <aetgt:ErrorMessage>Received error from SDN-C: No availability zone available.</aetgt:ErrorMessage>
80                                                         <aetgt:ErrorCode>5300</aetgt:ErrorCode>
81                                                 </aetgt:WorkflowException>
82                                         </aetgt:FalloutHandlerRequest>"""
83
84            String completeMsoProcessRequest =
85                                            """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
86                             xmlns:ns="http://org.onap/so/request/types/v1"
87                             xmlns="http://org.onap/so/infra/vnf-request/v1">
88    <request-info>
89       <request-id>88f65519-9a38-4c4b-8445-9eb4a5a5af56</request-id>
90       <action>UPDATE</action>
91       <source>VID</source>
92    </request-info>
93    <aetgt:status-message>Network has been updated successfully.</aetgt:status-message>
94    <aetgt:mso-bpel-name>BPMN Network action: UPDATE</aetgt:mso-bpel-name>
95 </aetgt:MsoCompletionRequest>"""
96
97
98 String jsonIncomingRequest =
99 """{ "requestDetails": {
100               "modelInfo": {
101                         "modelType": "networkTyp",
102                         "modelId": "modelId",
103                         "modelNameVersionId": "modelNameVersionId",
104                         "modelName": "CONTRAIL_EXTERNAL",
105                         "modelVersion": "1"
106                   },
107                   "cloudConfiguration": {
108                         "lcpCloudRegionId": "RDM2WAGPLCP",
109                         "tenantId": "7dd5365547234ee8937416c65507d266"
110                   },
111                   "requestInfo": {
112                         "instanceName": "MNS-25180-L-01-dmz_direct_net_1",
113                         "source": "VID",
114                         "callbackUrl": "",
115             "suppressRollback": true,
116                 "productFamilyId": "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"
117                   },
118                   "relatedInstanceList": [
119                         {
120                         "relatedInstance": {
121                                 "instanceId": "f70e927b-6087-4974-9ef8-c5e4d5847ca4",
122                                 "modelInfo": {
123                                         "modelType": "serviceT",
124                                         "modelId": "modelI",
125                                         "modelNameVersionId": "modelNameVersionI",
126                                         "modelName": "modleNam",
127                                         "modelVersion": "1"
128                                 }
129                         }
130                 }
131                   ],
132                   "requestParameters": {
133                         "userParams": [
134                {
135                                  "name": "someUserParam1",
136                                  "value": "someValue1"
137                            }
138             ]
139                   }
140   }}"""
141
142             @Before
143                 public void init()
144                 {
145                         MockitoAnnotations.initMocks(this)
146                 }
147
148                 public void initializeVariables(DelegateExecution mockExecution) {
149
150                         verify(mockExecution).setVariable(Prefix + "source", "")
151                         verify(mockExecution).setVariable(Prefix + "Success", false)
152
153                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", "")
154                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", "")
155
156                 }
157
158                 @Test
159                 //@Ignore
160                 public void preProcessRequest() {
161
162                         println "************ preProcessRequest() ************* "
163                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
164                         // Initialize prerequisite variables
165                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
166                         when(mockExecution.getVariable("bpmnRequest")).thenReturn(jsonIncomingRequest)
167
168                         when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
169                         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
170                         
171                         // preProcessRequest(DelegateExecution execution)
172                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
173                         UpdateNetworkInstance.preProcessRequest(mockExecution)
174
175 //                      verify(mockExecution).getVariable("isDebugLogEnabled")
176                         verify(mockExecution).setVariable("prefix", Prefix)
177
178                         initializeVariables(mockExecution)
179                         //verify(mockExecution).setVariable(Prefix + "Success", false)
180
181                 }
182
183
184                 @Test
185                 //@Ignore
186                 public void getNetworkModelInfo() {
187
188                         println "************ getNetworkModelInfo() ************* "
189
190                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
191                         // Initialize prerequisite variables
192                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
193
194                         // preProcessRequest(DelegateExecution execution)
195                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
196                         UpdateNetworkInstance.getNetworkModelInfo(mockExecution)
197
198 //                      verify(mockExecution).getVariable("isDebugLogEnabled")
199                         verify(mockExecution).setVariable("prefix", Prefix)
200
201                 }
202
203                 @Test
204                 //@Ignore
205                 public void sendSyncResponse() {
206
207                         println "************ sendSyncResponse ************* "
208
209                         ExecutionEntity mockExecution = setupMock()
210                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
211                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
212                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
213                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
214
215                         // preProcessRequest(DelegateExecution execution)
216                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
217                         UpdateNetworkInstance.sendSyncResponse(mockExecution)
218
219                         verify(mockExecution).setVariable("prefix", Prefix)
220                         verify(mockExecution).setVariable("UpdateNetworkInstanceResponseCode", "202")
221
222                 }
223
224                 @Test
225                 //@Ignore
226                 public void sendSyncError() {
227
228                         println "************ sendSyncError ************* "
229
230                         ExecutionEntity mockExecution = setupMock()
231                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
232                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
233                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
234                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
235
236                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
237                         UpdateNetworkInstance.sendSyncError(mockExecution)
238
239                         verify(mockExecution).setVariable("prefix", Prefix)
240                         verify(mockExecution).setVariable("UpdateNetworkInstanceResponseCode", "500")
241
242                 }
243
244                 @Test
245                 //@Ignore
246                 public void prepareCompletion() {
247
248                         println "************ postProcessResponse ************* "
249                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
250                         // Initialize prerequisite variables
251                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
252                         when(mockExecution.getVariable("mso-request-id")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
253                         when(mockExecution.getVariable(Prefix + "dbReturnCode")).thenReturn("200")
254
255                         // postProcessResponse(DelegateExecution execution)
256                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
257                         UpdateNetworkInstance.prepareCompletion(mockExecution)
258
259                         // check the sequence of variable invocation
260                         //MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
261                         //preDebugger.printInvocations(mockExecution)
262
263                         verify(mockExecution).setVariable("prefix", Prefix)
264                         verify(mockExecution).setVariable(Prefix + "Success", true)
265                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", completeMsoProcessRequest)
266
267                 }
268
269                 @Test
270                 //@Ignore
271                 public void buildErrorResponse() {
272
273                         println "************ buildErrorResponse ************* "
274
275
276                         WorkflowException sndcWorkflowException = new WorkflowException("UpdateNetworkInstance", 5300, "Received error from SDN-C: No availability zone available.")
277
278                         ExecutionEntity mockExecution = setupMock()
279                         // Initialize prerequisite variables
280                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
281                         when(mockExecution.getVariable("mso-request-id")).thenReturn("b69c9054-da09-4a2c-adf5-51042b62bfac")
282                         //when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowException)
283                         when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowException)
284
285                         // buildErrorResponse(DelegateExecution execution)
286                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
287                         UpdateNetworkInstance.buildErrorResponse(mockExecution)
288
289                         verify(mockExecution, atLeast(1)).setVariable("prefix", Prefix)
290                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest)
291
292                         //MockitoDebuggerImpl debugger = new MockitoDebuggerImpl()
293                         //debugger.printInvocations(mockExecution)
294
295                 }
296
297                 @Test
298                 //@Ignore
299                 public void postProcessResponse() {
300
301                         println "************ postProcessResponse() ************* "
302                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
303                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
304                         when(mockExecution.getVariable("CMSO_ResponseCode")).thenReturn("200")
305
306                         // postProcessResponse(DelegateExecution execution)
307                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
308                         UpdateNetworkInstance.postProcessResponse(mockExecution)
309
310                         //verify(mockExecution).getVariable("isDebugLogEnabled")
311                         //verify(mockExecution).setVariable("prefix", Prefix)
312
313                         verify(mockExecution).setVariable(Prefix + "Success", true)
314
315                 }
316
317                 @Test
318                 //@Ignore
319                 public void processRollbackData() {
320
321                         println "************ callDBCatalog() ************* "
322                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
323                         // Initialize prerequisite variables
324                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
325
326                         // preProcessRequest(DelegateExecution execution)
327                         UpdateNetworkInstance UpdateNetworkInstance = new UpdateNetworkInstance()
328                         UpdateNetworkInstance.processRollbackData(mockExecution)
329
330 //                      verify(mockExecution).getVariable("isDebugLogEnabled")
331                         verify(mockExecution).setVariable("prefix", Prefix)
332
333                 }
334
335                 private ExecutionEntity setupMock() {
336
337                         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
338                         when(mockProcessDefinition.getKey()).thenReturn("UpdateNetworkInstance")
339                         RepositoryService mockRepositoryService = mock(RepositoryService.class)
340                         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
341                         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("UpdateNetworkInstance")
342                         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
343                         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
344                         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
345
346                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
347                         // Initialize prerequisite variables
348
349                         when(mockExecution.getId()).thenReturn("100")
350                         when(mockExecution.getProcessDefinitionId()).thenReturn("UpdateNetworkInstance")
351                         when(mockExecution.getProcessInstanceId()).thenReturn("UpdateNetworkInstance")
352                         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
353                         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
354
355                         return mockExecution
356                 }
357
358 }