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;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Category({ MiscTests.class, SmallTests.class })
040public class TestCoprocessorDescriptor {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestCoprocessorDescriptor.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestCoprocessorDescriptor.class);
047
048  @Rule
049  public TestName name = new TestName();
050
051  @Test
052  public void testBuild() {
053    String className = "className";
054    String path = "path";
055    int priority = 100;
056    String propertyKey = "propertyKey";
057    String propertyValue = "propertyValue";
058    CoprocessorDescriptor cp =
059      CoprocessorDescriptorBuilder.newBuilder(className).setJarPath(path).setPriority(priority)
060        .setProperty(propertyKey, propertyValue).build();
061    assertEquals(className, cp.getClassName());
062    assertEquals(path, cp.getJarPath().get());
063    assertEquals(priority, cp.getPriority());
064    assertEquals(1, cp.getProperties().size());
065    assertEquals(propertyValue, cp.getProperties().get(propertyKey));
066  }
067
068  @Test
069  public void testSetCoprocessor() throws IOException {
070    String propertyKey = "propertyKey";
071    List<CoprocessorDescriptor> cps = new ArrayList<>();
072    for (String className : Arrays.asList("className0", "className1", "className2")) {
073      String path = "path";
074      int priority = Math.abs(className.hashCode());
075      String propertyValue = "propertyValue";
076      cps.add(
077        CoprocessorDescriptorBuilder.newBuilder(className).setJarPath(path).setPriority(priority)
078          .setProperty(propertyKey, propertyValue).build());
079    }
080    TableDescriptor tableDescriptor =
081      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
082        .setCoprocessors(cps).build();
083    for (CoprocessorDescriptor cp : cps) {
084      boolean match = false;
085      for (CoprocessorDescriptor that : tableDescriptor.getCoprocessorDescriptors()) {
086        if (cp.getClassName().equals(that.getClassName())) {
087          assertEquals(cp.getJarPath().get(), that.getJarPath().get());
088          assertEquals(cp.getPriority(), that.getPriority());
089          assertEquals(cp.getProperties().size(), that.getProperties().size());
090          assertEquals(cp.getProperties().get(propertyKey), that.getProperties().get(propertyKey));
091          match = true;
092          break;
093        }
094      }
095      if (!match) {
096        fail("expect:" + cp + ", actual:" + tableDescriptor.getCoprocessorDescriptors());
097      }
098    }
099  }
100}