2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.bpmn.infrastructure.scripts
22 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
23 import org.junit.Before
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
44 import javax.ws.rs.core.Response
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
56 class DoDeallocateNSSITest extends MsoGroovyTest {
58 private HttpClientFactory httpClientFactoryMock
59 private HttpClient httpClientMock
62 void init() throws IOException {
63 super.init("DoDeallocateNSSITest")
67 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
71 void testPreProcessRequest(){
73 currentNSSI.put("nssiServiceInstanceId","5G-999")
74 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
76 DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
77 ddnssi.preProcessRequest(mockExecution)
78 Mockito.verify(mockExecution,times(1)).getVariable("currentNSSI")
82 void testPrepareDecomposeService(){
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)
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)
97 void testProcessDecomposition(){
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)
110 DoDeallocateNSSI ddnssi = new DoDeallocateNSSI()
111 ddnssi.processDecomposition(mockExecution)
112 String vendor = currentNSSI.get("vendor")
113 assertNotNull(vendor)
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)
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)
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)
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)
148 obj.delSliceProfileFromAAI(mockExecution)
149 Mockito.verify(client,times(1)).delete(profileUri)
153 void testSendRequestToNSSMF(){
154 httpClientFactoryMock = mock(HttpClientFactory.class)
155 httpClientMock = mock(HttpClient.class)
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")
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"
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)
179 obj.sendRequestToNSSMF(mockExecution)
180 String jobId = currentNSSI['jobId']
185 void testGetJobStatus(){
186 httpClientFactoryMock = mock(HttpClientFactory.class)
187 httpClientMock = mock(HttpClient.class)
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)
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"
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)
215 obj.getJobStatus(mockExecution)
216 Mockito.verify(mockExecution,times(1)).setVariable(eq("isNSSIDeAllocated"), captor.capture())
217 boolean value = captor.getValue()
222 private String getArtifactContent(){
227 "id":"NSST-C-001-HDBNJ-NSSMF-01-A-HW",
231 "description":"eMBB for demo",
238 "constrainstsl":"less_or_equal",
243 "constrainstsl":"less_or_equal",
248 "constrainstsl":"less_or_equal",
253 "constrainstsl":"less_or_equal",