aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java
blob: fbe532d1fe0d5b94247898e1d3920bb2245ff1e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2019 Samsung Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.openstack.utils;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;

import org.apache.http.HttpStatus;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.onap.so.BaseTest;
import org.onap.so.adapters.vdu.CloudInfo;
import org.onap.so.adapters.vdu.VduException;
import org.onap.so.adapters.vdu.VduInstance;
import org.onap.so.adapters.vdu.VduModelInfo;
import org.onap.so.adapters.vdu.VduStateType;
import org.onap.so.cloud.CloudConfig;
import org.onap.so.db.catalog.beans.CloudIdentity;
import org.onap.so.db.catalog.beans.CloudSite;
import org.onap.so.openstack.beans.HeatStatus;
import org.onap.so.openstack.beans.StackInfo;
import org.onap.so.openstack.exceptions.MsoException;
import org.springframework.beans.factory.annotation.Autowired;

public class MsoMulticloudUtilsTest extends BaseTest {

    @Autowired
    private MsoMulticloudUtils multicloudUtils;

    @InjectMocks
    private MsoMulticloudUtils multicloudUtilsMock;

    @Mock
    private CloudConfig cloudConfigMock;

    private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
        + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";

    private static final String MULTICLOUD_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";

    @Test
    public void createStackSuccess() throws MsoException, IOException {
        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
            .willReturn(aResponse().withHeader("Content-Type", "application/json")
                .withBody(CREATE_STACK_RESPONSE)
                .withStatus(HttpStatus.SC_CREATED)));
        StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
            "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
            new HashMap<>(), new HashMap<>(), false);
        assertNotNull(result);
        assertEquals("TEST-stack", result.getName());
    }

    @Test
    public void deleteStack() throws MsoException {
        StackInfo result = multicloudUtils.deleteStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
        assertNotNull(result);
        assertTrue(HeatStatus.NOTFOUND == result.getStatus());
    }

    @Test
    public void queryStack() throws MsoException {
        StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
        assertTrue(HeatStatus.NOTFOUND == result.getStatus());
    }

    @Test(expected = VduException.class)
    public void updateVdu() throws MsoException {
        multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
            false);
    }

    @Test
    public void deleteVdu() throws VduException {
        CloudInfo cloudInfo = new CloudInfo("cloudSiteId", "cloudOwner", "tenantId", "tenantName");
        VduInstance vduInstance = multicloudUtils.deleteVdu(cloudInfo, "instanceId", 3);
        assertNotNull(vduInstance);
        assertTrue(VduStateType.DELETED == vduInstance.getStatus().getState());
    }

    @Ignore @Test
    public void createStackMulticloudClientIsNull() {
        try {
            multicloudUtilsMock.cloudConfig = cloudConfigMock;
            CloudSite cloudSite = new CloudSite();
            cloudSite.setIdentityService(new CloudIdentity());
            when(cloudConfigMock.getCloudSite("MTN13")).
                thenReturn(Optional.of(cloudSite));
            multicloudUtilsMock.createStack("MNT14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
                "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
                new HashMap<>(), new HashMap<>(), false);
        } catch (MsoException e) {
            assertEquals("0 : Multicloud client could not be initialized", e.toString());
            return;
        }
        fail("MsoOpenstackException expected!");
    }

    @Test
    public void createStackBadRequest() {
        try {
            wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
                .willReturn(aResponse().withHeader("Content-Type", "application/json")
                    .withStatus(HttpStatus.SC_BAD_REQUEST)));
            multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
                "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
                new HashMap<>(), new HashMap<>(), false);
        } catch (MsoException e) {
            assertEquals("0 : Bad Request", e.toString());
            return;
        }
        fail("MsoOpenstackException expected!");
    }

    @Test
    public void createStackEmptyResponseEntity() throws MsoException {
        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
            .willReturn(aResponse().withHeader("Content-Type", "application/json")
                .withStatus(HttpStatus.SC_CREATED)));
        StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
            "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
            new HashMap<>(), new HashMap<>(), false);
        assertNotNull(result);
        assertEquals("TEST-stack/", result.getName());
    }
}