Allow programmatic module sharding configuration
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / config / ConfigurationImplHybridTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.config;
9
10 import com.google.common.collect.ImmutableList;
11 import com.typesafe.config.Config;
12 import com.typesafe.config.ConfigFactory;
13 import java.util.List;
14 import org.junit.Test;
15
16 public class ConfigurationImplHybridTest extends ConfigurationImplBaseTest {
17
18     @Override
19     public ConfigurationImpl createConfiguration() {
20         Config moduleShardsConf = generateModuleShards(ImmutableList.of(
21                 generateShard("default", "default", ImmutableList.of("member-1", "member-2", "member-3")),
22                 generateShard("people", "people-1", ImmutableList.of("member-1")),
23                 generateShard("cars", "cars-1", ImmutableList.of("member-1")),
24                 generateShard("test", "test-1", ImmutableList.of("member-1"))
25         ));
26         return new ConfigurationImpl(new HybridModuleShardConfigProvider(moduleShardsConf, "modules.conf"));
27     }
28
29     @Test(expected = NullPointerException.class)
30     public void testNullModuleShardsConf() {
31         new HybridModuleShardConfigProvider(null, "modules.conf");
32     }
33
34     private static Config generateModuleShards(final List<String> shards) {
35         String moduleShardsContent = String.format("module-shards = [%n%s]", String.join(",\n", shards));
36         return ConfigFactory.parseString(moduleShardsContent);
37     }
38
39     private static String generateShard(final String name, final String shardsName, final List<String> replicas) {
40         return "    {"
41                 + "        name = \"" + name + "\"\n"
42                 + "        shards = [\n"
43                 + "            {\n"
44                 + "                name=\"" + shardsName + "\"\n"
45                 + "                replicas = " + replicas
46                 + "                \n"
47                 + "            }\n"
48                 + "        ]\n"
49                 + "    }";
50     }
51 }