8e1b4da9e68cf6186b8edb6526d0256ee2586066
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfigReaderTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
9
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;
14
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.MockitoAnnotations;
22
23 public class EntityOwnerSelectionStrategyConfigReaderTest {
24
25     @Before
26     public void setup() throws IOException {
27         MockitoAnnotations.initMocks(this);
28     }
29
30     @Test
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");
35
36
37         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
38                 .loadStrategyWithConfig(props);
39
40         assertTrue(config.isStrategyConfigured("test"));
41
42         final EntityOwnerSelectionStrategy strategy = config.createStrategy("test",
43                 Collections.<String, Long>emptyMap());
44         assertTrue(strategy.toString(), strategy instanceof LastCandidateSelectionStrategy);
45         assertEquals(100L, strategy.getSelectionDelayInMillis());
46
47         final EntityOwnerSelectionStrategy strategy1 = config.createStrategy("test", Collections.emptyMap());
48         assertEquals(strategy, strategy1);
49
50         config.clearStrategies();
51
52         final EntityOwnerSelectionStrategy strategy2 = config.createStrategy("test", Collections.emptyMap());
53         assertNotEquals(strategy1, strategy2);
54     }
55
56     @Test
57     public void testReadStrategiesWithEmptyConfiguration() {
58
59         final Map<Object, Object> props = new HashMap<>();
60         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
61                 .loadStrategyWithConfig(props);
62
63         assertFalse(config.isStrategyConfigured("test"));
64     }
65
66     @Test
67     public void testReadStrategiesWithNullConfiguration() {
68         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
69                 .loadStrategyWithConfig(null);
70         assertFalse(config.isStrategyConfigured("test"));
71     }
72
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);
79     }
80
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);
86     }
87
88     @Test
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");
95
96
97         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
98                 .loadStrategyWithConfig(props);
99
100         assertEquals(100, config.createStrategy("test", Collections.emptyMap()).getSelectionDelayInMillis());
101         assertEquals(0, config.createStrategy("test2", Collections.emptyMap()).getSelectionDelayInMillis());
102     }
103 }