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