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.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31 import static org.mockito.Mockito.when;
32 import static org.onap.so.openstack.utils.MsoMulticloudUtils.MULTICLOUD_QUERY_BODY_NULL;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Optional;
38 import org.apache.http.HttpStatus;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.onap.so.BaseTest;
45 import org.onap.so.adapters.vdu.CloudInfo;
46 import org.onap.so.adapters.vdu.VduException;
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.cloud.CloudConfig;
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.HeatStatus;
54 import org.onap.so.openstack.beans.StackInfo;
55 import org.onap.so.openstack.exceptions.MsoException;
56 import org.springframework.beans.factory.annotation.Autowired;
58 public class MsoMulticloudUtilsTest extends BaseTest {
61 private MsoMulticloudUtils multicloudUtils;
64 private MsoMulticloudUtils multicloudUtilsMock;
67 private CloudConfig cloudConfigMock;
69 private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
70 + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
71 private static final String UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
72 + "\"TEST-workload\"}";
73 private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
74 + "\"TEST-workload\", \"workload_status\": \"CREATE_COMPLETE\"}";
75 private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
76 + "\"TEST-workload\", \"workload_status\": \"UPDATE_COMPLETE\"}";
78 private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
79 private static final String MULTICLOUD_UPDATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
80 private static final String MULTICLOUD_GET_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
83 public void createStackSuccess() throws MsoException, IOException {
84 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
85 .willReturn(aResponse().withHeader("Content-Type", "application/json")
86 .withBody(CREATE_STACK_RESPONSE)
87 .withStatus(HttpStatus.SC_CREATED))
88 .willSetStateTo("CREATING"));
89 wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
90 .inScenario("CREATE").whenScenarioStateIs("CREATING")
91 .willReturn(aResponse().withHeader("Content-Type", "application/json")
92 .withBody(GET_CREATE_STACK_RESPONSE)
93 .withStatus(HttpStatus.SC_OK)));
94 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH)).inScenario("CREATE")
95 .willReturn(aResponse().withHeader("Content-Type", "application/json")
96 .withBody(UPDATE_STACK_RESPONSE)
97 .withStatus(HttpStatus.SC_ACCEPTED))
98 .willSetStateTo("UPDATING"));
99 wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
100 .inScenario("CREATE").whenScenarioStateIs("UPDATING")
101 .willReturn(aResponse().withHeader("Content-Type", "application/json")
102 .withBody(GET_UPDATE_STACK_RESPONSE)
103 .withStatus(HttpStatus.SC_OK)));
104 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
105 "TEST-heat", new HashMap<>(), true, 200, "TEST-env",
106 new HashMap<>(), new HashMap<>(), false);
107 wireMockServer.resetScenarios();
108 assertNotNull(result);
109 assertEquals("TEST-stack", result.getName());
113 public void deleteStack() throws MsoException {
114 StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
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")
129 .withBody(CREATE_STACK_RESPONSE)
130 .withStatus(HttpStatus.SC_OK)));
132 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
133 assertTrue(HeatStatus.FAILED == result.getStatus());
134 assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
137 @Test(expected = VduException.class)
138 public void updateVdu() throws MsoException {
139 multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(),
144 public void deleteVdu() throws VduException {
145 CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
146 VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
147 assertNotNull(vduInstance);
148 assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
152 public void createStackMulticloudClientIsNull() {
154 multicloudUtilsMock.cloudConfig = cloudConfigMock;
155 CloudSite cloudSite = new CloudSite();
156 cloudSite.setIdentityService(new CloudIdentity());
157 when(cloudConfigMock.getCloudSite("MTN13")).
158 thenReturn(Optional.of(cloudSite));
159 multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
160 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
161 new HashMap<>(), new HashMap<>(), false);
162 } catch (MsoException e) {
163 assertEquals("0 : Multicloud client could not be initialized", e.toString());
166 fail("MsoOpenstackException expected!");
170 public void createStackBadRequest() {
172 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
173 .willReturn(aResponse().withHeader("Content-Type", "application/json")
174 .withStatus(HttpStatus.SC_BAD_REQUEST)));
175 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
176 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
177 new HashMap<>(), new HashMap<>(), false);
178 } catch (MsoException e) {
179 assertEquals("0 : Bad Request", e.toString());
182 fail("MsoOpenstackException expected!");
186 public void createStackEmptyResponseEntity() throws MsoException {
187 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
188 .willReturn(aResponse().withHeader("Content-Type", "application/json")
189 .withStatus(HttpStatus.SC_CREATED)));
190 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
191 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
192 new HashMap<>(), new HashMap<>(), false);
193 assertNotNull(result);
194 assertEquals("TEST-stack/", result.getName());