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.thrift2;
019
020import java.net.InetAddress;
021import java.util.ArrayList;
022import java.util.function.Supplier;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.ClientTests;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.thrift.ImplType;
027import org.apache.hadoop.hbase.thrift.TestThriftServerCmdLine;
028import org.apache.hadoop.hbase.thrift.ThriftServer;
029import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
030import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
031import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
032import org.apache.hadoop.hbase.thrift2.generated.TTableName;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.thrift.protocol.TBinaryProtocol;
035import org.apache.thrift.protocol.TCompactProtocol;
036import org.apache.thrift.protocol.TProtocol;
037import org.apache.thrift.transport.TSocket;
038import org.apache.thrift.transport.TTransport;
039import org.apache.thrift.transport.layered.TFramedTransport;
040import org.junit.Assert;
041import org.junit.ClassRule;
042import org.junit.experimental.categories.Category;
043
044@Category({ ClientTests.class, MediumTests.class })
045public class TestThrift2ServerCmdLine extends TestThriftServerCmdLine {
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestThrift2ServerCmdLine.class);
049
050  private static final String TABLENAME = "TestThrift2ServerCmdLineTable";
051
052  public TestThrift2ServerCmdLine(ImplType implType, boolean specifyFramed, boolean specifyBindIP,
053    boolean specifyCompact) {
054    super(implType, specifyFramed, specifyBindIP, specifyCompact);
055  }
056
057  @Override
058  protected Supplier<ThriftServer> getThriftServerSupplier() {
059    return () -> new org.apache.hadoop.hbase.thrift2.ThriftServer(TEST_UTIL.getConfiguration());
060  }
061
062  @Override
063  protected void talkToThriftServer(int port) throws Exception {
064    TSocket sock = new TSocket(InetAddress.getLoopbackAddress().getHostName(), port);
065    TTransport transport = sock;
066    if (specifyFramed || implType.isAlwaysFramed()) {
067      transport = new TFramedTransport(transport);
068    }
069
070    sock.open();
071    try {
072      TProtocol tProtocol;
073      if (specifyCompact) {
074        tProtocol = new TCompactProtocol(transport);
075      } else {
076        tProtocol = new TBinaryProtocol(transport);
077      }
078      THBaseService.Client client = new THBaseService.Client(tProtocol);
079      TTableName tTableName = new TTableName();
080      tTableName.setNs(Bytes.toBytes(""));
081      tTableName.setQualifier(Bytes.toBytes(TABLENAME));
082      if (!tableCreated) {
083        Assert.assertTrue(!client.tableExists(tTableName));
084        TTableDescriptor tTableDescriptor = new TTableDescriptor();
085        tTableDescriptor.setTableName(tTableName);
086        TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
087        columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
088        tTableDescriptor.addToColumns(columnFamilyDescriptor);
089        client.createTable(tTableDescriptor, new ArrayList<>());
090        tableCreated = true;
091      }
092      Assert.assertTrue("tableCreated " + tableCreated, client.tableExists(tTableName));
093    } finally {
094      sock.close();
095    }
096  }
097}