Copilot commented on code in PR #681: URL: https://github.com/apache/ranger/pull/681#discussion_r2481851733
########## hive-agent/src/test/java/org/apache/ranger/services/hive/client/TestHiveClient.java: ########## @@ -0,0 +1,584 @@ +/* + * 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.ranger.services.hive.client; + +import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.ranger.plugin.client.HadoopConfigHolder; +import org.apache.ranger.plugin.client.HadoopException; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.security.auth.Subject; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.Permission; +import java.security.PrivilegedExceptionAction; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.ResultSet; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLTimeoutException; +import java.sql.Statement; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Logger; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * @generated by Cursor + * @description : Unit Test cases for HiveClient + */ +@ExtendWith(MockitoExtension.class) +@TestMethodOrder(MethodOrderer.MethodName.class) +public class TestHiveClient { + @Test + public void test01_connectionTestEmptyDbListSetsFailureMessage() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenReturn(Collections.emptyList()); + })) { + Map<String, Object> resp = HiveClient.connectionTest("svc", Collections.emptyMap()); + assertNotNull(resp); + assertNotEquals(Boolean.TRUE, resp.get("connectivityStatus")); + } + } + + @Test + public void test02_connectionTestSuccessSetsSuccessMessageAndCloses() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenReturn(Collections.singletonList("db1")); + })) { + Map<String, Object> resp = HiveClient.connectionTest("svc", new HashMap<>()); + assertNotNull(resp); + List<HiveClient> constructed = cons.constructed(); + if (!constructed.isEmpty()) { + Mockito.verify(constructed.get(0), Mockito.times(1)).close(); + } + } + } + + @Test + public void test03_connectionTestPropagatesException() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenThrow(new RuntimeException("boom")); + })) { + assertThrows(Exception.class, () -> HiveClient.connectionTest("svc", Collections.emptyMap())); + } + } + + @Test + public void test04_constructorsInvokeInitHive() throws Exception { + NoopHiveClient c1 = new NoopHiveClient("svc"); + assertTrue(c1.initCalled); + NoopHiveClient c2 = new NoopHiveClient("svc", new HashMap<>()); + assertTrue(c2.initCalled); + } + + @Test + public void test05_getDatabaseList_usesHMWhenEnabled() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getAllDatabases()).thenReturn(Arrays.asList("db1", "db2")); + fHM.set(client, hms); + List<String> out = client.getDatabaseList("*", Collections.singletonList("db2")); + assertEquals(Collections.singletonList("db1"), out); + } + + @Test + public void test06_getTableList_usesHMAndRespectsExcludes() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getTables("db1", "tbl*")).thenReturn(Arrays.asList("t1", "t2")); + fHM.set(client, hms); + List<String> out = client.getTableList("tbl*", Collections.singletonList("db1"), Collections.singletonList("t2")); + assertEquals(Collections.singletonList("t1"), out); + } + + @Test + public void test07_getColumnList_usesHMWithPatternAndExcludes() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getFields("db1", "t1")).thenReturn(Arrays.asList(new FieldSchema("c1", "int", null), new FieldSchema("c2", "string", null))); + fHM.set(client, hms); + List<String> out = client.getColumnList("c*", Collections.singletonList("db1"), Collections.singletonList("t1"), Collections.singletonList("c2")); + assertEquals(Collections.singletonList("c1"), out); + } + + @Test + public void test08_main_args2_noDatabaseFoundPrintedAndClosed() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getDatabaseList("db", null)).thenReturn(Collections.emptyList()); + })) { + HiveClient.main(new String[] {"svc", "db"}); + String out = bout.toString(); + assertNotNull(out); + // ensure constructed mock was closed by try-with-resources + List<HiveClient> constructed = cons.constructed(); + if (!constructed.isEmpty()) { + Mockito.verify(constructed.get(0), Mockito.times(1)).close(); + } + } finally { + System.setOut(origOut); + } + } + + @Test + public void test09_main_args3_printsTables() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getTableList("tbl", null, null)).thenReturn(Collections.singletonList("db.tbl1")); + })) { + HiveClient.main(new String[] {"svc", "db", "tbl"}); + String out = bout.toString(); + assertNotNull(out); + } finally { + System.setOut(origOut); + } + } + + @Test + public void test10_main_args4_printsColumns() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getColumnList("col", null, null, null)).thenReturn(Collections.singletonList("db.tbl.col1")); + })) { + HiveClient.main(new String[] {"svc", "db", "tbl", "col"}); + String out = bout.toString(); + assertNotNull(out); + } finally { + System.setOut(origOut); + } + } + + @Test + public void test11_getDatabaseList_jdbcPath() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + ResultSet rs = Mockito.mock(ResultSet.class); + when(con.createStatement()).thenReturn(stat); + when(stat.executeQuery(Mockito.anyString())).thenReturn(rs); + when(rs.next()).thenReturn(true, false); + when(rs.getString(1)).thenReturn("db1"); + fCon.set(client, con); + List<String> out = client.getDatabaseList("db*", null); + assertEquals(Collections.singletonList("db1"), out); + } + + @Test + public void test12_close_closesConnection() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + fCon.set(client, con); + client.close(); + Mockito.verify(con, Mockito.times(1)).close(); + } + + @Test + public void test13_getTableList_jdbcPath_excludesAndPattern() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + ResultSet rs = Mockito.mock(ResultSet.class); + when(con.createStatement()).thenReturn(stat); + when(stat.execute(Mockito.eq("use db1"))).thenReturn(true); + when(stat.executeQuery(Mockito.anyString())).thenReturn(rs); + when(rs.next()).thenReturn(true, true, false); + when(rs.getString(1)).thenReturn("t1", "t2"); + fCon.set(client, con); + List<String> out = client.getTableList("t*", Collections.singletonList("db1"), Collections.singletonList("t2")); + assertEquals(Collections.singletonList("t1"), out); + Mockito.verify(stat, Mockito.atLeastOnce()).execute(Mockito.eq("use db1")); + } + + @Test + public void test14_getTableList_jdbcPath_timeoutThrowsHadoopException() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + when(con.createStatement()).thenReturn(stat); + when(stat.execute(Mockito.anyString())).thenReturn(true); + when(stat.executeQuery(Mockito.anyString())).thenThrow(new SQLTimeoutException("timeout")); + fCon.set(client, con); + assertThrows(HadoopException.class, () -> client.getTableList("t*", Collections.singletonList("db1"), null)); + } + + @Test + public void test17_getClmList_jdbcPath_excludesAndPattern() throws Exception { Review Comment: Corrected spelling of 'getClmList' to 'getColumnList'. ```suggestion public void test17_getColumnList_jdbcPath_excludesAndPattern() throws Exception { ``` ########## hive-agent/src/test/java/org/apache/ranger/services/hive/client/TestHiveClient.java: ########## @@ -0,0 +1,584 @@ +/* + * 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.ranger.services.hive.client; + +import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.ranger.plugin.client.HadoopConfigHolder; +import org.apache.ranger.plugin.client.HadoopException; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.security.auth.Subject; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.Permission; +import java.security.PrivilegedExceptionAction; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.ResultSet; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLTimeoutException; +import java.sql.Statement; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Logger; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * @generated by Cursor + * @description : Unit Test cases for HiveClient + */ +@ExtendWith(MockitoExtension.class) +@TestMethodOrder(MethodOrderer.MethodName.class) +public class TestHiveClient { + @Test + public void test01_connectionTestEmptyDbListSetsFailureMessage() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenReturn(Collections.emptyList()); + })) { + Map<String, Object> resp = HiveClient.connectionTest("svc", Collections.emptyMap()); + assertNotNull(resp); + assertNotEquals(Boolean.TRUE, resp.get("connectivityStatus")); + } + } + + @Test + public void test02_connectionTestSuccessSetsSuccessMessageAndCloses() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenReturn(Collections.singletonList("db1")); + })) { + Map<String, Object> resp = HiveClient.connectionTest("svc", new HashMap<>()); + assertNotNull(resp); + List<HiveClient> constructed = cons.constructed(); + if (!constructed.isEmpty()) { + Mockito.verify(constructed.get(0), Mockito.times(1)).close(); + } + } + } + + @Test + public void test03_connectionTestPropagatesException() throws Exception { + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + Mockito.when(mock.getDatabaseList("*", null)).thenThrow(new RuntimeException("boom")); + })) { + assertThrows(Exception.class, () -> HiveClient.connectionTest("svc", Collections.emptyMap())); + } + } + + @Test + public void test04_constructorsInvokeInitHive() throws Exception { + NoopHiveClient c1 = new NoopHiveClient("svc"); + assertTrue(c1.initCalled); + NoopHiveClient c2 = new NoopHiveClient("svc", new HashMap<>()); + assertTrue(c2.initCalled); + } + + @Test + public void test05_getDatabaseList_usesHMWhenEnabled() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getAllDatabases()).thenReturn(Arrays.asList("db1", "db2")); + fHM.set(client, hms); + List<String> out = client.getDatabaseList("*", Collections.singletonList("db2")); + assertEquals(Collections.singletonList("db1"), out); + } + + @Test + public void test06_getTableList_usesHMAndRespectsExcludes() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getTables("db1", "tbl*")).thenReturn(Arrays.asList("t1", "t2")); + fHM.set(client, hms); + List<String> out = client.getTableList("tbl*", Collections.singletonList("db1"), Collections.singletonList("t2")); + assertEquals(Collections.singletonList("t1"), out); + } + + @Test + public void test07_getColumnList_usesHMWithPatternAndExcludes() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, true); + Field fHM = HiveClient.class.getDeclaredField("hiveClient"); + fHM.setAccessible(true); + HiveMetaStoreClient hms = Mockito.mock(HiveMetaStoreClient.class); + when(hms.getFields("db1", "t1")).thenReturn(Arrays.asList(new FieldSchema("c1", "int", null), new FieldSchema("c2", "string", null))); + fHM.set(client, hms); + List<String> out = client.getColumnList("c*", Collections.singletonList("db1"), Collections.singletonList("t1"), Collections.singletonList("c2")); + assertEquals(Collections.singletonList("c1"), out); + } + + @Test + public void test08_main_args2_noDatabaseFoundPrintedAndClosed() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getDatabaseList("db", null)).thenReturn(Collections.emptyList()); + })) { + HiveClient.main(new String[] {"svc", "db"}); + String out = bout.toString(); + assertNotNull(out); + // ensure constructed mock was closed by try-with-resources + List<HiveClient> constructed = cons.constructed(); + if (!constructed.isEmpty()) { + Mockito.verify(constructed.get(0), Mockito.times(1)).close(); + } + } finally { + System.setOut(origOut); + } + } + + @Test + public void test09_main_args3_printsTables() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getTableList("tbl", null, null)).thenReturn(Collections.singletonList("db.tbl1")); + })) { + HiveClient.main(new String[] {"svc", "db", "tbl"}); + String out = bout.toString(); + assertNotNull(out); + } finally { + System.setOut(origOut); + } + } + + @Test + public void test10_main_args4_printsColumns() throws Exception { + PrintStream origOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + try (MockedConstruction<HiveClient> cons = Mockito.mockConstruction(HiveClient.class, (mock, ctx) -> { + when(mock.getColumnList("col", null, null, null)).thenReturn(Collections.singletonList("db.tbl.col1")); + })) { + HiveClient.main(new String[] {"svc", "db", "tbl", "col"}); + String out = bout.toString(); + assertNotNull(out); + } finally { + System.setOut(origOut); + } + } + + @Test + public void test11_getDatabaseList_jdbcPath() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + ResultSet rs = Mockito.mock(ResultSet.class); + when(con.createStatement()).thenReturn(stat); + when(stat.executeQuery(Mockito.anyString())).thenReturn(rs); + when(rs.next()).thenReturn(true, false); + when(rs.getString(1)).thenReturn("db1"); + fCon.set(client, con); + List<String> out = client.getDatabaseList("db*", null); + assertEquals(Collections.singletonList("db1"), out); + } + + @Test + public void test12_close_closesConnection() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + fCon.set(client, con); + client.close(); + Mockito.verify(con, Mockito.times(1)).close(); + } + + @Test + public void test13_getTableList_jdbcPath_excludesAndPattern() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + ResultSet rs = Mockito.mock(ResultSet.class); + when(con.createStatement()).thenReturn(stat); + when(stat.execute(Mockito.eq("use db1"))).thenReturn(true); + when(stat.executeQuery(Mockito.anyString())).thenReturn(rs); + when(rs.next()).thenReturn(true, true, false); + when(rs.getString(1)).thenReturn("t1", "t2"); + fCon.set(client, con); + List<String> out = client.getTableList("t*", Collections.singletonList("db1"), Collections.singletonList("t2")); + assertEquals(Collections.singletonList("t1"), out); + Mockito.verify(stat, Mockito.atLeastOnce()).execute(Mockito.eq("use db1")); + } + + @Test + public void test14_getTableList_jdbcPath_timeoutThrowsHadoopException() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + when(con.createStatement()).thenReturn(stat); + when(stat.execute(Mockito.anyString())).thenReturn(true); + when(stat.executeQuery(Mockito.anyString())).thenThrow(new SQLTimeoutException("timeout")); + fCon.set(client, con); + assertThrows(HadoopException.class, () -> client.getTableList("t*", Collections.singletonList("db1"), null)); + } + + @Test + public void test17_getClmList_jdbcPath_excludesAndPattern() throws Exception { + NoopHiveClient client = new NoopHiveClient("svc", new HashMap<>()); + Field fFlag = HiveClient.class.getDeclaredField("enableHiveMetastoreLookup"); + fFlag.setAccessible(true); + fFlag.set(client, false); + Field fCon = HiveClient.class.getDeclaredField("con"); + fCon.setAccessible(true); + Connection con = Mockito.mock(Connection.class); + Statement stat = Mockito.mock(Statement.class); + ResultSet rs = Mockito.mock(ResultSet.class); + when(con.createStatement()).thenReturn(stat); + when(stat.execute(Mockito.eq("use db1"))).thenReturn(true); + when(stat.executeQuery(Mockito.eq("describe t1"))).thenReturn(rs); + when(rs.next()).thenReturn(true, true, false); + when(rs.getString(1)).thenReturn("c1", "c2"); + fCon.set(client, con); + List<String> out = client.getColumnList("c*", Collections.singletonList("db1"), Collections.singletonList("t1"), Collections.singletonList("c2")); + assertEquals(Collections.singletonList("c1"), out); + } + + @Test + public void test18_getClmList_jdbcPath_timeoutThrowsHadoopException() throws Exception { Review Comment: Corrected spelling of 'getClmList' to 'getColumnList'. ```suggestion public void test18_getColumnList_jdbcPath_timeoutThrowsHadoopException() throws Exception { ``` ########## hive-agent/pom.xml: ########## @@ -268,9 +268,15 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>log4j-over-slf4j</artifactId> - <version>${slf4j.version}</version> + <groupId>org.mockito</groupId> + <artifactId>mockito-inline</artifactId> + <version>${mockito.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-junit-jupiter</artifactId> + <version>${mockito.version}</version> <scope>test</scope> </dependency> Review Comment: The removed slf4j dependency may be needed elsewhere in the codebase. Consider adding it back or verify that log4j-over-slf4j is not required for proper logging functionality in tests. ```suggestion </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> <scope>test</scope> </dependency> ``` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
