001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertThrows;
021import static org.mockito.Mockito.times;
022import static org.mockito.Mockito.verify;
023import static org.mockito.Mockito.when;
024
025import java.io.IOException;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HRegionLocation;
029import org.apache.hadoop.hbase.RegionLocations;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.TableNotEnabledException;
033import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.runner.RunWith;
042import org.mockito.Mock;
043import org.mockito.runners.MockitoJUnitRunner;
044
045@RunWith(MockitoJUnitRunner.class)
046@Category({ ClientTests.class, SmallTests.class })
047public class TestScannerCallable {
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestScannerCallable.class);
051
052  private static final TableName TABLE_NAME = TableName.valueOf("TestScannerCallable");
053
054  private static final String HOSTNAME = "localhost";
055  private static final ServerName SERVERNAME = ServerName.valueOf(HOSTNAME, 60030, 123);
056  private static final byte[] ROW = Bytes.toBytes("row1");
057  private static final Scan DEFAULT_SCAN = new Scan().withStartRow(ROW, true);
058
059  @Mock
060  private ClusterConnection connection;
061  @Mock
062  private RpcControllerFactory rpcFactory;
063  @Mock
064  private RegionLocations regionLocations;
065  @Mock
066  private HRegionLocation regionLocation;
067
068  @Before
069  public void setUp() throws Exception {
070    when(connection.getConfiguration()).thenReturn(new Configuration());
071    when(regionLocations.size()).thenReturn(1);
072    when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation);
073    when(regionLocation.getHostname()).thenReturn(HOSTNAME);
074    when(regionLocation.getServerName()).thenReturn(SERVERNAME);
075  }
076
077  @Test
078  public void testPrepareAlwaysUsesCache() throws Exception {
079    when(connection.locateRegion(TABLE_NAME, ROW, true, true, 0)).thenReturn(regionLocations);
080
081    ScannerCallable callable =
082      new ScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null, rpcFactory, 0);
083    callable.prepare(false);
084    callable.prepare(true);
085
086    verify(connection, times(2)).locateRegion(TABLE_NAME, ROW, true, true, 0);
087  }
088
089  @Test
090  public void testHandleDisabledTable() throws IOException {
091    when(connection.isTableDisabled(TABLE_NAME)).thenReturn(true);
092
093    ScannerCallable callable =
094      new ScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null, rpcFactory, 0);
095
096    assertThrows(TableNotEnabledException.class, () -> callable.prepare(true));
097  }
098}