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