4d5f5065996881e5e1926efa0c92838351944609
[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:mso-bpel-name>BPMN Network action: CREATE</aetgt:mso-bpel-name>
89 </aetgt:MsoCompletionRequest>"""
90
91
92 String jsonIncomingRequest =
93 """{ "requestDetails": {
94               "modelInfo": {
95                         "modelType": "networkTyp",
96                         "modelId": "modelId",
97                         "modelNameVersionId": "modelNameVersionId",
98                         "modelName": "CONTRAIL_EXTERNAL",
99                         "modelVersion": "1"
100                   },
101                   "cloudConfiguration": {
102                         "lcpCloudRegionId": "RDM2WAGPLCP",
103                         "tenantId": "7dd5365547234ee8937416c65507d266"
104                   },
105                   "requestInfo": {
106                         "instanceName": "MNS-25180-L-01-dmz_direct_net_1",
107                         "source": "VID",
108                         "callbackUrl": "",
109             "suppressRollback": true,
110                 "productFamilyId": "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb"
111                   },
112                   "relatedInstanceList": [ 
113                         {  
114                         "relatedInstance": {
115                                 "instanceId": "f70e927b-6087-4974-9ef8-c5e4d5847ca4",
116                                 "modelInfo": {   
117                                         "modelType": "serviceT",
118                                         "modelId": "modelI",
119                                         "modelNameVersionId": "modelNameVersionI",
120                                         "modelName": "modleNam",
121                                         "modelVersion": "1"
122                                 }
123                         }
124                 }
125                   ],
126                   "requestParameters": {
127                         "userParams": [ 
128                {
129                                  "name": "someUserParam1",
130                                  "value": "someValue1"
131                            }
132             ]
133                   }
134   }}""" 
135
136             @Before
137                 public void init()
138                 {
139                         MockitoAnnotations.initMocks(this)
140                 }
141
142                 public void initializeVariables(DelegateExecution mockExecution) {
143                         
144                         verify(mockExecution).setVariable(Prefix + "Success", false)
145                         
146                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", "")
147                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", "")
148                         verify(mockExecution).setVariable(Prefix + "isSilentSuccess", false)
149                                 
150                 }
151                                 
152                 @Test
153                 //@Ignore  
154                 public void preProcessRequest() {
155                         
156                         println "************ preProcessRequest() ************* " 
157                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
158                         // Initialize prerequisite variables
159                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
160                         when(mockExecution.getVariable("bpmnRequest")).thenReturn(jsonIncomingRequest)
161                         
162                                                                         
163                         // preProcessRequest(DelegateExecution execution)                                               
164                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
165                         CreateNetworkInstance.preProcessRequest(mockExecution)
166
167                         //verify(mockExecution).getVariable("isDebugLogEnabled")
168                         verify(mockExecution).setVariable("prefix", Prefix)
169                         
170                         initializeVariables(mockExecution)
171                         //verify(mockExecution).setVariable(Prefix + "Success", false)
172                                                         
173                 }
174                 
175                 
176                 @Test
177                 //@Ignore
178                 public void getNetworkModelInfo() {
179                         
180                         println "************ getNetworkModelInfo() ************* "
181                         
182                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
183                         // Initialize prerequisite variables
184                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
185                                                                         
186                         // preProcessRequest(DelegateExecution execution)
187                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
188                         CreateNetworkInstance.getNetworkModelInfo(mockExecution)
189
190                         //verify(mockExecution).getVariable("isDebugLogEnabled")
191                         verify(mockExecution).setVariable("prefix", Prefix)
192                                                         
193                 }
194                 
195                 @Test
196                 //@Ignore
197                 public void sendSyncResponse() {
198                         
199                         println "************ sendSyncResponse ************* "
200                         
201                         ExecutionEntity mockExecution = setupMock()
202                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
203                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
204                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
205                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
206                         
207                         // preProcessRequest(DelegateExecution execution)
208                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
209                         CreateNetworkInstance.sendSyncResponse(mockExecution)
210
211                         verify(mockExecution).setVariable("prefix", Prefix)
212                         verify(mockExecution).setVariable("CreateNetworkInstanceResponseCode", "202")
213                         
214                 }
215
216                 @Test
217                 //@Ignore
218                 public void sendSyncError() {
219                         
220                         println "************ sendSyncError ************* "
221                         
222                         ExecutionEntity mockExecution = setupMock()
223                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
224                         when(mockExecution.getVariable("isAsyncProcess")).thenReturn(true)
225                         when(mockExecution.getVariable("mso-request-id")).thenReturn("e8ebf6a0-f8ea-4dc0-8b99-fe98a87722d6")
226                         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
227                         
228                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
229                         CreateNetworkInstance.sendSyncError(mockExecution)
230
231                         verify(mockExecution).setVariable("prefix", Prefix)
232                         verify(mockExecution).setVariable("CreateNetworkInstanceResponseCode", "500")
233                         
234                 }
235                 
236                 @Test
237                 //@Ignore
238                 public void prepareDBRequestError() {
239                         
240                         println "************ prepareDBRequestError ************* "
241                         
242                         WorkflowException sndcWorkflowException = new WorkflowException("CreateNetworkInstance", 500, "Received error from SDN-C: No availability zone available")
243                         
244                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
245                         // Initialize prerequisite variables
246                         when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowException)
247                         //when(mockExecution.getVariable("msoRequestId")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
248                         when(mockExecution.getVariable(Prefix + "requestId")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
249                         when(mockExecution.getVariable("networkId")).thenReturn("")
250                         when(mockExecution.getVariable("networkName")).thenReturn("")
251                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
252                         when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
253                         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
254
255                         // preProcessRequest(DelegateExecution execution)
256                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
257                         CreateNetworkInstance.prepareDBRequestError(mockExecution)
258                         
259                         verify(mockExecution).setVariable("prefix", Prefix)
260                         verify(mockExecution).setVariable(Prefix + "createDBRequest", createDBRequestError)
261                 
262                 }
263                 
264
265                 @Test
266                 //@Ignore
267                 public void prepareCompletion() {
268                         
269                         println "************ postProcessResponse ************* "
270                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
271                         // Initialize prerequisite variables
272                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
273                         when(mockExecution.getVariable("mso-request-id")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
274                         when(mockExecution.getVariable(Prefix + "dbReturnCode")).thenReturn("200")
275                         
276                         // postProcessResponse(DelegateExecution execution)
277                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
278                         CreateNetworkInstance.prepareCompletion(mockExecution)
279
280                         // check the sequence of variable invocation
281                         //MockitoDebuggerImpl preDebugger = new MockitoDebuggerImpl()
282                         //preDebugger.printInvocations(mockExecution)
283
284                         verify(mockExecution).setVariable("prefix", Prefix)
285                         verify(mockExecution).setVariable(Prefix + "Success", true)
286                         verify(mockExecution).setVariable(Prefix + "CompleteMsoProcessRequest", completeMsoProcessRequest)
287                 
288                 }
289
290                 @Test
291                 //@Ignore
292                 public void buildErrorResponse() {
293                         
294                         println "************ buildErrorResponse ************* "
295                         
296                         
297                         WorkflowException sndcWorkflowException = new WorkflowException("CreateNetworkInstance", 5300, "Received error from SDN-C: No availability zone available.")
298                         
299                         ExecutionEntity mockExecution = setupMock()
300                         // Initialize prerequisite variables
301                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
302                         when(mockExecution.getVariable("mso-request-id")).thenReturn("b69c9054-da09-4a2c-adf5-51042b62bfac")
303                         //when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowException)
304                         when(mockExecution.getVariable("WorkflowException")).thenReturn(sndcWorkflowException)
305                         
306                         // buildErrorResponse(DelegateExecution execution)
307                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
308                         CreateNetworkInstance.buildErrorResponse(mockExecution)
309                         
310                         verify(mockExecution, atLeast(1)).setVariable("prefix", "CRENI_")
311                         verify(mockExecution).setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest)
312                         
313                         //MockitoDebuggerImpl debugger = new MockitoDebuggerImpl()
314                         //debugger.printInvocations(mockExecution)
315
316                 }
317                 
318                 @Test
319                 //@Ignore
320                 public void postProcessResponse() {
321                         
322                         println "************ postProcessResponse() ************* "
323                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
324                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
325                         when(mockExecution.getVariable("CMSO_ResponseCode")).thenReturn("200")
326                         
327                         // postProcessResponse(DelegateExecution execution)                                             
328                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
329                         CreateNetworkInstance.postProcessResponse(mockExecution)
330
331                         //verify(mockExecution).getVariable("isDebugLogEnabled")
332                         //verify(mockExecution).setVariable("prefix", Prefix)
333
334                         verify(mockExecution).setVariable(Prefix + "Success", true)
335                 
336                 }
337                 
338                 @Test
339                 //@Ignore
340                 public void processRollbackData() {
341                         
342                         println "************ callDBCatalog() ************* "
343                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
344                         // Initialize prerequisite variables
345                         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
346                                                                         
347                         // preProcessRequest(DelegateExecution execution)
348                         CreateNetworkInstance CreateNetworkInstance = new CreateNetworkInstance()
349                         CreateNetworkInstance.processRollbackData(mockExecution)
350
351 //                      verify(mockExecution).getVariable("isDebugLogEnabled")
352                         verify(mockExecution).setVariable("prefix", Prefix)
353                                                         
354                 }
355                 
356                 private ExecutionEntity setupMock() {
357                         
358                         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
359                         when(mockProcessDefinition.getKey()).thenReturn("CreateNetworkInstance")
360                         RepositoryService mockRepositoryService = mock(RepositoryService.class)
361                         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
362                         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("CreateNetworkInstance")
363                         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
364                         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
365                         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
366                         
367                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
368                         // Initialize prerequisite variables
369                         
370                         when(mockExecution.getId()).thenReturn("100")
371                         when(mockExecution.getProcessDefinitionId()).thenReturn("CreateNetworkInstance")
372                         when(mockExecution.getProcessInstanceId()).thenReturn("CreateNetworkInstance")
373                         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
374                         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
375                         
376                         return mockExecution
377                 }
378                 
379 }