use of com.linkedin.d2.PartitionAccessorList in project rest.li by linkedin.
the class TestD2Config method testPartitionAccessorFactoryWithEmptyClassList.
// Test PartitionAccessorFactory: empty ClassList
@Test
public static void testPartitionAccessorFactoryWithEmptyClassList() throws IOException, InterruptedException, URISyntaxException, Exception {
@SuppressWarnings("serial") final Map<String, List<String>> clustersData = new HashMap<String, List<String>>() {
{
put("partitioned-cluster", Arrays.asList(new String[] { "partitioned-service-1", "partitioned-service-2" }));
}
};
final Map<String, Object> partitionProperties = new HashMap<>();
Map<String, Object> customized = new HashMap<>();
List<String> classList = Collections.emptyList();
customized.put("partitionType", "CUSTOM");
customized.put("partitionCount", "10");
customized.put("partitionAccessorList", classList);
partitionProperties.put("partitionProperties", customized);
final PartitionAccessorRegistry registry = new PartitionAccessorRegistry() {
private final Map<String, List<BasePartitionAccessor>> _registry = new HashMap<>();
@Override
public void register(String clusterName, BasePartitionAccessor accessor) {
List<BasePartitionAccessor> accessors = _registry.computeIfAbsent(clusterName, k -> new ArrayList<>());
accessors.add(accessor);
}
@Override
public List<BasePartitionAccessor> getPartitionAccessors(String clusterName) {
return _registry.get(clusterName);
}
};
class TestPartitionAccessor1 implements PartitionAccessor {
@Override
public int getPartitionId(URI uri) throws PartitionAccessException {
return testGetPartitionId(uri);
}
@Override
public int getMaxPartitionId() {
return 10;
}
}
;
class TestPartitionAccessor2 implements PartitionAccessor {
@Override
public int getPartitionId(URI uri) throws PartitionAccessException {
return 8;
}
@Override
public int getMaxPartitionId() {
return 10;
}
}
;
PartitionAccessor testAccessor1 = new TestPartitionAccessor1();
PartitionAccessor testAccessor2 = new TestPartitionAccessor2();
registry.register("partitioned-cluster", DefaultPartitionAccessor.getInstance());
registry.register("partitioned-cluster", testAccessor1);
registry.register("partitioned-cluster", testAccessor2);
D2ConfigTestUtil d2Conf = new D2ConfigTestUtil(clustersData, partitionProperties);
assertEquals(d2Conf.runDiscovery(_zkHosts), 0);
verifyPartitionProperties("partitioned-cluster", partitionProperties);
final ClusterProperties clusterprops = getClusterProperties(_zkclient, "partitioned-cluster");
final PartitionAccessor accessor = PartitionAccessorFactory.getPartitionAccessor("partitioned-cluster", registry, clusterprops.getPartitionProperties());
final String legalUri1 = "/profiles?field=position&id=100";
final String legalUri2 = "/cap?wid=99&id=176&randid=301";
final String legalUri3 = "/seas?id=3324";
final String illegalUri = "/?id=1000000000000000000000000000000000000000000000111111111";
assertEquals(0, accessor.getPartitionId(URI.create(legalUri1)));
assertEquals(0, accessor.getPartitionId(URI.create(legalUri2)));
assertEquals(0, accessor.getPartitionId(URI.create(legalUri3)));
assertEquals(0, accessor.getPartitionId(URI.create(illegalUri)));
}
Aggregations