Align tested boolean/Boolean expectations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreContextIntrospectorTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS;
16 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_OPERATION_TIMEOUT_IN_MS;
17 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
18 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE;
19 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
20 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
21 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
22 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
23
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Hashtable;
27 import java.util.Map;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
30 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
31 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStoreConfigProperties;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStorePropertiesContainer;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 /**
38  * Unit tests for DatastoreContextIntrospector.
39  *
40  * @author Thomas Pantelis
41  */
42 @SuppressWarnings("checkstyle:IllegalCatch")
43 public class DatastoreContextIntrospectorTest {
44
45     static SchemaContext SCHEMA_CONTEXT;
46     static DatastoreContextIntrospectorFactory INTROSPECTOR_FACTORY;
47
48     static {
49         final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
50         try {
51             moduleContext.addModuleInfos(Arrays.asList(
52                     BindingReflections.getModuleInfo(DataStorePropertiesContainer.class)));
53         } catch (Exception e) {
54             throw new RuntimeException(e);
55         }
56         SCHEMA_CONTEXT = moduleContext.tryToCreateSchemaContext().get();
57
58         DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
59         doReturn(SCHEMA_CONTEXT).when(mockSchemaService).getGlobalContext();
60         INTROSPECTOR_FACTORY = new DatastoreContextIntrospectorFactory(mockSchemaService,
61                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
62     }
63
64     @Test
65     public void testYangDefaults() {
66         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(
67                 DatastoreContext.newBuilder().shardBatchedModificationCount(2)
68                 .transactionDebugContextEnabled(true).build());
69         DatastoreContext context = introspector.getContext();
70
71         assertEquals(1000, context.getShardBatchedModificationCount());
72         assertFalse(context.isTransactionDebugContextEnabled());
73     }
74
75     @Test
76     public void testUpdate() {
77         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
78
79         final Map<String, Object> properties = new HashMap<>();
80         properties.put("shard-transaction-idle-timeout-in-minutes", "31");
81         properties.put("operation-timeout-in-seconds", "26");
82         properties.put("shard-transaction-commit-timeout-in-seconds", "100");
83         properties.put("shard-journal-recovery-log-batch-size", "199");
84         properties.put("shard-snapshot-batch-count", "212");
85         properties.put("shard-heartbeat-interval-in-millis", "101");
86         properties.put("shard-transaction-commit-queue-capacity", "567");
87         properties.put("shard-initialization-timeout-in-seconds", "82");
88         properties.put("shard-leader-election-timeout-in-seconds", "66");
89         properties.put("shard-isolated-leader-check-interval-in-millis", "123");
90         properties.put("shard-snapshot-data-threshold-percentage", "100");
91         properties.put("shard-election-timeout-factor", "21");
92         properties.put("shard-batched-modification-count", "901");
93         properties.put("transactionCreationInitialRateLimit", "200");
94         properties.put("MaxShardDataChangeExecutorPoolSize", "41");
95         properties.put("Max-Shard-Data-Change Executor-Queue Size", "1111");
96         properties.put(" max shard data change listener queue size", "2222");
97         properties.put("mAx-shaRd-data-STORE-executor-quEUe-size", "3333");
98         properties.put("persistent", "false");
99
100         boolean updated = introspector.update(properties);
101         assertTrue("updated", updated);
102         DatastoreContext context = introspector.getContext();
103
104         assertEquals(31, context.getShardTransactionIdleTimeout().toMinutes());
105         assertEquals(26000, context.getOperationTimeoutInMillis());
106         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
107         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
108         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
109         assertEquals(101, context.getShardRaftConfig().getHeartBeatInterval().length());
110         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
111         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
112         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
113         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
114         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
115         assertEquals(21, context.getShardRaftConfig().getElectionTimeoutFactor());
116         assertEquals(901, context.getShardBatchedModificationCount());
117         assertEquals(200, context.getTransactionCreationInitialRateLimit());
118         assertEquals(41, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
119         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
120         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
121         assertEquals(3333, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
122         assertFalse(context.isPersistent());
123
124         properties.put("shard-transaction-idle-timeout-in-minutes", "32");
125         properties.put("operation-timeout-in-seconds", "27");
126         properties.put("shard-heartbeat-interval-in-millis", "102");
127         properties.put("shard-election-timeout-factor", "22");
128         properties.put("max-shard-data-change-executor-pool-size", "42");
129         properties.put("max-shard-data-store-executor-queue-size", "4444");
130         properties.put("persistent", "true");
131
132         updated = introspector.update(properties);
133         assertTrue("updated", updated);
134         context = introspector.getContext();
135
136         assertEquals(32, context.getShardTransactionIdleTimeout().toMinutes());
137         assertEquals(27000, context.getOperationTimeoutInMillis());
138         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
139         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
140         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
141         assertEquals(102, context.getShardRaftConfig().getHeartBeatInterval().length());
142         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
143         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
144         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
145         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
146         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
147         assertEquals(22, context.getShardRaftConfig().getElectionTimeoutFactor());
148         assertEquals(200, context.getTransactionCreationInitialRateLimit());
149         assertEquals(42, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
150         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
151         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
152         assertEquals(4444, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
153         assertTrue(context.isPersistent());
154
155         updated = introspector.update(null);
156         assertFalse("updated", updated);
157
158         updated = introspector.update(new Hashtable<>());
159         assertFalse("updated", updated);
160     }
161
162
163     @Test
164     public void testUpdateWithInvalidValues() {
165         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
166
167         final Map<String, Object> properties = new HashMap<>();
168         properties.put("shard-transaction-idle-timeout-in-minutes", "0"); // bad - must be > 0
169         properties.put("shard-journal-recovery-log-batch-size", "199");
170         properties.put("shard-transaction-commit-timeout-in-seconds", "bogus"); // bad - NaN
171         properties.put("shard-snapshot-batch-count", "212"); // good
172         properties.put("operation-timeout-in-seconds", "4"); // bad - must be >= 5
173         properties.put("shard-heartbeat-interval-in-millis", "99"); // bad - must be >= 100
174         properties.put("shard-transaction-commit-queue-capacity", "567"); // good
175         properties.put("shard-snapshot-data-threshold-percentage", "101"); // bad - must be 0-100
176         properties.put("shard-initialization-timeout-in-seconds", "-1"); // bad - must be > 0
177         properties.put("max-shard-data-change-executor-pool-size", "bogus"); // bad - NaN
178         properties.put("unknownProperty", "1"); // bad - invalid property name
179
180         final boolean updated = introspector.update(properties);
181         assertTrue("updated", updated);
182         DatastoreContext context = introspector.getContext();
183
184         assertEquals(DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT, context.getShardTransactionIdleTimeout());
185         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
186         assertEquals(DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS, context.getShardTransactionCommitTimeoutInSeconds());
187         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
188         assertEquals(DEFAULT_OPERATION_TIMEOUT_IN_MS, context.getOperationTimeoutInMillis());
189         assertEquals(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS,
190                 context.getShardRaftConfig().getHeartBeatInterval().length());
191         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
192         assertEquals(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE,
193                 context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
194         assertEquals(DEFAULT_SHARD_INITIALIZATION_TIMEOUT, context.getShardInitializationTimeout());
195         assertEquals(InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
196                 context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
197     }
198
199     @Test
200     public void testUpdateWithDatastoreTypeSpecificProperties() {
201         final Map<String, Object> properties = new HashMap<>();
202         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
203         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
204         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
205
206         properties.put("max-shard-data-change-executor-pool-size", "222"); // global setting
207         properties.put("operational.max-shard-data-change-executor-pool-size", "333"); // operational override
208         properties.put("config.max-shard-data-change-executor-pool-size", "444"); // config override
209
210         properties.put("persistent", "false"); // global setting
211         properties.put("operational.Persistent", "true"); // operational override
212
213         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
214         boolean updated = operIntrospector.update(properties);
215         assertTrue("updated", updated);
216         DatastoreContext operContext = operIntrospector.getContext();
217
218         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
219         assertTrue(operContext.isPersistent());
220         assertEquals(333, operContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
221
222         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
223         updated = configIntrospector.update(properties);
224         assertTrue("updated", updated);
225         DatastoreContext configContext = configIntrospector.getContext();
226
227         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
228         assertFalse(configContext.isPersistent());
229         assertEquals(444, configContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
230     }
231
232     @Test
233     public void testGetDatastoreContextForShard() {
234         final Map<String, Object> properties = new HashMap<>();
235         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
236         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
237         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
238         properties.put("topology.shard-transaction-idle-timeout-in-minutes", "55"); // global shard override
239
240         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
241
242         DatastoreContext shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
243         assertEquals(10, shardContext.getShardTransactionIdleTimeout().toMinutes());
244
245         operIntrospector.update(properties);
246         DatastoreContext operContext = operIntrospector.getContext();
247         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
248
249         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
250         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
251
252         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
253         configIntrospector.update(properties);
254         DatastoreContext configContext = configIntrospector.getContext();
255         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
256
257         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
258         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
259
260         // operational shard override
261         properties.put("operational.topology.shard-transaction-idle-timeout-in-minutes", "66");
262         // config shard override
263         properties.put("config.topology.shard-transaction-idle-timeout-in-minutes", "77");
264
265         operIntrospector.update(properties);
266         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
267         assertEquals(66, shardContext.getShardTransactionIdleTimeout().toMinutes());
268
269         configIntrospector.update(properties);
270         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
271         assertEquals(77, shardContext.getShardTransactionIdleTimeout().toMinutes());
272
273         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("default");
274         assertEquals(44, shardContext.getShardTransactionIdleTimeout().toMinutes());
275     }
276 }