2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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;
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;
16 import java.lang.reflect.Field;
17 import java.util.HashMap;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.DatastoreContextPropertiesUpdater.Listener;
22 public class DatastoreContextContextPropertiesUpdaterTest {
24 @SuppressWarnings("unchecked")
26 public void updateOnConstructionTest() throws Exception {
27 final Map<String, Object> properties = new HashMap<>();
28 properties.put("shardTransactionIdleTimeoutInMinutes", 10);
30 final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
32 final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
34 assertNotNull(updater);
36 final Map<String, Object> props = (Map<String, Object>) resolveField("currentProperties", introspector);
38 assertEquals(props.get("shardTransactionIdleTimeoutInMinutes"), 10);
41 @SuppressWarnings("unchecked")
43 public void onUpdateTest() throws Exception {
44 final Map<String, Object> properties = new HashMap<>();
45 properties.put("shardTransactionIdleTimeoutInMinutes", 10);
47 final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
48 assertNotNull(introspector);
49 final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
51 assertNotNull(updater);
53 Map<String, Object> props = (Map<String, Object>) resolveField("currentProperties", introspector);
54 assertTrue(!props.isEmpty());
56 properties.put("shardTransactionIdleTimeoutInMinutes", 20);
57 updater.update(properties);
59 props = (Map<String, Object>) resolveField("currentProperties", introspector);
60 assertEquals(props.get("shardTransactionIdleTimeoutInMinutes"), 20);
63 @SuppressWarnings("resource")
65 public void listenerTest() {
66 final Map<String, Object> properties = new HashMap<>();
67 properties.put("shardTransactionIdleTimeoutInMinutes", 10);
69 final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
70 final DatastoreContextPropertiesUpdater updater = new DatastoreContextPropertiesUpdater(introspector,
72 final DummyListenerImpl dummyListener = new DummyListenerImpl();
73 updater.setListener(dummyListener);
75 assertTrue(dummyListener.getContextFactory() == null);
76 updater.setListener(dummyListener);
77 properties.put("shardTransactionIdleTimeoutInMinutes", 20);
78 updater.update(properties);
80 final DatastoreContextFactory contextFactory = dummyListener.getContextFactory();
81 assertNotNull(contextFactory);
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);
91 private class DummyListenerImpl implements Listener {
93 private DatastoreContextFactory contextFactory;
96 @SuppressWarnings("checkstyle:hiddenField")
97 public void onDatastoreContextUpdated(final DatastoreContextFactory contextFactory) {
98 this.contextFactory = contextFactory;
101 public DatastoreContextFactory getContextFactory() {
102 return contextFactory;