Merge "JUnit tests for VfcManager"
[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.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
70     @Test
71     public void createStackSuccess() throws MsoException, IOException {
72         wireMockServer.stubFor(post(urlPathEqualTo("/v2.0"))
73             .willReturn(aResponse().withHeader("Content-Type", "application/json")
74                 .withBody(CREATE_STACK_RESPONSE)
75                 .withStatus(HttpStatus.SC_CREATED)));
76         StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
77             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
78             new HashMap<>(), new HashMap<>(), false);
79         assertNotNull(result);
80         assertEquals("TEST-stack", result.getName());
81     }
82
83     @Test
84     public void deleteStack() throws MsoException {
85         StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
86         assertNotNull(result);
87         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
88     }
89
90     @Test
91     public void queryStack() throws MsoException {
92         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
93         assertTrue(HeatStatus.NOTFOUND == result.getStatus());
94     }
95
96     @Test(expected = VduException.class)
97     public void updateVdu() throws MsoException {
98         multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
99             false);
100     }
101
102     @Test
103     public void deleteVdu() throws VduException {
104         CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
105         VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
106         assertNotNull(vduInstance);
107         assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
108     }
109
110     @Test
111     public void createStackMulticloudClientIsNull() {
112         try {
113             multicloudUtilsMock.cloudConfig = cloudConfigMock;
114             CloudSite cloudSite = new CloudSite();
115             cloudSite.setIdentityService(new CloudIdentity());
116             when(cloudConfigMock.getCloudSite("MTN13")).
117                 thenReturn(Optional.of(cloudSite));
118             multicloudUtilsMock.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
119                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
120                 new HashMap<>(), new HashMap<>(), false);
121         } catch (MsoException e) {
122             assertEquals("0 : Multicloud client could not be initialized", e.toString());
123             return;
124         }
125         fail("MsoOpenstackException expected!");
126     }
127
128     @Test
129     public void createStackBadRequest() {
130         try {
131             wireMockServer.stubFor(post(urlPathEqualTo("/v2.0"))
132                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
133                     .withStatus(HttpStatus.SC_BAD_REQUEST)));
134             multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
135                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
136                 new HashMap<>(), new HashMap<>(), false);
137         } catch (MsoException e) {
138             assertEquals("0 : Bad Request", e.toString());
139             return;
140         }
141         fail("MsoOpenstackException expected!");
142     }
143
144     @Test
145     public void createStackEmptyResponseEntity() throws MsoException {
146         wireMockServer.stubFor(post(urlPathEqualTo("/v2.0"))
147             .willReturn(aResponse().withHeader("Content-Type", "application/json")
148                 .withStatus(HttpStatus.SC_CREATED)));
149         StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
150             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
151             new HashMap<>(), new HashMap<>(), false);
152         assertNotNull(result);
153         assertEquals("TEST-stack/", result.getName());
154     }
155 }