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