Merge "moved object declarations from class level"
[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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
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.fail;
30 import static org.mockito.Mockito.when;
31
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.Optional;
35 import org.apache.http.HttpStatus;
36 import org.junit.Test;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.onap.so.BaseTest;
40 import org.onap.so.cloud.CloudConfig;
41 import org.onap.so.db.catalog.beans.CloudIdentity;
42 import org.onap.so.db.catalog.beans.CloudSite;
43 import org.onap.so.openstack.beans.StackInfo;
44 import org.onap.so.openstack.exceptions.MsoException;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 public class MsoMulticloudUtilsTest extends BaseTest {
48
49     @Autowired
50     private MsoMulticloudUtils multicloudUtils;
51
52     @InjectMocks
53     private MsoMulticloudUtils multicloudUtilsMock;
54
55     @Mock
56     private CloudConfig cloudConfigMock;
57
58     private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
59         + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
60
61     @Test
62     public void createStackSuccess() throws MsoException, IOException {
63         stubFor(post(urlPathEqualTo("/v2.0"))
64             .willReturn(aResponse().withHeader("Content-Type", "application/json")
65                 .withBody(CREATE_STACK_RESPONSE)
66                 .withStatus(HttpStatus.SC_CREATED)));
67         StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack",
68             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
69             new HashMap<>(), new HashMap<>());
70         assertNotNull(result);
71         assertEquals("TEST-stack", result.getName());
72     }
73
74     @Test
75     public void createStackMulticloudClientIsNull() {
76         try {
77             multicloudUtilsMock.cloudConfig = cloudConfigMock;
78             CloudSite cloudSite = new CloudSite();
79             cloudSite.setIdentityService(new CloudIdentity());
80             when(cloudConfigMock.getCloudSite("MTN13")).
81                 thenReturn(Optional.of(cloudSite));
82             multicloudUtilsMock.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack",
83                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
84                 new HashMap<>(), new HashMap<>());
85         } catch (MsoException e) {
86             assertEquals("0 : Multicloud client could not be initialized", e.toString());
87             return;
88         }
89         fail("MsoOpenstackException expected!");
90     }
91
92     @Test
93     public void createStackBadRequest() {
94         try {
95             stubFor(post(urlPathEqualTo("/v2.0"))
96                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
97                     .withStatus(HttpStatus.SC_BAD_REQUEST)));
98             multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack",
99                 "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
100                 new HashMap<>(), new HashMap<>());
101         } catch (MsoException e) {
102             assertEquals("0 : Bad Request", e.toString());
103             return;
104         }
105         fail("MsoOpenstackException expected!");
106     }
107
108     @Test
109     public void createStackEmptyResponseEntity() throws MsoException {
110         stubFor(post(urlPathEqualTo("/v2.0"))
111             .willReturn(aResponse().withHeader("Content-Type", "application/json")
112                 .withStatus(HttpStatus.SC_CREATED)));
113         StackInfo result = multicloudUtils.createStack("MTN13", "CloudOwner", "TEST-tenant", "TEST-stack",
114             "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
115             new HashMap<>(), new HashMap<>());
116         assertNotNull(result);
117         assertEquals("TEST-stack/", result.getName());
118     }
119 }