pkuwm commented on a change in pull request #775: Add HttpRoutingDataReader
URL: https://github.com/apache/helix/pull/775#discussion_r384089316
 
 

 ##########
 File path: 
zookeeper-api/src/test/java/org/apache/helix/zookeeper/util/TestHttpRoutingDataReader.java
 ##########
 @@ -0,0 +1,124 @@
+package org.apache.helix.zookeeper.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.helix.msdcommon.constant.MetadataStoreRoutingConstants;
+import org.apache.helix.msdcommon.datamodel.MetadataStoreRoutingData;
+import org.apache.helix.msdcommon.exception.InvalidRoutingDataException;
+import org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer;
+import org.apache.helix.zookeeper.impl.ZkTestBase;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+
+public class TestHttpRoutingDataReader extends ZkTestBase {
+  private MockMetadataStoreDirectoryServer _msdsServer;
+  private Map<String, Collection<String>> _testRawRoutingData;
+  private final String _host = "localhost";
+  private final int _port = 1991;
+  private final String _namespace = "TestHttpRoutingDataReader";
+
+  @BeforeClass
+  public void beforeClass() throws IOException {
+    // Create fake routing data
+    _testRawRoutingData = new HashMap<>();
+    _testRawRoutingData
+        .put("zk-0", ImmutableSet.of("/sharding-key-0", "/sharding-key-1", 
"/sharding-key-2"));
+    _testRawRoutingData
+        .put("zk-1", ImmutableSet.of("/sharding-key-3", "/sharding-key-4", 
"/sharding-key-5"));
+    _testRawRoutingData
+        .put("zk-2", ImmutableSet.of("/sharding-key-6", "/sharding-key-7", 
"/sharding-key-8"));
+
+    // Start MockMSDS
+    _msdsServer =
+        new MockMetadataStoreDirectoryServer(_host, _port, _namespace, 
_testRawRoutingData);
+    _msdsServer.startServer();
+
+    // Register the endpoint as a System property
+    String msdsEndpoint = "http://"; + _host + ":" + _port + 
"/admin/v2/namespaces/" + _namespace;
+    System.setProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, 
msdsEndpoint);
+  }
+
+  @AfterClass
+  public void afterClass() {
+    _msdsServer.stopServer();
+  }
+
+  @Test
+  public void testGetRawRoutingData() throws IOException {
+    Map<String, List<String>> rawRoutingData = 
HttpRoutingDataReader.getRawRoutingData();
+    _testRawRoutingData
+        .forEach((realm, keys) -> 
Assert.assertEquals(rawRoutingData.get(realm), keys));
+  }
+
+  @Test(dependsOnMethods = "testGetRawRoutingData")
+  public void testGetMetadataStoreRoutingData() throws IOException, 
InvalidRoutingDataException {
+    MetadataStoreRoutingData data = 
HttpRoutingDataReader.getMetadataStoreRoutingData();
+    Map<String, String> allMappings = data.getAllMappingUnderPath("/");
+    Map<String, Set<String>> groupedMappings = 
allMappings.entrySet().stream().collect(Collectors
+        .groupingBy(Map.Entry::getValue,
+            Collectors.mapping(Map.Entry::getKey, Collectors.toSet())));
+    _testRawRoutingData.forEach((realm, keys) -> {
+      // Two way containsAll because AssertEquals on two set collections is 
buggy in that
 
 Review comment:
   I understand `Two-way containsAll is basically an equality check`.
   My point is, if the 2 sets have the same string elements, assertEqual() 
should pass, no matter the ordering of elements is equal or not.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to