2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.impl;
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;
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;
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.
64 @RunWith(MockitoJUnitRunner.class)
65 @Category(org.onap.appc.adapter.iaas.impl.TestProviderAdapterImpl.class)
66 public class TestProviderAdapterImpl {
68 @SuppressWarnings("nls")
69 private static final String PROVIDER_NAME = "ILAB";
71 @SuppressWarnings("nls")
72 private static final String PROVIDER_TYPE = "OpenStackProvider";
74 private static String IDENTITY_URL;
76 private static String SERVER_URL;
78 private static String SERVER_ID;
80 private static Class<?> providerAdapterImplClass;
81 private static Class<?> configurationFactoryClass;
82 private static Field providerCacheField;
83 private static Field configField;
85 private static ProviderAdapterImpl adapter;
88 ProviderOperationFactory factory;
91 IProviderOperation providerOperation;
94 EvacuateServer evacuateServer;
96 private Map<String, ProviderCache> providerCache;
98 private SvcLogicContext svcContext;
100 private Map<String, String> params;
104 private Server server;
109 * Use reflection to locate fields and methods so that they can be manipulated during the test
110 * to change the internal state accordingly.
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
116 @SuppressWarnings("nls")
118 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
119 providerAdapterImplClass = ProviderAdapterImpl.class;
120 configurationFactoryClass = ConfigurationFactory.class;
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";
133 * Use reflection to locate fields and methods so that they can be manipulated during the test
134 * to change the internal state accordingly.
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.
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);
154 image.setStatus(Image.Status.ACTIVE);
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);
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;
173 * This test case is used to invoke the default constructor
177 public void testDefaultConstructor() {
178 ProviderAdapter adapter = new ProviderAdapterImpl();
179 assertNotNull(adapter);
183 * This test case is used to invoke the argument constructor
187 public void testArgumentedConstructor() {
188 ProviderAdapter adapter = new ProviderAdapterImpl(true);
189 assertNotNull(adapter);
193 * Tests that we get the Adapter name
196 public void testAdapterName() {
197 assertNotNull(adapter.getAdapterName());
201 * This test case is used to restart the server
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
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());
220 * This test case is used to start the server
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
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());
239 * This test case is used to stop the server
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
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());
258 * Tests that the vmStatuschecker method works and returns the correct status of the VM
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
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());
278 * Tests that the terminate stack method works
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
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());
298 * Tests that the snapshot method works and returns snapshot of the stack
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
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());
317 * Tests that the restore method works and returns restored stack
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
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());
336 * Tests that the lookup server will lookup for the server with server id
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
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());
355 * Tests that the to create a snapshot and return a image
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
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());
374 * Tests that the to calculate the server volume and attach it to the server
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
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());
393 * Tests that the to detach the calculated volume from the server
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
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());
411 /****************************************/
415 * Tests that we can restart a server that is already stopped
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
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());
435 * Tests that we can rebuild a server (not created from a bootable volume)
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
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());
455 * Tests that we can terminate a running server
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
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());
475 * Tests that we can evacuate a server to move it to non-pending state
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
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());
495 * Tests that we can migrate a server. Migration can be done only on certain statuses like
496 * READY, RUNNING & SUSPENDED
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
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());
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);
523 Function<Operation, IProviderOperation> fetchOperation = operation -> {
524 if (operation.equals(Operation.EVACUATE_SERVICE))
525 return evacuateServer;
527 return providerOperation;
530 Function<Server.Status, Server> fetchServer = status -> {
531 server.setStatus(status);
535 BiFunction<Operation, Server.Status, ModelObject> fetchModelObject = (operation, status) -> {
536 if (operation.equals(Operation.SNAPSHOT_SERVICE))
538 else if (operation == Operation.RESTORE_STACK || operation == Operation.SNAPSHOT_STACK
539 || operation == Operation.TERMINATE_STACK)
542 return fetchServer.apply(status);