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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import java.net.InetAddress;
022import java.net.UnknownHostException;
023import java.util.Optional;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.client.RegionInfoBuilder;
026import org.apache.hadoop.hbase.ipc.RpcCall;
027import org.apache.hadoop.hbase.ipc.RpcServer;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.mockito.Mockito;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Test parts of {@link RSRpcServices}
039 */
040@Category({ RegionServerTests.class, MediumTests.class})
041public class TestRSRpcServices {
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestRSRpcServices.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestRSRpcServices.class);
047
048  /**
049   * Simple test of the toString on RegionScannerHolder works.
050   * Just creates one and calls #toString on it.
051   */
052  @Test
053  public void testRegionScannerHolderToString() throws UnknownHostException {
054    RpcCall call = Mockito.mock(RpcCall.class);
055    int port = 1234;
056    Mockito.when(call.getRemotePort()).thenReturn(port);
057    InetAddress address = InetAddress.getLocalHost();
058    Mockito.when(call.getRemoteAddress()).thenReturn(address);
059    Optional<String> userName = Optional.ofNullable("test");
060    Mockito.when(call.getRequestUserName()).thenReturn(userName);
061    RpcServer.setCurrentCall(call);
062    String clientIpAndPort = RSRpcServices.getRemoteClientIpAndPort();
063    String userNameTest = RSRpcServices.getUserName();
064    assertEquals("test", userNameTest);
065    HRegion region = Mockito.mock(HRegion.class);
066    Mockito.when(region.getRegionInfo()).thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO);
067    RSRpcServices.RegionScannerHolder rsh = new RSRpcServices.RegionScannerHolder(null, region,
068      null, null, false, false, clientIpAndPort,
069      userNameTest);
070    LOG.info("rsh: {}", rsh);
071  }
072}