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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
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.fail;
30 import static org.mockito.Mockito.when;
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.Optional;
35 import org.apache.http.HttpStatus;
36 import org.junit.Test;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.onap.so.BaseTest;
40 import org.onap.so.adapters.vdu.VduModelInfo;
41 import org.onap.so.cloud.CloudConfig;
42 import org.onap.so.db.catalog.beans.CloudIdentity;
43 import org.onap.so.db.catalog.beans.CloudSite;
44 import org.onap.so.openstack.beans.StackInfo;
45 import org.onap.so.openstack.exceptions.MsoException;
46 import org.springframework.beans.factory.annotation.Autowired;
48 public class MsoMulticloudUtilsTest extends BaseTest {
51 private MsoMulticloudUtils multicloudUtils;
54 private MsoMulticloudUtils multicloudUtilsMock;
57 private CloudConfig cloudConfigMock;
59 private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
60 + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
63 public void createStackSuccess() throws MsoException, IOException {
64 stubFor(post(urlPathEqualTo("/v2.0"))
65 .willReturn(aResponse().withHeader("Content-Type", "application/json")
66 .withBody(CREATE_STACK_RESPONSE)
67 .withStatus(HttpStatus.SC_CREATED)));
68 StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
69 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
70 new HashMap<>(), new HashMap<>(), false);
71 assertNotNull(result);
72 assertEquals("TEST-stack", result.getName());
76 public void createStackMulticloudClientIsNull() {
78 multicloudUtilsMock.cloudConfig = cloudConfigMock;
79 CloudSite cloudSite = new CloudSite();
80 cloudSite.setIdentityService(new CloudIdentity());
81 when(cloudConfigMock.getCloudSite("MTN13")).
82 thenReturn(Optional.of(cloudSite));
83 multicloudUtilsMock.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
84 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
85 new HashMap<>(), new HashMap<>(), false);
86 } catch (MsoException e) {
87 assertEquals("0 : Multicloud client could not be initialized", e.toString());
90 fail("MsoOpenstackException expected!");
94 public void createStackBadRequest() {
96 stubFor(post(urlPathEqualTo("/v2.0"))
97 .willReturn(aResponse().withHeader("Content-Type", "application/json")
98 .withStatus(HttpStatus.SC_BAD_REQUEST)));
99 multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
100 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
101 new HashMap<>(), new HashMap<>(), false);
102 } catch (MsoException e) {
103 assertEquals("0 : Bad Request", e.toString());
106 fail("MsoOpenstackException expected!");
110 public void createStackEmptyResponseEntity() throws MsoException {
111 stubFor(post(urlPathEqualTo("/v2.0"))
112 .willReturn(aResponse().withHeader("Content-Type", "application/json")
113 .withStatus(HttpStatus.SC_CREATED)));
114 StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
115 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
116 new HashMap<>(), new HashMap<>(), false);
117 assertNotNull(result);
118 assertEquals("TEST-stack/", result.getName());