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;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.thrift.ImplType;
028import org.apache.hadoop.hbase.thrift.TestThriftServerCmdLine;
029import org.apache.hadoop.hbase.thrift.ThriftServer;
030import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
031import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
032import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
033import org.apache.hadoop.hbase.thrift2.generated.TTableName;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.thrift.protocol.TBinaryProtocol;
036import org.apache.thrift.protocol.TCompactProtocol;
037import org.apache.thrift.protocol.TProtocol;
038import org.apache.thrift.transport.TSocket;
039import org.apache.thrift.transport.TTransport;
040import org.apache.thrift.transport.layered.TFramedTransport;
041import org.junit.Assert;
042import org.junit.ClassRule;
043import org.junit.experimental.categories.Category;
044
045@Category({ ClientTests.class, MediumTests.class})
046public class TestThrift2ServerCmdLine extends TestThriftServerCmdLine {
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestThrift2ServerCmdLine.class);
050
051  private static final String TABLENAME = "TestThrift2ServerCmdLineTable";
052
053  public TestThrift2ServerCmdLine(ImplType implType, boolean specifyFramed,
054      boolean specifyBindIP, boolean specifyCompact) {
055    super(implType, specifyFramed, specifyBindIP, specifyCompact);
056  }
057
058  @Override 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}