You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/01/12 20:32:24 UTC

[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #6414: Increase code coverage for pinot-java-client

jackjlli commented on a change in pull request #6414:
URL: https://github.com/apache/incubator-pinot/pull/6414#discussion_r556064181



##########
File path: pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/DynamicBrokerSelector.java
##########
@@ -88,6 +96,7 @@ public void handleDataChange(String dataPath, Object data)
     refresh();
   }
 
+

Review comment:
       Extra line

##########
File path: pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/ExternalViewReaderTest.java
##########
@@ -0,0 +1,116 @@
+/**
+ * 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.
+ */
+package org.apache.pinot.client;
+
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+import static org.testng.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.I0Itec.zkclient.ZkClient;
+import org.apache.commons.io.IOUtils;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class ExternalViewReaderTest {
+
+  @Mock
+  private ZkClient mockZkClient;
+
+  private ExternalViewReader externalViewReaderUnderTest;
+
+  @BeforeMethod
+  public void setUp() throws Exception {
+    initMocks(this);
+    InputStream mockInputStream = IOUtils.toInputStream(
+        "{\"mapFields\":{\"field1\":{\"Broker_12.34.56.78_1234\":\"ONLINE\"}}}", "UTF-8");
+    externalViewReaderUnderTest = Mockito.spy(new ExternalViewReader(mockZkClient) {
+      @Override
+      protected ByteArrayInputStream getInputStream(byte[] brokerResourceNodeData) {
+        return (ByteArrayInputStream) mockInputStream;
+      }
+    });
+  }
+
+  @Test
+  public void testGetLiveBrokers() throws IOException {
+    // Setup
+    final List<String> expectedResult = Arrays.asList("12.34.56.78:1234");
+    when(mockZkClient.readData(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(
+        "json".getBytes());
+
+    // Run the test
+    final List<String> result = externalViewReaderUnderTest.getLiveBrokers();
+
+    // Verify the results
+    assertEquals(expectedResult, result);
+  }
+
+  @Test
+  public void testGetLiveBrokers_exception() throws IOException {

Review comment:
       Avoid using underscore in the method name

##########
File path: pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/DynamicBrokerSelectorTest.java
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.
+ */
+package org.apache.pinot.client;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+import static org.testng.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.I0Itec.zkclient.ZkClient;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class DynamicBrokerSelectorTest {
+
+  @Mock
+  private ExternalViewReader mockExternalViewReader;
+
+  @Mock
+  private ZkClient mockZkClient;
+
+  private DynamicBrokerSelector dynamicBrokerSelectorUnderTest;
+
+  @BeforeMethod
+  public void setUp() throws Exception {
+    initMocks(this);
+    Map<String, List<String>> tableToBrokerListMap = new HashMap<>();
+    tableToBrokerListMap.put("table1", Arrays.asList("broker1"));
+    when(mockExternalViewReader.getTableToBrokersMap()).thenReturn(tableToBrokerListMap);
+    dynamicBrokerSelectorUnderTest = Mockito.spy(new DynamicBrokerSelector("zkServers") {
+      @Override
+      protected ExternalViewReader getEvReader(ZkClient zkClient) {
+        return mockExternalViewReader;
+      }
+
+      @Override
+      protected ZkClient getZkClient(String zkServers) {
+        return mockZkClient;
+      }
+    });
+  }
+
+  @Test(priority = 0)
+  public void testHandleDataChange() throws Exception {
+    // Run the test
+    dynamicBrokerSelectorUnderTest.handleDataChange("dataPath", "data");
+
+    // Verify the results
+    verify(mockExternalViewReader, times(2)).getTableToBrokersMap();
+  }
+
+  @Test(priority = 0)
+  public void testHandleDataDeleted() throws Exception {
+    // Run the test
+    dynamicBrokerSelectorUnderTest.handleDataDeleted("dataPath");
+
+    // Verify the results
+    verify(mockExternalViewReader, times(2)).getTableToBrokersMap();
+  }
+
+  @Test(priority = 1)
+  public void testSelectBroker() throws Exception {
+    // Setup
+    dynamicBrokerSelectorUnderTest.handleDataChange("dataPath", "data");
+
+    // Run the test
+    final String result = dynamicBrokerSelectorUnderTest.selectBroker("table1");
+
+    // Verify the results
+    assertEquals("broker1", result);
+  }
+
+  @Test(priority = 1)
+  public void testSelectBroker_nullTable() throws Exception {

Review comment:
       It's recommended to use camel case in the method name. So please avoid using underscore here.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org