Merge "Updated to use vnfOperationalEnvironmentId"
[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.stubFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
30 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
31 import static org.junit.Assert.assertNotNull;
32
33 import com.woorea.openstack.heat.Heat;
34 import com.woorea.openstack.heat.model.CreateStackParam;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import org.apache.http.HttpStatus;
41 import org.junit.Assert;
42 import org.junit.Test;
43 import org.onap.so.BaseTest;
44 import org.onap.so.StubOpenStack;
45 import org.onap.so.adapters.vdu.CloudInfo;
46 import org.onap.so.adapters.vdu.PluginAction;
47 import org.onap.so.adapters.vdu.VduArtifact;
48 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
49 import org.onap.so.adapters.vdu.VduInstance;
50 import org.onap.so.adapters.vdu.VduModelInfo;
51 import org.onap.so.adapters.vdu.VduStateType;
52 import org.onap.so.adapters.vdu.VduStatus;
53 import org.onap.so.db.catalog.beans.CloudIdentity;
54 import org.onap.so.db.catalog.beans.CloudSite;
55 import org.onap.so.openstack.beans.StackInfo;
56 import org.onap.so.openstack.exceptions.MsoAdapterException;
57 import org.onap.so.openstack.exceptions.MsoException;
58 import org.onap.so.openstack.exceptions.MsoIOException;
59 import org.onap.so.openstack.exceptions.MsoOpenstackException;
60 import org.springframework.beans.factory.annotation.Autowired;
61
62 public class MsoHeatUtilsTest extends BaseTest{
63
64         @Autowired
65         private MsoHeatUtils heatUtils;
66         
67         @Test
68         public void instantiateVduTest() throws MsoException, IOException {
69                 VduInstance expected = new VduInstance();
70                 expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
71                 expected.setVduInstanceName("name");
72                 VduStatus status = new VduStatus();
73                 status.setState(VduStateType.INSTANTIATED);
74                 status.setLastAction((new PluginAction("create", "complete", null)));
75                 expected.setStatus(status);
76
77                 CloudInfo cloudInfo = new CloudInfo();
78                 cloudInfo.setCloudSiteId("MTN13");
79                 cloudInfo.setTenantId("tenantId");
80                 VduModelInfo vduModel = new VduModelInfo();
81                 vduModel.setModelCustomizationUUID("blueprintId");
82                 vduModel.setTimeoutMinutes(1);
83                 VduArtifact artifact = new VduArtifact();
84                 artifact.setName("name");
85                 artifact.setType(ArtifactType.MAIN_TEMPLATE);
86                 byte[] content = new byte[1];
87                 artifact.setContent(content);
88                 List<VduArtifact> artifacts = new ArrayList<>();
89                 artifacts.add(artifact);
90                 vduModel.setArtifacts(artifacts);
91                 Map<String, byte[]> blueprintFiles = new HashMap<>();
92                 blueprintFiles.put(artifact.getName(), artifact.getContent());
93                 String instanceName = "instanceName";
94                 Map<String, Object> inputs = new HashMap<>();
95                 boolean rollbackOnFailure = true;
96
97                 StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
98                 StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json");
99                 
100                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceName/stackId"))
101                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
102                                                 .withBodyFile("OpenstackResponse_StackId.json")
103                                                 .withStatus(HttpStatus.SC_OK)));
104                 
105                 VduInstance actual = heatUtils.instantiateVdu(cloudInfo, instanceName, inputs, vduModel, rollbackOnFailure);
106                 
107                 assertThat(actual, sameBeanAs(expected));
108         }
109
110         
111         @Test
112         public void queryVduTest() throws Exception {
113                 VduInstance expected = new VduInstance();
114                 expected.setVduInstanceId("name/da886914-efb2-4917-b335-c8381528d90b");
115                 expected.setVduInstanceName("name");
116                 VduStatus status = new VduStatus();
117                 status.setState(VduStateType.INSTANTIATED);
118                 status.setLastAction((new PluginAction("create", "complete",null)));
119                 expected.setStatus(status);
120
121                 CloudInfo cloudInfo = new CloudInfo();
122                 cloudInfo.setCloudSiteId("mtn13");
123                 cloudInfo.setTenantId("tenantId");
124                 String instanceId = "instanceId";
125
126                 StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
127                 StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json");
128
129                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId"))
130                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
131                                                 .withBodyFile("OpenstackResponse_StackId.json")
132                                                 .withStatus(HttpStatus.SC_OK)));
133
134                 VduInstance actual = heatUtils.queryVdu(cloudInfo, instanceId);
135                 
136                 assertThat(actual, sameBeanAs(expected));
137         }
138
139         @Test
140         public void deleteVduTest() throws Exception {
141                 VduInstance expected = new VduInstance();
142                 expected.setVduInstanceId("instanceId");
143                 expected.setVduInstanceName("instanceId");
144                 VduStatus status = new VduStatus();
145                 status.setState(VduStateType.DELETED);
146                 expected.setStatus(status);
147
148                 CloudInfo cloudInfo = new CloudInfo();
149                 cloudInfo.setCloudSiteId("mtn13");
150                 cloudInfo.setTenantId("tenantId");
151                 String instanceId = "instanceId";
152
153                 int timeoutInMinutes = 1;
154
155                 StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
156                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/instanceId")).willReturn(aResponse().withBodyFile("OpenstackResponse_StackId.json").withStatus(HttpStatus.SC_OK)));
157                 StubOpenStack.mockOpenStackDelete("name/da886914-efb2-4917-b335-c8381528d90b");
158                 stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/name/da886914-efb2-4917-b335-c8381528d90b")).willReturn(aResponse().withBodyFile("OpenstackResponse_Stack_DeleteComplete.json").withStatus(HttpStatus.SC_OK)));
159                 
160                 VduInstance actual = heatUtils.deleteVdu(cloudInfo, instanceId, timeoutInMinutes);
161                 
162                 assertThat(actual, sameBeanAs(expected));
163         }
164
165         @Test
166         public final void requestToStringBuilderTest() {
167                 CreateStackParam param = new CreateStackParam();
168                 param.setDisableRollback(false);
169                 param.setEnvironment("environment");
170                 param.setFiles(new HashMap<String, Object>());
171                 param.setParameters(new HashMap<>());
172                 param.setStackName("stackName");
173                 param.setTemplate("template");
174                 param.setTemplateUrl("http://templateUrl");
175                 param.setTimeoutMinutes(1);
176
177                 StringBuilder stringBuilder =  heatUtils.requestToStringBuilder(param);
178
179                 Assert.assertTrue(stringBuilder.toString().contains("StackName:"));
180         }
181
182         @Test
183         public final void copyBaseOutputsToInputsTest() {
184                 Map<String, Object> inputs = new HashMap<>();
185                 inputs.put("str1", "str");
186                 Map<String, Object> otherStackOutputs = new HashMap<>();
187                 otherStackOutputs.put("str", "str");
188                 List<String> paramNames = new ArrayList<>();
189                 Map<String, String> aliases = new HashMap<>();
190                 aliases.put("str", "str");
191                 heatUtils.copyBaseOutputsToInputs(inputs, otherStackOutputs, null, aliases);
192                 Assert.assertEquals("str",otherStackOutputs.get("str"));
193         }
194
195     @Test
196     public final void getHeatClientSuccessTest() throws MsoException, IOException {
197         CloudSite cloudSite = getCloudSite(getCloudIdentity());
198         StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
199         Heat heatClient = heatUtils.getHeatClient(cloudSite, "TEST-tenant");
200         assertNotNull(heatClient);
201     }
202
203     @Test(expected = MsoOpenstackException.class)
204     public final void getHeatClientOpenStackResponseException404Test() throws MsoException, IOException {
205         CloudSite cloudSite = getCloudSite(getCloudIdentity());
206         // mo mocks setup will cause 404 response from wiremock
207         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
208     }
209
210     @Test(expected = MsoAdapterException.class)
211     public final void getHeatClientOpenStackResponseException401Test() throws MsoException, IOException {
212         CloudSite cloudSite = getCloudSite(getCloudIdentity());
213         StubOpenStack.mockOpenStackResponseUnauthorized(wireMockPort);
214         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
215     }
216
217     @Test(expected = MsoIOException.class)
218     public final void getHeatClientOpenStackConnectExceptionTest() throws MsoException, IOException {
219         CloudIdentity identity = getCloudIdentity();
220         identity.setIdentityUrl("http://unreachable");
221         CloudSite cloudSite = getCloudSite(identity);
222         // mo mocks setup will cause 404 response from wiremock
223         heatUtils.getHeatClient(cloudSite, "TEST-tenant");
224     }
225
226     @Test
227     public final void createStackSuccessTest() throws MsoException, IOException {
228         CloudSite cloudSite = getCloudSite(getCloudIdentity());
229         StubOpenStack.mockOpenStackResponseAccess(wireMockPort);
230         StubOpenStack.mockOpenStackPostStack_200("OpenstackResponse_Stack_Created.json");
231         StubOpenStack.mockOpenStackGet("TEST-stack/stackId");
232         StackInfo stackInfo = heatUtils.createStack(cloudSite.getId(), "CloudOwner", "tenantId", "TEST-stack",
233             "TEST-heat", new HashMap<>(), false, 1, "TEST-env",
234             new HashMap<>(), new HashMap<>(), false);
235         assertNotNull(stackInfo);
236     }
237 }