Allow incremental recovery
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreContextContextPropertiesUpdaterTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospectorTest.INTROSPECTOR_FACTORY;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
15
16 import java.lang.reflect.Field;
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.DatastoreContextPropertiesUpdater.Listener;
21
22 public class DatastoreContextContextPropertiesUpdaterTest {
23
24     @SuppressWarnings("unchecked")
25     @Test
26     public void updateOnConstructionTest() throws Exception {
27         final Map<String, Object> properties = new HashMap<>();
28         properties.put("shardTransactionIdleTimeoutInMinutes", 10);
29
30         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
31
32         final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
33                 properties);
34         assertNotNull(updater);
35
36         final Map<String, Object> props = (Map<String, Object>) resolveField("currentProperties", introspector);
37
38         assertEquals(props.get("shardTransactionIdleTimeoutInMinutes"), 10);
39     }
40
41     @SuppressWarnings("unchecked")
42     @Test
43     public void onUpdateTest() throws Exception {
44         final Map<String, Object> properties = new HashMap<>();
45         properties.put("shardTransactionIdleTimeoutInMinutes", 10);
46
47         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
48         assertNotNull(introspector);
49         final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
50                 properties);
51         assertNotNull(updater);
52
53         Map<String, Object> props = (Map<String, Object>) resolveField("currentProperties", introspector);
54         assertTrue(!props.isEmpty());
55
56         properties.put("shardTransactionIdleTimeoutInMinutes", 20);
57         updater.update(properties);
58
59         props = (Map<String, Object>) resolveField("currentProperties", introspector);
60         assertEquals(props.get("shardTransactionIdleTimeoutInMinutes"), 20);
61     }
62
63     @SuppressWarnings("resource")
64     @Test
65     public void listenerTest() {
66         final Map<String, Object> properties = new HashMap<>();
67         properties.put("shardTransactionIdleTimeoutInMinutes", 10);
68
69         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
70         final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
71                 properties);
72         final DummyListenerImpl dummyListener = new DummyListenerImpl();
73         updater.setListener(dummyListener);
74
75         assertTrue(dummyListener.getContextFactory() == null);
76         updater.setListener(dummyListener);
77         properties.put("shardTransactionIdleTimeoutInMinutes", 20);
78         updater.update(properties);
79
80         final DatastoreContextFactory contextFactory = dummyListener.getContextFactory();
81         assertNotNull(contextFactory);
82         updater.close();
83     }
84
85     private static Object resolveField(final String name, final Object obj) throws Exception {
86         final Field currProps = obj.getClass().getDeclaredField(name);
87         currProps.setAccessible(true);
88         return currProps.get(obj);
89     }
90
91     private class DummyListenerImpl implements Listener {
92
93         private DatastoreContextFactory contextFactory;
94
95         @Override
96         @SuppressWarnings("checkstyle:hiddenField")
97         public void onDatastoreContextUpdated(final DatastoreContextFactory contextFactory) {
98             this.contextFactory = contextFactory;
99         }
100
101         public DatastoreContextFactory getContextFactory() {
102             return contextFactory;
103         }
104     }
105 }