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