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