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.client.example; 019 020import static org.junit.Assert.assertEquals; 021 022import java.nio.charset.StandardCharsets; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.HBaseTestingUtility; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.testclassification.ClientTests; 027import org.apache.hadoop.hbase.testclassification.MediumTests; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.apache.http.HttpStatus; 030import org.apache.http.client.entity.EntityBuilder; 031import org.apache.http.client.methods.CloseableHttpResponse; 032import org.apache.http.client.methods.HttpGet; 033import org.apache.http.client.methods.HttpPut; 034import org.apache.http.entity.ContentType; 035import org.apache.http.impl.client.CloseableHttpClient; 036import org.apache.http.impl.client.HttpClientBuilder; 037import org.junit.AfterClass; 038import org.junit.BeforeClass; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042 043import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams; 044 045@Category({ ClientTests.class, MediumTests.class }) 046public class TestHttpProxyExample { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestHttpProxyExample.class); 051 052 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 053 054 private static final TableName TABLE_NAME = TableName.valueOf("test"); 055 056 private static final String FAMILY = "cf"; 057 058 private static final String QUALIFIER = "cq"; 059 060 private static final String URL_TEMPLCATE = "http://localhost:%d/%s/%s/%s:%s"; 061 062 private static final String ROW = "row"; 063 064 private static final String VALUE = "value"; 065 066 private static HttpProxyExample PROXY; 067 068 private static int PORT; 069 070 @BeforeClass 071 public static void setUp() throws Exception { 072 UTIL.startMiniCluster(1); 073 UTIL.createTable(TABLE_NAME, Bytes.toBytes(FAMILY)); 074 PROXY = new HttpProxyExample(UTIL.getConfiguration(), 0); 075 PROXY.start(); 076 PORT = PROXY.port(); 077 } 078 079 @AfterClass 080 public static void tearDown() throws Exception { 081 if (PROXY != null) { 082 PROXY.stop(); 083 } 084 UTIL.shutdownMiniCluster(); 085 } 086 087 @Test 088 public void test() throws Exception { 089 try (CloseableHttpClient client = HttpClientBuilder.create().build()) { 090 HttpPut put = new HttpPut( 091 String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); 092 put.setEntity(EntityBuilder.create().setText(VALUE) 093 .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build()); 094 try (CloseableHttpResponse resp = client.execute(put)) { 095 assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); 096 } 097 HttpGet get = new HttpGet( 098 String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); 099 try (CloseableHttpResponse resp = client.execute(get)) { 100 assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); 101 assertEquals("value", 102 Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent()))); 103 } 104 get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever", 105 FAMILY, QUALIFIER)); 106 try (CloseableHttpResponse resp = client.execute(get)) { 107 assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode()); 108 } 109 } 110 } 111}