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