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.apache.thrift.protocol.TBinaryProtocol;
035import org.apache.thrift.protocol.TProtocol;
036import org.apache.thrift.transport.THttpClient;
037import org.junit.jupiter.api.AfterAll;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.Tag;
040
041@Tag(ClientTests.TAG)
042@Tag(LargeTests.TAG)
043public class TestThrift2HttpServer extends TestThriftHttpServerBase {
044  private static final String TABLENAME = "TestThrift2HttpServerTable";
045
046  @BeforeAll
047  public static void beforeAll() throws Exception {
048    TestThriftHttpServerBase.setUpBeforeClass();
049  }
050
051  @AfterAll
052  public static void afterAll() throws Exception {
053    TestThriftHttpServerBase.tearDownAfterClass();
054  }
055
056  @Override
057  protected Supplier<ThriftServer> getThriftServerSupplier() {
058    return () -> new org.apache.hadoop.hbase.thrift2.ThriftServer(TEST_UTIL.getConfiguration());
059  }
060
061  @Override
062  protected void talkToThriftServer(String url, int customHeaderSize) throws Exception {
063    THttpClient httpClient = new THttpClient(url);
064    httpClient.open();
065
066    if (customHeaderSize > 0) {
067      StringBuilder sb = new StringBuilder();
068      for (int i = 0; i < customHeaderSize; i++) {
069        sb.append("a");
070      }
071      httpClient.setCustomHeader("User-Agent", sb.toString());
072    }
073
074    try {
075      TProtocol prot;
076      prot = new TBinaryProtocol(httpClient);
077      THBaseService.Client client = new THBaseService.Client(prot);
078      TTableName tTableName = new TTableName();
079      tTableName.setNs(Bytes.toBytes(""));
080      tTableName.setQualifier(Bytes.toBytes(TABLENAME));
081      if (!tableCreated) {
082        assertFalse(client.tableExists(tTableName));
083        TTableDescriptor tTableDescriptor = new TTableDescriptor();
084        tTableDescriptor.setTableName(tTableName);
085        TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
086        columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
087        tTableDescriptor.addToColumns(columnFamilyDescriptor);
088        client.createTable(tTableDescriptor, new ArrayList<>());
089        tableCreated = true;
090      }
091      assertTrue(client.tableExists(tTableName));
092    } finally {
093      httpClient.close();
094    }
095  }
096}