b304cba93f7eedc3f59d6f6bb621b0e9913dea6c
[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  * 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.openstack.utils;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
28 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import com.woorea.openstack.heat.model.CreateStackParam;
37 import org.apache.http.HttpStatus;
38 import org.junit.Assert;
39 import org.junit.Test;
40 import org.onap.so.BaseTest;
41 import org.onap.so.StubOpenStack;
42 import org.onap.so.adapters.vdu.CloudInfo;
43 import org.onap.so.adapters.vdu.PluginAction;
44 import org.onap.so.adapters.vdu.VduArtifact;
45 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
46 import org.onap.so.adapters.vdu.VduInstance;
47 import org.onap.so.adapters.vdu.VduModelInfo;
48 import org.onap.so.adapters.vdu.VduStateType;
49 import org.onap.so.adapters.vdu.VduStatus;
50 import org.onap.so.cloud.CloudConfig;
51 import org.onap.so.db.catalog.beans.CloudSite;
52 import org.onap.so.openstack.beans.HeatStatus;
53 import org.onap.so.openstack.beans.StackInfo;
54 import org.onap.so.openstack.exceptions.MsoException;
55
56 import org.springframework.beans.factory.annotation.Autowired;
57
58 public class MsoHeatUtilsTest extends BaseTest{
59
60         @Autowired
61         private MsoHeatUtils heatUtils;
62         
63         @Test
64         public void instantiateVduTest() throws MsoException, IOException {
65                 VduInstance expected = new VduInstance();
66                 expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
67                 expected.setVduInstanceName("name");
68                 VduStatus status = new VduStatus();
69                 status.setState(VduStateType.INSTANTIATED);
70                 status.setLastAction((new PluginAction("create", "complete", null)));
71                 expected.setStatus(status);
72
73                 CloudInfo cloudInfo = new CloudInfo();
74                 cloudInfo.setCloudSiteId("MTN13");
75                 cloudInfo.setTenantId("tenantId");
76                 VduModelInfo vduModel = new VduModelInfo();
77                 vduModel.setModelCustomizationUUID("blueprintId");
78                 vduModel.setTimeoutMinutes(1);
79                 VduArtifact artifact = new VduArtifact();
80                 artifact.setName("name");
81                 artifact.setType(ArtifactType.MAIN_TEMPLATE);
82                 byte[] content = new byte[1];
83                 artifact.setContent(content);
84                 List<VduArtifact> artifacts = new ArrayList<>();
85                 artifacts.add(artifact);
86                 vduModel.setArtifacts(artifacts);
87                 Map<String, byte[]> blueprintFiles = new HashMap<>();
88                 blueprintFiles.put(artifact.getName(), artifact.getContent());
89                 String instanceName = "instanceName";
90                 Map<String, Object> inputs = new HashMap<>();
91                 boolean rollbackOnFailure = true;
92
93                 StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
94                 StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json");
95                 
96                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceName/stackId"))
97                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
98                                                 .withBodyFile("OpenstackResponse_StackId.json")
99                                                 .withStatus(HttpStatus.SC_OK)));
100                 
101                 VduInstance actual = heatUtils.instantiateVdu(cloudInfo, instanceName, inputs, vduModel, rollbackOnFailure);
102                 
103                 assertThat(actual, sameBeanAs(expected));
104         }
105
106         
107         @Test
108         public void queryVduTest() throws Exception {
109                 VduInstance expected = new VduInstance();
110                 expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
111                 expected.setVduInstanceName("name");
112                 VduStatus status = new VduStatus();
113                 status.setState(VduStateType.INSTANTIATED);
114                 status.setLastAction((new PluginAction("create", "complete",null)));
115                 expected.setStatus(status);
116
117                 CloudInfo cloudInfo = new CloudInfo();
118                 cloudInfo.setCloudSiteId("mtn13");
119                 cloudInfo.setTenantId("tenantId");
120                 String instanceId = "instanceId";
121
122                 StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
123                 StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json");
124
125                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId"))
126                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
127                                                 .withBodyFile("OpenstackResponse_StackId.json")
128                                                 .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(wireMockPort);
152                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId")).willReturn(aResponse().withBodyFile("OpenstackResponse_StackId.json").withStatus(HttpStatus.SC_OK)));
153                 StubOpenStack.mockOpenStackDelete("name/da886914-efb2-4917-b335-c8381528d90b");
154                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/name/da886914-efb2-4917-b335-c8381528d90b")).willReturn(aResponse().withBodyFile("OpenstackResponse_Stack_DeleteComplete.json").withStatus(HttpStatus.SC_OK)));
155                 
156                 VduInstance actual = heatUtils.deleteVdu(cloudInfo, instanceId, timeoutInMinutes);
157                 
158                 assertThat(actual, sameBeanAs(expected));
159         }
160
161         @Test
162         public final void requestToStringBuilderTest() {
163                 CreateStackParam param = new CreateStackParam();
164                 param.setDisableRollback(false);
165                 param.setEnvironment("environment");
166                 param.setFiles(new HashMap<String, Object>());
167                 param.setParameters(new HashMap<>());
168                 param.setStackName("stackName");
169                 param.setTemplate("template");
170                 param.setTemplateUrl("http://templateUrl");
171                 param.setTimeoutMinutes(1);
172
173                 StringBuilder stringBuilder =  heatUtils.requestToStringBuilder(param);
174
175                 Assert.assertTrue(stringBuilder.toString().contains("StackName:"));
176         }
177
178         @Test
179         public final void copyBaseOutputsToInputsTest() {
180                 Map<String, Object> inputs = new HashMap<>();
181                 inputs.put("str1", "str");
182                 Map<String, Object> otherStackOutputs = new HashMap<>();
183                 otherStackOutputs.put("str", "str");
184                 List<String> paramNames = new ArrayList<>();
185                 Map<String, String> aliases = new HashMap<>();
186                 aliases.put("str", "str");
187                 heatUtils.copyBaseOutputsToInputs(inputs, otherStackOutputs, null, aliases);
188                 Assert.assertEquals("str",otherStackOutputs.get("str"));
189         }
190         
191
192 }