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