c1ece719e98cf34a672318b093a5c5bcc9abed0a
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
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.flowspecific.tasks;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
26 import static org.junit.Assert.*;
27 import static org.mockito.ArgumentMatchers.isA;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.onap.so.BaseIntegrationTest;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.AllottedResource;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceProxy;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.VpnBondingLink;
45 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
46 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
47 import org.onap.so.bpmn.servicedecomposition.homingobjects.Candidate;
48 import org.onap.so.bpmn.servicedecomposition.homingobjects.CandidateType;
49 import org.onap.so.client.exception.BadResponseException;
50 import org.onap.so.client.sniro.beans.SniroManagerRequest;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 public class SniroHomingV2IT extends BaseIntegrationTest {
54
55     private ServiceInstance serviceInstance;
56
57     private RequestContext requestContext;
58
59     private Customer customer;
60     ObjectMapper mapper = new ObjectMapper();
61
62     private static final String RESOURCE_PATH = "__files/BuildingBlocks/SniroHoming/";
63
64
65     String mockResponse =
66             "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"corys cool\", \"requestStatus\": \"accepted\"}";
67
68     @Before
69     public void before() {
70         serviceInstance = setServiceInstance();
71         customer = setCustomer();
72         customer.setGlobalCustomerId("testCustomerId");
73         customer.setSubscriberName("testCustomerName");
74         customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
75
76         requestContext = setRequestContext();
77         requestContext.setMsoRequestId("testRequestId");
78         RequestParameters params = new RequestParameters();
79         params.setaLaCarte(false);
80         params.setSubscriptionServiceType("testSubscriptionServiceType");
81         requestContext.setRequestParameters(params);
82     }
83
84     public void beforeVpnBondingLink(String id) {
85         VpnBondingLink bondingLink = new VpnBondingLink();
86         bondingLink.setVpnBondingLinkId("testVpnBondingId" + id);
87         bondingLink.getServiceProxies().add(setServiceProxy("1", "transport"));
88         ServiceProxy sp2 = setServiceProxy("2", "infrastructure");
89         Candidate requiredCandidate = new Candidate();
90         requiredCandidate.setIdentifierType(CandidateType.VNF_ID);
91         List<String> c = new ArrayList<String>();
92         c.add("testVnfId");
93         requiredCandidate.setIdentifiers(c);
94         sp2.addRequiredCandidates(requiredCandidate);
95         bondingLink.getServiceProxies().add(sp2);
96         serviceInstance.getVpnBondingLinks().add(bondingLink);
97
98     }
99
100     public void beforeAllottedResource() {
101         serviceInstance.getAllottedResources().add(setAllottedResource("1"));
102         serviceInstance.getAllottedResources().add(setAllottedResource("2"));
103         serviceInstance.getAllottedResources().add(setAllottedResource("3"));
104     }
105
106     public void beforeServiceProxy() {
107         ServiceProxy sp = setServiceProxy("1", "infrastructure");
108         Candidate filteringAttributes = new Candidate();
109         filteringAttributes.setIdentifierType(CandidateType.CLOUD_REGION_ID);
110         List<String> c = new ArrayList<String>();
111         c.add("testCloudRegionId");
112         filteringAttributes.setCloudOwner("att");
113         filteringAttributes.setIdentifiers(c);
114         sp.getFilteringAttributes().add(filteringAttributes);
115         serviceInstance.getServiceProxies().add(sp);
116     }
117
118     public void beforeVnf() {
119         setGenericVnf();
120     }
121
122     @Test(expected = Test.None.class)
123     public void testCallSniro_success_1VpnLink() throws BadResponseException {
124         beforeVpnBondingLink("1");
125
126         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
127                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
128
129         sniroHoming.callSniro(execution);
130
131         String request = readResourceFile(RESOURCE_PATH + "SniroManagerRequest1Vpn.json");
132         request = request.replace("28080", wireMockPort);
133
134         ArgumentCaptor<SniroManagerRequest> argument = ArgumentCaptor.forClass(SniroManagerRequest.class);
135         verify(sniroClient, times(1)).postDemands(argument.capture());
136         assertEquals(request, argument.getValue().toJsonString());
137     }
138
139     @Test
140     public void testCallSniro_success_3VpnLink() throws BadResponseException {
141         beforeVpnBondingLink("1");
142         beforeVpnBondingLink("2");
143         beforeVpnBondingLink("3");
144
145         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
146                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
147
148         sniroHoming.callSniro(execution);
149
150         String request = readResourceFile(RESOURCE_PATH + "SniroManagerRequest3Vpn.json");
151         request = request.replace("28080", wireMockPort);
152
153         ArgumentCaptor<SniroManagerRequest> argument = ArgumentCaptor.forClass(SniroManagerRequest.class);
154         verify(sniroClient, times(1)).postDemands(argument.capture());
155         assertEquals(request, argument.getValue().toJsonString());
156     }
157
158     @Test
159     public void testCallSniro_success_3Allotteds() throws BadResponseException {
160         beforeAllottedResource();
161
162         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
163                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
164
165         sniroHoming.callSniro(execution);
166
167         String request = readResourceFile(RESOURCE_PATH + "SniroManagerRequest3AR.json");
168         request = request.replace("28080", wireMockPort);
169
170         ArgumentCaptor<SniroManagerRequest> argument = ArgumentCaptor.forClass(SniroManagerRequest.class);
171         verify(sniroClient, times(1)).postDemands(argument.capture());
172         assertEquals(request, argument.getValue().toJsonString());
173     }
174
175     @Test
176     public void testCallSniro_success_1Vnf() throws BadResponseException {
177         beforeVnf();
178
179         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
180                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
181
182         sniroHoming.callSniro(execution);
183
184         ArgumentCaptor<SniroManagerRequest> argument = ArgumentCaptor.forClass(SniroManagerRequest.class);
185         verify(sniroClient, times(1)).postDemands(argument.capture());
186         // TODO assertEquals(request, argument.getValue().toJsonString());
187     }
188
189     @Test
190     public void testCallSniro_success_3Allotteds1Vnf() throws BadResponseException {
191         beforeAllottedResource();
192         beforeVnf();
193
194         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
195                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
196
197         sniroHoming.callSniro(execution);
198
199         verify(sniroClient, times(1)).postDemands(isA(SniroManagerRequest.class));
200     }
201
202     @Test
203     public void testCallSniro_success_1ServiceProxy() throws BadResponseException {
204         beforeServiceProxy();
205
206         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
207                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
208
209         sniroHoming.callSniro(execution);
210
211         String request = readResourceFile(RESOURCE_PATH + "SniroManagerRequest1SP.json");
212         request = request.replace("28080", wireMockPort);
213
214         ArgumentCaptor<SniroManagerRequest> argument = ArgumentCaptor.forClass(SniroManagerRequest.class);
215         verify(sniroClient, times(1)).postDemands(argument.capture());
216         assertEquals(request, argument.getValue().toJsonString());
217     }
218
219     @Test(expected = Test.None.class)
220     public void testProcessSolution_success_1VpnLink_1Solution() {
221         beforeVpnBondingLink("1");
222
223         JSONObject asyncResponse = new JSONObject();
224         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
225                 "completed");
226         JSONArray solution1 = new JSONArray();
227         solution1.put(new JSONObject().put("serviceResourceId", "testProxyId1")
228                 .put("solution",
229                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
230                                 new JSONArray().put("testServiceInstanceId1")))
231                 .put("assignmentInfo",
232                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
233                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
234                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName1"))
235                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli1"))
236                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
237                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
238                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId1"))));
239         solution1.put(new JSONObject().put("serviceResourceId", "testProxyId2")
240                 .put("solution",
241                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
242                                 new JSONArray().put("testServiceInstanceId2")))
243                 .put("assignmentInfo",
244                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
245                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
246                                 .put(new JSONObject().put("key", "primaryPnfName").put("value", "testPrimaryPnfName2"))
247                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
248                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
249                                 .put(new JSONObject().put("key", "secondaryPnfName").put("value",
250                                         "testSecondaryPnfName2"))
251                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId2"))));
252
253         asyncResponse.put("solutions", new JSONObject().put("placementSolutions", new JSONArray().put(solution1))
254                 .put("licenseSolutions", new JSONArray()));
255
256         sniroHoming.processSolution(execution, asyncResponse.toString());
257
258         ServiceInstance si =
259                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
260
261         assertFalse(si.getVpnBondingLinks().isEmpty());
262         VpnBondingLink link = si.getVpnBondingLinks().get(0);
263         assertNotNull(link);
264         assertFalse(link.getServiceProxies().isEmpty());
265
266         assertEquals("testServiceInstanceId1",
267                 link.getServiceProxy("testProxyId1").getServiceInstance().getServiceInstanceId());
268         assertNotNull(link.getServiceProxy("testProxyId1").getServiceInstance().getSolutionInfo());
269         assertEquals("testVnfHostName1",
270                 link.getServiceProxy("testProxyId1").getServiceInstance().getVnfs().get(0).getVnfName());
271
272         assertEquals("testServiceInstanceId2",
273                 link.getServiceProxy("testProxyId2").getServiceInstance().getServiceInstanceId());
274         assertNotNull(link.getServiceProxy("testProxyId2").getServiceInstance().getSolutionInfo());
275         assertFalse(link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().isEmpty());
276         assertEquals("testPrimaryPnfName2",
277                 link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getPnfName());
278         assertEquals("primary", link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getRole());
279         assertEquals("testSecondaryPnfName2",
280                 link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getPnfName());
281         assertEquals("secondary", link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getRole());
282     }
283
284     @Test
285     public void testProcessSolution_success_1VpnLink_2Solutions() {
286         beforeVpnBondingLink("1");
287
288         JSONObject asyncResponse = new JSONObject();
289         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
290                 "completed");
291         JSONArray solution1 = new JSONArray();
292         solution1.put(new JSONObject().put("serviceResourceId", "testProxyId1")
293                 .put("solution",
294                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
295                                 new JSONArray().put("testServiceInstanceId1")))
296                 .put("assignmentInfo",
297                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
298                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
299                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName1"))
300                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli1"))
301                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
302                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
303                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId1"))));
304         solution1.put(new JSONObject().put("serviceResourceId", "testProxyId2")
305                 .put("solution",
306                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
307                                 new JSONArray().put("testServiceInstanceId2")))
308                 .put("assignmentInfo",
309                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
310                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
311                                 .put(new JSONObject().put("key", "primaryPnfName").put("value", "testPrimaryPnfName2"))
312                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
313                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
314                                 .put(new JSONObject().put("key", "secondaryPnfName").put("value",
315                                         "testSecondaryPnfName2"))
316                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId2"))));
317
318         JSONArray solution2 = new JSONArray();
319         solution2.put(new JSONObject().put("serviceResourceId", "testProxyId1")
320                 .put("solution",
321                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
322                                 new JSONArray().put("testServiceInstanceId3")))
323                 .put("assignmentInfo",
324                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
325                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
326                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName3"))
327                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli3"))
328                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
329                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId3"))
330                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId3"))));
331         solution2.put(new JSONObject().put("serviceResourceId", "testProxyId2")
332                 .put("solution",
333                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
334                                 new JSONArray().put("testServiceInstanceId4")))
335                 .put("assignmentInfo",
336                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
337                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
338                                 .put(new JSONObject().put("key", "primaryPnfName").put("value", "testPrimaryPnfName4"))
339                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli4"))
340                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
341                                 .put(new JSONObject().put("key", "secondaryPnfName").put("value",
342                                         "testSecondaryPnfName4"))
343                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testSloudRegionId4"))));
344
345         asyncResponse.put("solutions",
346                 new JSONObject().put("placementSolutions", new JSONArray().put(solution1).put(solution2))
347                         .put("licenseSolutions", new JSONArray()));
348
349         sniroHoming.processSolution(execution, asyncResponse.toString());
350
351         ServiceInstance si =
352                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
353
354         assertFalse(si.getVpnBondingLinks().isEmpty());
355         VpnBondingLink link = si.getVpnBondingLinks().get(0);
356         VpnBondingLink link2 = si.getVpnBondingLinks().get(1);
357         assertNotNull(link);
358         assertFalse(link.getServiceProxies().isEmpty());
359
360         assertEquals("testServiceInstanceId1",
361                 link.getServiceProxy("testProxyId1").getServiceInstance().getServiceInstanceId());
362         assertNotNull(link.getServiceProxy("testProxyId1").getServiceInstance().getSolutionInfo());
363         assertEquals("testVnfHostName1",
364                 link.getServiceProxy("testProxyId1").getServiceInstance().getVnfs().get(0).getVnfName());
365
366         assertEquals("testServiceInstanceId2",
367                 link.getServiceProxy("testProxyId2").getServiceInstance().getServiceInstanceId());
368         assertNotNull(link.getServiceProxy("testProxyId2").getServiceInstance().getSolutionInfo());
369         assertFalse(link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().isEmpty());
370         assertEquals("testPrimaryPnfName2",
371                 link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getPnfName());
372         assertEquals("primary", link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getRole());
373         assertEquals("testSecondaryPnfName2",
374                 link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getPnfName());
375         assertEquals("secondary", link.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getRole());
376
377         assertNotNull(link2);
378         assertFalse(link2.getServiceProxies().isEmpty());
379
380         assertEquals("testServiceInstanceId3",
381                 link2.getServiceProxy("testProxyId1").getServiceInstance().getServiceInstanceId());
382         assertNotNull(link2.getServiceProxy("testProxyId1").getServiceInstance().getSolutionInfo());
383         assertEquals("testVnfHostName3",
384                 link2.getServiceProxy("testProxyId1").getServiceInstance().getVnfs().get(0).getVnfName());
385
386         assertEquals("testServiceInstanceId4",
387                 link2.getServiceProxy("testProxyId2").getServiceInstance().getServiceInstanceId());
388         assertNotNull(link2.getServiceProxy("testProxyId2").getServiceInstance().getSolutionInfo());
389         assertFalse(link2.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().isEmpty());
390         assertEquals("testPrimaryPnfName4",
391                 link2.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getPnfName());
392         assertEquals("primary", link2.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(0).getRole());
393         assertEquals("testSecondaryPnfName4",
394                 link2.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getPnfName());
395         assertEquals("secondary",
396                 link2.getServiceProxy("testProxyId2").getServiceInstance().getPnfs().get(1).getRole());
397
398     }
399
400     @Test
401     public void testProcessSolution_success_3VpnLink_2Solutions() {
402         // TODO
403     }
404
405     @Test
406     public void testProcessSolution_success_3Allotteds_1Solution() {
407         beforeAllottedResource();
408
409         JSONObject asyncResponse = new JSONObject();
410         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
411                 "completed");
412         JSONArray solution1 = new JSONArray();
413         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId1")
414                 .put("solution",
415                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
416                                 new JSONArray().put("testServiceInstanceId1")))
417                 .put("assignmentInfo",
418                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
419                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
420                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName1"))
421                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli1"))
422                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
423                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
424                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testCloudRegionId1"))));
425         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId2")
426                 .put("solution",
427                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
428                                 new JSONArray().put("testServiceInstanceId2")))
429                 .put("assignmentInfo",
430                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
431                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
432                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName2"))
433                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
434                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
435                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
436                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testCloudRegionId2"))));
437         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId3")
438                 .put("solution",
439                         new JSONObject().put("identifierType", "cloudRegionId").put("identifiers",
440                                 new JSONArray().put("testCloudRegionId3")))
441                 .put("assignmentInfo",
442                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
443                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
444                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
445                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))));
446
447         asyncResponse.put("solutions", new JSONObject().put("placementSolutions", new JSONArray().put(solution1))
448                 .put("licenseSolutions", new JSONArray()));
449
450         sniroHoming.processSolution(execution, asyncResponse.toString());
451
452         ServiceInstance si =
453                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
454
455         assertFalse(si.getAllottedResources().isEmpty());
456         AllottedResource ar = si.getAllottedResources().get(0);
457         assertNotNull(ar);
458         assertEquals("testServiceInstanceId1", ar.getParentServiceInstance().getServiceInstanceId());
459         assertNotNull(ar.getParentServiceInstance().getSolutionInfo());
460         assertEquals("testVnfHostName1", ar.getParentServiceInstance().getVnfs().get(0).getVnfName());
461
462         AllottedResource ar2 = si.getAllottedResources().get(1);
463         assertNotNull(ar2);
464         assertEquals("testServiceInstanceId2", ar2.getParentServiceInstance().getServiceInstanceId());
465         assertNotNull(ar2.getParentServiceInstance().getSolutionInfo());
466         assertEquals("testVnfHostName2", ar2.getParentServiceInstance().getVnfs().get(0).getVnfName());
467
468         AllottedResource ar3 = si.getAllottedResources().get(2);
469         assertNotNull(ar3);
470         assertNotNull(ar3.getParentServiceInstance().getSolutionInfo());
471         assertEquals("testCloudRegionId3",
472                 ar3.getParentServiceInstance().getSolutionInfo().getTargetedCloudRegion().getLcpCloudRegionId());
473     }
474
475     @Test
476     public void testProcessSolution_success_3Allotteds1Vnf_1Solution() {
477         beforeVnf();
478         beforeAllottedResource();
479
480         JSONObject asyncResponse = new JSONObject();
481         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
482                 "completed");
483         JSONArray solution1 = new JSONArray();
484         JSONArray licenseSolution = new JSONArray();
485         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId1")
486                 .put("solution",
487                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
488                                 new JSONArray().put("testServiceInstanceId1")))
489                 .put("assignmentInfo",
490                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
491                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
492                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName1"))
493                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli1"))
494                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
495                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
496                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testCloudRegionId1"))));
497         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId2")
498                 .put("solution",
499                         new JSONObject().put("identifierType", "serviceInstanceId").put("identifiers",
500                                 new JSONArray().put("testServiceInstanceId2")))
501                 .put("assignmentInfo",
502                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
503                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
504                                 .put(new JSONObject().put("key", "vnfHostName").put("value", "testVnfHostName2"))
505                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
506                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
507                                 .put(new JSONObject().put("key", "vnfId").put("value", "testVnfId1"))
508                                 .put(new JSONObject().put("key", "cloudRegionId").put("value", "testCloudRegionId2"))));
509         solution1.put(new JSONObject().put("serviceResourceId", "testAllottedResourceId3")
510                 .put("solution",
511                         new JSONObject().put("identifierType", "cloudRegionId").put("identifiers",
512                                 new JSONArray().put("testCloudRegionId3")))
513                 .put("assignmentInfo",
514                         new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "True"))
515                                 .put(new JSONObject().put("key", "cloudOwner").put("value", "aic"))
516                                 .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli2"))
517                                 .put(new JSONObject().put("key", "aicVersion").put("value", "3"))));
518
519         licenseSolution.put(new JSONObject().put("serviceResourceId", "testVnfId1")
520                 .put("entitlementPoolUUID",
521                         new JSONArray().put("f1d563e8-e714-4393-8f99-cc480144a05e")
522                                 .put("j1d563e8-e714-4393-8f99-cc480144a05e"))
523                 .put("licenseKeyGroupUUID", new JSONArray().put("s1d563e8-e714-4393-8f99-cc480144a05e")
524                         .put("b1d563e8-e714-4393-8f99-cc480144a05e")));
525
526         asyncResponse.put("solutions", new JSONObject().put("placementSolutions", new JSONArray().put(solution1))
527                 .put("licenseSolutions", licenseSolution));
528
529         sniroHoming.processSolution(execution, asyncResponse.toString());
530
531         ServiceInstance si =
532                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
533
534         assertFalse(si.getAllottedResources().isEmpty());
535         AllottedResource ar = si.getAllottedResources().get(0);
536         assertNotNull(ar);
537         assertEquals("testServiceInstanceId1", ar.getParentServiceInstance().getServiceInstanceId());
538         assertNotNull(ar.getParentServiceInstance().getSolutionInfo());
539         assertEquals("testVnfHostName1", ar.getParentServiceInstance().getVnfs().get(0).getVnfName());
540
541         AllottedResource ar2 = si.getAllottedResources().get(1);
542         assertNotNull(ar2);
543         assertEquals("testServiceInstanceId2", ar2.getParentServiceInstance().getServiceInstanceId());
544         assertNotNull(ar2.getParentServiceInstance().getSolutionInfo());
545         assertEquals("testVnfHostName2", ar2.getParentServiceInstance().getVnfs().get(0).getVnfName());
546
547         AllottedResource ar3 = si.getAllottedResources().get(2);
548         assertNotNull(ar3);
549         assertNotNull(ar3.getParentServiceInstance().getSolutionInfo());
550         assertEquals("testCloudRegionId3",
551                 ar3.getParentServiceInstance().getSolutionInfo().getTargetedCloudRegion().getLcpCloudRegionId());
552
553         GenericVnf vnf = si.getVnfs().get(0);
554         assertNotNull(vnf);
555         assertNotNull(vnf.getLicense());
556         assertEquals(2, vnf.getLicense().getEntitlementPoolUuids().size());
557         assertEquals("s1d563e8-e714-4393-8f99-cc480144a05e", vnf.getLicense().getLicenseKeyGroupUuids().get(0));
558
559     }
560
561     @Test
562     public void testProcessSolution_success_1Vnf_1Solution() {
563         beforeVnf();
564
565         JSONObject asyncResponse = new JSONObject();
566         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
567                 "completed");
568         JSONArray licenseSolution = new JSONArray();
569
570         licenseSolution.put(new JSONObject().put("serviceResourceId", "testVnfId1")
571                 .put("entitlementPoolUUID",
572                         new JSONArray().put("f1d563e8-e714-4393-8f99-cc480144a05e")
573                                 .put("j1d563e8-e714-4393-8f99-cc480144a05e"))
574                 .put("licenseKeyGroupUUID", new JSONArray().put("s1d563e8-e714-4393-8f99-cc480144a05e")
575                         .put("b1d563e8-e714-4393-8f99-cc480144a05e")));
576
577         asyncResponse.put("solutions", new JSONObject().put("licenseSolutions", licenseSolution));
578
579         sniroHoming.processSolution(execution, asyncResponse.toString());
580
581         ServiceInstance si =
582                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
583
584         GenericVnf vnf = si.getVnfs().get(0);
585         assertNotNull(vnf);
586         assertNotNull(vnf.getLicense());
587         assertEquals(2, vnf.getLicense().getEntitlementPoolUuids().size());
588         assertEquals(2, vnf.getLicense().getLicenseKeyGroupUuids().size());
589         assertEquals("f1d563e8-e714-4393-8f99-cc480144a05e", vnf.getLicense().getEntitlementPoolUuids().get(0));
590         assertEquals("s1d563e8-e714-4393-8f99-cc480144a05e", vnf.getLicense().getLicenseKeyGroupUuids().get(0));
591     }
592
593     @Test
594     public void testProcessSolution_success_1ServiceProxy_1Solutions() {
595         beforeServiceProxy();
596
597         JSONObject asyncResponse = new JSONObject();
598         asyncResponse.put("transactionId", "testRequestId").put("requestId", "testRequestId").put("requestState",
599                 "completed");
600         JSONArray solution1 = new JSONArray();
601         solution1
602                 .put(new JSONObject()
603                         .put("serviceResourceId", "testProxyId1").put(
604                                 "solution",
605                                 new JSONObject()
606                                         .put("identifierType", "serviceInstanceId")
607                                         .put("identifiers", new JSONArray().put("testServiceInstanceId1")))
608                         .put("assignmentInfo",
609                                 new JSONArray().put(new JSONObject().put("key", "isRehome").put("value", "False"))
610                                         .put(new JSONObject().put("key", "cloudOwner").put("value", ""))
611                                         .put(new JSONObject().put("key", "aicClli").put("value", "testAicClli1"))
612                                         .put(new JSONObject().put("key", "aicVersion").put("value", "3"))
613                                         .put(new JSONObject().put("key", "cloudRegionId").put("value", ""))
614                                         .put(new JSONObject().put("key", "primaryPnfName").put("value",
615                                                 "testPrimaryPnfName"))
616                                         .put(new JSONObject().put("key", "secondaryPnfName").put("value",
617                                                 "testSecondaryPnfName"))));
618
619         asyncResponse.put("solutions", new JSONObject().put("placementSolutions", new JSONArray().put(solution1))
620                 .put("licenseSolutions", new JSONArray()));
621
622         sniroHoming.processSolution(execution, asyncResponse.toString());
623
624         ServiceInstance si =
625                 execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
626
627         ServiceProxy sp = si.getServiceProxies().get(0);
628         assertNotNull(sp);
629         assertNotNull(sp.getServiceInstance());
630
631         assertEquals("testServiceInstanceId1", sp.getServiceInstance().getServiceInstanceId());
632         assertNotNull(sp.getServiceInstance().getSolutionInfo());
633
634         assertFalse(sp.getServiceInstance().getPnfs().isEmpty());
635         assertEquals("testPrimaryPnfName", sp.getServiceInstance().getPnfs().get(0).getPnfName());
636         assertEquals("primary", sp.getServiceInstance().getPnfs().get(0).getRole());
637         assertEquals("testSecondaryPnfName", sp.getServiceInstance().getPnfs().get(1).getPnfName());
638         assertEquals("secondary", sp.getServiceInstance().getPnfs().get(1).getRole());
639     }
640
641
642     @Test(expected = BpmnError.class)
643     public void testCallSniro_error_0Resources() throws BadResponseException {
644
645         sniroHoming.callSniro(execution);
646
647         verify(sniroClient, times(0)).postDemands(isA(SniroManagerRequest.class));
648     }
649
650     @Test(expected = BpmnError.class)
651     public void testCallSniro_error_badResponse() throws BadResponseException {
652         beforeAllottedResource();
653
654         mockResponse =
655                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"\", \"requestStatus\": \"failed\"}";
656         wireMockServer.stubFor(post(urlEqualTo("/sniro/api/placement/v2")).willReturn(
657                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
658
659         sniroHoming.callSniro(execution);
660
661         verify(sniroClient, times(1)).postDemands(isA(SniroManagerRequest.class));
662     }
663
664 }