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.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\": \"TEST-template\", \"workload_id\": "
69 + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
71 private static final String MULTICLOUD_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
74 public void createStackSuccess() throws MsoException, IOException {
75 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
76 .willReturn(aResponse().withHeader("Content-Type", "application/json")
77 .withBody(CREATE_STACK_RESPONSE)
78 .withStatus(HttpStatus.SC_CREATED)));
79 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
80 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
81 new HashMap<>(), new HashMap<>(), false);
82 assertNotNull(result);
83 assertEquals("TEST-stack", result.getName());
87 public void deleteStack() throws MsoException {
88 StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
89 assertNotNull(result);
90 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
94 public void queryStack() throws MsoException {
95 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
96 assertTrue(HeatStatus.NOTFOUND == result.getStatus());
100 public void queryStackWithNullMulticloudQueryBody() throws MsoException {
101 wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/instanceId"))
102 .willReturn(aResponse().withHeader("Content-Type", "application/json")
103 .withBody(CREATE_STACK_RESPONSE)
104 .withStatus(HttpStatus.SC_OK)));
106 StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
107 assertTrue(HeatStatus.FAILED == result.getStatus());
108 assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
111 @Test(expected = VduException.class)
112 public void updateVdu() throws MsoException {
113 multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(),
118 public void deleteVdu() throws VduException {
119 CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
120 VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
121 assertNotNull(vduInstance);
122 assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
126 public void createStackMulticloudClientIsNull() {
128 multicloudUtilsMock.cloudConfig = cloudConfigMock;
129 CloudSite cloudSite = new CloudSite();
130 cloudSite.setIdentityService(new CloudIdentity());
131 when(cloudConfigMock.getCloudSite("MTN13")).
132 thenReturn(Optional.of(cloudSite));
133 multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
134 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
135 new HashMap<>(), new HashMap<>(), false);
136 } catch (MsoException e) {
137 assertEquals("0 : Multicloud client could not be initialized", e.toString());
140 fail("MsoOpenstackException expected!");
144 public void createStackBadRequest() {
146 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
147 .willReturn(aResponse().withHeader("Content-Type", "application/json")
148 .withStatus(HttpStatus.SC_BAD_REQUEST)));
149 multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
150 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
151 new HashMap<>(), new HashMap<>(), false);
152 } catch (MsoException e) {
153 assertEquals("0 : Bad Request", e.toString());
156 fail("MsoOpenstackException expected!");
160 public void createStackEmptyResponseEntity() throws MsoException {
161 wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
162 .willReturn(aResponse().withHeader("Content-Type", "application/json")
163 .withStatus(HttpStatus.SC_CREATED)));
164 StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
165 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
166 new HashMap<>(), new HashMap<>(), false);
167 assertNotNull(result);
168 assertEquals("TEST-stack/", result.getName());