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