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.security.access; 019 020import static org.junit.jupiter.api.Assertions.assertThrows; 021import static org.mockito.Mockito.mock; 022 023import org.apache.hadoop.hbase.WriteAttemptedOnReadOnlyClusterException; 024import org.apache.hadoop.hbase.coprocessor.ObserverContext; 025import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 026import org.apache.hadoop.hbase.testclassification.SecurityTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.jupiter.api.AfterEach; 029import org.junit.jupiter.api.BeforeEach; 030import org.junit.jupiter.api.Tag; 031import org.junit.jupiter.api.Test; 032 033import org.apache.hbase.thirdparty.com.google.protobuf.Message; 034import org.apache.hbase.thirdparty.com.google.protobuf.Service; 035 036// Tests methods of Endpoint Observer which are implemented in ReadOnlyController, 037// by mocking the coprocessor environment and dependencies. 038@Tag(SecurityTests.TAG) 039@Tag(SmallTests.TAG) 040public class TestReadOnlyControllerEndpointObserver { 041 042 EndpointReadOnlyController endpointReadOnlyController; 043 // Region Server Coprocessor mocking variables. 044 ObserverContext<? extends RegionCoprocessorEnvironment> ctx; 045 Service service; 046 String methodName; 047 Message request; 048 049 @BeforeEach 050 public void setup() throws Exception { 051 endpointReadOnlyController = new EndpointReadOnlyController(); 052 053 // mocking variables initialization 054 ctx = mock(ObserverContext.class); 055 service = mock(Service.class); 056 methodName = "testMethod"; 057 request = mock(Message.class); 058 059 // Linking the mocks 060 } 061 062 @AfterEach 063 public void tearDown() throws Exception { 064 065 } 066 067 @Test 068 public void testPreEndpointInvocationReadOnlyException() { 069 assertThrows(WriteAttemptedOnReadOnlyClusterException.class, () -> { 070 endpointReadOnlyController.preEndpointInvocation(ctx, service, methodName, request); 071 }); 072 } 073}