Remove unused exceptions
[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.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.MockitoAnnotations;
21
22 public class EntityOwnerSelectionStrategyConfigReaderTest {
23
24     @Before
25     public void setup() {
26         MockitoAnnotations.initMocks(this);
27     }
28
29     @Test
30     public void testReadStrategies() {
31         final Map<Object, Object> props = new java.util.HashMap<>();
32         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
33                 + "selectionstrategy.LastCandidateSelectionStrategy,100");
34
35
36         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
37                 .loadStrategyWithConfig(props);
38
39         assertTrue(config.isStrategyConfigured("test"));
40
41         final EntityOwnerSelectionStrategy strategy = config.createStrategy("test",
42                 Collections.<String, Long>emptyMap());
43         assertTrue(strategy.toString(), strategy instanceof LastCandidateSelectionStrategy);
44         assertEquals(100L, strategy.getSelectionDelayInMillis());
45
46         final EntityOwnerSelectionStrategy strategy1 = config.createStrategy("test", Collections.emptyMap());
47         assertEquals(strategy, strategy1);
48
49         config.clearStrategies();
50
51         final EntityOwnerSelectionStrategy strategy2 = config.createStrategy("test", Collections.emptyMap());
52         assertNotEquals(strategy1, strategy2);
53     }
54
55     @Test
56     public void testReadStrategiesWithEmptyConfiguration() {
57
58         final Map<Object, Object> props = new HashMap<>();
59         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
60                 .loadStrategyWithConfig(props);
61
62         assertFalse(config.isStrategyConfigured("test"));
63     }
64
65     @Test
66     public void testReadStrategiesWithNullConfiguration() {
67         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
68                 .loadStrategyWithConfig(null);
69         assertFalse(config.isStrategyConfigured("test"));
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void testReadStrategiesInvalidDelay() {
74         final Map<Object, Object> props = new HashMap<>();
75         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
76                 + "selectionstrategy.LastCandidateSelectionStrategy,foo");
77         EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void testReadStrategiesInvalidClassType() {
82         final Map<Object, Object> props = new HashMap<>();
83         props.put("entity.type.test", "String,100");
84         EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
85     }
86
87     @Test
88     public void testReadStrategiesMissingDelay() {
89         final Map<Object, Object> props = new HashMap<>();
90         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
91                 + "selectionstrategy.LastCandidateSelectionStrategy,100");
92         props.put("entity.type.test1", "org.opendaylight.controller.cluster.datastore.entityownership."
93                 + "selectionstrategy.LastCandidateSelectionStrategy");
94
95
96         final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
97                 .loadStrategyWithConfig(props);
98
99         assertEquals(100, config.createStrategy("test", Collections.emptyMap()).getSelectionDelayInMillis());
100         assertEquals(0, config.createStrategy("test2", Collections.emptyMap()).getSelectionDelayInMillis());
101     }
102 }