39d0334befdd67fc27884a85d910ec57684f420e
[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 package org.onap.so.bpmn.infrastructure.scripts
21
22 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
23 import org.junit.Before
24 import org.junit.Test
25 import org.mockito.ArgumentCaptor
26 import org.mockito.Captor
27 import org.mockito.Mockito
28 import org.onap.logging.filter.base.ONAPComponents
29 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
30 import org.onap.so.bpmn.core.WorkflowException
31 import org.onap.so.client.HttpClient
32 import org.onap.so.client.HttpClientFactory
33 import org.onap.aaiclient.client.aai.AAIObjectType
34 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
35 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
36 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
37
38 import javax.ws.rs.NotFoundException
39 import javax.ws.rs.core.Response
40
41 import static org.junit.Assert.assertEquals
42 import static org.junit.Assert.assertNotNull
43 import static org.mockito.ArgumentMatchers.eq
44 import static org.mockito.Mockito.*
45
46 class DeleteCommunicationServiceTest extends MsoGroovyTest {
47
48     private HttpClientFactory httpClientFactoryMock
49     private HttpClient httpClientMock
50
51     @Before
52     void init() throws IOException {
53         super.init("DeleteCommunicationServiceTest")
54     }
55
56     @Captor
57     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
58
59     @Test
60     void testPreProcessRequest(){
61         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
62         when(mockExecution.getVariable("bpmnRequest")).thenReturn("""
63         {
64             "globalSubscriberId ":"5GCustomer",
65             "serviceType ":"5G"
66         }""".replaceAll("\\\\s+", ""))
67         when(mockExecution.getVariable("mso-request-id")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
68
69         DeleteCommunicationService delCS = new DeleteCommunicationService()
70         delCS.preProcessRequest(mockExecution)
71         Mockito.verify(mockExecution,times(3)).setVariable(captor.capture() as String, captor.capture())
72         List<ExecutionEntity> values = captor.getAllValues()
73         assertNotNull(values)
74     }
75
76     @Test
77     void testPreInitUpdateOperationStatus(){
78         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
79         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
80         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
81         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
82         when(mockExecution.getVariable("result")).thenReturn("processing")
83         when(mockExecution.getVariable("progress")).thenReturn("0")
84         when(mockExecution.getVariable("operationContent")).thenReturn("delete communication service operation start")
85
86         DeleteCommunicationService delCS = new DeleteCommunicationService()
87         delCS.preInitUpdateOperationStatus(mockExecution)
88         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
89         String updateOperationStatus= captor.getValue()
90         assertNotNull(updateOperationStatus)
91     }
92
93     @Test
94     void testQueryCommunicationSeriveFromAAI(){
95         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
96         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
97         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
98
99         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
100         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
101
102         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceReturn())
103         when(obj.getAAIClient()).thenReturn(client)
104         when(client.exists(resourceUri)).thenReturn(true)
105         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
106         obj.queryCommunicationSeriveFromAAI(mockExecution)
107         Mockito.verify(mockExecution,times(1)).setVariable(eq("e2eSliceServiceInstanceId"), captor.capture())
108         String e2eSliceServiceInstanceId = captor.getValue()
109         assertNotNull(e2eSliceServiceInstanceId)
110     }
111
112     @Test
113     void testPrepareCallCheckProcessStatus(){
114         DeleteCommunicationService dcs = new DeleteCommunicationService()
115         dcs.prepareCallCheckProcessStatus(mockExecution)
116         Mockito.verify(mockExecution,times(1)).setVariable(eq("endProgress"), captor.capture())
117         int endProgress = captor.getValue()
118         assertEquals(90,endProgress)
119     }
120
121     @Test
122     void testDelCSProfileFromAAI()
123     {
124         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
125         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
126         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
127
128         AAIResultWrapper wrapper = new AAIResultWrapper(mockQueryCommunicationServiceProfile())
129         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_PROFILE_ALL, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
130         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.COMMUNICATION_SERVICE_PROFILE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be", "5G-111111")
131
132         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
133         when(obj.getAAIClient()).thenReturn(client)
134         when(client.exists(resourceUri)).thenReturn(true)
135         when(client.exists(profileUri)).thenReturn(true)
136         when(client.get(resourceUri, NotFoundException.class)).thenReturn(wrapper)
137         doNothing().when(client).delete(profileUri)
138         obj.delCSProfileFromAAI(mockExecution)
139         Mockito.verify(client,times(1)).delete(profileUri)
140     }
141
142     @Test
143     void testPrepareFailureStatus(){
144         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
145         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
146         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
147         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
148         when(mockExecution.getVariable("result")).thenReturn("finished")
149         when(mockExecution.getVariable("progress")).thenReturn("100")
150         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure.")
151
152         DeleteCommunicationService dcs = new DeleteCommunicationService()
153         dcs.prepareFailureStatus(mockExecution)
154         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
155         String updateOperationStatus= captor.getValue()
156         assertNotNull(updateOperationStatus)
157     }
158
159     @Test
160     void testDelCSFromAAI(){
161         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
162         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
163         when(mockExecution.getVariable("serviceType")).thenReturn("5G")
164
165         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "5GCustomer", "5G", "5ad89cf9-0569-4a93-9306-d8324321e2be")
166         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
167         when(obj.getAAIClient()).thenReturn(client)
168         doNothing().when(client).delete(serviceInstanceUri)
169
170         obj.delCSFromAAI(mockExecution)
171         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
172         String updateOperationStatus= captor.getValue()
173         assertNotNull(updateOperationStatus)
174     }
175
176     @Test
177     void testPreFailedOperationStatus(){
178         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
179         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
180         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
181         when(mockExecution.getVariable("serviceInstanceName")).thenReturn("5G-test")
182         when(mockExecution.getVariable("result")).thenReturn("error")
183         when(mockExecution.getVariable("progress")).thenReturn("100")
184         when(mockExecution.getVariable("operationContent")).thenReturn("terminate service failure")
185
186         DeleteCommunicationService deleteCommunicationService = new DeleteCommunicationService()
187         WorkflowException exception = new WorkflowException("11113",7000,"terminate service failure")
188         when(mockExecution.getVariable("WorkflowException")).thenReturn(exception)
189         deleteCommunicationService.preFailedOperationStatus(mockExecution)
190
191         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
192         String updateOperationStatus= captor.getValue()
193         assertNotNull(updateOperationStatus)
194     }
195
196     @Test
197     void testSendRequest2NSMFWF(){
198         httpClientMock = mock(HttpClient.class)
199         httpClientFactoryMock = mock(HttpClientFactory.class)
200         String url ="http://so.onap:8080/onap/so/infra/e2eServiceInstances/v3/5G-777"
201         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
202         when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
203         when(mockExecution.getVariable("progress")).thenReturn("20")
204         when(mockExecution.getVariable("operationContent")).thenReturn("waiting nsmf service delete finished")
205         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
206         when(mockExecution.getVariable("mso.infra.endpoint.url")).thenReturn("http://so.onap:8080/onap/so/infra")
207         when(mockExecution.getVariable("e2eSliceServiceInstanceId")).thenReturn("5G-777")
208         when(mockExecution.getVariable("e2eOperationId")).thenReturn("e151059a-d924-4629-845f-264db19e50b3")
209         when(httpClientFactoryMock.newJsonClient(new URL(url), ONAPComponents.SO)).thenReturn(httpClientMock)
210         DeleteCommunicationService obj = spy(DeleteCommunicationService.class)
211
212         Response responseMock = mock(Response.class)
213         when(responseMock.getStatus()).thenReturn(200)
214         when(responseMock.hasEntity()).thenReturn(true)
215         when(responseMock.getEntity()).thenReturn(getNSSMFResponse())
216         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
217         when(httpClientMock.delete(anyString())).thenReturn(responseMock)
218
219         obj.sendRequest2NSMFWF(mockExecution)
220         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
221         String updateOperationStatus= captor.getValue()
222         assertNotNull(updateOperationStatus)
223     }
224
225     private String getNSSMFResponse(){
226         String response = """{
227             "service":{        
228                 "serviceId":"e151059a-d924-4629-845f-264db19e50b4",        
229                 "operationId":"e151059a-d924-4629-845f-264db19e50b3"        
230             }
231         }"""
232         return response
233     }
234
235
236
237
238     private String mockQueryCommunicationServiceReturn()
239     {
240         String expect =
241                 """{
242                         "service-instance-id": "5G-666",
243                         "service-instance-name": "eMBB_Slice_Communication_Service_5GCustomer",
244                         "service-type": "eMBB",
245                         "service-role": "communication-service",
246                         "environment-context": "01-010101",
247                         "workload-context": "12",
248                         "created-at": "2019-12-11 19:56:00",
249                         "description": "",
250                         "model-invariant-id": "e75698d9-925a-4cdd-a6c0-edacbe6a0b51",
251                         "model-version-id": "8ee5926d-720b-4bb2-86f9-d20e921c143b",
252                         "service-instance-location-id": "300-01|300-02",
253                         "resource-version": "1582623470778",
254                         "orchestration-status": "created",
255                         "relationship-list": {
256                             "relationship": [
257                                 {
258                                     "related-to": "service-instance",
259                                     "relationship-label": "org.onap.relationships.inventory.ComposedOf",
260                                     "related-link": "/aai/v16/business/customers/customer/5GCustomer/service-subscriptions/service-subscription/5G/service-instances/service-instance/5G-777",
261                                     "relationship-data": [
262                                         {
263                                             "relationship-key": "customer.global-customer-id",
264                                             "relationship-value": "5GCustomer"
265                                         },
266                                         {
267                                             "relationship-key": "service-subscription.service-type",
268                                             "relationship-value": "5G"
269                                         },
270                                         {
271                                             "relationship-key": "service-instance.service-instance-id",
272                                             "relationship-value": "5G-777"
273                                         }
274                                     ],
275                                     "related-to-property": [
276                                         {
277                                             "property-key": "service-instance.service-instance-name",
278                                             "property-value": "eMBB_e2e_Slice_Service_5GCustomer"
279                                         }
280                                     ]
281                                 }
282                             ]
283                         }
284                     }"""
285         return expect
286     }
287
288     private String mockQueryCommunicationServiceProfile()
289     {
290         String expect =
291         """{
292             "communication-service-profile": [
293                 {
294                     "profile-id": "5G-111111",
295                     "max-number-of-UEs": 50,
296                     "coverage-area-list": "longgang,futian",
297                     "latency": 20,
298                     "exp-data-rate-UL": 300,
299                     "exp-data-rate-DL": 500,
300                     "ue-mobility-level": "stationary",
301                     "resource-sharing-level": "Non-Shared",
302                     "resource-version": "1577454950460"
303                 }
304         ]
305         }"""
306         return expect
307     }
308 }