2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.doThrow;
15 import java.io.IOException;
16 import java.util.Hashtable;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23 import org.osgi.service.cm.Configuration;
24 import org.osgi.service.cm.ConfigurationAdmin;
26 public class EntityOwnerSelectionStrategyConfigReaderTest {
28 private BundleContext mockBundleContext;
31 private ServiceReference<ConfigurationAdmin> mockConfigAdminServiceRef;
34 private ConfigurationAdmin mockConfigAdmin;
37 private Configuration mockConfig;
40 public void setup() throws IOException {
41 MockitoAnnotations.initMocks(this);
43 doReturn(mockConfigAdminServiceRef).when(mockBundleContext).getServiceReference(ConfigurationAdmin.class);
44 doReturn(mockConfigAdmin).when(mockBundleContext).getService(mockConfigAdminServiceRef);
46 doReturn(mockConfig).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
50 public void testReadStrategies(){
51 Hashtable<String, Object> props = new Hashtable<>();
52 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
54 doReturn(props).when(mockConfig).getProperties();
56 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
58 assertTrue(config.isStrategyConfigured("test"));
60 EntityOwnerSelectionStrategy strategy = config.createStrategy("test");
61 assertTrue(strategy instanceof LastCandidateSelectionStrategy);
62 assertEquals(100L, strategy.getSelectionDelayInMillis());
66 public void testReadStrategiesWithIOException() throws IOException {
67 doThrow(IOException.class).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
69 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
71 assertFalse(config.isStrategyConfigured("test"));
75 public void testReadStrategiesWithNullConfiguration() throws IOException {
76 doReturn(null).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
78 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
80 assertFalse(config.isStrategyConfigured("test"));
84 public void testReadStrategiesWithNullConfigurationProperties() throws IOException {
85 doReturn(null).when(mockConfig).getProperties();
87 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
89 assertFalse(config.isStrategyConfigured("test"));
93 public void testReadStrategiesInvalidDelay(){
94 Hashtable<String, Object> props = new Hashtable<>();
95 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,foo");
97 doReturn(props).when(mockConfig).getProperties();
99 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
101 assertFalse(config.isStrategyConfigured("test"));
105 public void testReadStrategiesInvalidClassType(){
106 Hashtable<String, Object> props = new Hashtable<>();
107 props.put("entity.type.test", "String,100");
109 doReturn(props).when(mockConfig).getProperties();
111 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
113 assertFalse(config.isStrategyConfigured("test"));
117 public void testReadStrategiesMissingDelay(){
118 Hashtable<String, Object> props = new Hashtable<>();
119 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
120 props.put("entity.type.test1", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy");
122 doReturn(props).when(mockConfig).getProperties();
124 EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
126 assertEquals(100, config.createStrategy("test").getSelectionDelayInMillis());
127 assertEquals(0, config.createStrategy("test2").getSelectionDelayInMillis());