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 java.util.Collections;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HRegionLocation;
030import org.apache.hadoop.hbase.RegionLocations;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.TableNotEnabledException;
034import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
035import org.apache.hadoop.hbase.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.runner.RunWith;
043import org.mockito.Mock;
044import org.mockito.junit.MockitoJUnitRunner;
045
046@RunWith(MockitoJUnitRunner.class)
047@Category({ ClientTests.class, SmallTests.class })
048public class TestScannerCallable {
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestScannerCallable.class);
052
053  private static final TableName TABLE_NAME = TableName.valueOf("TestScannerCallable");
054
055  private static final String HOSTNAME = "localhost";
056  private static final ServerName SERVERNAME = ServerName.valueOf(HOSTNAME, 60030, 123);
057  private static final byte[] ROW = Bytes.toBytes("row1");
058  private static final Scan DEFAULT_SCAN = new Scan().withStartRow(ROW, true);
059
060  @Mock
061  private ClusterConnection connection;
062  @Mock
063  private RpcControllerFactory rpcFactory;
064  @Mock
065  private RegionLocations regionLocations;
066  @Mock
067  private HRegionLocation regionLocation;
068
069  @Before
070  public void setUp() throws Exception {
071    when(connection.getConfiguration()).thenReturn(new Configuration());
072    when(regionLocations.size()).thenReturn(1);
073    when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation);
074    when(regionLocation.getHostname()).thenReturn(HOSTNAME);
075    when(regionLocation.getServerName()).thenReturn(SERVERNAME);
076  }
077
078  @Test
079  public void testPrepareAlwaysUsesCache() throws Exception {
080    when(connection.locateRegion(TABLE_NAME, ROW, true, true, 0)).thenReturn(regionLocations);
081
082    ScannerCallable callable = new ScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null,
083      rpcFactory, 0, Collections.emptyMap());
084    callable.prepare(false);
085    callable.prepare(true);
086
087    verify(connection, times(2)).locateRegion(TABLE_NAME, ROW, true, true, 0);
088  }
089
090  @Test
091  public void testHandleDisabledTable() throws IOException {
092    when(connection.isTableDisabled(TABLE_NAME)).thenReturn(true);
093
094    ScannerCallable callable = new ScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null,
095      rpcFactory, 0, Collections.emptyMap());
096
097    assertThrows(TableNotEnabledException.class, () -> callable.prepare(true));
098  }
099}