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.jupiter.api.Assertions.assertSame;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.mockStatic;
025
026import org.apache.hadoop.hbase.ClientMetaTableAccessor;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.HBaseIOException;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.FutureUtils;
033import org.junit.jupiter.api.AfterEach;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037import org.mockito.MockedStatic;
038
039@Tag(ClientTests.TAG)
040@Tag(SmallTests.TAG)
041public class TestTableEnableDisableError {
042
043  private MockedStatic<ClientMetaTableAccessor> mockedClientMetaTableAccessor;
044
045  private AsyncAdmin admin;
046
047  @BeforeEach
048  public void setUp() {
049    mockedClientMetaTableAccessor = mockStatic(ClientMetaTableAccessor.class);
050    AsyncConnectionImpl conn = mock(AsyncConnectionImpl.class);
051    AsyncAdminBuilderBase builder =
052      new AsyncAdminBuilderBase(new AsyncConnectionConfiguration(HBaseConfiguration.create())) {
053
054        @Override
055        public AsyncAdmin build() {
056          return new RawAsyncHBaseAdmin(conn, null, this);
057        }
058      };
059    admin = builder.build();
060  }
061
062  @AfterEach
063  public void tearDown() {
064    mockedClientMetaTableAccessor.closeOnDemand();
065  }
066
067  @Test
068  public void testIsTableEnabledError() {
069    HBaseIOException expected = new HBaseIOException("expected");
070    mockedClientMetaTableAccessor.when(() -> ClientMetaTableAccessor.getTableState(any(), any()))
071      .thenReturn(FutureUtils.failedFuture(expected));
072    HBaseIOException actual = assertThrows(HBaseIOException.class,
073      () -> FutureUtils.get(admin.isTableEnabled(TableName.valueOf("whatever"))));
074    assertSame(expected, actual);
075  }
076
077  @Test
078  public void testIsTableDisabledError() {
079    HBaseIOException expected = new HBaseIOException("expected");
080    mockedClientMetaTableAccessor.when(() -> ClientMetaTableAccessor.getTableState(any(), any()))
081      .thenReturn(FutureUtils.failedFuture(expected));
082    HBaseIOException actual = assertThrows(HBaseIOException.class,
083      () -> FutureUtils.get(admin.isTableDisabled(TableName.valueOf("whatever"))));
084    assertSame(expected, actual);
085  }
086}