Construct multicloud infra_workload endpoint
[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.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 import static org.mockito.Mockito.when;
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(expected = VduException.class)
100     public void updateVdu() throws MsoException {
101         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
102             false);
103     }
104
105     @Test
106     public void deleteVdu() throws VduException {
107         CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
108         VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
109         assertNotNull(vduInstance);
110         assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
111     }
112
113     @Ignore @Test
114     public void createStackMulticloudClientIsNull() {
115         try {
116             multicloudUtilsMock.cloudConfig = cloudConfigMock;
117             CloudSite cloudSite = new CloudSite();
118             cloudSite.setIdentityService(new CloudIdentity());
119             when(cloudConfigMock.getCloudSite("MTN13")).
120                 thenReturn(Optional.of(cloudSite));
121             multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
122                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
123                 new HashMap<>(), new HashMap<>(), false);
124         } catch (MsoException e) {
125             assertEquals("0 : Multicloud client could not be initialized", e.toString());
126             return;
127         }
128         fail("MsoOpenstackException expected!");
129     }
130
131     @Test
132     public void createStackBadRequest() {
133         try {
134             wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
135                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
136                     .withStatus(HttpStatus.SC_BAD_REQUEST)));
137             multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
138                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
139                 new HashMap<>(), new HashMap<>(), false);
140         } catch (MsoException e) {
141             assertEquals("0 : Bad Request", e.toString());
142             return;
143         }
144         fail("MsoOpenstackException expected!");
145     }
146
147     @Test
148     public void createStackEmptyResponseEntity() throws MsoException {
149         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
150             .willReturn(aResponse().withHeader("Content-Type", "application/json")
151                 .withStatus(HttpStatus.SC_CREATED)));
152         StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
153             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
154             new HashMap<>(), new HashMap<>(), false);
155         assertNotNull(result);
156         assertEquals("TEST-stack/", result.getName());
157     }
158 }