ac6f897dfaba16ac069fbabbe783d93fe5f66a2f
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoModifyCoreNSSITest.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Telecom Italia
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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Before
25 import org.junit.Test
26 import org.mockito.Mockito
27 import org.onap.aai.domain.yang.v19.AllottedResource
28 import org.onap.aai.domain.yang.v19.ServiceInstance
29 import org.onap.aai.domain.yang.v19.SliceProfile
30 import org.onap.aai.domain.yang.v19.SliceProfiles
31 import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
37
38 import static org.junit.Assert.assertNotNull
39 import static org.junit.Assert.assertTrue
40 import static org.mockito.Mockito.*
41
42 class DoModifyCoreNSSITest extends MsoGroovyTest  {
43
44     @Before
45     void init() throws IOException {
46         super.init("DoModifyCoreNSSITest")
47     }
48
49
50     @Test
51     void testCalculateSNSSAISliceProfileInstanceHasToBeDeleted() {
52         def currentNSSI = [:]
53         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
54
55         when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("false")
56
57         String theSNSSAI = "theS-NSSAI"
58
59         currentNSSI.put("S-NSSAI", theSNSSAI)
60
61         List<SliceProfile> associatedProfiles = new ArrayList<>()
62         SliceProfile sliceProfile1 = new SliceProfile()
63         sliceProfile1.setSNssai("snssai1")
64
65         SliceProfile sliceProfile2 = new SliceProfile()
66         sliceProfile2.setSNssai(theSNSSAI)
67
68         SliceProfile sliceProfile3 = new SliceProfile()
69         sliceProfile3.setSNssai("snssai2")
70
71         associatedProfiles.add(sliceProfile1)
72         associatedProfiles.add(sliceProfile2)
73         associatedProfiles.add(sliceProfile3)
74
75         int sizeBefore = associatedProfiles.size()
76
77         currentNSSI.put("associatedProfiles", associatedProfiles)
78
79         DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
80         obj.calculateSNSSAI(mockExecution)
81
82         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
83         SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
84
85         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore - 1)))
86         assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
87         assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
88     }
89
90
91     @Test
92     void testCalculateSNSSAISliceProfileInstanceHasToBeCreated() {
93         def currentNSSI = [:]
94         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
95
96         when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("true")
97
98         String theSNSSAI = "theS-NSSAI"
99
100         currentNSSI.put("S-NSSAI", theSNSSAI)
101
102         List<SliceProfile> associatedProfiles = new ArrayList<>()
103         SliceProfile sliceProfile1 = new SliceProfile()
104         sliceProfile1.setSNssai("snssai1")
105
106         SliceProfile sliceProfile2 = new SliceProfile()
107         sliceProfile2.setSNssai("snssai2")
108
109         associatedProfiles.add(sliceProfile1)
110         associatedProfiles.add(sliceProfile2)
111
112         int sizeBefore = associatedProfiles.size()
113
114         currentNSSI.put("associatedProfiles", associatedProfiles)
115
116         DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
117         obj.calculateSNSSAI(mockExecution)
118
119         List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
120
121         assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore + 1)))
122
123     }
124
125
126     @Test
127     void testCreateSliceProfileInstance() {
128         def currentNSSI = [:]
129
130         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
131
132         String sliceProfileId = "sliceProfileId"
133
134         currentNSSI.put("sliceProfile", "{\"sliceProfileId\":\"slice-profile-id\",\"snssaiList\":[\"S-NSSAI\"],\"expDataRateUL\":\"12\",\"expDataRateDL\":\"5\"," +
135                         "\"activityFactor\":\"2\",\"resourceSharingLevel\":\"resource-sharing-level\",\"uEMobilityLevel\":\"ue-mobility-level\",\"coverageAreaTAList\":\"coverage-area-ta-list\"," +
136                         "\"maxNumberofUEs\":\"10000\",\"latency\":\"7\"}")
137         currentNSSI.put("sliceProfileId", sliceProfileId)
138
139         List<String> snssais = new ArrayList<>()
140         snssais.add("s-nssai")
141         currentNSSI.put("S-NSSAIs", snssais)
142
143         DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
144         when(spy.getAAIClient()).thenReturn(client)
145
146         String globalSubscriberId = "globalSubscriberId"
147         String subscriptionServiceType = "subscription-service-type"
148         String nssiId = "nssiId"
149
150         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
151         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
152
153         currentNSSI.put("nssiId", nssiId)
154
155         ServiceInstance sliceProfileInstance = new ServiceInstance()
156         sliceProfileInstance.setServiceInstanceId(UUID.randomUUID().toString())
157
158         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).
159                                                             serviceInstance(sliceProfileInstance.getServiceInstanceId()))
160
161         SliceProfile sliceProfile = new SliceProfile()
162         sliceProfile.setProfileId(sliceProfileId)
163
164         doNothing().when(client).create(uri, sliceProfileInstance)
165
166         spy.createSliceProfileInstance(mockExecution)
167
168         assertTrue("Slice Profile Instance Id doesn't exist", (currentNSSI.get("createdSliceProfileInstanceId")) != null)
169
170     }
171
172
173     @Test
174     void testAssociateSliceProfileInstanceWithNSSI() {
175         def currentNSSI = [:]
176
177         when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
178
179         DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
180         when(spy.getAAIClient()).thenReturn(client)
181
182         String sliceProfileId = "sliceProfileId"
183
184         when(mockExecution.getVariable("sliceProfileID")).thenReturn(sliceProfileId)
185
186         String nssiId = "5G-999"
187         currentNSSI.put("nssiId", nssiId)
188
189         String globalSubscriberId = "globalSubscriberId"
190         String subscriptionServiceType = "subscriptionServiceType"
191
192         String sliceProfileInstanceId = "slice-rpofile-instance-id"
193         currentNSSI.put("createdSliceProfileInstanceId", sliceProfileInstanceId)
194
195         AllottedResource allottedResource = new AllottedResource()
196
197         String allottedResourceId = UUID.randomUUID().toString()
198
199         allottedResource.setId(allottedResourceId)
200
201         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstanceId).allottedResource(allottedResourceId))
202         doNothing().when(client).create(allottedResourceUri, allottedResource)
203
204         currentNSSI.put("allottedResourceUri", allottedResourceUri)
205
206         when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
207         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
208
209         currentNSSI.put("sliceProfileId", sliceProfileId)
210
211         SliceProfile sliceProfile = new SliceProfile()
212         currentNSSI.put("createdSliceProfile", sliceProfile)
213
214         AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstanceId))
215
216         AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
217
218         ServiceInstance sliceProfileInstance = new ServiceInstance()
219         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
220         Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
221
222         when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
223         doNothing().when(client).update(sliceProfileInstanceUri, sliceProfileInstance)
224
225         ServiceInstance nssi = new ServiceInstance()
226         nssi.setServiceInstanceId(nssiId)
227         nssi.setSliceProfiles(new SliceProfiles())
228         currentNSSI.put("nssi", nssi)
229
230         doNothing().when(client).connect(nssiUri, sliceProfileInstanceUri, AAIEdgeLabel.USES)
231
232         spy.associateSliceProfileInstanceWithNSSI(mockExecution)
233
234     }
235
236 }