[SO] Release so 1.13.0 image
[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.Mock
33 import org.mockito.MockitoAnnotations
34 import org.mockito.Spy
35 import org.mockito.runners.MockitoJUnitRunner
36 import org.onap.so.bpmn.common.scripts.MsoUtils;
37 import org.onap.so.db.request.beans.InfraActiveRequests
38 import org.onap.so.db.request.client.RequestsDbClient
39 import org.onap.so.bpmn.common.scripts.FalloutHandler;
40
41 @RunWith(MockitoJUnitRunner.class)
42 class FalloutHandlerTest {
43
44     public MsoUtils utils = new MsoUtils()
45
46     @Spy
47     FalloutHandler falloutHandler = new FalloutHandler()
48
49     @Mock
50     RequestsDbClient requestsDbClient;
51
52     @Before
53     public void init() {
54         MockitoAnnotations.initMocks(this)
55     }
56
57     private String falloutHandlerRequest = """
58                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1" xmlns:ns7="http://org.onap/so/request/types/v1">
59                                                 <ns7:request-info>
60                                                         <ns7:request-id>uCPE1020_STUW105_5002</ns7:request-id>
61                                                         <ns7:request-action>Layer3ServiceActivateRequest</ns7:request-action>
62                                                         <ns7:request-sub-action>CANCEL</ns7:request-sub-action>
63                                                         <ns7:source>OMX</ns7:source>
64                                                         <ns7:order-number>10205000</ns7:order-number>
65                                                         <ns7:order-version>1</ns7:order-version>
66                                                 </ns7:request-info>
67                                                 <sdncadapterworkflow:WorkflowException>
68                                                         <sdncadapterworkflow:ErrorMessage>Some Error Message - Fallout Handler</sdncadapterworkflow:ErrorMessage>
69                                                         <sdncadapterworkflow:ErrorCode>Some Error Code - Fallout Handler</sdncadapterworkflow:ErrorCode>
70                                                         <sdncadapterworkflow:SourceSystemErrorCode>Some Source System Error Code- Fallout Handler</sdncadapterworkflow:SourceSystemErrorCode>
71                                                 </sdncadapterworkflow:WorkflowException>
72                                 </sdncadapterworkflow:FalloutHandlerRequest>
73                 """
74
75     private String falloutHandlerResponse = """<workflow:FalloutHandlerResponse xmlns:workflow="http://org.onap/so/workflow/schema/v1">
76   <workflow:out>Fallout Handler Failed</workflow:out>
77 </workflow:FalloutHandlerResponse>"""
78
79     @Test
80     public void testPreProcessRequest() {
81
82         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
83
84         when(mockExecution.getVariable("FalloutHandlerRequest")).thenReturn(falloutHandlerRequest)
85
86         falloutHandler.preProcessRequest(mockExecution)
87
88         verify(mockExecution).setVariable("FH_success", true)
89         verify(mockExecution).setVariable("FH_request_id","uCPE1020_STUW105_5002")
90         verify(mockExecution).setVariable("FH_ErrorCode","Some Error Code - Fallout Handler")
91         verify(mockExecution).setVariable("FH_ErrorMessage","Some Error Message - Fallout Handler")
92     }
93
94     @Test
95     public void testpostProcessResponse(){
96
97         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
98
99         when(mockExecution.getVariable("FH_success")).thenReturn(false)
100
101         falloutHandler.postProcessResponse(mockExecution)
102
103         // Capture the arguments to setVariable
104         ArgumentCaptor<String> captor1 = ArgumentCaptor.forClass(String.class);
105         ArgumentCaptor<String> captor2 = ArgumentCaptor.forClass(String.class);
106
107         verify(mockExecution, times(4)).setVariable(captor1.capture(), captor2.capture())
108         List<String> arg2List = captor2.getAllValues()
109         String payloadResponseActual = arg2List.get(1)
110
111         assertEquals(falloutHandlerResponse.replaceAll("\\s+", ""), payloadResponseActual.replaceAll("\\s+", ""))
112
113         verify(mockExecution).setVariable("FH_ResponseCode","500")
114     }
115
116
117     @Test
118     public void testUpdateInfraRequestDB(){
119
120         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
121         when(mockExecution.getVariable("FH_request_id")).thenReturn("testReqId")
122         when(mockExecution.getVariable("FH_ErrorMessage")).thenReturn("ErrorMessage")
123         when(falloutHandler.getDbClient()).thenReturn(requestsDbClient)
124
125         falloutHandler.updateInfraRequestDB(mockExecution)
126
127         InfraActiveRequests infraRequest = new InfraActiveRequests();
128         infraRequest.setLastModifiedBy("BPMN")
129         infraRequest.setStatusMessage("ErrorMessage")
130         infraRequest.setRequestStatus("FAILED")
131         infraRequest.setProgress(100)
132
133         verify(requestsDbClient, times(1)).updateInfraActiveRequests(infraRequest, null, null)
134     }
135 }