|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.hbase.client; |
| 19 | + |
| 20 | +import org.apache.seatunnel.connectors.seatunnel.hbase.config.HbaseParameters; |
| 21 | + |
| 22 | +import org.apache.hadoop.hbase.HBaseConfiguration; |
| 23 | +import org.apache.hadoop.hbase.TableName; |
| 24 | +import org.apache.hadoop.hbase.client.Admin; |
| 25 | +import org.apache.hadoop.hbase.client.BufferedMutator; |
| 26 | +import org.apache.hadoop.hbase.client.BufferedMutatorParams; |
| 27 | +import org.apache.hadoop.hbase.client.Connection; |
| 28 | +import org.apache.hadoop.hbase.client.Result; |
| 29 | +import org.apache.hadoop.hbase.client.ResultScanner; |
| 30 | +import org.apache.hadoop.hbase.client.Scan; |
| 31 | +import org.apache.hadoop.hbase.client.Table; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.mockito.Mockito; |
| 35 | + |
| 36 | +import java.lang.reflect.Constructor; |
| 37 | + |
| 38 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | + |
| 42 | +class HbaseClientTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + void testIsExistsDataReturnsFalseWhenScannerNextReturnsNull() throws Exception { |
| 46 | + Connection connection = Mockito.mock(Connection.class); |
| 47 | + Table table = Mockito.mock(Table.class); |
| 48 | + ResultScanner scanner = Mockito.mock(ResultScanner.class); |
| 49 | + Mockito.when(connection.getTable(any(TableName.class))).thenReturn(table); |
| 50 | + Mockito.when(table.getScanner(any(Scan.class))).thenReturn(scanner); |
| 51 | + Mockito.when(scanner.next()).thenReturn(null); |
| 52 | + |
| 53 | + HbaseClient client = newHbaseClient(connection); |
| 54 | + |
| 55 | + assertFalse(client.isExistsData("ns", "tbl")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testIsExistsDataReturnsTrueWhenScannerHasResult() throws Exception { |
| 60 | + Connection connection = Mockito.mock(Connection.class); |
| 61 | + Table table = Mockito.mock(Table.class); |
| 62 | + ResultScanner scanner = Mockito.mock(ResultScanner.class); |
| 63 | + Result result = Mockito.mock(Result.class); |
| 64 | + Mockito.when(result.isEmpty()).thenReturn(false); |
| 65 | + Mockito.when(connection.getTable(any(TableName.class))).thenReturn(table); |
| 66 | + Mockito.when(table.getScanner(any(Scan.class))).thenReturn(scanner); |
| 67 | + Mockito.when(scanner.next()).thenReturn(result); |
| 68 | + |
| 69 | + HbaseClient client = newHbaseClient(connection); |
| 70 | + |
| 71 | + assertTrue(client.isExistsData("ns", "tbl")); |
| 72 | + } |
| 73 | + |
| 74 | + private HbaseClient newHbaseClient(Connection connection) throws Exception { |
| 75 | + HbaseClient.hbaseConfiguration = HBaseConfiguration.create(); |
| 76 | + Mockito.when(connection.getAdmin()).thenReturn(Mockito.mock(Admin.class)); |
| 77 | + Mockito.when(connection.getBufferedMutator(any(BufferedMutatorParams.class))) |
| 78 | + .thenReturn(Mockito.mock(BufferedMutator.class)); |
| 79 | + HbaseParameters hbaseParameters = Mockito.mock(HbaseParameters.class); |
| 80 | + Mockito.when(hbaseParameters.getNamespace()).thenReturn("ns"); |
| 81 | + Mockito.when(hbaseParameters.getTable()).thenReturn("tbl"); |
| 82 | + Mockito.when(hbaseParameters.getWriteBufferSize()).thenReturn(1); |
| 83 | + |
| 84 | + Constructor<HbaseClient> constructor = |
| 85 | + HbaseClient.class.getDeclaredConstructor(Connection.class, HbaseParameters.class); |
| 86 | + constructor.setAccessible(true); |
| 87 | + return constructor.newInstance(connection, hbaseParameters); |
| 88 | + } |
| 89 | +} |
0 commit comments