4b0c33ab99744ffdc902840b26ed4d6b80341311
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / CompleteMsoProcessTest.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.common.scripts
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.mockito.ArgumentCaptor
30 import org.mockito.MockitoAnnotations
31 import org.mockito.runners.MockitoJUnitRunner
32 import org.onap.so.bpmn.core.WorkflowException
33
34 import static org.assertj.core.api.Assertions.assertThat
35 import static org.assertj.core.api.Assertions.assertThatThrownBy
36 import static org.mockito.Matchers.eq
37 import static org.mockito.Mockito.*
38
39 @RunWith(MockitoJUnitRunner.class)
40 class CompleteMsoProcessTest {
41         @Before
42         public void init() {
43                 MockitoAnnotations.initMocks(this)
44         }
45
46         private String completeMsoProcessRequest = """
47                                 <sdncadapterworkflow:MsoCompletionRequest xmlns:ns="http://org.onap/so/request/types/v1" xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
48                                                 <ns:request-information>
49                                                         <ns:request-id>uCPE1020_STUW105_5002</ns:request-id>
50                                                         <ns:request-action>Layer3ServiceActivateRequest</ns:request-action>                             
51                                                         <ns:request-sub-action>COMPLETE</ns:request-sub-action>
52                                                         <ns:source>OMX</ns:source>
53                                                         <ns:notification-url>http://localhost:28090/CCD/StatusNotification</ns:notification-url>                                
54                                                         <ns:order-number>10205000</ns:order-number>                             
55                                                         <ns:order-version>1</ns:order-version>
56                                                 </ns:request-information>                               
57                                                 <sdncadapterworkflow:mso-bpel-name>UCPELayer3ServiceActivateV1</sdncadapterworkflow:mso-bpel-name>
58                                 </sdncadapterworkflow:MsoCompletionRequest>
59                 """
60         
61                 private String completeMsoNetworkProcessRequest = """
62                                         <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
63                                                                 xmlns:ns="http://org.onap/so/request/types/v1">
64                                                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
65                                                         <request-id>bd631913-cfc6-488b-ba22-6b98504f703d</request-id>
66                                                         <action>CREATE</action>
67                                                         <source>VID</source>
68                                                 </request-info>
69                                                 <aetgt:status-message>Resource Completed Successfully</aetgt:status-message>
70                         <aetgt:networkId>bd631913-cfc6-488b-ba22-6b98504f703d</aetgt:networkId>
71                                                 <aetgt:mso-bpel-name>BPMN Network action: CREATE</aetgt:mso-bpel-name>
72                                         </aetgt:MsoCompletionRequest>"""
73
74         @Test
75         public void testPreProcessRequest() {
76
77                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
78                 when(mockExecution.getVariable("CompleteMsoProcessRequest")).thenReturn(completeMsoProcessRequest)
79                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC");
80                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
81
82                 CompleteMsoProcess completeMsoProcess = new CompleteMsoProcess()
83                 completeMsoProcess.preProcessRequest(mockExecution)
84
85                 /* Initialize all the process request variables in this block */
86                 verify(mockExecution).setVariable("prefix","CMSO_")
87                 //verify(mockExecution).setVariable("getLayer3ServiceDetailsV1Response","")
88                 verify(mockExecution).setVariable("CMSO_request_id","")
89                 verify(mockExecution).setVariable("CMSO_notification-url","")
90                 verify(mockExecution).setVariable("CMSO_mso-bpel-name","")
91                 verify(mockExecution).setVariable("CMSO_request_action","")
92
93                 verify(mockExecution).setVariable("CMSO_notification-url-Ok", false)
94                 verify(mockExecution).setVariable("CMSO_request_id-Ok", false)
95
96                 //updateRequest Adapter process variables
97                 verify(mockExecution).setVariable("CMSO_updateRequestResponse", "")
98                 verify(mockExecution).setVariable("CMSO_updateRequestResponseCode", "")
99                 verify(mockExecution).setVariable("CMSO_updateFinalNotifyAckStatusFailedPayload", "")
100
101                 //Set DB adapter variables here
102                 verify(mockExecution).setVariable("CMSO_updateDBStatusToSuccessPayload", "")
103                 verify(mockExecution).setVariable("CMSO_updateInfraRequestDBPayload", "")
104                 verify(mockExecution).setVariable("CMSO_setUpdateDBstatustoSuccessPayload", "")
105
106                 //Auth variables
107                 verify(mockExecution).setVariable("BasicAuthHeaderValue","")
108
109                 //Response variables
110                 verify(mockExecution).setVariable("CompletionHandlerResponse","")
111                 verify(mockExecution).setVariable("CMSO_ErrorResponse", null)
112                 verify(mockExecution).setVariable("CMSO_ResponseCode", "")
113
114                 verify(mockExecution).setVariable("CMSO_notification-url-Ok",true)
115                 verify(mockExecution).setVariable("CMSO_request_id-Ok",true)
116                 verify(mockExecution).setVariable("CMSO_notification-url","http://localhost:28090/CCD/StatusNotification")
117                 verify(mockExecution).setVariable("CMSO_request_id","uCPE1020_STUW105_5002")
118                 verify(mockExecution).setVariable("CMSO_request_action","Layer3ServiceActivateRequest")
119                 verify(mockExecution).setVariable("CMSO_source","OMX")
120
121         }
122
123         private String setUpdateDBstatustoSuccessPayload = """
124                                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
125                                                    <soapenv:Header/>
126                                                    <soapenv:Body>
127                                                       <req:updateInfraRequest>
128                                                          <requestId>testReqId</requestId>
129                                                          <lastModifiedBy>BPEL</lastModifiedBy>
130                                                          <statusMessage>Resource Completed Successfully</statusMessage>
131                                                          <requestStatus>COMPLETE</requestStatus>
132                                                                  <progress>100</progress>
133                                                                  <networkId>bd631913-cfc6-488b-ba22-6b98504f703d</networkId>
134                                                       </req:updateInfraRequest>
135                                                    </soapenv:Body>
136                                                 </soapenv:Envelope>"""
137
138         @Test
139         public void testsetUpdateDBstatustoSuccessPayload(){
140
141                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
142                 when(mockExecution.getVariable("CMSO_request_id")).thenReturn("testReqId")
143                 when(mockExecution.getVariable("CMSO_mso-bpel-name")).thenReturn("BPEL")
144                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC");
145                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
146                 when(mockExecution.getVariable("CompleteMsoProcessRequest")).thenReturn(completeMsoNetworkProcessRequest);
147                 
148                 CompleteMsoProcess completeMsoProcess = new CompleteMsoProcess()
149                 completeMsoProcess.setUpdateDBstatustoSuccessPayload(mockExecution)
150
151                 verify(mockExecution).setVariable("CMSO_setUpdateDBstatustoSuccessPayload",setUpdateDBstatustoSuccessPayload)
152         }
153
154         private String msoCompletionResponse = """onse xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
155    <sdncadapterworkflow:out>BPEL BPEL-NAME FAILED</sdncadapterworkflow:out>
156 </sdncadapterworkflow:MsoCompletionResponse>"""
157
158         @Test
159     void testBuildDataError() {
160                 // given
161                 def message = "Some-Message"
162
163                 def mockExecution = mock ExecutionEntity.class
164                 when mockExecution.getVariable("CMSO_mso-bpel-name") thenReturn "BPEL-NAME"
165                 when mockExecution.getVariable("testProcessKey") thenReturn "CompleteMsoProcess"
166
167                 def completeMsoProcess = new CompleteMsoProcess()
168                 // when
169                 assertThatThrownBy { completeMsoProcess.buildDataError(mockExecution, message) } isInstanceOf BpmnError
170                 // then
171                 verify mockExecution setVariable("CompleteMsoProcessResponse", msoCompletionResponse)
172                 def argumentCaptor = ArgumentCaptor.forClass WorkflowException.class
173                 verify mockExecution setVariable(eq("WorkflowException"), argumentCaptor.capture())
174                 def capturedException = argumentCaptor.value
175
176                 assertThat capturedException.processKey isEqualTo "CompleteMsoProcess"
177                 assertThat capturedException.errorCode isEqualTo 500
178                 assertThat capturedException.errorMessage isEqualTo message
179     }
180
181         @Test
182         void postProcessResponse_successful() {
183                 DelegateExecution mockExecution = mock(DelegateExecution.class)
184                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
185                 when(mockExecution.getVariable("CMSO_mso-bpel-name")).thenReturn("mso-bpel-test")
186                 new CompleteMsoProcess().postProcessResponse(mockExecution)
187
188                 String expectedResponse = "<sdncadapterworkflow:MsoCompletionResponse xmlns:sdncadapterworkflow=\"http://ecomp.com/mso/workflow/schema/v1\">\n" +
189                                 "  <sdncadapterworkflow:out>BPEL mso-bpel-test completed</sdncadapterworkflow:out>\n" +
190                                 "</sdncadapterworkflow:MsoCompletionResponse>"
191
192                 verify(mockExecution).setVariable("WorkflowResponse", expectedResponse)
193                 verify(mockExecution).setVariable("CompleteMsoProcessResponse", expectedResponse)
194                 verify(mockExecution).setVariable("CMSO_ResponseCode", "200")
195         }
196 }