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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.List;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036
037@Category({ MiscTests.class, SmallTests.class })
038public class TestCoprocessorDescriptor {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestCoprocessorDescriptor.class);
043
044  @Rule
045  public TestName name = new TestName();
046
047  @Test
048  public void testBuild() {
049    String className = "className";
050    String path = "path";
051    int priority = 100;
052    String propertyKey = "propertyKey";
053    String propertyValue = "propertyValue";
054    CoprocessorDescriptor cp = CoprocessorDescriptorBuilder.newBuilder(className).setJarPath(path)
055      .setPriority(priority).setProperty(propertyKey, propertyValue).build();
056    assertEquals(className, cp.getClassName());
057    assertEquals(path, cp.getJarPath().get());
058    assertEquals(priority, cp.getPriority());
059    assertEquals(1, cp.getProperties().size());
060    assertEquals(propertyValue, cp.getProperties().get(propertyKey));
061  }
062
063  @Test
064  public void testSetCoprocessor() throws IOException {
065    String propertyKey = "propertyKey";
066    List<CoprocessorDescriptor> cps = new ArrayList<>();
067    for (String className : Arrays.asList("className0", "className1", "className2")) {
068      String path = "path";
069      int priority = Math.abs(className.hashCode());
070      String propertyValue = "propertyValue";
071      cps.add(CoprocessorDescriptorBuilder.newBuilder(className).setJarPath(path)
072        .setPriority(priority).setProperty(propertyKey, propertyValue).build());
073    }
074    TableDescriptor tableDescriptor = TableDescriptorBuilder
075      .newBuilder(TableName.valueOf(name.getMethodName())).setCoprocessors(cps).build();
076    for (CoprocessorDescriptor cp : cps) {
077      boolean match = false;
078      for (CoprocessorDescriptor that : tableDescriptor.getCoprocessorDescriptors()) {
079        if (cp.getClassName().equals(that.getClassName())) {
080          assertEquals(cp.getJarPath().get(), that.getJarPath().get());
081          assertEquals(cp.getPriority(), that.getPriority());
082          assertEquals(cp.getProperties().size(), that.getProperties().size());
083          assertEquals(cp.getProperties().get(propertyKey), that.getProperties().get(propertyKey));
084          match = true;
085          break;
086        }
087      }
088      if (!match) {
089        fail("expect:" + cp + ", actual:" + tableDescriptor.getCoprocessorDescriptors());
090      }
091    }
092  }
093}