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