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.ipc;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.Collections;
024import java.util.Map;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.net.Address;
029import org.apache.hadoop.hbase.security.User;
030import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProviders;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.ClassRule;
034import org.junit.Rule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.junit.rules.TestName;
038
039import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
040
041import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MasterService;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos;
044
045@Category(SmallTests.class)
046public class TestRpcConnectionHeader {
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestRpcClientLeaks.class);
050
051  @Rule
052  public TestName name = new TestName();
053
054  private final ConnectionId connectionId;
055
056  public TestRpcConnectionHeader() throws IOException {
057    User user = User.createUserForTesting(HBaseConfiguration.create(), "test", new String[] {});
058    String serviceName = MasterService.getDescriptor().getName();
059
060    connectionId = new ConnectionId(user, serviceName, Address.fromParts("localhost", 12345));
061  }
062
063  private class MyRpcConnection extends RpcConnection {
064
065    protected MyRpcConnection(Configuration conf, Map<String, byte[]> connectionAttributes)
066      throws IOException {
067      super(conf, null, connectionId, "cluster-id", false,
068        new SaslClientAuthenticationProviders(conf), null, null, null, null, connectionAttributes);
069    }
070
071    @Override
072    protected void callTimeout(Call call) {
073    }
074
075    @Override
076    public boolean isActive() {
077      return false;
078    }
079
080    @Override
081    public void shutdown() {
082    }
083
084    @Override
085    public void sendRequest(Call call, HBaseRpcController hrc) throws IOException {
086    }
087
088    @Override
089    public void cleanupConnection() {
090    }
091  }
092
093  @Test
094  public void testEmptyHeaders() throws IOException {
095    Configuration configuration = HBaseConfiguration.create();
096
097    MyRpcConnection connection = new MyRpcConnection(configuration, Collections.emptyMap());
098    RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader();
099
100    assertEquals(0, connectionHeader.getAttributeCount());
101  }
102
103  @Test
104  public void testConfigHeaders() throws IOException {
105    Configuration configuration = HBaseConfiguration.create();
106    configuration.set("hbase.client.header.test", "true");
107
108    MyRpcConnection connection = new MyRpcConnection(configuration, Collections.emptyMap());
109    RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader();
110
111    assertEquals(1, connectionHeader.getAttributeCount());
112
113    HBaseProtos.NameBytesPair attribute = connectionHeader.getAttribute(0);
114    assertEquals("hbase.client.header.test", attribute.getName());
115    assertEquals("true", attribute.getValue().toStringUtf8());
116  }
117
118  @Test
119  public void testConfigHeadersNoOverride() throws IOException {
120    Configuration configuration = HBaseConfiguration.create();
121    configuration.set("hbase.client.header.test", "true");
122    configuration.set("hbase.client.header.test2", "true");
123
124    Map<String, byte[]> attributes =
125      ImmutableMap.of("hbase.client.header.test", Bytes.toBytes("false"));
126
127    MyRpcConnection connection = new MyRpcConnection(configuration, attributes);
128    RPCProtos.ConnectionHeader connectionHeader = connection.getConnectionHeader();
129
130    assertEquals(2, connectionHeader.getAttributeCount());
131
132    HBaseProtos.NameBytesPair attribute0 = connectionHeader.getAttribute(0);
133    assertEquals("hbase.client.header.test", attribute0.getName());
134    assertEquals("false", attribute0.getValue().toStringUtf8());
135
136    HBaseProtos.NameBytesPair attribute1 = connectionHeader.getAttribute(1);
137    assertEquals("hbase.client.header.test2", attribute1.getName());
138    assertEquals("true", attribute1.getValue().toStringUtf8());
139  }
140}