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.util.ArrayList;
024import java.util.function.Supplier;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.thrift.TestThriftHttpServerBase;
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.junit.jupiter.api.AfterAll;
035import org.junit.jupiter.api.BeforeAll;
036import org.junit.jupiter.api.Tag;
037
038import org.apache.hbase.thirdparty.org.apache.thrift.protocol.TBinaryProtocol;
039import org.apache.hbase.thirdparty.org.apache.thrift.protocol.TProtocol;
040import org.apache.hbase.thirdparty.org.apache.thrift.transport.THttpClient;
041
042@Tag(ClientTests.TAG)
043@Tag(LargeTests.TAG)
044public class TestThrift2HttpServer extends TestThriftHttpServerBase {
045  private static final String TABLENAME = "TestThrift2HttpServerTable";
046
047  @BeforeAll
048  public static void beforeAll() throws Exception {
049    TestThriftHttpServerBase.setUpBeforeClass();
050  }
051
052  @AfterAll
053  public static void afterAll() throws Exception {
054    TestThriftHttpServerBase.tearDownAfterClass();
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(String url, int customHeaderSize) throws Exception {
064    THttpClient httpClient = new THttpClient(url);
065    httpClient.open();
066
067    if (customHeaderSize > 0) {
068      StringBuilder sb = new StringBuilder();
069      for (int i = 0; i < customHeaderSize; i++) {
070        sb.append("a");
071      }
072      httpClient.setCustomHeader("User-Agent", sb.toString());
073    }
074
075    try {
076      TProtocol prot;
077      prot = new TBinaryProtocol(httpClient);
078      THBaseService.Client client = new THBaseService.Client(prot);
079      TTableName tTableName = new TTableName();
080      tTableName.setNs(Bytes.toBytes(""));
081      tTableName.setQualifier(Bytes.toBytes(TABLENAME));
082      if (!tableCreated) {
083        assertFalse(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      assertTrue(client.tableExists(tTableName));
093    } finally {
094      httpClient.close();
095    }
096  }
097}