sonar issue, MsoMulticloudUtilsTest Use assertSame instead
[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) 2022 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.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.fail;
33 import static org.mockito.Mockito.when;
34 import static org.onap.so.openstack.utils.MsoMulticloudUtils.MULTICLOUD_QUERY_BODY_NULL;
35 import java.io.IOException;
36 import java.util.HashMap;
37 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;
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\": \"heat\", \"workload_id\": "
69             + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
70     private static final String UPDATE_STACK_RESPONSE =
71             "{\"template_type\": \"heat\", \"workload_id\": " + "\"TEST-workload\"}";
72
73     private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
74     private static final String MULTICLOUD_UPDATE_PATH =
75             "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
76     private static final String MULTICLOUD_GET_PATH =
77             "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
78     private static final String MULTICLOUD_DELETE_PATH =
79             "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
80
81     @Test
82     public void createStackSuccess() throws MsoException, IOException {
83         wireMockServer
84                 .stubFor(post(urlEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
85                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
86                                 .withBody(CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_CREATED))
87                         .willSetStateTo("CREATING"));
88         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE")
89                 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
90                         .withBodyFile("MulticloudGetCreateResponse.json").withStatus(HttpStatus.SC_OK)));
91         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
92                 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
93                         .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
94                 .willSetStateTo("UPDATING"));
95         wireMockServer.stubFor(get(urlEqualTo(MULTICLOUD_GET_PATH)).inScenario("CREATE").whenScenarioStateIs("UPDATING")
96                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
97                         .withBodyFile("MulticloudGetUpdateResponse.json").withStatus(HttpStatus.SC_OK)));
98         StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack",
99                 new VduModelInfo(), "TEST-heat", new HashMap<>(), true, 200, "TEST-env", new HashMap<>(),
100                 new HashMap<>(), false, false);
101         wireMockServer.resetScenarios();
102         assertNotNull(result);
103         assertEquals("TEST-stack", result.getName());
104     }
105
106     @Test
107     public void deleteStack() throws MsoException {
108         wireMockServer.stubFor(delete(urlEqualTo(MULTICLOUD_DELETE_PATH)).willReturn(
109                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NO_CONTENT)));
110         wireMockServer.stubFor(get(urlEqualTo(MULTICLOUD_GET_PATH))
111                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
112                         .withBodyFile("MulticloudGetDeleteResponse.json").withStatus(HttpStatus.SC_OK)));
113         StackInfo result =
114                 multicloudUtils.deleteStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack/TEST-workload");
115         assertNotNull(result);
116         assertSame(HeatStatus.NOTFOUND, result.getStatus());
117     }
118
119     @Test
120     public void queryStack() throws MsoException {
121         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
122         assertSame(HeatStatus.NOTFOUND, result.getStatus());
123     }
124
125     @Test
126     public void queryStackWithNullMulticloudQueryBody() throws MsoException {
127         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/instanceId"))
128                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(CREATE_STACK_RESPONSE)
129                         .withStatus(HttpStatus.SC_OK)));
130
131         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceName/instanceId");
132         assertSame(HeatStatus.FAILED, result.getStatus());
133         assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
134     }
135
136     @Test(expected = VduException.class)
137     public void updateVdu() throws MsoException {
138         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new VduModelInfo(), false);
139     }
140
141     @Test
142     public void deleteVdu() throws VduException {
143         CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
144         VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
145         assertNotNull(vduInstance);
146         assertSame(VduStateType.DELETED, vduInstance.getStatus().getState());
147     }
148
149     @Ignore
150     @Test
151     public void createStackMulticloudClientIsNull() {
152         try {
153             multicloudUtilsMock.cloudConfig = cloudConfigMock;
154             CloudSite cloudSite = new CloudSite();
155             cloudSite.setIdentityService(new CloudIdentity());
156             when(cloudConfigMock.getCloudSite("MTN13")).thenReturn(Optional.of(cloudSite));
157             multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
158                     "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false,
159                     false);
160         } catch (MsoException e) {
161             assertEquals("0 : Multicloud client could not be initialized", e.toString());
162             return;
163         }
164         fail("MsoOpenstackException expected!");
165     }
166
167     @Test
168     public void createStackBadRequest() {
169         try {
170             wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(
171                     aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_BAD_REQUEST)));
172             multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
173                     "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(), new HashMap<>(), false,
174                     false);
175         } catch (MsoException e) {
176             assertEquals("0 : Bad Request", e.toString());
177             return;
178         }
179         fail("MsoOpenstackException expected!");
180     }
181
182     @Test
183     public void createStackEmptyResponseEntity() throws MsoException {
184         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).willReturn(aResponse()
185                 .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_CREATED).withBody("{}")));
186         StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack",
187                 new VduModelInfo(), "TEST-heat", new HashMap<>(), false, 200, "TEST-env", new HashMap<>(),
188                 new HashMap<>(), false, false);
189         assertNotNull(result);
190         assertEquals("TEST-stack", result.getName());
191     }
192 }