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