016d968a12d471a00f474cfa0264e8f0189fe0d2
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / FalloutHandlerTest.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.junit.runner.RunWith;
24 import static org.mockito.Mockito.*
25 import static org.junit.Assert.*;
26
27 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
28 import org.junit.Before
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.MockitoAnnotations
33 import org.mockito.runners.MockitoJUnitRunner
34 import org.onap.so.bpmn.common.scripts.MsoUtils;
35 import org.onap.so.bpmn.common.scripts.FalloutHandler;
36
37 @RunWith(MockitoJUnitRunner.class)
38 class FalloutHandlerTest {
39         
40         public MsoUtils utils = new MsoUtils()
41         
42         @Before
43         public void init() {
44                 MockitoAnnotations.initMocks(this)
45         }
46
47         private String falloutHandlerRequest = """
48                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1" xmlns:ns7="http://org.onap/so/request/types/v1">
49                                                 <ns7:request-information>
50                                                         <ns7:request-id>uCPE1020_STUW105_5002</ns7:request-id>
51                                                         <ns7:request-action>Layer3ServiceActivateRequest</ns7:request-action>
52                                                         <ns7:request-sub-action>CANCEL</ns7:request-sub-action>
53                                                         <ns7:source>OMX</ns7:source>
54                                                         <ns7:order-number>10205000</ns7:order-number>
55                                                         <ns7:order-version>1</ns7:order-version>
56                                                 </ns7:request-information>
57                                                 <sdncadapterworkflow:WorkflowException>
58                                                         <sdncadapterworkflow:ErrorMessage>Some Error Message - Fallout Handler</sdncadapterworkflow:ErrorMessage>
59                                                         <sdncadapterworkflow:ErrorCode>Some Error Code - Fallout Handler</sdncadapterworkflow:ErrorCode>
60                                                         <sdncadapterworkflow:SourceSystemErrorCode>Some Source System Error Code- Fallout Handler</sdncadapterworkflow:SourceSystemErrorCode>
61                                                 </sdncadapterworkflow:WorkflowException>
62                                 </sdncadapterworkflow:FalloutHandlerRequest>
63                 """
64
65         private String falloutHandlerResponse = """<workflow:FalloutHandlerResponse xmlns:workflow="http://org.onap/so/workflow/schema/v1">
66   <workflow:out>Fallout Handler Failed</workflow:out>
67 </workflow:FalloutHandlerResponse>"""
68
69         @Test
70         public void testPreProcessRequest() {
71
72                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
73
74                 when(mockExecution.getVariable("FalloutHandlerRequest")).thenReturn(falloutHandlerRequest)
75                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC");
76                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
77
78                 FalloutHandler falloutHandler = new FalloutHandler()
79                 falloutHandler.preProcessRequest(mockExecution)
80
81                 /* Initialize all the process request variables in this block */
82                 verify(mockExecution).setVariable("prefix","FH_")
83                 //verify(mockExecution).setVariable("getLayer3ServiceDetailsV1Response","")
84
85                 //These variables are form the input Message to the BPMN
86                 verify(mockExecution).setVariable("FH_request_id","")
87                 verify(mockExecution).setVariable("FH_request_action","")
88                 verify(mockExecution).setVariable("FH_notification-url","")
89                 verify(mockExecution).setVariable("FH_mso-bpel-name","")
90                 verify(mockExecution).setVariable("FH_ErrorCode", "")
91                 verify(mockExecution).setVariable("FH_ErrorMessage", "")
92
93                 verify(mockExecution).setVariable("FH_notification-url-Ok", false)
94                 verify(mockExecution).setVariable("FH_request_id-Ok", false)
95
96                 //These variables are for Get Mso Aai Password Adapter
97                 verify(mockExecution).setVariable("FH_deliveryStatus", true)
98
99                 //update Response Status to pending ...Adapter variables
100                 verify(mockExecution).setVariable("FH_updateResponseStatusPayload", null)
101                 verify(mockExecution).setVariable("FH_updateResponseStatusResponse", null)
102
103                 //update Request Gamma ...Adapter variables
104                 verify(mockExecution).setVariable("FH_updateRequestGammaPayload", "")
105                 verify(mockExecution).setVariable("FH_updateRequestGammaResponse", null)
106                 verify(mockExecution).setVariable("FH_updateRequestGammaResponseCode", null)
107
108                 //update Request Infra ...Adapter variables
109                 verify(mockExecution).setVariable("FH_updateRequestInfraPayload", "")
110                 verify(mockExecution).setVariable("FH_updateRequestInfraResponse", null)
111                 verify(mockExecution).setVariable("FH_updateRequestInfraResponseCode", null)
112
113                 //assign False to success variable
114                 verify(mockExecution).setVariable("FH_success", true)
115
116                 //Set notify status to Failed variable
117                 verify(mockExecution).setVariable("FH_NOTIFY_STATUS", "SUCCESS")
118
119                 //Set DB update variable
120                 verify(mockExecution).setVariable("FH_updateRequestPayload", "")
121                 verify(mockExecution).setVariable("FH_updateRequestResponse", null)
122                 verify(mockExecution).setVariable("FH_updateRequestResponseCode", null)
123
124                 //Auth variables
125                 verify(mockExecution).setVariable("BasicAuthHeaderValue","")
126
127                 //Response variables
128                 verify(mockExecution).setVariable("FalloutHandlerResponse","")
129                 verify(mockExecution).setVariable("FH_ErrorResponse", null)
130                 verify(mockExecution).setVariable("FH_ResponseCode", "")
131
132                 verify(mockExecution).setVariable("FH_request_id-Ok",true)
133                 verify(mockExecution).setVariable("FH_request_id","uCPE1020_STUW105_5002")
134                 verify(mockExecution).setVariable("FH_request_action","Layer3ServiceActivateRequest")
135                 verify(mockExecution).setVariable("FH_source","OMX")
136                 verify(mockExecution).setVariable("FH_ErrorCode","Some Error Code - Fallout Handler")
137                 verify(mockExecution).setVariable("FH_ErrorMessage","Some Error Message - Fallout Handler")
138
139         }
140
141         @Test
142         public void testpostProcessResponse(){
143
144                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
145
146                 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
147                 when(mockExecution.getVariable("FH_success")).thenReturn(false)
148
149                 FalloutHandler falloutHandler = new FalloutHandler()
150                 falloutHandler.postProcessResponse(mockExecution)
151
152                 // Capture the arguments to setVariable
153                 ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
154                 ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
155                 
156                 verify(mockExecution, times(4)).setVariable(captor1.capture(), captor2.capture())
157                 List<String> arg2List = captor2.getAllValues()
158                 String payloadResponseActual = arg2List.get(1)
159                 
160                 assertEquals(falloutHandlerResponse.replaceAll("\\s+", ""), payloadResponseActual.replaceAll("\\s+", ""))
161                 
162                 verify(mockExecution).setVariable("FH_ResponseCode","500")
163         }
164
165         private String updateRequestPayload = """
166                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
167                         <soapenv:Header/>
168                         <soapenv:Body>
169                            <req:updateRequest>
170                                   <requestId>testReqId</requestId>
171                                   <lastModifiedBy>BPEL</lastModifiedBy>
172                                   <finalErrorMessage>ErrorMessage</finalErrorMessage>
173                                   <finalErrorCode>ErrorCode</finalErrorCode>
174                                   <status>FAILED</status>
175                                   <responseStatus>NotifyStatus</responseStatus>
176                            </req:updateRequest>
177                         </soapenv:Body>
178                  </soapenv:Envelope>
179                 """
180         
181                 @Test
182                 public void testupdateRequestPayload(){
183         
184                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
185                         when(mockExecution.getVariable("FH_request_id")).thenReturn("testReqId")
186                         when(mockExecution.getVariable("FH_ErrorMessage")).thenReturn("ErrorMessage")
187                         when(mockExecution.getVariable("FH_ErrorCode")).thenReturn("ErrorCode")
188                         when(mockExecution.getVariable("FH_NOTIFY_STATUS")).thenReturn("NotifyStatus")
189         
190                         FalloutHandler falloutHandler = new FalloutHandler()
191                         falloutHandler.updateRequestPayload(mockExecution)
192                         
193                         // Capture the arguments to setVariable
194                         ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
195                         ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
196                         
197                         verify(mockExecution, times(1)).setVariable(captor1.capture(), captor2.capture())
198                         List<String> arg2List = captor2.getAllValues()
199                         String payloadRequestActual = arg2List.get(0)
200                         
201                         assertEquals(updateRequestPayload.replaceAll("\\s+", ""), payloadRequestActual.replaceAll("\\s+", ""))
202                 }
203                 
204                 private String updateRequestInfraPayload = """
205                                                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
206                                                            <soapenv:Header/>
207                                                            <soapenv:Body>
208                                                               <req:updateInfraRequest>
209                                                                  <requestId>testReqId</requestId>
210                                                                  <lastModifiedBy>BPEL</lastModifiedBy>
211                                                                          <statusMessage>ErrorMessage</statusMessage>
212                                                                          <requestStatus>FAILED</requestStatus>
213                                                                          <progress>100</progress>
214                                                               </req:updateInfraRequest>
215                                                            </soapenv:Body>
216                                                         </soapenv:Envelope>
217                         """
218                 
219                         @Test
220                         public void testupdateRequestInfraPayload(){
221                 
222                                 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
223                                 when(mockExecution.getVariable("FH_request_id")).thenReturn("testReqId")
224                                 when(mockExecution.getVariable("FH_ErrorMessage")).thenReturn("ErrorMessage")
225                 
226                                 FalloutHandler falloutHandler = new FalloutHandler()
227                                 falloutHandler.updateRequestInfraPayload(mockExecution)
228                                 
229                                 // Capture the arguments to setVariable
230                                 ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
231                                 ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
232                                 
233                                 verify(mockExecution, times(1)).setVariable(captor1.capture(), captor2.capture())
234                                 List<String> arg2List = captor2.getAllValues()
235                                 String payloadRequestActual = arg2List.get(0)
236                 
237                                 assertEquals(updateRequestInfraPayload.replaceAll("\\s+", ""), payloadRequestActual.replaceAll("\\s+", ""))
238                         }
239                         
240                         private String updateRequestGammaPayload = """
241                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
242                         <soapenv:Header/>
243                         <soapenv:Body>
244                            <req:updateRequest>
245                                   <requestId>testReqId</requestId>
246                                   <lastModifiedBy>BPEL</lastModifiedBy>
247                                   <finalErrorMessage>ErrorMessage</finalErrorMessage>
248                                   <finalErrorCode>ErrorCode</finalErrorCode>
249                                   <status>FAILED</status>
250                            </req:updateRequest>
251                         </soapenv:Body>
252                  </soapenv:Envelope>
253                 """
254                         
255                 @Test
256                 public void testupdateRequestGammaPayload(){
257         
258                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
259                         when(mockExecution.getVariable("FH_request_id")).thenReturn("testReqId")
260                         when(mockExecution.getVariable("FH_ErrorMessage")).thenReturn("ErrorMessage")
261                         when(mockExecution.getVariable("FH_ErrorCode")).thenReturn("ErrorCode")
262                         when(mockExecution.getVariable("mso.default.adapter.namespace")).thenReturn("http://org.onap.so");
263
264                         FalloutHandler falloutHandler = new FalloutHandler()
265                         falloutHandler.updateRequestGammaPayload(mockExecution)
266         
267                         // Capture the arguments to setVariable
268                         ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
269                         ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
270                         
271                         verify(mockExecution, times(1)).setVariable(captor1.capture(), captor2.capture())
272                         List<String> arg2List = captor2.getAllValues()
273                         String payloadRequestActual = arg2List.get(0)
274                         
275                         assertEquals(updateRequestGammaPayload.replaceAll("\\s+", ""), payloadRequestActual.replaceAll("\\s+", ""))
276                 }
277         
278                 
279                 String updateResponseStatusPayload = """
280                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
281                         <soapenv:Header/>
282                         <soapenv:Body>
283                            <req:updateResponseStatus>
284                                   <requestId>testReqId</requestId>
285                                   <lastModifiedBy>BPEL</lastModifiedBy>
286                                   <responseStatus>SENDING_FINAL_NOTIFY</responseStatus>
287                            </req:updateResponseStatus>
288                         </soapenv:Body>
289                  </soapenv:Envelope>
290                 """
291                 
292                 @Test
293                 public void testupdateResponseStatusPayload(){
294         
295                         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
296                         when(mockExecution.getVariable("FH_request_id")).thenReturn("testReqId")
297
298                         FalloutHandler falloutHandler = new FalloutHandler()
299                         falloutHandler.updateResponseStatusPayload(mockExecution)
300                         
301                         // Capture the arguments to setVariable
302                         ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
303                         ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
304                         
305                         verify(mockExecution, times(1)).setVariable(captor1.capture(), captor2.capture())
306                         List<String> arg2List = captor2.getAllValues()
307                         String payloadResponseActual = arg2List.get(0)
308         
309                         assertEquals(updateResponseStatusPayload.replaceAll("\\s+", ""), payloadResponseActual.replaceAll("\\s+", ""))
310                 }
311
312 }