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