2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.adapter_utils.tests;
23 import java.io.IOException;
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.TextNode;
30 import com.woorea.openstack.base.client.HttpMethod;
31 import com.woorea.openstack.base.client.OpenStackRequest;
32 import com.woorea.openstack.heat.model.Stack;
33 import com.woorea.openstack.keystone.model.Access;
34 import com.woorea.openstack.keystone.utils.KeystoneUtils;
35 import mockit.Deencapsulation;
36 import mockit.Invocation;
39 import mockit.integration.junit4.JMockit;
40 import org.json.JSONException;
41 import org.json.JSONObject;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.openecomp.mso.cloud.CloudConfigFactory;
46 import org.openecomp.mso.cloud.CloudConfigTest;
47 import org.openecomp.mso.openstack.beans.StackInfo;
48 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
49 import org.openecomp.mso.openstack.exceptions.MsoCloudSiteNotFound;
50 import org.openecomp.mso.openstack.exceptions.MsoException;
51 import org.openecomp.mso.openstack.exceptions.MsoIOException;
52 import org.openecomp.mso.openstack.exceptions.MsoStackAlreadyExists;
53 import org.openecomp.mso.openstack.exceptions.MsoTenantNotFound;
54 import org.openecomp.mso.openstack.utils.MsoCommonUtils;
55 import org.openecomp.mso.openstack.utils.MsoHeatUtils;
56 import org.openecomp.mso.properties.MsoPropertiesException;
57 import org.openecomp.mso.properties.MsoPropertiesFactory;
59 import com.woorea.openstack.heat.model.CreateStackParam;
62 * This class implements test methods of the MsoHeatUtils
66 public class MsoHeatUtilsTest extends MsoCommonUtils {
67 public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
68 public static CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
69 public static MsoHeatUtils msoHeatUtils;
72 public static final void loadClasses() throws MsoCloudIdentityNotFound, MsoPropertiesException {
73 ClassLoader classLoader = MsoHeatUtilsTest.class.getClassLoader();
74 String cloudConfigJson = classLoader.getResource("cloud_config.json").getPath();
75 cloudConfigFactory.initializeCloudConfig(cloudConfigJson, 1);
76 msoPropertiesFactory.initializeMsoProperties("NO_PROP", classLoader.getResource("mso.properties").getPath());
77 msoHeatUtils = new MsoHeatUtils("NO_PROP", msoPropertiesFactory, cloudConfigFactory);
81 public final void testCreateStackBadCloudConfig()
82 throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
84 msoHeatUtils.createStack("DOESNOTEXIST", "test", "stackName", "test", new HashMap<>(),
86 } catch (MsoCloudSiteNotFound e) {
88 } catch (java.lang.NullPointerException npe) {
95 public final void testCreateStackFailedConnectionHeatClient()
96 throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
98 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE,
100 } catch (MsoIOException e) {
107 public final void testCreateStackFailedConnection()
108 throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
110 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE,
112 } catch (MsoIOException e) {
119 public final void createStackSuccessWithEnvironment() throws MsoException {
120 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
122 public Object execute(Invocation invocation) {
123 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
124 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
127 if (returnType == Access.class) {
128 ObjectMapper mapper = new ObjectMapper();
129 String json = "{\"token\":{\"id\":\"tokenId\",\"tenant\":{\"id\":\"tenantId\",\"name\":\"tenantName\"}},\"serviceCatalog\":[{\"type\":\"orchestration\",\"name\":\"orchestration\",\"endpoints\":[{\"region\":\"region1\",\"publicURL\":\"http://localhost:5000\",\"internalURL\":\"http://localhost:5000\",\"adminURL\":\"http://localhost:5000\"}]}]}";
130 return mapper.readValue(json, Access.class);
131 } else if (returnType == Stack.class) {
132 final Stack stack = new Stack();
133 stack.setId("stackId");
134 stack.setStackName("stackName");
135 stack.setStackStatus("CREATE_COMPLETE");
139 } catch (Exception e) {
140 throw new RuntimeException(e);
145 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
147 String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
148 return "http://localhost:5000";
152 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
155 mockRequest.tearDown();
156 mockKeystone.tearDown();
160 public final void createStackSuccessWithFiles() throws MsoException {
161 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
163 public Object execute(Invocation invocation) {
164 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
165 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
168 if (returnType == Access.class) {
169 ObjectMapper mapper = new ObjectMapper();
170 String json = "{\"token\":{\"id\":\"tokenId\",\"tenant\":{\"id\":\"tenantId\",\"name\":\"tenantName\"}},\"serviceCatalog\":[{\"type\":\"orchestration\",\"name\":\"orchestration\",\"endpoints\":[{\"region\":\"region1\",\"publicURL\":\"http://localhost:5000\",\"internalURL\":\"http://localhost:5000\",\"adminURL\":\"http://localhost:5000\"}]}]}";
171 return mapper.readValue(json, Access.class);
172 } else if (returnType == Stack.class) {
173 final Stack stack = new Stack();
174 stack.setId("stackId");
175 stack.setStackName("stackName");
176 stack.setStackStatus("CREATE_COMPLETE");
180 } catch (Exception e) {
181 throw new RuntimeException(e);
186 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
188 String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
189 return "http://localhost:5000";
193 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
194 "environment", new HashMap<>());
196 mockRequest.tearDown();
197 mockKeystone.tearDown();
201 public final void createStackSuccessWithHeatFiles() throws MsoException {
203 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
205 public Object execute(Invocation invocation) {
206 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
207 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
210 if (returnType == Access.class) {
211 ObjectMapper mapper = new ObjectMapper();
212 String json = "{\"token\":{\"id\":\"tokenId\",\"tenant\":{\"id\":\"tenantId\",\"name\":\"tenantName\"}},\"serviceCatalog\":[{\"type\":\"orchestration\",\"name\":\"orchestration\",\"endpoints\":[{\"region\":\"region1\",\"publicURL\":\"http://localhost:5000\",\"internalURL\":\"http://localhost:5000\",\"adminURL\":\"http://localhost:5000\"}]}]}";
213 return mapper.readValue(json, Access.class);
214 } else if (returnType == Stack.class) {
215 final Stack stack = new Stack();
216 stack.setId("stackId");
217 stack.setStackName("stackName");
218 stack.setStackStatus("CREATE_COMPLETE");
222 } catch (Exception e) {
223 throw new RuntimeException(e);
228 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
230 String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
231 return "http://localhost:5000";
235 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
236 "environment", new HashMap<>(), new HashMap<>());
238 mockRequest.tearDown();
239 mockKeystone.tearDown();
243 public final void requestToStringBuilderTest() {
244 CreateStackParam param = new CreateStackParam();
245 param.setDisableRollback(false);
246 param.setEnvironment("environment");
247 param.setFiles(new HashMap<>());
248 param.setParameters(new HashMap<>());
249 param.setStackName("stackName");
250 param.setTemplate("template");
251 param.setTemplateUrl("http://templateUrl");
252 param.setTimeoutMinutes(1);
254 msoHeatUtils.requestToStringBuilder(param);
258 public final void heatCacheResetTest() {
259 msoHeatUtils.heatCacheReset();
263 public final void expireHeatClientTest() {
264 msoHeatUtils.expireHeatClient("tenantId", "cloudId");
268 public final void heatCacheCleanupTest() {
269 msoHeatUtils.heatCacheCleanup();
273 public void queryStackTest() throws MsoException {
274 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
276 public Object execute(Invocation invocation) {
277 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
278 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
281 if (returnType == Access.class) {
282 ObjectMapper mapper = new ObjectMapper();
283 String json = "{\"token\":{\"id\":\"tokenId\",\"tenant\":{\"id\":\"tenantId\",\"name\":\"tenantName\"}},\"serviceCatalog\":[{\"type\":\"orchestration\",\"name\":\"orchestration\",\"endpoints\":[{\"region\":\"region1\",\"publicURL\":\"http://localhost:5000\",\"internalURL\":\"http://localhost:5000\",\"adminURL\":\"http://localhost:5000\"}]}]}";
284 return mapper.readValue(json, Access.class);
285 } else if (returnType == Stack.class) {
286 final Stack stack = new Stack();
287 stack.setId("stackId");
288 stack.setStackName("stackName");
289 stack.setStackStatus("CREATE_COMPLETE");
293 } catch (Exception e) {
294 throw new RuntimeException(e);
299 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
301 String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
302 return "http://localhost:5000";
306 final StackInfo stackInfo = msoHeatUtils.queryStack("MT", "test", "stackName");
308 mockRequest.tearDown();
309 mockKeystone.tearDown();
313 public void deleteStack() throws MsoException {
314 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
316 public Object execute(Invocation invocation) {
317 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
318 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
319 final String path = Deencapsulation.getField(invokedInstance, "endpoint");
320 // final String stackName = path.substring(path.lastIndexOf("/"));
323 if (returnType == Access.class) {
324 ObjectMapper mapper = new ObjectMapper();
325 String json = "{\"token\":{\"id\":\"tokenId\",\"tenant\":{\"id\":\"tenantId\",\"name\":\"tenantName\"}},\"serviceCatalog\":[{\"type\":\"orchestration\",\"name\":\"orchestration\",\"endpoints\":[{\"region\":\"region1\",\"publicURL\":\"http://localhost:5000\",\"internalURL\":\"http://localhost:5000\",\"adminURL\":\"http://localhost:5000\"}]}]}";
326 return mapper.readValue(json, Access.class);
327 } else if (returnType == Stack.class) {
328 final Stack stack = new Stack();
329 stack.setId("stackId");
330 stack.setStackName("stackName");
331 final String status = "DELETE_COMPLETE";
332 stack.setStackStatus(status);
336 } catch (Exception e) {
337 throw new RuntimeException(e);
342 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
344 String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
345 return "http://localhost:5000";
349 final StackInfo stackInfo = msoHeatUtils.deleteStack("test", "MT", "stackName", true);
351 mockRequest.tearDown();
352 mockKeystone.tearDown();
356 public void copyStringOutputsToInputsTest() {
357 Map<String, String> inputs = new HashMap<String, String>(){{put("key41", "value41");}};
358 Map<String, Object> outputs = new HashMap<String, Object>(){{
360 put("key3", new TextNode("val3"));
361 put("key4", new LinkedHashMap<String, String>(){{put("key41", "value41");}});
363 msoHeatUtils.copyStringOutputsToInputs(inputs, outputs, true);