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.assertNotEquals;
13 import static org.junit.Assert.assertTrue;
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.HashMap;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.MockitoAnnotations;
23 public class EntityOwnerSelectionStrategyConfigReaderTest {
26 public void setup() throws IOException {
27 MockitoAnnotations.initMocks(this);
31 public void testReadStrategies() {
32 final Map<Object, Object> props = new java.util.HashMap<>();
33 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
34 + "selectionstrategy.LastCandidateSelectionStrategy,100");
37 final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
38 .loadStrategyWithConfig(props);
40 assertTrue(config.isStrategyConfigured("test"));
42 final EntityOwnerSelectionStrategy strategy = config.createStrategy("test",
43 Collections.<String, Long>emptyMap());
44 assertTrue(strategy.toString(), strategy instanceof LastCandidateSelectionStrategy);
45 assertEquals(100L, strategy.getSelectionDelayInMillis());
47 final EntityOwnerSelectionStrategy strategy1 = config.createStrategy("test", Collections.emptyMap());
48 assertEquals(strategy, strategy1);
50 config.clearStrategies();
52 final EntityOwnerSelectionStrategy strategy2 = config.createStrategy("test", Collections.emptyMap());
53 assertNotEquals(strategy1, strategy2);
57 public void testReadStrategiesWithEmptyConfiguration() {
59 final Map<Object, Object> props = new HashMap<>();
60 final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
61 .loadStrategyWithConfig(props);
63 assertFalse(config.isStrategyConfigured("test"));
67 public void testReadStrategiesWithNullConfiguration() {
68 final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
69 .loadStrategyWithConfig(null);
70 assertFalse(config.isStrategyConfigured("test"));
73 @Test(expected = IllegalArgumentException.class)
74 public void testReadStrategiesInvalidDelay() {
75 final Map<Object, Object> props = new HashMap<>();
76 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
77 + "selectionstrategy.LastCandidateSelectionStrategy,foo");
78 EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
81 @Test(expected = IllegalArgumentException.class)
82 public void testReadStrategiesInvalidClassType() {
83 final Map<Object, Object> props = new HashMap<>();
84 props.put("entity.type.test", "String,100");
85 EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
89 public void testReadStrategiesMissingDelay() {
90 final Map<Object, Object> props = new HashMap<>();
91 props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
92 + "selectionstrategy.LastCandidateSelectionStrategy,100");
93 props.put("entity.type.test1", "org.opendaylight.controller.cluster.datastore.entityownership."
94 + "selectionstrategy.LastCandidateSelectionStrategy");
97 final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
98 .loadStrategyWithConfig(props);
100 assertEquals(100, config.createStrategy("test", Collections.emptyMap()).getSelectionDelayInMillis());
101 assertEquals(0, config.createStrategy("test2", Collections.emptyMap()).getSelectionDelayInMillis());