summaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/base/TestProviderOperation.java
blob: 496bbce51e2228e6ea06dc20be43a818d257f1e1 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * ============LICENSE_START=======================================================
 * Copyright (C) 2018 Ericsson. All rights reserved.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.adapter.iaas.provider.operation.impl.base;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.mockito.Mockito;
import com.att.cdp.openstack.OpenStackContext;
import com.att.cdp.zones.model.Server.Status;
import org.onap.appc.adapter.iaas.impl.ProviderCache;
import org.onap.appc.adapter.iaas.impl.RequestContext;
import org.onap.appc.adapter.iaas.impl.RequestFailedException;
import org.onap.appc.adapter.iaas.impl.TenantCache;
import org.onap.appc.adapter.iaas.impl.VMURL;
import org.onap.appc.adapter.iaas.provider.operation.impl.AttachVolumeServer;
import org.onap.appc.adapter.iaas.provider.operation.impl.MockGenerator;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.exceptions.APPCException;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.appc.pool.Pool;
import org.onap.appc.pool.PoolDrainedException;
import org.onap.appc.pool.PoolExtensionException;
import org.glassfish.grizzly.http.util.HttpStatus;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class TestProviderOperation {

    ProviderServerOperation underTest = spy(new AttachVolumeServer());

    @Test
    public void testDoFailureRequestContextHttpStatusString() throws APPCException {
        RequestContext rc = mock(RequestContext.class);
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        HttpStatus code = HttpStatus.NOT_FOUND_404;
        String message = "PALOS\n";
        underTest.doFailure(rc, code, message);
        verify(underTest).doFailure(rc, code, message, null);
    }

    @Test(expected = APPCException.class)
    public void testDoFailureRequestContextHttpStatusStringException() throws APPCException {
        RequestContext rc = mock(RequestContext.class);
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        HttpStatus code = spy(HttpStatus.NOT_FOUND_404);
        doThrow(new RuntimeException("TEST")).when(code).getStatusCode();
        String message = "PALOS\n";
        underTest.doFailure(rc, code, message, new Throwable("TEST"));
        verify(underTest).doFailure(rc, code, message, null);
    }

    @Test
    public void testDoFailureRequestContextHttpStatusStringAPPCException() throws APPCException {
        RequestContext rc = mock(RequestContext.class);
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        HttpStatus code = HttpStatus.NOT_FOUND_404;
        String message = "PALOS\n";
        doThrow(new APPCException("TEST")).when(underTest).doFailure(rc, code, message, null);
        underTest.doFailure(rc, code, message);
        verify(underTest).doFailure(rc, code, message, null);
    }

    @Test(expected = APPCException.class)
    public void testDoFailureRequestContextHttpStatusStringThrowableAPPCException() throws APPCException {
        RequestContext rc = mock(RequestContext.class);
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        HttpStatus code = HttpStatus.NOT_FOUND_404;
        String message = "PALOS\n";
        underTest.doFailure(rc, code, message, new Throwable());
    }

    @Test
    public void testValidateVM() throws RequestFailedException {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        RequestContext rc = mock(RequestContext.class);
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        assertTrue(underTest.validateVM(rc, "TEST", "TEST", null));
    }

    @Test
    public void testGetContextNullVM() {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        assertNull(underTest.getContext(rc, "%£$%^$", "%£$%^$"));
        verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
                Mockito.anyString());
    }

    @Test
    public void testGetContextNullCache() {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        underTest.setProviderCache(mg.getProviderCacheMap());
        assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                + "abc12345-1234-5678-890a-abcdefb12345", "%£$%^$"));
        verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
                Mockito.anyString());
    }

    @Test
    public void testGetContextNullTenantCache() {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap();
        providerCacheMap.put("TEST", mock(ProviderCache.class));
        underTest.setProviderCache(mg.getProviderCacheMap());
        assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                + "abc12345-1234-5678-890a-abcdefb12345", "TEST"));
        verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
                Mockito.anyString());
    }

    @Test
    public void testGetContextPoolNullRegion() {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        Map<String, ProviderCache> providerCacheMap = mg.getProviderCacheMap();
        TenantCache tenantCache = mock(TenantCache.class);
        when(tenantCache.getTenantName()).thenReturn("TEST");
        when(tenantCache.getTenantId()).thenReturn("TEST");
        when(tenantCache.determineRegion(Mockito.anyObject())).thenReturn(null);
        ProviderCache providerCache = mock(ProviderCache.class);
        when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
        providerCacheMap.put("TEST", providerCache);
        underTest.setProviderCache(mg.getProviderCacheMap());
        assertNull(underTest.getContext(rc, "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                + "abc12345-1234-5678-890a-abcdefb12345", "TEST"));
        verify(underTest).doFailure(Mockito.any(RequestContext.class), Mockito.any(HttpStatus.class),
                Mockito.anyString());
    }

    @Test
    public void testGetContextAttemptFailed() {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        underTest.setProviderCache(mg.getProviderCacheMap());
        assertNull(underTest.getContext(rc,
                "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                        + "abc12345-1234-5678-890a-abcdefb12345",
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3"));
    }

    @Test
    public void testGetContextRelogin() throws PoolExtensionException, PoolDrainedException {
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        ProviderCache providerCache = mock(ProviderCache.class);
        Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
        providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);
        when(rc.attempt()).thenReturn(true).thenReturn(false);
        OpenStackContext context = mock(OpenStackContext.class);
        when(context.isStale()).thenReturn(true);
        TenantCache tenantCache = mock(TenantCache.class);
        doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
        Pool pool = mock(Pool.class);
        Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
        tenantCachePools.put("cloudowner_region", pool);
        doReturn(tenantCachePools).when(tenantCache).getPools();
        when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
        doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
        doReturn(context).when(pool).reserve();
        underTest.setProviderCache(providerCacheMap);
        assertTrue(underTest.getContext(rc,
                "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                        + "abc12345-1234-5678-890a-abcdefb12345",
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3") instanceof OpenStackContext);
    }

    @Test
    public void testGetContextPoolException() throws PoolExtensionException, PoolDrainedException {
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        ProviderCache providerCache = mock(ProviderCache.class);
        Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
        providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);

        when(rc.attempt()).thenReturn(true).thenReturn(false);
        OpenStackContext context = mock(OpenStackContext.class);
        when(context.isStale()).thenReturn(true);
        TenantCache tenantCache = mock(TenantCache.class);
        doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
        Pool pool = mock(Pool.class);
        Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
        tenantCachePools.put("cloudowner_region", pool);
        doReturn(tenantCachePools).when(tenantCache).getPools();
        when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
        doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
        doThrow(new PoolExtensionException("TEST")).when(pool).reserve();
        underTest.setProviderCache(providerCacheMap);
        underTest.getContext(rc,
                "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                        + "abc12345-1234-5678-890a-abcdefb12345",
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3");
        verify(rc).delay();
    }

    @Test
    public void testGetContextException() {
        RequestContext rc = mock(RequestContext.class);
        SvcLogicContext svcLogicContext = mock(SvcLogicContext.class);
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        ProviderCache providerCache = mock(ProviderCache.class);
        Map<String, ProviderCache> providerCacheMap = new HashMap<String, ProviderCache>();
        providerCacheMap.put("http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3", providerCache);

        when(rc.attempt()).thenReturn(true).thenReturn(false);
        OpenStackContext context = mock(OpenStackContext.class);
        when(context.isStale()).thenReturn(true);
        TenantCache tenantCache = mock(TenantCache.class);
        doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
        Pool pool = mock(Pool.class);
        Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
        tenantCachePools.put("cloudowner_region", pool);
        doReturn(tenantCachePools).when(tenantCache).getPools();
        when(providerCache.getTenant(Mockito.anyString())).thenReturn(tenantCache);
        doReturn(tenantCache).when(providerCache).getTenant(Mockito.anyString());
        doThrow(new RuntimeException("TEST")).when(rc).delay();
        underTest.setProviderCache(providerCacheMap);
        assertNull(underTest.getContext(rc,
                "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/"
                        + "abc12345-1234-5678-890a-abcdefb12345",
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3"));
    }

    @Test
    public void testValidateVMURLRequestFailedExceptionWellFormed() {
        VMURL vm = mock(VMURL.class);
        try {
            underTest.validateVMURL(vm);
            fail("Exception not thrown");
        } catch (RequestFailedException rfe) {
            assert (rfe.getMessage().startsWith("The value vm-id is not well formed"));
        }
    }

    @Test
    public void testValidateVMURLRequestFailedExceptionTenantId() throws RequestFailedException {
        VMURL vm = mock(VMURL.class);
        when(vm.toString()).thenReturn("192.168.0.1");
        when(vm.getTenantId()).thenReturn("%£$%^$");
        try {
            underTest.validateVMURL(vm);
            fail("Exception not thrown");
        } catch (RequestFailedException rfe) {
            assert (rfe.getMessage().startsWith("The value vm-id has an invalid tenantId"));
        }
    }

    @Test
    public void testValidateVMURLRequestFailedExceptionServerId() throws RequestFailedException {
        VMURL vm = mock(VMURL.class);
        when(vm.toString()).thenReturn("192.168.0.1");
        when(vm.getTenantId()).thenReturn("0000000000000000000000000000000a");
        when(vm.getServerId()).thenReturn("%£$%^$");
        try {
            underTest.validateVMURL(vm);
            fail("Exception not thrown");
        } catch (RequestFailedException rfe) {
            assert (rfe.getMessage().startsWith("The value vm-id has an invalid serverId"));
        }
    }

    @Test
    public void testResolveContext() throws RequestFailedException {
        MockGenerator mg = new MockGenerator(Status.RUNNING);
        SvcLogicContext svcLogicContext = mg.getSvcLogicContext();
        RequestContext rc = mock(RequestContext.class);
        when(rc.getSvcLogicContext()).thenReturn(svcLogicContext);
        assertNull(underTest.resolveContext(rc, new HashMap<String, String>(), "TEST", "TEST"));
    }

    @Test(expected = RequestFailedException.class)
    public void testValidateParameters() throws RequestFailedException {
        Properties properties = new Properties();
        properties.putAll(ConfigurationFactory.getConfiguration().getProperties());
        properties.put("TEST", "");
        Map<String, String> propertyMap = new HashMap<String, String>();
        for (String keys: properties.stringPropertyNames()) { propertyMap.put(keys, properties.getProperty(keys) );}
        underTest.validateParametersExist(propertyMap, org.onap.appc.Constants.PROPERTY_APPLICATION_NAME, "TEST");
    }

}