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