rename package for external use
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / appc / tasks / AppcRunTasksIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.so.bpmn.infrastructure.appc.tasks;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
23 import static com.github.tomakehurst.wiremock.client.WireMock.get;
24 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.HashMap;
34 import java.util.Optional;
35 import java.util.UUID;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.appc.client.lcm.model.Action;
39 import org.onap.so.BaseIntegrationTest;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
41 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
42 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
43 import org.onap.aaiclient.client.aai.AAIVersion;
44 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 public class AppcRunTasksIT extends BaseIntegrationTest {
48
49     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
50
51     @Autowired
52     private AppcRunTasks appcRunTasks;
53
54     private GenericVnf genericVnf;
55     private RequestContext requestContext;
56     private String msoRequestId;
57
58     @Before
59     public void before() {
60         genericVnf = setGenericVnf();
61         msoRequestId = UUID.randomUUID().toString();
62         requestContext = setRequestContext();
63         requestContext.setMsoRequestId(msoRequestId);
64         gBBInput.setRequestContext(requestContext);
65     }
66
67     @Test
68     public void preProcessActivityWithVserversTest() throws Exception {
69         final String aaiVnfJson =
70                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiGenericVnfWithVservers.json")));
71         wireMockServer.stubFor(
72                 get(urlEqualTo("/aai/" + AAIVersion.LATEST + "/network/generic-vnfs/generic-vnf/testVnfId1?depth=all"))
73                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(aaiVnfJson)
74                                 .withStatus(200)));
75
76         final String aaiVserverJson =
77                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiVserverFullQueryResponse.json")));
78         wireMockServer.stubFor(get(urlEqualTo("/aai/" + AAIVersion.LATEST
79                 + "/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/mtn23a/tenants/tenant/e6beab145f6b49098277ac163ac1b4f3/vservers/vserver/48bd7f11-408f-417c-b834-b41c1b98f7d7"))
80                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(aaiVserverJson)
81                                 .withStatus(200)));
82         wireMockServer.stubFor(get(urlEqualTo("/aai/" + AAIVersion.LATEST
83                 + "/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/mtn23a/tenants/tenant/e6beab145f6b49098277ac163ac1b4f3/vservers/vserver/1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5"))
84                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(aaiVserverJson)
85                                 .withStatus(200)));
86         wireMockServer.stubFor(get(urlEqualTo("/aai/" + AAIVersion.LATEST
87                 + "/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/mtn23a/tenants/tenant/e6beab145f6b49098277ac163ac1b4f3/vservers/vserver/14551849-1e70-45cd-bc5d-a256d49548a2"))
88                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(aaiVserverJson)
89                                 .withStatus(200)));
90
91         appcRunTasks.preProcessActivity(execution);
92         String vserverIdList = execution.getVariable("vserverIdList");
93         String expectedVserverIdList =
94                 "{\"vserverIds\":\"[\\\"1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5\\\",\\\"14551849-1e70-45cd-bc5d-a256d49548a2\\\",\\\"48bd7f11-408f-417c-b834-b41c1b98f7d7\\\"]\"}";
95         String vmIdList = execution.getVariable("vmIdList");
96         String expectedVmIdList =
97                 "{\"vmIds\":\"[\\\"http://VSERVER-link.com\\\",\\\"http://VSERVER-link.com\\\",\\\"http://VSERVER-link.com\\\"]\"}";
98
99         assertEquals(vserverIdList, expectedVserverIdList);
100         assertEquals(vmIdList, expectedVmIdList);
101         assertEquals(execution.getVariable("actionQuiesceTraffic"), Action.QuiesceTraffic);
102         assertEquals(execution.getVariable("rollbackQuiesceTraffic"), false);
103     }
104
105     @Test
106     public void preProcessActivityNoVserversTest() throws Exception {
107         final String aaiVnfJson = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiGenericVnf.json")));
108         wireMockServer.stubFor(
109                 get(urlEqualTo("/aai/v15/network/generic-vnfs/generic-vnf/testVnfId1?depth=all")).willReturn(aResponse()
110                         .withHeader("Content-Type", "application/json").withBody(aaiVnfJson).withStatus(200)));
111         appcRunTasks.preProcessActivity(execution);
112         assertNull(execution.getVariable("vmIdList"));
113         assertNull(execution.getVariable("vServerIdList"));
114         assertEquals(execution.getVariable("actionQuiesceTraffic"), Action.QuiesceTraffic);
115         assertEquals(execution.getVariable("rollbackQuiesceTraffic"), false);
116     }
117
118     @Test
119     public void runAppcCommandTest() throws Exception {
120         Action action = Action.QuiesceTraffic;
121         ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
122         controllerSelectionReference.setControllerName("testName");
123         controllerSelectionReference.setActionCategory(action.toString());
124         controllerSelectionReference.setVnfType("testVnfType");
125
126         doReturn(controllerSelectionReference).when(catalogDbClient)
127                 .getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(),
128                         Action.QuiesceTraffic.toString());
129
130         execution.setVariable("aicIdentity", "testAicIdentity");
131
132         String vnfId = genericVnf.getVnfId();
133         genericVnf.setIpv4OamAddress("testOamIpAddress");
134         String payload = "{\"testName\":\"testValue\",}";
135         RequestParameters requestParameters = new RequestParameters();
136         requestParameters.setPayload(payload);
137         gBBInput.getRequestContext().setRequestParameters(requestParameters);
138
139         String controllerType = "testName";
140         HashMap<String, String> payloadInfo = new HashMap<String, String>();
141         payloadInfo.put("vnfName", "testVnfName1");
142         payloadInfo.put("aicIdentity", "testAicIdentity");
143         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
144         payloadInfo.put("vserverIdList", null);
145         payloadInfo.put("vfModuleId", null);
146         payloadInfo.put("identityUrl", null);
147         payloadInfo.put("vmIdList", null);
148
149         doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
150                 controllerType);
151
152         appcRunTasks.runAppcCommand(execution, action);
153         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
154                 controllerType);
155     }
156 }