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.coprocessor;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
027import org.apache.hadoop.hbase.master.MasterServices;
028import org.apache.hadoop.hbase.master.assignment.MockMasterServices;
029import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.jupiter.api.AfterEach;
032import org.junit.jupiter.api.BeforeEach;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035
036/**
037 * Test CoreCoprocessor Annotation works giving access to facility not usually available. Test
038 * MasterCoprocessor.
039 */
040@Tag(CoprocessorTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestCoreMasterCoprocessor {
043  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
044  private MasterServices ms;
045  private MasterCoprocessorHost mch;
046
047  @BeforeEach
048  public void before() throws Exception {
049    this.ms = new MockMasterServices(HTU.getConfiguration());
050    this.mch = new MasterCoprocessorHost(this.ms, HTU.getConfiguration());
051    this.mch.preMasterInitialization();
052  }
053
054  @AfterEach
055  public void after() throws IOException {
056    this.mch.preStopMaster();
057  }
058
059  /**
060   * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a
061   * MasterServices instance after some gymnastics.
062   */
063  public static class NotCoreMasterCoprocessor implements MasterCoprocessor {
064    public NotCoreMasterCoprocessor() {
065    }
066  }
067
068  /**
069   * Annotate with CoreCoprocessor. This should make it so I can get at instance of a MasterServices
070   * instance after some gymnastics.
071   */
072  @CoreCoprocessor
073  public static class CoreMasterCoprocessor implements MasterCoprocessor {
074    public CoreMasterCoprocessor() {
075    }
076  }
077
078  /**
079   * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to access
080   * a MasterServices instance. Assert the opposite too. Do it to MasterCoprocessors.
081   */
082  @Test
083  public void testCoreRegionCoprocessor() throws IOException {
084    MasterCoprocessorEnvironment env =
085      this.mch.load(null, NotCoreMasterCoprocessor.class.getName(), 0, HTU.getConfiguration());
086    assertFalse(env instanceof HasMasterServices);
087    env = this.mch.load(null, CoreMasterCoprocessor.class.getName(), 1, HTU.getConfiguration());
088    assertTrue(env instanceof HasMasterServices);
089    assertEquals(this.ms, ((HasMasterServices) env).getMasterServices());
090  }
091}