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;
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.Optional;
36 import org.apache.http.HttpStatus;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.onap.so.BaseTest;
43 import org.onap.so.adapters.vdu.CloudInfo;
44 import org.onap.so.adapters.vdu.VduException;
45 import org.onap.so.adapters.vdu.VduInstance;
46 import org.onap.so.adapters.vdu.VduModelInfo;
47 import org.onap.so.adapters.vdu.VduStateType;
48 import org.onap.so.cloud.CloudConfig;
49 import org.onap.so.db.catalog.beans.CloudIdentity;
50 import org.onap.so.db.catalog.beans.CloudSite;
51 import org.onap.so.openstack.beans.HeatStatus;
52 import org.onap.so.openstack.beans.StackInfo;
53 import org.onap.so.openstack.exceptions.MsoException;
54 import org.springframework.beans.factory.annotation.Autowired;
56 public class MsoMulticloudUtilsTest extends BaseTest {
59 private MsoMulticloudUtils multicloudUtils;
62 private MsoMulticloudUtils multicloudUtilsMock;
65 private CloudConfig cloudConfigMock;
67 private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
68 + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
69 private static final String UPDATE_STACK_RESPONSE =
70 "{\"template_type\": \"TEST-template\", \"workload_id\": " + "\"TEST-workload\"}";
71 private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
72 + "\"TEST-workload\", \"workload_status\": \"CREATE_COMPLETE\"}";
73 private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
74 + "\"TEST-workload\", \"workload_status\": \"UPDATE_COMPLETE\"}";
76 private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
77 private static final String MULTICLOUD_UPDATE_PATH =
78 "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
79 private static final String MULTICLOUD_GET_PATH =
80 "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
83 public void createStackSuccess() throws MsoException, IOException {
85 .stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
86 .willReturn(aResponse().withHeader("Content-Type", "application/json")
87 .withBody(CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_CREATED))
88 .willSetStateTo("CREATING"));
89 wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE")
90 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
91 .withBody(GET_CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
92 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
93 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
94 .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
95 .willSetStateTo("UPDATING"));
96 wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE")
97 .whenScenarioStateIs("UPDATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
98 .withBody(GET_UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
100 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
101 "TEST-heat", new HashMap<>(), true, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false);
102 wireMockServer.resetScenarios();
103 assertNotNull(result);
104 assertEquals("TEST-stack", result.getName());
108 public void deleteStack() throws MsoException {
109 StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
110 assertNotNull(result);
111 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
115 public void queryStack() throws MsoException {
116 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
117 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
121 public void queryStackWithNullMulticloudQueryBody() throws MsoException {
122 wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/instanceId"))
123 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(CREATE_STACK_RESPONSE)
124 .withStatus(HttpStatus.SC_OK)));
126 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
127 assertTrue(HeatStatus.FAILED == result.getStatus());
128 assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
131 @Test(expected = VduException.class)
132 public void updateVdu() throws MsoException {
133 multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(), false);
137 public void deleteVdu() throws VduException {
138 CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
139 VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
140 assertNotNull(vduInstance);
141 assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
146 public void createStackMulticloudClientIsNull() {
148 multicloudUtilsMock.cloudConfig = cloudConfigMock;
149 CloudSite cloudSite = new CloudSite();
150 cloudSite.setIdentityService(new CloudIdentity());
151 when(cloudConfigMock.getCloudSite("MTN13")).thenReturn(Optional.of(cloudSite));
152 multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
153 "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false);
154 } catch (MsoException e) {
155 assertEquals("0 : Multicloud client could not be initialized", e.toString());
158 fail("MsoOpenstackException expected!");
162 public void createStackBadRequest() {
164 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(
165 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_BAD_REQUEST)));
166 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
167 "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false);
168 } catch (MsoException e) {
169 assertEquals("0 : Bad Request", e.toString());
172 fail("MsoOpenstackException expected!");
176 public void createStackEmptyResponseEntity() throws MsoException {
177 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(
178 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_CREATED)));
180 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
181 "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false);
182 assertNotNull(result);
183 assertEquals("TEST-stack/", result.getName());