Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoMulticloudUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.openstack.utils;
22
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;
55
56 public class MsoMulticloudUtilsTest extends BaseTest {
57
58     @Autowired
59     private MsoMulticloudUtils multicloudUtils;
60
61     @InjectMocks
62     private MsoMulticloudUtils multicloudUtilsMock;
63
64     @Mock
65     private CloudConfig cloudConfigMock;
66
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\"}";
75
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";
81
82     @Test
83     public void createStackSuccess() throws MsoException, IOException {
84         wireMockServer
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)));
99         StackInfo result =
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());
105     }
106
107     @Test
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());
112     }
113
114     @Test
115     public void queryStack() throws MsoException {
116         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
117         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
118     }
119
120     @Test
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)));
125
126         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
127         assertTrue(HeatStatus.FAILED == result.getStatus());
128         assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
129     }
130
131     @Test(expected = VduException.class)
132     public void updateVdu() throws MsoException {
133         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(), false);
134     }
135
136     @Test
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());
142     }
143
144     @Ignore
145     @Test
146     public void createStackMulticloudClientIsNull() {
147         try {
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());
156             return;
157         }
158         fail("MsoOpenstackException expected!");
159     }
160
161     @Test
162     public void createStackBadRequest() {
163         try {
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());
170             return;
171         }
172         fail("MsoOpenstackException expected!");
173     }
174
175     @Test
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)));
179         StackInfo result =
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());
184     }
185 }