Merge "Reorder modifiers"
[so.git] / adapters / mso-adapter-utils / src / test / java / org / openecomp / mso / adapter_utils / tests / MsoHeatUtilsTest.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
21 package org.openecomp.mso.adapter_utils.tests;
22
23 import java.io.IOException;
24 import java.util.*;
25
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;
37 import mockit.Mock;
38 import mockit.MockUp;
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;
58
59 import com.woorea.openstack.heat.model.CreateStackParam;
60
61 /**
62  * This class implements test methods of the MsoHeatUtils
63  *
64  *
65  */
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;
70
71         @BeforeClass
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);
78         }
79
80         @Test
81         public final void testCreateStackBadCloudConfig()
82                         throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
83                 try {
84                         msoHeatUtils.createStack("DOESNOTEXIST", "test", "stackName", "test", new HashMap<>(),
85                                         Boolean.TRUE, 10);
86                 } catch (MsoCloudSiteNotFound e) {
87
88                 } catch (java.lang.NullPointerException npe) {
89
90                 }
91
92         }
93
94         @Test
95         public final void testCreateStackFailedConnectionHeatClient()
96                         throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
97                 try {
98                         msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE,
99                                         10);
100                 } catch (MsoIOException e) {
101
102                 }
103
104         }
105
106         @Test
107         public final void testCreateStackFailedConnection()
108                         throws MsoStackAlreadyExists, MsoTenantNotFound, MsoException, MsoCloudSiteNotFound {
109                 try {
110                         msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE,
111                                         10);
112                 } catch (MsoIOException e) {
113
114                 }
115
116         }
117
118         @Test
119         public final void createStackSuccessWithEnvironment() throws MsoException {
120                 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
121                         @Mock
122                         public Object execute(Invocation invocation) {
123                                 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
124                                 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
125
126                                 try {
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");
136                                                 return stack;
137                                         }
138                                         return null;
139                                 } catch (Exception e) {
140                                         throw new RuntimeException(e);
141                                 }
142                         }
143                 };
144
145                 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
146                         @Mock
147                         String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
148                                 return "http://localhost:5000";
149                         }
150                 };
151
152                 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
153                                 "environment");
154
155                 mockRequest.tearDown();
156                 mockKeystone.tearDown();
157         }
158
159         @Test
160         public final void createStackSuccessWithFiles() throws MsoException {
161                 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
162                         @Mock
163                         public Object execute(Invocation invocation) {
164                                 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
165                                 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
166
167                                 try {
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");
177                                                 return stack;
178                                         }
179                                         return null;
180                                 } catch (Exception e) {
181                                         throw new RuntimeException(e);
182                                 }
183                         }
184                 };
185
186                 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
187                         @Mock
188                         String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
189                                 return "http://localhost:5000";
190                         }
191                 };
192
193                 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
194                                 "environment", new HashMap<>());
195
196                 mockRequest.tearDown();
197                 mockKeystone.tearDown();
198         }
199
200         @Test
201         public final void createStackSuccessWithHeatFiles() throws MsoException {
202
203                 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
204                         @Mock
205                         public Object execute(Invocation invocation) {
206                                 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
207                                 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
208
209                                 try {
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");
219                                                 return stack;
220                                         }
221                                         return null;
222                                 } catch (Exception e) {
223                                         throw new RuntimeException(e);
224                                 }
225                         }
226                 };
227
228                 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
229                         @Mock
230                         String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
231                                 return "http://localhost:5000";
232                         }
233                 };
234
235                 msoHeatUtils.createStack("MT", "test", "stackName", "test", new HashMap<>(), Boolean.TRUE, 10,
236                                 "environment", new HashMap<>(), new HashMap<>());
237
238                 mockRequest.tearDown();
239                 mockKeystone.tearDown();
240         }
241
242         @Test
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);
253
254                 msoHeatUtils.requestToStringBuilder(param);
255         }
256
257         @Test
258         public final void heatCacheResetTest() {
259                 msoHeatUtils.heatCacheReset();
260         }
261
262         @Test
263         public final void expireHeatClientTest() {
264                 msoHeatUtils.expireHeatClient("tenantId", "cloudId");
265         }
266
267         @Test
268         public final void heatCacheCleanupTest() {
269                 msoHeatUtils.heatCacheCleanup();
270         }
271
272         @Test
273         public void queryStackTest() throws MsoException {
274                 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
275                         @Mock
276                         public Object execute(Invocation invocation) {
277                                 final OpenStackRequest invokedInstance = invocation.getInvokedInstance();
278                                 final Class<?> returnType = Deencapsulation.getField(invokedInstance, "returnType");
279
280                                 try {
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");
290                                                 return stack;
291                                         }
292                                         return null;
293                                 } catch (Exception e) {
294                                         throw new RuntimeException(e);
295                                 }
296                         }
297                 };
298
299                 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
300                         @Mock
301                         String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
302                                 return "http://localhost:5000";
303                         }
304                 };
305
306                 final StackInfo stackInfo = msoHeatUtils.queryStack("MT", "test", "stackName");
307
308                 mockRequest.tearDown();
309                 mockKeystone.tearDown();
310         }
311
312         @Test
313         public void deleteStack() throws MsoException {
314                 final MockUp<OpenStackRequest<Access>> mockRequest = new MockUp<OpenStackRequest<Access>>() {
315                         @Mock
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("/"));
321
322                                 try {
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);
333                                                 return stack;
334                                         }
335                                         return null;
336                                 } catch (Exception e) {
337                                         throw new RuntimeException(e);
338                                 }
339                         }
340                 };
341
342                 final MockUp<KeystoneUtils> mockKeystone = new MockUp<KeystoneUtils>() {
343                         @Mock
344                         String findEndpointURL(List<Access.Service> serviceCatalog, String type, String region, String facing) {
345                                 return "http://localhost:5000";
346                         }
347                 };
348
349                 final StackInfo stackInfo = msoHeatUtils.deleteStack("test", "MT", "stackName", true);
350
351                 mockRequest.tearDown();
352                 mockKeystone.tearDown();
353         }
354
355         @Test
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>(){{
359                         put("key2", "val2");
360                         put("key3", new TextNode("val3"));
361                         put("key4", new LinkedHashMap<String, String>(){{put("key41", "value41");}});
362                 }};
363                 msoHeatUtils.copyStringOutputsToInputs(inputs, outputs, true);
364         }
365 }