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.ipc;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.ResultScanner;
031import org.apache.hadoop.hbase.client.Scan;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.RPCTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.AfterClass;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044
045@Category({ RPCTests.class, MediumTests.class })
046public class TestNettyRpcServer {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestNettyRpcServer.class);
051
052  @Rule
053  public TestName name = new TestName();
054  private static HBaseTestingUtility TEST_UTIL;
055
056  private static TableName TABLE;
057  private static byte[] FAMILY = Bytes.toBytes("f1");
058  private static byte[] PRIVATE_COL = Bytes.toBytes("private");
059  private static byte[] PUBLIC_COL = Bytes.toBytes("public");
060
061  @Before
062  public void setup() {
063    TABLE = TableName.valueOf(name.getMethodName());
064  }
065
066  @BeforeClass
067  public static void setupBeforeClass() throws Exception {
068    TEST_UTIL = new HBaseTestingUtility();
069    TEST_UTIL.getConfiguration().set(
070        RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
071        NettyRpcServer.class.getName());
072    TEST_UTIL.startMiniCluster();
073  }
074
075  @AfterClass
076  public static void tearDownAfterClass() throws Exception {
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  @Test
081  public void testNettyRpcServer() throws Exception {
082    final Table table = TEST_UTIL.createTable(TABLE, FAMILY);
083    try {
084      // put some test data
085      List<Put> puts = new ArrayList<Put>(100);
086      for (int i = 0; i < 100; i++) {
087        Put p = new Put(Bytes.toBytes(i));
088        p.addColumn(FAMILY, PRIVATE_COL, Bytes.toBytes("secret " + i));
089        p.addColumn(FAMILY, PUBLIC_COL, Bytes.toBytes("info " + i));
090        puts.add(p);
091      }
092      table.put(puts);
093
094      // read to verify it.
095      Scan scan = new Scan();
096      scan.setCaching(16);
097      ResultScanner rs = table.getScanner(scan);
098      int rowcnt = 0;
099      for (Result r : rs) {
100        rowcnt++;
101        int rownum = Bytes.toInt(r.getRow());
102        assertTrue(r.containsColumn(FAMILY, PRIVATE_COL));
103        assertEquals("secret " + rownum,
104            Bytes.toString(r.getValue(FAMILY, PRIVATE_COL)));
105        assertTrue(r.containsColumn(FAMILY, PUBLIC_COL));
106        assertEquals("info " + rownum,
107            Bytes.toString(r.getValue(FAMILY, PUBLIC_COL)));
108      }
109      assertEquals("Expected 100 rows returned", 100, rowcnt);
110    } finally {
111      table.close();
112    }
113  }
114
115}