Merge branch 'recursive-orch'
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoAllocateTnNssiTest.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  #
8  # Licensed under the Apache License, Version 2.0 (the "License")
9  # you may not use this file except in compliance with the License.
10  # You may obtain a copy of the License at
11  #
12  #       http://www.apache.org/licenses/LICENSE-2.0
13  #
14  # Unless required by applicable law or agreed to in writing, software
15  # distributed under the License is distributed on an "AS IS" BASIS,
16  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  # See the License for the specific language governing permissions and
18  # limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
24 import org.junit.Before
25 import org.junit.Test
26 import org.mockito.ArgumentCaptor
27 import org.mockito.Captor
28 import org.mockito.Mockito
29 import org.onap.aaiclient.client.aai.AAIObjectType
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
34 import org.onap.so.beans.nsmf.oof.TemplateInfo
35 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
36 import org.onap.so.bpmn.common.scripts.OofUtils
37
38 import static org.junit.Assert.assertNotNull
39 import static org.junit.Assert.assertEquals
40 import static org.mockito.ArgumentMatchers.eq
41 import static org.mockito.Mockito.*
42
43 class DoAllocateTnNssiTest extends MsoGroovyTest {
44     @Before
45     void init() throws IOException {
46         super.init("DoAllocateTnNssiTest")
47     }
48
49     @Captor
50     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
51
52     @Test
53     void testPreProcessRequest() {
54         when(mockExecution.getVariable("msoRequestId")).thenReturn("4c614769-f58a-4556-8ad9-dcd903077c82")
55         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
56         when(mockExecution.getVariable("modelInvariantUuid")).thenReturn("f85cbcc0-ad74-45d7-a5a1-17c8744fdb71")
57         when(mockExecution.getVariable("modelUuid")).thenReturn("36a3a8ea-49a6-4ac8-b06c-89a54544b9b6")
58         //when(mockExecution.getVariable("serviceInstanceID")).thenReturn("eb0863e9-a69b-4b17-8a56-f05ad110bef7")
59         //when(mockExecution.getVariable("operationId")).thenReturn("998c2081-5a71-4a39-9ae6-d6b7c5bb50c0")
60         when(mockExecution.getVariable("operationType")).thenReturn("opTypeTest")
61         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
62         when(mockExecution.getVariable("servicename")).thenReturn("5G-test")
63         when(mockExecution.getVariable("networkType")).thenReturn("5G-network")
64         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("5G-service")
65         when(mockExecution.getVariable("nsiId")).thenReturn("88f65519-9a38-4c4b-8445-9eb4a5a5af56")
66         when(mockExecution.getVariable("jobId")).thenReturn("f70e927b-6087-4974-9ef8-c5e4d5847ca4")
67         when(mockExecution.getVariable("sliceParams")).thenReturn(mockSliceParams())
68
69         TnAllocateNssi obj = new TnAllocateNssi()
70         obj.preProcessRequest(mockExecution)
71         Mockito.verify(mockExecution, times(1)).setVariable(eq("sliceServiceInstanceId"), captor.capture())
72         String sliceServiceInstanceId = captor.getValue()
73         assertNotNull(sliceServiceInstanceId)
74     }
75
76     @Test
77     void testCreateServiceInstance() {
78         when(mockExecution.getVariable("sliceServiceInstanceId")).thenReturn("5ad89cf9-0569-4a93-9306-d8324321e2be")
79         when(mockExecution.getVariable("sliceServiceInstanceName")).thenReturn("5G-service")
80         when(mockExecution.getVariable("globalSubscriberId")).thenReturn("5GCustomer")
81         when(mockExecution.getVariable("subscriptionServiceType")).thenReturn("5G")
82         when(mockExecution.getVariable("modelInvariantUuid")).thenReturn("f85cbcc0-ad74-45d7-a5a1-17c8744fdb71")
83         when(mockExecution.getVariable("modelUuid")).thenReturn("36a3a8ea-49a6-4ac8-b06c-89a54544b9b6")
84         when(mockExecution.getVariable("sliceProfile")).thenReturn(mockSliceProfile())
85
86 //        JsonUtils jsonUtil = new JsonUtils()
87 //        String sliceProfile = jsonUtil.getJsonValue(mockSliceParams(), "sliceProfile")
88 //        when(mockExecution.getVariable("sliceProfile")).thenReturn(sliceProfile)
89
90         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer("5GCustomer").serviceSubscription("5G").serviceInstance("5ad89cf9-0569-4a93-9306-d8324321e2be"))
91         DoCreateTnNssiInstance obj = spy(DoCreateTnNssiInstance.class)
92         when(obj.getAAIClient()).thenReturn(client)
93
94         obj.createServiceInstance(mockExecution)
95     }
96
97
98     private String mockSliceParams() {
99         String expect = """{
100     "sliceProfile": {
101       "snssaiList": [
102         "001-100001"
103       ],
104       "sliceProfileId": "ab9af40f13f721b5f13539d87484098",
105       "plmnIdList": [
106         "460-00",
107         "460-01"
108       ],
109       "perfReq": {
110       },
111       "coverageAreaTAList": [
112       ],
113       "latency": 2,
114       "maxBandwidth": 100,
115       "resourceSharingLevel": "non-shared"
116     },
117     "transportSliceNetworks": [
118             {
119                 "connectionLinks": [
120                     {
121                         "transportEndpointA": "tranportEp_ID_XXX",
122                         "transportEndpointB": "tranportEp_ID_YYY"
123                     },
124                     {
125                         "transportEndpointA": "tranportEp_ID_AAA",
126                         "transportEndpointB": "tranportEp_ID_BBB"
127                     }
128                 ]
129             },
130             {
131                 "connectionLinks": [
132                     {
133                         "transportEndpointA": "tranportEp_ID_CCC",
134                         "transportEndpointB": "tranportEp_ID_DDD"
135                     },
136                     {
137                         "transportEndpointA": "tranportEp_ID_EEE",
138                         "transportEndpointB": "tranportEp_ID_FFF"
139                     }
140                 ]
141             }
142     ],
143     "nsiInfo": {
144       "nsiId": "NSI-M-001-HDBNJ-NSMF-01-A-ZX",
145       "nsiName": "eMBB-001"
146     },
147     "scriptName": "AN1"
148         }"""
149         return expect.replaceAll("\\\\s+", "")
150     }
151
152     private String mockSliceProfile() {
153         String expect = """{
154       "snssaiList": [
155         "001-100001"
156       ],
157       "sliceProfileId": "ab9af40f13f721b5f13539d87484098",
158       "plmnIdList": [
159         "460-00",
160         "460-01"
161       ],
162       "perfReq": {
163       },
164       "coverageAreaTAList": [
165       ],
166       "latency": 2,
167       "maxBandwidth": 100,
168       "resourceSharingLevel": "non-shared"
169      }"""
170         return expect.replaceAll("\\\\s+", "")
171     }
172
173     @Test
174     void testPrepareOofSelection() {
175         when(mockExecution.getVariable("msoRequestId")).thenReturn("10dad82d-4bd9-467a-b113-5f8ea7eaae3c")
176         when(mockExecution.getVariable("modelUuid")).thenReturn("e2eb2fe3-92a7-447b-8582-077db5cd0862")
177         when(mockExecution.getVariable("modelInvariantUuid")).thenReturn("22e6ce80-a55f-4171-a457-a7ecb1865669")
178         when(mockExecution.getVariable("tnModelName")).thenReturn("Tn_ONAP_internal_BH")
179         when(mockExecution.getVariable("mso.adapters.oof.timeout")).thenReturn("")
180         TnAllocateNssi obj = spy(TnAllocateNssi.class)
181         OofUtils oofUtils = spy(OofUtils.class)
182         String requestId = "10dad82d-4bd9-467a-b113-5f8ea7eaae3c"
183         String nsstInfo = "\"{\"UUID\":\"e2eb2fe3-92a7-447b-8582-077db5cd0862\",\"invariantUUID\":\"22e6ce80-a55f-4171-a457-a7ecb1865669\",\"name\":\"Tn_ONAP_internal_BH\"}"
184         String messageType = "NSSISelectionResponse"
185         String sliceProfile = "{\"maxBandwidth\":3000,\"sliceProfileId\":\"ab9af40f13f721b5f13539d87484098\",\"latency\":10,\"snssaiList\":[\"001-100001\"],\"pLMNIdList\":[\"460-00\",\"460-01\"],\"perfReq\":{},\"coverageAreaTAList\":[],\"resourceSharingLevel\":\"shared\"}}"
186         Integer timeout = 600
187
188         when(oofUtils.buildSelectNSSIRequest(requestId, nsstInfo as TemplateInfo, messageType, sliceProfile, timeout)).thenReturn("""
189         {
190     "apiPath": "/api/oof/selection/nssi/v1",
191     "requestDetails": "{\\"requestInfo\\":{\\"transactionId\\":\\"10dad82d-4bd9-467a-b113-5f8ea7eaae3c\\",\\"requestId\\":\\"10dad82d-4bd9-467a-b113-5f8ea7eaae3c\\",\\"callbackUrl\\":\\"http://so-oof-adapter.onap:8090/so/adapters/oof/callback/v1/NSSISelectionResponse/10dad82d-4bd9-467a-b113-5f8ea7eaae3c\\",\\"sourceId\\":\\"SO\\",\\"timeout\\":600,\\"numSolutions\\":1},\\"NSSTInfo\\":{\\"UUID\\":\\"e2eb2fe3-92a7-447b-8582-077db5cd0862\\",\\"invariantUUID\\":\\"22e6ce80-a55f-4171-a457-a7ecb1865669\\",\\"name\\":\\"Tn_ONAP_internal_BH\\"},\\"sliceProfile\\":{\\"maxBandwidth\\":3000,\\"sliceProfileId\\":\\"ab9af40f13f721b5f13539d87484098\\",\\"latency\\":10,\\"snssaiList\\":[\\"001-100001\\"],\\"pLMNIdList\\":[\\"460-00\\",\\"460-01\\"],\\"perfReq\\":{},\\"coverageAreaTAList\\":[],\\"resourceSharingLevel\\":\\"shared\\"}}"
192     }""".replaceAll("\\\\s+", ""))
193         obj.prepareOofSelection(mockExecution)
194         verify(mockExecution, times(1)).setVariable(eq("nssiSelection_oofRequest"), captor.capture())
195         String nssiSelection_oofRequest = captor.getValue()
196         assertNotNull(nssiSelection_oofRequest)
197     }
198
199     @Test
200     void testprocessOofSelection() {
201         when(mockExecution.getVariable("nssiSelection_oofRequest")).thenReturn("""
202         {
203     "requestId": "a727643a-bf89-4fd9-b33c-b7bdda18c2b7",
204     "transactionId": "a727643a-bf89-4fd9-b33c-b7bdda18c2b7",
205     "requestStatus": "completed",
206     "statusMessage": "",
207     "solutions": [
208         {
209             "UUID": "e2eb2fe3-92a7-447b-8582-077db5cd0862",
210             "invariantUUID": "22e6ce80-a55f-4171-a457-a7ecb1865669",
211             "NSSIName": "nssi_tnservice12",
212             "NSSIId": "fe26a924-3845-4001-b84e-a439a27a88c0"
213         }
214     ]
215     }""".replaceAll("\\\\s+", ""))
216         TnAllocateNssi obj = spy(TnAllocateNssi.class)
217         ArrayList<String> solution = new ArrayList<>()
218         assertEquals(1, solution.size())
219         verify(mockExecution, times(1)).setVariable(eq("isOofTnNssiSelected"), captor.capture())
220         String isOofTnNssiSelected = captor.getValue()
221         assertEquals("true", isOofTnNssiSelected)
222     }
223 }