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.util.ArrayList;
021import java.util.function.Supplier;
022
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.TestThriftHttpServer;
027import org.apache.hadoop.hbase.thrift.ThriftServer;
028import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
029import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
030import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
031import org.apache.hadoop.hbase.thrift2.generated.TTableName;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.thrift.protocol.TBinaryProtocol;
034import org.apache.thrift.protocol.TProtocol;
035import org.apache.thrift.transport.THttpClient;
036import org.junit.Assert;
037import org.junit.ClassRule;
038import org.junit.experimental.categories.Category;
039
040@Category({ ClientTests.class, MediumTests.class})
041public class TestThrift2HttpServer extends TestThriftHttpServer {
042  private static final String TABLENAME = "TestThrift2HttpServerTable";
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestThrift2HttpServer.class);
047
048  @Override protected Supplier<ThriftServer> getThriftServerSupplier() {
049    return () -> new org.apache.hadoop.hbase.thrift2.ThriftServer(TEST_UTIL.getConfiguration());
050  }
051
052  @Override
053  protected void talkToThriftServer(String url, int customHeaderSize) throws Exception {
054    THttpClient httpClient = new THttpClient(url);
055    httpClient.open();
056
057    if (customHeaderSize > 0) {
058      StringBuilder sb = new StringBuilder();
059      for (int i = 0; i < customHeaderSize; i++) {
060        sb.append("a");
061      }
062      httpClient.setCustomHeader("User-Agent", sb.toString());
063    }
064
065    try {
066      TProtocol prot;
067      prot = new TBinaryProtocol(httpClient);
068      THBaseService.Client client = new THBaseService.Client(prot);
069      TTableName tTableName = new TTableName();
070      tTableName.setNs(Bytes.toBytes(""));
071      tTableName.setQualifier(Bytes.toBytes(TABLENAME));
072      if (!tableCreated){
073        Assert.assertTrue(!client.tableExists(tTableName));
074        TTableDescriptor tTableDescriptor = new TTableDescriptor();
075        tTableDescriptor.setTableName(tTableName);
076        TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
077        columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
078        tTableDescriptor.addToColumns(columnFamilyDescriptor);
079        client.createTable(tTableDescriptor, new ArrayList<>());
080        tableCreated = true;
081      }
082      Assert.assertTrue(client.tableExists(tTableName));
083    } finally {
084      httpClient.close();
085    }
086  }
087
088
089}