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