Fixed Sonar blocker issues
[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
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Optional;
37
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;
56
57 public class MsoMulticloudUtilsTest extends BaseTest {
58
59     @Autowired
60     private MsoMulticloudUtils multicloudUtils;
61
62     @InjectMocks
63     private MsoMulticloudUtils multicloudUtilsMock;
64
65     @Mock
66     private CloudConfig cloudConfigMock;
67
68     private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
69         + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
70
71     private static final String MULTICLOUD_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
72
73     @Test
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());
84     }
85
86     @Test
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());
91     }
92
93     @Test
94     public void queryStack() throws MsoException {
95         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
96         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
97     }
98
99     @Test
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)));
105
106         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
107         assertTrue(HeatStatus.FAILED == result.getStatus());
108         assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
109     }
110
111     @Test(expected = VduException.class)
112     public void updateVdu() throws MsoException {
113         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
114             false);
115     }
116
117     @Test
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());
123     }
124
125     @Ignore @Test
126     public void createStackMulticloudClientIsNull() {
127         try {
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());
138             return;
139         }
140         fail("MsoOpenstackException expected!");
141     }
142
143     @Test
144     public void createStackBadRequest() {
145         try {
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());
154             return;
155         }
156         fail("MsoOpenstackException expected!");
157     }
158
159     @Test
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());
169     }
170 }