Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoHeatUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.openstack.utils;
24
25 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
26 import static com.github.tomakehurst.wiremock.client.WireMock.get;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
28 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
29 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
30 import static org.junit.Assert.assertNotNull;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import org.apache.http.HttpStatus;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.onap.so.BaseTest;
40 import org.onap.so.StubOpenStack;
41 import org.onap.so.adapters.vdu.CloudInfo;
42 import org.onap.so.adapters.vdu.PluginAction;
43 import org.onap.so.adapters.vdu.VduArtifact;
44 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
45 import org.onap.so.adapters.vdu.VduInstance;
46 import org.onap.so.adapters.vdu.VduModelInfo;
47 import org.onap.so.adapters.vdu.VduStateType;
48 import org.onap.so.adapters.vdu.VduStatus;
49 import org.onap.so.db.catalog.beans.CloudIdentity;
50 import org.onap.so.db.catalog.beans.CloudSite;
51 import org.onap.so.openstack.beans.StackInfo;
52 import org.onap.so.openstack.exceptions.MsoAdapterException;
53 import org.onap.so.openstack.exceptions.MsoException;
54 import org.onap.so.openstack.exceptions.MsoIOException;
55 import org.onap.so.openstack.exceptions.MsoOpenstackException;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import com.woorea.openstack.heat.Heat;
58 import com.woorea.openstack.heat.model.CreateStackParam;
59
60 public class MsoHeatUtilsTest extends BaseTest {
61
62     @Autowired
63     private MsoHeatUtils heatUtils;
64
65     @Test
66     public void instantiateVduTest() throws MsoException, IOException {
67         VduInstance expected = new VduInstance();
68         expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
69         expected.setVduInstanceName("name");
70         VduStatus status = new VduStatus();
71         status.setState(VduStateType.INSTANTIATED);
72         status.setLastAction((new PluginAction("create", "complete", null)));
73         expected.setStatus(status);
74
75         CloudInfo cloudInfo = new CloudInfo();
76         cloudInfo.setCloudSiteId("MTN13");
77         cloudInfo.setTenantId("tenantId");
78         VduModelInfo vduModel = new VduModelInfo();
79         vduModel.setModelCustomizationUUID("blueprintId");
80         vduModel.setTimeoutMinutes(1);
81         VduArtifact artifact = new VduArtifact();
82         artifact.setName("name");
83         artifact.setType(ArtifactType.MAIN_TEMPLATE);
84         byte[] content = new byte[1];
85         artifact.setContent(content);
86         List<VduArtifact> artifacts = new ArrayList<>();
87         artifacts.add(artifact);
88         vduModel.setArtifacts(artifacts);
89         Map<String, byte[]> blueprintFiles = new HashMap<>();
90         blueprintFiles.put(artifact.getName(), artifact.getContent());
91         String instanceName = "instanceName";
92         Map<String, Object> inputs = new HashMap<>();
93         boolean rollbackOnFailure = true;
94
95         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
96         StubOpenStack.mockOpenStackPostStack_200(wireMockServer, "OpenstackResponse_Stack_Created.json");
97
98         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceName/stackId"))
99                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
100                         .withBodyFile("OpenstackResponse_StackId.json").withStatus(HttpStatus.SC_OK)));
101
102         VduInstance actual = heatUtils.instantiateVdu(cloudInfo, instanceName, inputs, vduModel, rollbackOnFailure);
103
104         assertThat(actual, sameBeanAs(expected));
105     }
106
107
108     @Test
109     public void queryVduTest() throws Exception {
110         VduInstance expected = new VduInstance();
111         expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
112         expected.setVduInstanceName("name");
113         VduStatus status = new VduStatus();
114         status.setState(VduStateType.INSTANTIATED);
115         status.setLastAction((new PluginAction("create", "complete", null)));
116         expected.setStatus(status);
117
118         CloudInfo cloudInfo = new CloudInfo();
119         cloudInfo.setCloudSiteId("mtn13");
120         cloudInfo.setTenantId("tenantId");
121         String instanceId = "instanceId";
122
123         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
124         StubOpenStack.mockOpenStackPostStack_200(wireMockServer, "OpenstackResponse_Stack_Created.json");
125
126         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId"))
127                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
128                         .withBodyFile("OpenstackResponse_StackId.json").withStatus(HttpStatus.SC_OK)));
129
130         VduInstance actual = heatUtils.queryVdu(cloudInfo, instanceId);
131
132         assertThat(actual, sameBeanAs(expected));
133     }
134
135     @Test
136     public void deleteVduTest() throws Exception {
137         VduInstance expected = new VduInstance();
138         expected.setVduInstanceId("instanceId");
139         expected.setVduInstanceName("instanceId");
140         VduStatus status = new VduStatus();
141         status.setState(VduStateType.DELETED);
142         expected.setStatus(status);
143
144         CloudInfo cloudInfo = new CloudInfo();
145         cloudInfo.setCloudSiteId("mtn13");
146         cloudInfo.setTenantId("tenantId");
147         String instanceId = "instanceId";
148
149         int timeoutInMinutes = 1;
150
151         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
152         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId"))
153                 .willReturn(aResponse().withBodyFile("OpenstackResponse_StackId.json").withStatus(HttpStatus.SC_OK)));
154         StubOpenStack.mockOpenStackDelete(wireMockServer, "name/da886914-efb2-4917-b335-c8381528d90b");
155         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/name/da886914-efb2-4917-b335-c8381528d90b"))
156                 .willReturn(aResponse().withBodyFile("OpenstackResponse_Stack_DeleteComplete.json")
157                         .withStatus(HttpStatus.SC_OK)));
158
159         VduInstance actual = heatUtils.deleteVdu(cloudInfo, instanceId, timeoutInMinutes);
160
161         assertThat(actual, sameBeanAs(expected));
162     }
163
164     @Test
165     public final void requestToStringBuilderTest() {
166         CreateStackParam param = new CreateStackParam();
167         param.setDisableRollback(false);
168         param.setEnvironment("environment");
169         param.setFiles(new HashMap<String, Object>());
170         param.setParameters(new HashMap<>());
171         param.setStackName("stackName");
172         param.setTemplate("template");
173         param.setTemplateUrl("http://templateUrl");
174         param.setTimeoutMinutes(1);
175
176         StringBuilder stringBuilder = heatUtils.requestToStringBuilder(param);
177
178         Assert.assertTrue(stringBuilder.toString().contains("StackName:"));
179     }
180
181     @Test
182     public final void copyBaseOutputsToInputsTest() {
183         Map<String, Object> inputs = new HashMap<>();
184         inputs.put("str1", "str");
185         Map<String, Object> otherStackOutputs = new HashMap<>();
186         otherStackOutputs.put("str", "str");
187         List<String> paramNames = new ArrayList<>();
188         Map<String, String> aliases = new HashMap<>();
189         aliases.put("str", "str");
190         heatUtils.copyBaseOutputsToInputs(inputs, otherStackOutputs, null, aliases);
191         Assert.assertEquals("str", otherStackOutputs.get("str"));
192     }
193
194     @Test
195     public final void getHeatClientSuccessTest() throws MsoException, IOException {
196         CloudSite cloudSite = getCloudSite(getCloudIdentity());
197         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
198         Heat heatClient = heatUtils.getHeatClient(cloudSite, "TEST-tenant");
199         assertNotNull(heatClient);
200     }
201
202     @Test(expected = MsoOpenstackException.class)
203     public final void getHeatClientOpenStackResponseException404Test() throws MsoException, IOException {
204         CloudSite cloudSite = getCloudSite(getCloudIdentity());
205         // mo mocks setup will cause 404 response from wiremock
206         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
207     }
208
209     @Test(expected = MsoAdapterException.class)
210     public final void getHeatClientOpenStackResponseException401Test() throws MsoException, IOException {
211         CloudSite cloudSite = getCloudSite(getCloudIdentity());
212         StubOpenStack.mockOpenStackResponseUnauthorized(wireMockServer, wireMockPort);
213         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
214     }
215
216     @Test(expected = MsoIOException.class)
217     public final void getHeatClientOpenStackConnectExceptionTest() throws MsoException, IOException {
218         CloudIdentity identity = getCloudIdentity();
219         identity.setIdentityUrl("http://unreachable");
220         CloudSite cloudSite = getCloudSite(identity);
221         // mo mocks setup will cause 404 response from wiremock
222         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
223     }
224
225     @Test
226     public final void createStackSuccessTest() throws MsoException, IOException {
227         CloudSite cloudSite = getCloudSite(getCloudIdentity());
228         StubOpenStack.mockOpenStackResponseAccess(wireMockServer, wireMockPort);
229         StubOpenStack.mockOpenStackPostStack_200(wireMockServer, "OpenstackResponse_Stack_Created.json");
230         StubOpenStack.mockOpenStackGet(wireMockServer, "TEST-stack/stackId");
231         StackInfo stackInfo = heatUtils.createStack(cloudSite.getId(), "CloudOwner", "tenantId", "TEST-stack", null,
232                 "TEST-heat", new HashMap<>(), false, 1, "TEST-env", new HashMap<>(), new HashMap<>(), false);
233         assertNotNull(stackInfo);
234     }
235 }