2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Samsung 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.onap.so.openstack.utils;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33 import static org.mockito.Mockito.when;
34 import static org.onap.so.openstack.utils.MsoMulticloudUtils.MULTICLOUD_QUERY_BODY_NULL;
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Optional;
38 import org.apache.http.HttpStatus;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.onap.so.BaseTest;
44 import org.onap.so.adapters.vdu.CloudInfo;
45 import org.onap.so.adapters.vdu.VduException;
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.cloud.CloudConfig;
50 import org.onap.so.db.catalog.beans.CloudIdentity;
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 import org.springframework.beans.factory.annotation.Autowired;
57 public class MsoMulticloudUtilsTest extends BaseTest {
60 private MsoMulticloudUtils multicloudUtils;
63 private MsoMulticloudUtils multicloudUtilsMock;
66 private CloudConfig cloudConfigMock;
68 private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
69 + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
70 private static final String UPDATE_STACK_RESPONSE =
71 "{\"template_type\": \"heat\", \"workload_id\": " + "\"TEST-workload\"}";
73 private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
74 private static final String MULTICLOUD_UPDATE_PATH =
75 "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
76 private static final String MULTICLOUD_GET_PATH =
77 "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
78 private static final String MULTICLOUD_DELETE_PATH =
79 "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
82 public void createStackSuccess() throws MsoException, IOException {
84 .stubFor(post(urlEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
85 .willReturn(aResponse().withHeader("Content-Type", "application/json")
86 .withBody(CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_CREATED))
87 .willSetStateTo("CREATING"));
88 wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE")
89 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
90 .withBodyFile("MulticloudGetCreateResponse.json").withStatus(HttpStatus.SC_OK)));
91 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
92 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
93 .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
94 .willSetStateTo("UPDATING"));
95 wireMockServer.stubFor(get(urlEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE").whenScenarioStateIs("UPDATING")
96 .willReturn(aResponse().withHeader("Content-Type", "application/json")
97 .withBodyFile("MulticloudGetUpdateResponse.json").withStatus(HttpStatus.SC_OK)));
98 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack",
99 new VduModelInfo(), "TEST-heat", new HashMap<>(), true, 200, "TEST-env", new HashMap<>(),
100 new HashMap<>(), false, false);
101 wireMockServer.resetScenarios();
102 assertNotNull(result);
103 assertEquals("TEST-stack", result.getName());
107 public void deleteStack() throws MsoException {
108 wireMockServer.stubFor(delete(urlEqualTo(MULTICLOUD_DELETE_PATH)).willReturn(
109 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NO_CONTENT)));
110 wireMockServer.stubFor(get(urlEqualTo(MULTICLOUD_GET_PATH))
111 .willReturn(aResponse().withHeader("Content-Type", "application/json")
112 .withBodyFile("MulticloudGetDeleteResponse.json").withStatus(HttpStatus.SC_OK)));
114 multicloudUtils.deleteStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack/TEST-workload");
115 assertNotNull(result);
116 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
120 public void queryStack() throws MsoException {
121 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
122 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
126 public void queryStackWithNullMulticloudQueryBody() throws MsoException {
127 wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/instanceId"))
128 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(CREATE_STACK_RESPONSE)
129 .withStatus(HttpStatus.SC_OK)));
131 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceName/instanceId");
132 assertTrue(HeatStatus.FAILED == result.getStatus());
133 assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
136 @Test(expected = VduException.class)
137 public void updateVdu() throws MsoException {
138 multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(), false);
142 public void deleteVdu() throws VduException {
143 CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
144 VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
145 assertNotNull(vduInstance);
146 assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
151 public void createStackMulticloudClientIsNull() {
153 multicloudUtilsMock.cloudConfig = cloudConfigMock;
154 CloudSite cloudSite = new CloudSite();
155 cloudSite.setIdentityService(new CloudIdentity());
156 when(cloudConfigMock.getCloudSite("MTN13")).thenReturn(Optional.of(cloudSite));
157 multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
158 "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false,
160 } catch (MsoException e) {
161 assertEquals("0 : Multicloud client could not be initialized", e.toString());
164 fail("MsoOpenstackException expected!");
168 public void createStackBadRequest() {
170 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(
171 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_BAD_REQUEST)));
172 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
173 "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false,
175 } catch (MsoException e) {
176 assertEquals("0 : Bad Request", e.toString());
179 fail("MsoOpenstackException expected!");
183 public void createStackEmptyResponseEntity() throws MsoException {
184 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(
185 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_CREATED)));
186 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack",
187 new VduModelInfo(), "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(),
188 new HashMap<>(), false, false);
189 assertNotNull(result);
190 assertEquals("TEST-stack/", result.getName());