7e02779f102b9d105b533207d8897e3560de34de
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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.impl.persistence.entity.ExecutionEntity
24 import org.junit.Before
25 import org.junit.Test
26 import org.mockito.ArgumentCaptor
27 import org.mockito.Captor
28 import org.mockito.Mockito
29 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
30
31 import static org.junit.Assert.assertEquals
32 import static org.junit.Assert.assertNotNull
33 import static org.mockito.ArgumentMatchers.eq
34 import static org.mockito.Mockito.times
35 import static org.mockito.Mockito.when
36
37 class ActivateCommunicationServiceTest extends MsoGroovyTest  {
38
39     @Before
40     void init() throws IOException {
41         super.init("ActivateCommunicationService")
42     }
43
44     @Captor
45     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
46
47     @Test
48     void testPreProcessRequest() {
49
50         String req = """
51                 {
52                     "globalSubscriberId": "5GCustomer",
53                     "serviceType": "5G",
54                     "operationId": "test123"
55                 }
56             """
57         when(mockExecution.getVariable("bpmnRequest")).thenReturn(req)
58         when(mockExecution.getVariable("mso-request-id")).thenReturn("54321")
59         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
60
61
62         ActivateCommunicationService service = new ActivateCommunicationService()
63         service.preProcessRequest(mockExecution)
64         Mockito.verify(mockExecution, times(5)).setVariable(captor.capture(), captor.capture())
65     }
66
67     @Test
68     void testPrepareInitOperationStatus() {
69
70         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
71         when(mockExecution.getVariable("operationId")).thenReturn("54321")
72
73         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("11111")
74
75         ActivateCommunicationService service = new ActivateCommunicationService()
76
77         service.prepareInitOperationStatus(mockExecution)
78         Mockito.verify(mockExecution, times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
79         String res = captor.getValue()
80         assertNotNull(res)
81     }
82
83
84     @Test
85     void testSendSyncResponse() {
86         when(mockExecution.getVariable("operationId")).thenReturn("123456")
87         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
88         ActivateCommunicationService service = new ActivateCommunicationService()
89         service.sendSyncResponse(mockExecution)
90         Mockito.verify(mockExecution, times(1)).setVariable(eq("sentSyncResponse"), captor.capture())
91         def updateVolumeGroupRequest = captor.getValue()
92         assertEquals(updateVolumeGroupRequest, true)
93     }
94
95     @Test
96     void testPreRequestSend2NSMF() {
97         when(mockExecution.getVariable("e2e_service-instance.service-instance-id")).thenReturn("12333")
98         when(mockExecution.getVariable("requestParam")).thenReturn("activate")
99         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("test111")
100         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("5GConsumer")
101         ActivateCommunicationService service = new ActivateCommunicationService()
102         service.preRequestSend2NSMF(mockExecution)
103         Mockito.verify(mockExecution, times(1)).setVariable(eq("CSMF_NSMFRequest"), captor.capture())
104         String resultSuccess = captor.getValue()
105
106         String expect = """
107                 {
108                     "globalSubscriberId":"test111",
109                     "serviceType":"5GConsumer"
110                 }
111             """
112         assertEquals(expect.replaceAll("\\s+", ""), resultSuccess.replaceAll("\\s+", ""))
113     }
114
115     @Test
116     void testProcessNSMFResponseSuccess() {
117         when(mockExecution.getVariable("CSMF_NSMFResponseCode")).thenReturn(202)
118         when(mockExecution.getVariable("CSMF_NSMFResponse")).thenReturn("""
119                 {                  
120                     "operationId": "e3819a23-3777-4172-a834-35ee78acf3f4"
121                 }
122         """)
123
124         ActivateCommunicationService service = new ActivateCommunicationService()
125         service.processNSMFResponse(mockExecution)
126         Mockito.verify(mockExecution, times(2)).setVariable(captor.capture(), captor.capture())
127         def resultSuccess = captor.getAllValues()
128
129         def expect = new ArrayList<>()
130         expect.add("e2eOperationId")
131         expect.add("e3819a23-3777-4172-a834-35ee78acf3f4")
132         expect.add("ProcessNsmfSuccess")
133         expect.add("OK")
134         assertEquals(expect, resultSuccess)
135     }
136
137     @Test
138     void testProcessNSMFResponseError() {
139         when(mockExecution.getVariable("CSMF_NSMFResponseCode")).thenReturn(500)
140         ActivateCommunicationService service = new ActivateCommunicationService()
141         service.processNSMFResponse(mockExecution)
142         Mockito.verify(mockExecution, times(1)).setVariable(eq("ProcessNsmfSuccess"), captor.capture())
143         String resultSuccess = captor.getValue()
144         assertEquals("ERROR", resultSuccess)
145     }
146
147     @Test
148     void testPrepareUpdateOperationStatus() {
149         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
150         when(mockExecution.getVariable("operationId")).thenReturn("54321")
151         when(mockExecution.getVariable("operationType")).thenReturn("activate")
152
153         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("11111")
154         when(mockExecution.getVariable("mso.adapters.openecomp.db.endpoint"))
155                 .thenReturn("http://localhost:28090/dbadapters/RequestsDbAdapter")
156         ActivateCommunicationService service = new ActivateCommunicationService()
157
158         service.prepareUpdateOperationStatus(mockExecution)
159         Mockito.verify(mockExecution, times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
160         String res = captor.getValue()
161
162         String expect = getExpectPayload("updateServiceOperationStatus", "processing", "20",
163                 "communication service activate operation processing: waiting nsmf service create finished")
164
165         assertEquals(expect.replaceAll("\\s+", ""), res.replaceAll("\\s+", ""))
166     }
167
168     private static getExpectPayload = { String type, String result, String progress, String operationContent ->
169         String expect =
170                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
171                         xmlns:ns="http://org.onap.so/requestsdb">
172                             <soapenv:Header/>
173                             <soapenv:Body>
174                                 <ns:${type} xmlns:ns="http://org.onap.so/requestsdb">
175                                     <serviceId>12345</serviceId>
176                                     <operationId>54321</operationId>
177                                     <operationType>activate</operationType>
178                                     <userId>11111</userId>
179                                     <result>${result}</result>
180                                     <operationContent>${operationContent}</operationContent>
181                                     <progress>${progress}</progress>
182                                     <reason></reason>
183                                 </ns:${type}>
184                             </soapenv:Body>
185                    </soapenv:Envelope>
186                 """
187         return expect
188     }
189
190     @Test
191     void testPrepareCallCheckProcessStatus() {
192         ActivateCommunicationService service = new ActivateCommunicationService()
193         service.prepareCallCheckProcessStatus(mockExecution)
194         Mockito.verify(mockExecution, times(9)).setVariable(captor.capture(), captor.capture())
195         def resultSuccess = captor.getAllValues()
196         assertNotNull(resultSuccess)
197     }
198
199     @Test
200     void testPrepareCompleteStatus() {
201         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
202         when(mockExecution.getVariable("operationId")).thenReturn("54321")
203         when(mockExecution.getVariable("operationType")).thenReturn("activate")
204         when(mockExecution.getVariable("operationContent"))
205                 .thenReturn("communication service activate operation finished")
206
207         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("11111")
208         when(mockExecution.getVariable("operationStatus"))
209                 .thenReturn("deactivated")
210         ActivateCommunicationService service = new ActivateCommunicationService()
211
212         service.prepareCompleteStatus(mockExecution)
213         Mockito.verify(mockExecution, times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
214         String res = captor.getValue()
215
216         String expect = getExpectPayload("updateServiceOperationStatus", "deactivated", "100",
217                 "communication service activate operation finished")
218
219         assertEquals(expect.replaceAll("\\s+", ""), res.replaceAll("\\s+", ""))
220     }
221
222 }
223
224