Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteNetworkInstanceTest.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.apache.commons.lang3.*
27 import org.camunda.bpm.engine.ProcessEngineServices
28 import org.camunda.bpm.engine.RepositoryService
29 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
30 import org.camunda.bpm.engine.repository.ProcessDefinition
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.junit.Before
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
43
44 @RunWith(MockitoJUnitRunner.class)
45 class DeleteNetworkInstanceTest  {
46
47         @Rule
48         public WireMockRule wireMockRule = new WireMockRule(8090);
49         
50         String Prefix="DELNI_"
51         def utils = new MsoUtils()
52
53         String falloutHandlerRequest = 
54     """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
55                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
56                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
57                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
58                                               <request-id>b69c9054-da09-4a2c-adf5-51042b62bfac</request-id>
59                                               <action>DELETE</action>
60                                               <source>PORTAL</source>
61                                            </request-info>
62                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
63    <aetgt:ErrorMessage>Received error from SDN-C: No availability zone available</aetgt:ErrorMessage>
64    <aetgt:ErrorCode>5300</aetgt:ErrorCode>
65    <aetgt:SourceSystemErrorCode>200</aetgt:SourceSystemErrorCode>
66 </aetgt:WorkflowException>
67                                         </aetgt:FalloutHandlerRequest>"""
68
69         String falloutHandlerRequestObj =
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>DELETE</action>
76                                               <source>PORTAL</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>7000</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>DELETE</action>
91       <source>VID</source>
92    </request-info>
93    <aetgt:status-message>Network has been deleted successfully.</aetgt:status-message>
94    <aetgt:mso-bpel-name>BPMN Network action: DELETE</aetgt:mso-bpel-name>
95 </aetgt:MsoCompletionRequest>"""
96
97 String deleteDBRequest =
98 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
99                   xmlns:ns="http://org.onap.so/requestsdb">
100    <soapenv:Header/>
101    <soapenv:Body>
102       <ns:updateInfraRequest>
103          <requestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</requestId>
104          <lastModifiedBy>BPMN</lastModifiedBy>
105          <statusMessage>Network successfully deleted.</statusMessage>
106          <responseBody/>
107          <requestStatus>COMPLETED</requestStatus>
108          <progress>100</progress>
109          <vnfOutputs>&lt;network-outputs xmlns="http://org.onap/so/infra/vnf-request/v1" xmlns:aetgt="http://org.onap/so/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"/&gt;</vnfOutputs>
110       </ns:updateInfraRequest>
111    </soapenv:Body>
112 </soapenv:Envelope>"""  
113
114 String deleteDBRequestErrorObj =
115 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
116                                                 <soapenv:Header/>
117                                                 <soapenv:Body>
118                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
119                                                                 <requestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</requestId>
120                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
121                                                                 <statusMessage>Received error from SDN-C: No availability zone available</statusMessage>
122                                                                 <responseBody></responseBody>
123                                                                 <requestStatus>FAILED</requestStatus>
124                                                                 <progress></progress>
125                                                                 <vnfOutputs>&lt;network-outputs xmlns="http://org.onap/so/infra/vnf-request/v1" xmlns:aetgt="http://org.onap/so/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"/&gt;</vnfOutputs>
126                                                         </ns:updateInfraRequest>
127                                                 </soapenv:Body>
128                                            </soapenv:Envelope>"""
129
130 String deleteDBRequestErrorString =
131 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
132                                                 <soapenv:Header/>
133                                                 <soapenv:Body>
134                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
135                                                                 <requestId>88f65519-9a38-4c4b-8445-9eb4a5a5af56</requestId>
136                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
137                                                                 <statusMessage>Received error from SDN-C: No availability zone available</statusMessage>
138                                                                 <responseBody></responseBody>
139                                                                 <requestStatus>FAILED</requestStatus>
140                                                                 <progress></progress>
141                                                                 <vnfOutputs>&lt;network-outputs xmlns="http://org.onap/so/infra/vnf-request/v1" xmlns:aetgt="http://org.onap/so/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"/&gt;</vnfOutputs>
142                                                         </ns:updateInfraRequest>
143                                                 </soapenv:Body>
144                                            </soapenv:Envelope>"""
145                                            
146                                            String jsonIncomingRequest =
147                                            """{ "requestDetails": {
148               "modelInfo": {
149                         "modelType": "networkTyp",
150                         "modelId": "modelId",
151                         "modelNameVersionId": "modelNameVersionId",
152                         "modelName": "CONTRAIL_EXTERNAL",
153                         "modelVersion": "1"
154                   },
155                   "cloudConfiguration": {
156                         "lcpCloudRegionId": "RDM2WAGPLCP",
157                         "tenantId": "7dd5365547234ee8937416c65507d266"
158                   },
159                   "requestInfo": {
160                         "instanceName": "MNS-25180-L-01-dmz_direct_net_1",
161                         "source": "VID",
162                         "callbackUrl": "",
163             "suppressRollback": true,
164                 "productFamilyId": "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"
165                   },
166                   "relatedInstanceList": [ 
167                         {  
168                         "relatedInstance": {
169                                 "instanceId": "f70e927b-6087-4974-9ef8-c5e4d5847ca4",
170                                 "modelInfo": {   
171                                         "modelType": "serviceT",
172                                         "modelId": "modelI",
173                                         "modelNameVersionId": "modelNameVersionI",
174                                         "modelName": "modleNam",
175                                         "modelVersion": "1"
176                                 }
177                         }
178                 }
179                   ],
180                   "requestParameters": {
181                         "userParams": [ 
182                {
183                                  "name": "someUserParam1",
184                                  "value": "someValue1"
185                            }
186             ]
187                   }
188   }}""" 
189                                            
190 // - - - - - - - -
191
192
193             @Before
194                 public void init()
195                 {
196                         MockitoAnnotations.initMocks(this)
197                 }
198                 
199                 @Test
200                 //@Ignore  
201                 public void preProcessRequest() {
202                         
203                         println "************ preProcessRequest_Payload ************* " 
204                         ExecutionEntity mockExecution = setupMock()
205                         // Initialize prerequisite variables
206                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
207                         when(mockExecution.getVariable("bpmnRequest")).thenReturn(jsonIncomingRequest)
208                                                                         
209                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
210                         DeleteNetworkInstance.preProcessRequest(mockExecution)
211                         
212                         verify(mockExecution).getVariable("isDebugLogEnabled")
213                         verify(mockExecution).setVariable("prefix", Prefix)
214                         
215                         //verify variable initialization
216                         initializeVariables(mockExecution)
217
218
219                 }
220
221                 public void initializeVariables (DelegateExecution mockExecution) {
222                         
223                         
224                         verify(mockExecution).setVariable(Prefix + "Success", false)
225                         
226                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", "")
227                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", "")
228                         verify(mockExecution).setVariable(Prefix + "isSilentSuccess", false)
229                 
230                 }
231                 
232                         
233                 @Test
234                 //@Ignore
235                 public void getNetworkModelInfo() {
236                         
237                         println "************ getNetworkModelInfo() ************* "
238                         
239                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
240                         // Initialize prerequisite variables
241                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
242                                                                         
243                         // preProcessRequest(DelegateExecution execution)
244                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
245                         DeleteNetworkInstance.getNetworkModelInfo(mockExecution)
246
247                         verify(mockExecution).getVariable("isDebugLogEnabled")
248                         verify(mockExecution).setVariable("prefix", Prefix)
249                                                         
250                 }
251                         
252                 @Test
253                 //@Ignore
254                 public void sendSyncResponse() {
255                         
256                         println "************ sendSyncResponse ************* "
257                         
258                         ExecutionEntity mockExecution = setupMock()
259                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
260                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
261                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
262                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
263                         
264                         // preProcessRequest(DelegateExecution execution)
265                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
266                         DeleteNetworkInstance.sendSyncResponse(mockExecution)
267
268                         verify(mockExecution).setVariable("prefix", Prefix)
269                         verify(mockExecution).setVariable("DeleteNetworkInstanceResponseCode", "202")
270                         
271                 }
272                 
273                 
274                 @Test
275                 //@Ignore
276                 public void sendSyncError() {
277                         
278                         println "************ sendSyncError ************* "
279                         
280                         ExecutionEntity mockExecution = setupMock()
281                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
282                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
283                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
284                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
285                         
286                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
287                         DeleteNetworkInstance.sendSyncError(mockExecution)
288
289                         verify(mockExecution).setVariable("prefix", Prefix)
290                         verify(mockExecution).setVariable("DeleteNetworkInstanceResponseCode", "500")
291                         
292                 }
293                 
294
295                 @Test
296                 //@Ignore
297                 public void prepareCompletion() {
298                         
299                         println "************ postProcessResponse ************* "
300                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
301                         // Initialize prerequisite variables
302                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
303                         when(mockExecution.getVariable(Prefix + "source")).thenReturn("PORTAL")
304                         when(mockExecution.getVariable("mso-request-id")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
305                         
306                         // postProcessResponse(DelegateExecution execution)
307                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
308                         DeleteNetworkInstance.prepareCompletion(mockExecution)
309
310                         // check the sequence of variable invocation
311                         //MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
312                         //preDebugger.printInvocations(mockExecution)
313
314                         verify(mockExecution).getVariable("isDebugLogEnabled")
315                         verify(mockExecution).setVariable("prefix", Prefix)
316                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", completeMsoProcessRequest)
317                 
318                 }
319                 
320                 
321                 @Test
322                 //@Ignore
323                 public void prepareDBRequestError() {
324                         
325                         println "************ prepareDBRequestError ************* "
326                         
327                         WorkflowException sndcWorkflowExceptionObj = new WorkflowException("CreateNetworkV2", 500, "Received error from SDN-C: No availability zone available")
328                         
329                         ExecutionEntity mockExecution = setupMock()
330                         // Initialize prerequisite variables
331                         when(mockExecution.getVariable(Prefix + "requestId")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
332                         when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowExceptionObj)
333                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
334                         when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
335                         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
336
337                         // preProcessRequest(DelegateExecution execution)
338                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
339                         DeleteNetworkInstance.prepareDBRequestError(mockExecution)
340                         
341                         verify(mockExecution).setVariable("prefix", Prefix)
342                         verify(mockExecution).setVariable(Prefix + "deleteDBRequest", deleteDBRequestErrorObj)
343                 
344                 }
345                 
346         
347                 @Test
348                 //@Ignore
349                 public void postProcessResponse() {
350                         
351                         println "************ postProcessResponse ************* "
352                         ExecutionEntity mockExecution = setupMock()
353                         // Initialize prerequisite variables
354                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
355                         when(mockExecution.getVariable("CMSO_ResponseCode")).thenReturn("200")
356                         
357                         // postProcessResponse(DelegateExecution execution)                                             
358                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
359                         DeleteNetworkInstance.postProcessResponse(mockExecution)
360
361                         verify(mockExecution).setVariable("prefix", Prefix)
362                         verify(mockExecution).setVariable(Prefix + "Success", true)
363
364                 
365                 }
366
367                 
368                 @Test
369                 //@Ignore
370                 public void buildErrorResponse_FalloutHandlerRequest() {
371                         
372                         println "************ buildErrorResponse ************* "
373                         
374                         WorkflowException sndcWorkflowExceptionObj = new WorkflowException("DeleteNetworkInstance", 7000, "Received error from SDN-C: No availability zone available")
375                         
376                         ExecutionEntity mockExecution = setupMock()
377                         // Initialize prerequisite variables
378                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
379                         when(mockExecution.getVariable("mso-request-id")).thenReturn("b69c9054-da09-4a2c-adf5-51042b62bfac")
380                         when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowExceptionObj)
381                         when(mockExecution.getVariable(Prefix + "source")).thenReturn("PORTAL")
382                         
383                         // buildErrorResponse(DelegateExecution execution)
384                         DeleteNetworkInstance DeleteNetworkInstance = new DeleteNetworkInstance()
385                         DeleteNetworkInstance.buildErrorResponse(mockExecution)
386                         
387                         verify(mockExecution).setVariable("prefix", Prefix)
388                         verify(mockExecution).setVariable(Prefix + "Success", false)
389                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequestObj)
390                         
391                 }
392         
393         
394                 private ExecutionEntity setupMock() {
395                 
396                         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
397                         when(mockProcessDefinition.getKey()).thenReturn("DeleteNetworkInstance")
398                         RepositoryService mockRepositoryService = mock(RepositoryService.class)
399                         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
400                         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DeleteNetworkInstance")
401                         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
402                         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
403                         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
404                         
405                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
406                         // Initialize prerequisite variables
407                         
408                         when(mockExecution.getId()).thenReturn("100")
409                         when(mockExecution.getProcessDefinitionId()).thenReturn("DeleteNetworkInstance")
410                         when(mockExecution.getProcessInstanceId()).thenReturn("DeleteNetworkInstance")
411                         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
412                         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
413
414                         return mockExecution
415                         
416                 }
417                 
418 }