Merge "Add support to parse cdl inside LOB and platform"
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.openstack.utils;
24
25 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
26 import static com.github.tomakehurst.wiremock.client.WireMock.get;
27 import static com.github.tomakehurst.wiremock.client.WireMock.post;
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.assertTrue;
32 import static org.junit.Assert.fail;
33 import static org.mockito.Mockito.when;
34
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Optional;
38
39 import org.apache.http.HttpStatus;
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.onap.so.BaseTest;
46 import org.onap.so.adapters.vdu.CloudInfo;
47 import org.onap.so.adapters.vdu.VduException;
48 import org.onap.so.adapters.vdu.VduInstance;
49 import org.onap.so.adapters.vdu.VduModelInfo;
50 import org.onap.so.adapters.vdu.VduStateType;
51 import org.onap.so.cloud.CloudConfig;
52 import org.onap.so.db.catalog.beans.CloudIdentity;
53 import org.onap.so.db.catalog.beans.CloudSite;
54 import org.onap.so.openstack.beans.HeatStatus;
55 import org.onap.so.openstack.beans.StackInfo;
56 import org.onap.so.openstack.exceptions.MsoException;
57 import org.springframework.beans.factory.annotation.Autowired;
58
59 public class MsoMulticloudUtilsTest extends BaseTest {
60
61     @Autowired
62     private MsoMulticloudUtils multicloudUtils;
63
64     @InjectMocks
65     private MsoMulticloudUtils multicloudUtilsMock;
66
67     @Mock
68     private CloudConfig cloudConfigMock;
69
70     private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
71         + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
72     private static final String UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
73             + "\"TEST-workload\"}";
74     private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
75             + "\"TEST-workload\", \"workload_status\": \"CREATE_COMPLETE\"}";
76     private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
77             + "\"TEST-workload\", \"workload_status\": \"UPDATE_COMPLETE\"}";
78
79     private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
80     private static final String MULTICLOUD_UPDATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
81     private static final String MULTICLOUD_GET_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
82
83     @Test
84     public void createStackSuccess() throws MsoException, IOException {
85         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
86             .willReturn(aResponse().withHeader("Content-Type", "application/json")
87                 .withBody(CREATE_STACK_RESPONSE)
88                 .withStatus(HttpStatus.SC_CREATED))
89             .willSetStateTo("CREATING"));
90         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
91                 .inScenario("CREATE").whenScenarioStateIs("CREATING")
92                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
93                     .withBody(GET_CREATE_STACK_RESPONSE)
94                     .withStatus(HttpStatus.SC_OK)));
95         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH)).inScenario("CREATE")
96                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
97                     .withBody(UPDATE_STACK_RESPONSE)
98                     .withStatus(HttpStatus.SC_ACCEPTED))
99                 .willSetStateTo("UPDATING"));
100         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
101                 .inScenario("CREATE").whenScenarioStateIs("UPDATING")
102                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
103                     .withBody(GET_UPDATE_STACK_RESPONSE)
104                     .withStatus(HttpStatus.SC_OK)));
105         StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
106             "TEST-heat", new HashMap<>(), true, 200, "TEST-env",
107             new HashMap<>(), new HashMap<>(), false);
108         wireMockServer.resetScenarios();
109         assertNotNull(result);
110         assertEquals("TEST-stack", result.getName());
111     }
112
113     @Test
114     public void deleteStack() throws MsoException {
115         StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
116         assertNotNull(result);
117         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
118     }
119
120     @Test
121     public void queryStack() throws MsoException {
122         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
123         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
124     }
125
126     @Test(expected = VduException.class)
127     public void updateVdu() throws MsoException {
128         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
129             false);
130     }
131
132     @Test
133     public void deleteVdu() throws VduException {
134         CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
135         VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
136         assertNotNull(vduInstance);
137         assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
138     }
139
140     @Ignore @Test
141     public void createStackMulticloudClientIsNull() {
142         try {
143             multicloudUtilsMock.cloudConfig = cloudConfigMock;
144             CloudSite cloudSite = new CloudSite();
145             cloudSite.setIdentityService(new CloudIdentity());
146             when(cloudConfigMock.getCloudSite("MTN13")).
147                 thenReturn(Optional.of(cloudSite));
148             multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
149                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
150                 new HashMap<>(), new HashMap<>(), false);
151         } catch (MsoException e) {
152             assertEquals("0 : Multicloud client could not be initialized", e.toString());
153             return;
154         }
155         fail("MsoOpenstackException expected!");
156     }
157
158     @Test
159     public void createStackBadRequest() {
160         try {
161             wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
162                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
163                     .withStatus(HttpStatus.SC_BAD_REQUEST)));
164             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         } catch (MsoException e) {
168             assertEquals("0 : Bad Request", e.toString());
169             return;
170         }
171         fail("MsoOpenstackException expected!");
172     }
173
174     @Test
175     public void createStackEmptyResponseEntity() throws MsoException {
176         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
177             .willReturn(aResponse().withHeader("Content-Type", "application/json")
178                 .withStatus(HttpStatus.SC_CREATED)));
179         StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
180             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
181             new HashMap<>(), new HashMap<>(), false);
182         assertNotNull(result);
183         assertEquals("TEST-stack/", result.getName());
184     }
185 }