88140d83cec9e7fd56a6ff24be31278d814d63b2
[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.beans.nsmf.JobStatusResponse
30 import org.onap.so.beans.nsmf.NssiResponse
31 import org.onap.so.beans.nsmf.ResponseDescriptor
32 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
33 import org.onap.so.bpmn.core.domain.ServiceArtifact
34 import org.onap.so.bpmn.core.domain.ServiceDecomposition
35 import org.onap.so.bpmn.core.domain.ServiceInfo
36 import org.onap.so.client.HttpClient
37 import org.onap.so.client.HttpClientFactory
38 import org.onap.aaiclient.client.aai.AAIObjectType
39 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
40 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
41
42 import javax.ws.rs.core.Response
43
44 import static org.junit.Assert.assertNotNull
45 import static org.junit.Assert.assertTrue
46 import static org.mockito.ArgumentMatchers.anyString
47 import static org.mockito.ArgumentMatchers.eq
48 import static org.mockito.Mockito.doNothing
49 import static org.mockito.Mockito.mock
50 import static org.mockito.Mockito.spy
51 import static org.mockito.Mockito.times
52 import static org.mockito.Mockito.when
53
54 class DoDeallocateNSSITest extends MsoGroovyTest {
55
56     private HttpClientFactory httpClientFactoryMock
57     private HttpClient httpClientMock
58
59     @Before
60     void init() throws IOException {
61         super.init("DoDeallocateNSSITest")
62     }
63
64     @Captor
65     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
66
67
68     @Test
69     void testPreProcessRequest(){
70         def currentNSSI = [:]
71         currentNSSI.put("nssiServiceInstanceId","5G-999")
72         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
73
74         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
75         ddnssi.preProcessRequest(mockExecution)
76         Mockito.verify(mockExecution,times(1)).getVariable("currentNSSI")
77     }
78
79     @Test
80     void testPrepareDecomposeService(){
81         def currentNSSI = [:]
82         currentNSSI.put("modelInvariantId", "21d57d4b-52ad-4d3c-a798-248b5bb9124b")
83         currentNSSI.put("modelVersionId", "bfba363e-e39c-4bd9-a9d5-1371c28f4d22")
84         currentNSSI.put("nssiServiceInstanceId","5G-999")
85         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
86
87         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
88         ddnssi.prepareDecomposeService(mockExecution)
89         Mockito.verify(mockExecution,times(1)).setVariable(eq("serviceModelInfo"), captor.capture())
90         String serviceModelInfo = captor.getValue()
91         assertNotNull(serviceModelInfo)
92     }
93
94     @Test
95     void testProcessDecomposition(){
96         def currentNSSI = [:]
97         ServiceArtifact artifact = new ServiceArtifact()
98         artifact.setContent(getArtifactContent())
99         ServiceInfo serviceInfo = new ServiceInfo()
100         List<ServiceArtifact> artifactList = new ArrayList<>()
101         artifactList.add(artifact)
102         serviceInfo.setServiceArtifact(artifactList)
103         ServiceDecomposition decomposition = new ServiceDecomposition()
104         decomposition.setServiceInfo(serviceInfo)
105         when(mockExecution.getVariable("serviceDecomposition")).thenReturn(decomposition)
106         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
107
108         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
109         ddnssi.processDecomposition(mockExecution)
110         String vendor = currentNSSI.get("vendor")
111         assertNotNull(vendor)
112     }
113
114     @Test
115     void testHandleJobStatus(){
116         def currentNSSI = [:]
117         currentNSSI.put("jobProgress", 10)
118         currentNSSI.put("proportion", 90)
119         currentNSSI.put("statusDescription","")
120         currentNSSI.put("e2eServiceInstanceId","21d57d4b-52ad-4d3c-a798-248b5bb9124b")
121         currentNSSI.put("operationId","4c614769-f58a-4556-8ad9-dcd903077c82")
122         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
123
124         DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
125         ddnssi.handleJobStatus(mockExecution)
126         Mockito.verify(mockExecution,times(1)).setVariable(eq("updateOperationStatus"), captor.capture())
127         String updateOperationStatus= captor.getValue()
128         assertNotNull(updateOperationStatus)
129     }
130
131     @Test
132     void testDelSliceProfileFromAAI(){
133         def currentNSSI = [:]
134         currentNSSI.put("nssiServiceInstanceId", "5G-999")
135         currentNSSI.put("profileId", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
136         currentNSSI.put("globalSubscriberId","5GCustomer")
137         currentNSSI.put("serviceType","5G")
138         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
139
140         AAIResourceUri profileUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, "5GCustomer", "5G", "5G-999", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
141         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
142         when(obj.getAAIClient()).thenReturn(client)
143         when(client.exists(profileUri)).thenReturn(true)
144         doNothing().when(client).delete(profileUri)
145
146         obj.delSliceProfileFromAAI(mockExecution)
147         Mockito.verify(client,times(1)).delete(profileUri)
148     }
149
150     @Test
151     void testSendRequestToNSSMF(){
152         httpClientFactoryMock = mock(HttpClientFactory.class)
153         httpClientMock = mock(HttpClient.class)
154
155         def currentNSSI = [:]
156         currentNSSI.put("snssai", "01-010101")
157         currentNSSI.put("profileId", "ddf57704-fe8d-417b-882d-2f2a12ddb225")
158         currentNSSI.put("nssiServiceInstanceId","5G-999")
159         currentNSSI.put("nsiServiceInstanceId","5G-888")
160
161         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
162         when(mockExecution.getVariable("mso.adapters.nssmf.endpoint")).thenReturn("http://so-nssmf-adapter.onap:8088")
163         String nssmfRequest = "http://so-nssmf-adapter.onap:8088/api/rest/provMns/v1/NSS/SliceProfiles/ddf57704-fe8d-417b-882d-2f2a12ddb225"
164
165         when(httpClientFactoryMock.newJsonClient(new URL(nssmfRequest), ONAPComponents.EXTERNAL)).thenReturn(httpClientMock)
166         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
167         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
168         Response responseMock = mock(Response.class)
169         NssiResponse response = new NssiResponse()
170         response.setNssiId("NSSI-C-004-HDBHZ-NSSMF-01-A-HW")
171         response.setJobId("a5c5913d-448a-bcb1-9b800a944d84")
172         when(httpClientMock.post(anyString())).thenReturn(responseMock)
173         when(responseMock.getStatus()).thenReturn(202)
174         when(responseMock.readEntity(NssiResponse.class)) thenReturn(response)
175         when(responseMock.hasEntity()).thenReturn(true)
176
177         obj.sendRequestToNSSMF(mockExecution)
178         String jobId = currentNSSI['jobId']
179         assertNotNull(jobId)
180     }
181
182     @Test
183     void testGetJobStatus(){
184         httpClientFactoryMock = mock(HttpClientFactory.class)
185         httpClientMock = mock(HttpClient.class)
186
187         def currentNSSI = [:]
188         currentNSSI.put("jobId", "a5c5913d-448a-bcb1-9b800a944d84")
189         currentNSSI.put("nssiServiceInstanceId","5G-999")
190         currentNSSI.put("nsiServiceInstanceId","5G-888")
191         currentNSSI.put("jobProgress",60)
192
193         when(mockExecution.getVariable("isNSSIDeAllocated")).thenReturn(false)
194         when(mockExecution.getVariable("isNSSIDeAllocated")).thenReturn(false)
195         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
196         when(mockExecution.getVariable("mso.adapters.nssmf.endpoint")).thenReturn("http://so-nssmf-adapter.onap:8088")
197         String nssmfRequest = "http://so-nssmf-adapter.onap:8088/api/rest/provMns/v1/NSS/jobs/a5c5913d-448a-bcb1-9b800a944d84"
198
199         when(httpClientFactoryMock.newJsonClient(new URL(nssmfRequest), ONAPComponents.EXTERNAL)).thenReturn(httpClientMock)
200         DoDeallocateNSSI obj = spy(DoDeallocateNSSI.class)
201         when(obj.getHttpClientFactory()).thenReturn(httpClientFactoryMock)
202         Response responseMock = mock(Response.class)
203         ResponseDescriptor descriptor = new ResponseDescriptor()
204         descriptor.setProgress(100)
205         descriptor.setStatusDescription("finished deallocate nssi")
206         JobStatusResponse jobStatusResponse = new JobStatusResponse()
207         jobStatusResponse.setResponseDescriptor(descriptor)
208         when(httpClientMock.post(anyString())).thenReturn(responseMock)
209         when(responseMock.getStatus()).thenReturn(202)
210         when(responseMock.readEntity(JobStatusResponse.class)) thenReturn(jobStatusResponse)
211         when(responseMock.hasEntity()).thenReturn(true)
212
213         obj.getJobStatus(mockExecution)
214         Mockito.verify(mockExecution,times(1)).setVariable(eq("isNSSIDeAllocated"), captor.capture())
215         boolean value = captor.getValue()
216         assertTrue(value)
217     }
218
219
220     private String getArtifactContent(){
221         String content =
222                 """
223                     {
224                         "metadata":{
225                             "id":"NSST-C-001-HDBNJ-NSSMF-01-A-HW",
226                             "vendor":"HW",
227                             "version":"1.0",
228                             "name":"eMBB_demo",
229                             "description":"eMBB for demo",
230                             "type":"embb",
231                             "domainType":"cn"
232                         },
233                         "capabilities":{
234                             "latency":{
235                                 "type":"integer",
236                                 "constrainstsl":"less_or_equal",
237                                 "value":"20"
238                             },
239                             "areaTrafficCapDL":{
240                                 "type":"integer",
241                                 "constrainstsl":"less_or_equal",
242                                 "value":"300"
243                             },
244                             "areaTrafficCapUL":{
245                                 "type":"integer",
246                                 "constrainstsl":"less_or_equal",
247                                 "value":"300"
248                             },
249                             "maxNumberofUEs":{
250                                 "type":"integer",
251                                 "constrainstsl":"less_or_equal",
252                                 "value":"300"
253                             }
254                         }
255                     }
256                 """
257     }
258 }