Unit test cases for iaas impl package
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / onap / appc / adapter / iaas / impl / TestProviderAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.iaas.impl;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Mockito.when;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.function.BiFunction;
36 import java.util.function.Function;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.appc.adapter.iaas.ProviderAdapter;
45 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
46 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
47 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
48 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
49 import org.onap.appc.configuration.ConfigurationFactory;
50 import org.onap.appc.exceptions.APPCException;
51 import org.onap.appc.exceptions.UnknownProviderException;
52 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
53 import com.att.cdp.exceptions.ZoneException;
54 import com.att.cdp.zones.model.Image;
55 import com.att.cdp.zones.model.ModelObject;
56 import com.att.cdp.zones.model.Server;
57 import com.att.cdp.zones.model.Stack;
58 import com.google.common.collect.ImmutableMap;
59
60 /**
61  * This class is used to test methods and functions of the adapter implementation that do not
62  * require and do not set up connections to any providers.
63  */
64 @RunWith(MockitoJUnitRunner.class)
65 @Category(org.onap.appc.adapter.iaas.impl.TestProviderAdapterImpl.class)
66 public class TestProviderAdapterImpl {
67
68     @SuppressWarnings("nls")
69     private static final String PROVIDER_NAME = "ILAB";
70
71     @SuppressWarnings("nls")
72     private static final String PROVIDER_TYPE = "OpenStackProvider";
73
74     private static String IDENTITY_URL;
75
76     private static String SERVER_URL;
77
78     private static String SERVER_ID;
79
80     private static Class<?> providerAdapterImplClass;
81     private static Class<?> configurationFactoryClass;
82     private static Field providerCacheField;
83     private static Field configField;
84
85     private static ProviderAdapterImpl adapter;
86
87     @Mock
88     ProviderOperationFactory factory;
89
90     @Mock
91     IProviderOperation providerOperation;
92
93     @Mock
94     EvacuateServer evacuateServer;
95
96     private Map<String, ProviderCache> providerCache;
97
98     private SvcLogicContext svcContext;
99
100     private Map<String, String> params;
101
102     private Image image;
103
104     private Server server;
105
106     private Stack stack;
107
108     /**
109      * Use reflection to locate fields and methods so that they can be manipulated during the test
110      * to change the internal state accordingly.
111      * 
112      * @throws NoSuchFieldException if the field(s) dont exist
113      * @throws SecurityException if reflective access is not allowed
114      * @throws NoSuchMethodException If the method(s) dont exist
115      */
116     @SuppressWarnings("nls")
117     @BeforeClass
118     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
119         providerAdapterImplClass = ProviderAdapterImpl.class;
120         configurationFactoryClass = ConfigurationFactory.class;
121
122         providerCacheField = providerAdapterImplClass.getDeclaredField("providerCache");
123         providerCacheField.setAccessible(true);
124         Properties props = ConfigurationFactory.getConfiguration().getProperties();
125         IDENTITY_URL = props.getProperty("provider1.identity");
126         SERVER_URL = props.getProperty("test.url");
127         configField = configurationFactoryClass.getDeclaredField("config");
128         configField.setAccessible(true);
129         SERVER_ID = "server1";
130     }
131
132     /**
133      * Use reflection to locate fields and methods so that they can be manipulated during the test
134      * to change the internal state accordingly.
135      * 
136      * @throws IllegalAccessException if this Field object is enforcing Java language access control
137      *         and the underlying field is either inaccessible or final.
138      * @throws IllegalArgumentException if the specified object is not an instance of the class or
139      *         interface declaring the underlying field (or a subclass or implementor thereof), or
140      *         if an unwrapping conversion fails.
141      */
142     @Before
143     public void setup() throws IllegalArgumentException, IllegalAccessException {
144         configField.set(null, null);
145         Properties properties = new Properties();
146         adapter = new ProviderAdapterImpl(properties);
147         svcContext = new SvcLogicContext();
148         params = new HashMap<>();
149         params.put(ProviderAdapter.PROPERTY_INSTANCE_URL, SERVER_URL);
150         params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, PROVIDER_NAME);
151         server = new Server();
152         server.setId(SERVER_ID);
153         image = new Image();
154         image.setStatus(Image.Status.ACTIVE);
155         stack = new Stack();
156         stack.setStatus(Stack.Status.ACTIVE);
157         Map<String, Object> privateFields = ImmutableMap.<String, Object>builder().put("factory", factory)
158                 .put("providerCache", getProviderCache()).build();
159         CommonUtility.injectMockObjects(privateFields, adapter);
160     }
161
162     private Map<String, ProviderCache> getProviderCache() {
163         providerCache = new HashMap<String, ProviderCache>();
164         ProviderCache cache = new ProviderCache();
165         cache.setIdentityURL(IDENTITY_URL);
166         cache.setProviderName(PROVIDER_NAME);
167         cache.setProviderType(PROVIDER_TYPE);
168         providerCache.put(cache.getIdentityURL(), cache);
169         return providerCache;
170     }
171
172     /**
173      * This test case is used to invoke the default constructor
174      * 
175      */
176     @Test
177     public void testDefaultConstructor() {
178         ProviderAdapter adapter = new ProviderAdapterImpl();
179         assertNotNull(adapter);
180     }
181
182     /**
183      * This test case is used to invoke the argument constructor
184      * 
185      */
186     @Test
187     public void testArgumentedConstructor() {
188         ProviderAdapter adapter = new ProviderAdapterImpl(true);
189         assertNotNull(adapter);
190     }
191
192     /**
193      * Tests that we get the Adapter name
194      */
195     @Test
196     public void testAdapterName() {
197         assertNotNull(adapter.getAdapterName());
198     }
199
200     /**
201      * This test case is used to restart the server
202      * 
203      * @throws ZoneException If the login cannot be performed because the principal and/or
204      *         credentials are invalid.
205      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
206      *         the expected argument(s) are not defined or are invalid
207      * @throws IllegalStateException If the identity service is not available or cannot be created
208      * @throws IOException if an I/O error occurs
209      * @throws APPCException If the server cannot be restarted for some reason
210      */
211     @Test
212     public void testRestartServer()
213             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
214         prepareMock(Operation.RESTART_SERVICE, Server.Status.RUNNING);
215         Server actualServer = adapter.restartServer(params, svcContext);
216         assertEquals(Server.Status.RUNNING, actualServer.getStatus());
217     }
218
219     /**
220      * This test case is used to start the server
221      * 
222      * @throws ZoneException If the login cannot be performed because the principal and/or
223      *         credentials are invalid.
224      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
225      *         the expected argument(s) are not defined or are invalid
226      * @throws IllegalStateException If the identity service is not available or cannot be created
227      * @throws IOException if an I/O error occurs
228      * @throws APPCException If the server cannot be started for some reason
229      */
230     @Test
231     public void testStartServer()
232             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
233         prepareMock(Operation.START_SERVICE, Server.Status.RUNNING);
234         Server actualServer = adapter.startServer(params, svcContext);
235         assertEquals(Server.Status.RUNNING, actualServer.getStatus());
236     }
237
238     /**
239      * This test case is used to stop the server
240      * 
241      * @throws ZoneException If the login cannot be performed because the principal and/or
242      *         credentials are invalid.
243      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
244      *         the expected argument(s) are not defined or are invalid
245      * @throws IllegalStateException If the identity service is not available or cannot be created
246      * @throws IOException if an I/O error occurs
247      * @throws APPCException If the server cannot be stopped for some reason
248      */
249     @Test
250     public void testStopServer()
251             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
252         prepareMock(Operation.STOP_SERVICE, Server.Status.READY);
253         Server actualServer = adapter.stopServer(params, svcContext);
254         assertEquals(Server.Status.READY, actualServer.getStatus());
255     }
256
257     /**
258      * Tests that the vmStatuschecker method works and returns the correct status of the VM
259      * requested
260      * 
261      * @throws ZoneException If the login cannot be performed because the principal and/or
262      *         credentials are invalid.
263      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
264      *         the expected argument(s) are not defined or are invalid
265      * @throws IllegalStateException If the identity service is not available or cannot be created
266      * @throws IOException if an I/O error occurs
267      * @throws APPCException If the vm status can not be verified
268      */
269     @Test
270     public void testVmStatuschecker()
271             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
272         prepareMock(Operation.VMSTATUSCHECK_SERVICE, Server.Status.READY);
273         Server actualServer = adapter.vmStatuschecker(params, svcContext);
274         assertEquals(Server.Status.READY, actualServer.getStatus());
275     }
276
277     /**
278      * Tests that the terminate stack method works
279      * 
280      * @throws ZoneException If the login cannot be performed because the principal and/or
281      *         credentials are invalid.
282      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
283      *         the expected argument(s) are not defined or are invalid
284      * @throws IllegalStateException If the identity service is not available or cannot be created
285      * @throws IOException if an I/O error occurs
286      * @throws APPCException If the stack cannot be terminated for some reason
287      */
288     @Test
289     public void testTerminateStack()
290             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
291         prepareMock(Operation.TERMINATE_STACK, null);
292         stack.setStatus(Stack.Status.DELETED);
293         Stack actualStack = adapter.terminateStack(params, svcContext);
294         assertEquals(Stack.Status.DELETED, actualStack.getStatus());
295     }
296
297     /**
298      * Tests that the snapshot method works and returns snapshot of the stack
299      * 
300      * @throws ZoneException If the login cannot be performed because the principal and/or
301      *         credentials are invalid.
302      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
303      *         the expected argument(s) are not defined or are invalid
304      * @throws IllegalStateException If the identity service is not available or cannot be created
305      * @throws IOException if an I/O error occurs
306      * @throws APPCException If the stack snapshot can not be taken for some reason
307      */
308     @Test
309     public void testSnapshotStack()
310             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
311         prepareMock(Operation.SNAPSHOT_STACK, null);
312         Stack actualStack = adapter.snapshotStack(params, svcContext);
313         assertEquals(Stack.Status.ACTIVE, actualStack.getStatus());
314     }
315
316     /**
317      * Tests that the restore method works and returns restored stack
318      * 
319      * @throws ZoneException If the login cannot be performed because the principal and/or
320      *         credentials are invalid.
321      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
322      *         the expected argument(s) are not defined or are invalid
323      * @throws IllegalStateException If the identity service is not available or cannot be created
324      * @throws IOException if an I/O error occurs
325      * @throws APPCException If the stack cannot be restored for some reason
326      */
327     @Test
328     public void testRestoreStack()
329             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
330         prepareMock(Operation.RESTORE_STACK, null);
331         Stack actualStack = adapter.restoreStack(params, svcContext);
332         assertEquals(Stack.Status.ACTIVE, actualStack.getStatus());
333     }
334
335     /**
336      * Tests that the lookup server will lookup for the server with server id
337      * 
338      * @throws ZoneException If the login cannot be performed because the principal and/or
339      *         credentials are invalid.
340      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
341      *         the expected argument(s) are not defined or are invalid
342      * @throws IllegalStateException If the identity service is not available or cannot be created
343      * @throws IOException if an I/O error occurs
344      * @throws APPCException If the server cannot be found for some reason
345      */
346     @Test
347     public void testLookupServer()
348             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
349         prepareMock(Operation.LOOKUP_SERVICE, Server.Status.READY);
350         Server actualServer = adapter.lookupServer(params, svcContext);
351         assertEquals(SERVER_ID, actualServer.getId());
352     }
353
354     /**
355      * Tests that the to create a snapshot and return a image
356      * 
357      * @throws ZoneException If the login cannot be performed because the principal and/or
358      *         credentials are invalid.
359      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
360      *         the expected argument(s) are not defined or are invalid
361      * @throws IllegalStateException If the identity service is not available or cannot be created
362      * @throws IOException if an I/O error occurs
363      * @throws APPCException If the image snapshot can not be taken for some reason
364      */
365     @Test
366     public void testCreateSnapshot()
367             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
368         prepareMock(Operation.SNAPSHOT_SERVICE, Server.Status.READY);
369         Image actualImage = adapter.createSnapshot(params, svcContext);
370         assertEquals(image.getStatus(), actualImage.getStatus());
371     }
372
373     /**
374      * Tests that the to calculate the server volume and attach it to the server
375      * 
376      * @throws ZoneException If the login cannot be performed because the principal and/or
377      *         credentials are invalid.
378      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
379      *         the expected argument(s) are not defined or are invalid
380      * @throws IllegalStateException If the identity service is not available or cannot be created
381      * @throws IOException if an I/O error occurs
382      * @throws APPCException If the Server volume can not be calculated for some reason
383      */
384     @Test
385     public void testAttachVolume()
386             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
387         prepareMock(Operation.ATTACHVOLUME_SERVICE, Server.Status.READY);
388         Server actualServer = adapter.attachVolume(params, svcContext);
389         assertEquals(SERVER_ID, actualServer.getId());
390     }
391
392     /**
393      * Tests that the to detach the calculated volume from the server
394      * 
395      * @throws ZoneException If the login cannot be performed because the principal and/or
396      *         credentials are invalid.
397      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
398      *         the expected argument(s) are not defined or are invalid
399      * @throws IllegalStateException If the identity service is not available or cannot be created
400      * @throws IOException if an I/O error occurs
401      * @throws APPCException If the Server volume can not be detached for some reason
402      */
403     @Test
404     public void testDettachVolume()
405             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
406         prepareMock(Operation.DETACHVOLUME_SERVICE, Server.Status.READY);
407         Server actualServer = adapter.dettachVolume(params, svcContext);
408         assertEquals(SERVER_ID, actualServer.getId());
409     }
410
411     /****************************************/
412
413
414     /**
415      * Tests that we can restart a server that is already stopped
416      * 
417      * @throws ZoneException If the login cannot be performed because the principal and/or
418      *         credentials are invalid.
419      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
420      *         the expected argument(s) are not defined or are invalid.
421      * @throws IllegalStateException If the identity service is not available or cannot be created
422      * @throws IOException if an I/O error occurs
423      * @throws APPCException If the server cannot be restarted for some reason
424      */
425     @Test
426     public void testRestartStoppedServer()
427             throws IllegalStateException, IllegalArgumentException, ZoneException, IOException, APPCException {
428         prepareMock(Operation.RESTART_SERVICE, Server.Status.RUNNING);
429         Server actualServer = adapter.restartServer(params, svcContext);
430         assertEquals(Server.Status.RUNNING, actualServer.getStatus());
431
432     }
433
434     /**
435      * Tests that we can rebuild a server (not created from a bootable volume)
436      * 
437      * @throws ZoneException If the login cannot be performed because the principal and/or
438      *         credentials are invalid.
439      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
440      *         the expected argument(s) are not defined or are invalid.
441      * @throws IllegalStateException If the identity service is not available or cannot be created
442      * @throws UnknownProviderException If the provider cannot be found
443      * @throws IOException if an I/O error occurs
444      * @throws APPCException If the server cannot be rebuilt for some reason
445      */
446     @Test
447     public void testRebuildServer()
448             throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
449         prepareMock(Operation.REBUILD_SERVICE, Server.Status.READY);
450         Server actualServer = adapter.rebuildServer(params, svcContext);
451         assertEquals(Server.Status.READY, actualServer.getStatus());
452     }
453
454     /**
455      * Tests that we can terminate a running server
456      * 
457      * @throws ZoneException If the login cannot be performed because the principal and/or
458      *         credentials are invalid.
459      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
460      *         the expected argument(s) are not defined or are invalid.
461      * @throws IllegalStateException If the identity service is not available or cannot be created
462      * @throws UnknownProviderException If the provider cannot be found
463      * @throws IOException if an I/O error occurs
464      * @throws APPCException If the server cannot be terminated for some reason
465      */
466     @Test
467     public void testTerminateServer()
468             throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
469         prepareMock(Operation.TERMINATE_SERVICE, Server.Status.DELETED);
470         Server actualServer = adapter.terminateServer(params, svcContext);
471         assertEquals(Server.Status.DELETED, actualServer.getStatus());
472     }
473
474     /**
475      * Tests that we can evacuate a server to move it to non-pending state
476      * 
477      * @throws ZoneException If the login cannot be performed because the principal and/or
478      *         credentials are invalid.
479      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
480      *         the expected argument(s) are not defined or are invalid.
481      * @throws IllegalStateException If the identity service is not available or cannot be created
482      * @throws UnknownProviderException If the provider cannot be found
483      * @throws IOException if an I/O error occurs
484      * @throws APPCException If the server cannot be evacuated for some reason
485      */
486     @Test
487     public void testEvacuateServer()
488             throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
489         prepareMock(Operation.EVACUATE_SERVICE, Server.Status.READY);
490         Server actualServer = adapter.evacuateServer(params, svcContext);
491         assertEquals(Server.Status.READY, actualServer.getStatus());
492     }
493
494     /**
495      * Tests that we can migrate a server. Migration can be done only on certain statuses like
496      * READY, RUNNING & SUSPENDED
497      * 
498      * @throws ZoneException If the login cannot be performed because the principal and/or
499      *         credentials are invalid.
500      * @throws IllegalArgumentException If the principal and/or credential are null or empty, or if
501      *         the expected argument(s) are not defined or are invalid.
502      * @throws IllegalStateException If the identity service is not available or cannot be created
503      * @throws UnknownProviderException If the provider cannot be found
504      * @throws IOException if an I/O error occurs
505      * @throws APPCException If the server cannot be migrated for some reason
506      */
507     @Test
508     public void testMigrateServer()
509             throws IOException, IllegalStateException, IllegalArgumentException, ZoneException, APPCException {
510         prepareMock(Operation.MIGRATE_SERVICE, Server.Status.READY);
511         Server actualServer = adapter.migrateServer(params, svcContext);
512         assertEquals(Server.Status.READY, actualServer.getStatus());
513     }
514
515     private void prepareMock(Operation operation, Server.Status serverStatus) throws APPCException {
516         IProviderOperation providerOperation = fetchOperation.apply(operation);
517         ModelObject modelObject = fetchModelObject.apply(operation, serverStatus);
518         when(factory.getOperationObject(operation)).thenReturn(providerOperation);
519         when(providerOperation.doOperation(anyObject(), anyObject())).thenReturn(modelObject);
520
521     }
522
523     Function<Operation, IProviderOperation> fetchOperation = operation -> {
524         if (operation.equals(Operation.EVACUATE_SERVICE))
525             return evacuateServer;
526         else
527             return providerOperation;
528     };
529
530     Function<Server.Status, Server> fetchServer = status -> {
531         server.setStatus(status);
532         return server;
533     };
534
535     BiFunction<Operation, Server.Status, ModelObject> fetchModelObject = (operation, status) -> {
536         if (operation.equals(Operation.SNAPSHOT_SERVICE))
537             return image;
538         else if (operation == Operation.RESTORE_STACK || operation == Operation.SNAPSHOT_STACK
539                 || operation == Operation.TERMINATE_STACK)
540             return stack;
541         else
542             return fetchServer.apply(status);
543     };
544 }