Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.ha;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* @generated by Cursor
* @description : Unit Test cases for ActiveStateChangeHandler
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestActiveStateChangeHandler {
@Test
void testInstanceIsActive_negative_implementationMayThrow() {
ActiveStateChangeHandler handler = new ThrowingHandler(true, false);

Assertions.assertThrows(Exception.class, handler::instanceIsActive);
}

@Test
void testInstanceIsActive_positive_invokesWithoutError() throws Exception {
ActiveStateChangeHandler handler = new RecordingHandler();

Assertions.assertDoesNotThrow(handler::instanceIsActive);
}

@Test
void testInstanceIsPassive_negative_implementationMayThrow() {
ActiveStateChangeHandler handler = new ThrowingHandler(false, true);

Assertions.assertThrows(Exception.class, handler::instanceIsPassive);
}

@Test
void testInstanceIsPassive_positive_invokesWithoutError() throws Exception {
ActiveStateChangeHandler handler = new RecordingHandler();

Assertions.assertDoesNotThrow(handler::instanceIsPassive);
}

private static final class RecordingHandler implements ActiveStateChangeHandler {
@Override
public void instanceIsActive() {
}

@Override
public void instanceIsPassive() {
}
}

private static final class ThrowingHandler implements ActiveStateChangeHandler {
private final boolean failActive;
private final boolean failPassive;

private ThrowingHandler(boolean failActive, boolean failPassive) {
this.failActive = failActive;
this.failPassive = failPassive;
}

@Override
public void instanceIsActive() throws Exception {
if (failActive) {
throw new Exception("active");
}
}

@Override
public void instanceIsPassive() throws Exception {
if (failPassive) {
throw new Exception("passive");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.ha;

import org.apache.hadoop.conf.Configuration;
import org.apache.ranger.RangerHAInitializer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Field;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerHAInitializer
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerHAInitializer {
@Test
void testConfigurationConstructor_negative_swallowsInitFailure() throws Exception {
resetHaSingletons();

Configuration configuration = Mockito.mock(Configuration.class);

Mockito.when(configuration.get(ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenThrow(new RuntimeException("fail"));

StubRangerHAInitializer initializer = new StubRangerHAInitializer(configuration);

Assertions.assertNull(initializer.readServiceState());
}

@Test
void testConfigurationConstructor_positive_populatesHaComponents() throws Exception {
resetHaSingletons();

Configuration configuration = new Configuration(false);

configuration.set("ranger.service.name", "ri");
configuration.set("ri.server.ha.enabled", "false");

StubRangerHAInitializer initializer = new StubRangerHAInitializer(configuration);

Assertions.assertNotNull(initializer.readServiceState());
Assertions.assertNotNull(initializer.readCuratorFactory());
Assertions.assertNotNull(initializer.readActiveInstanceState());
}

@Test
void testInit_negative_nullConfigurationSkipsSingletonsButCreatesActiveInstanceState() throws Exception {
resetHaSingletons();

StubRangerHAInitializer initializer = new StubRangerHAInitializer();

Assertions.assertDoesNotThrow(() -> initializer.init(null));

Assertions.assertNull(initializer.readServiceState());
Assertions.assertNull(initializer.readCuratorFactory());
Assertions.assertNotNull(initializer.readActiveInstanceState());
}

@Test
void testInit_positive_wiresServiceStateCuratorAndActiveState() throws Exception {
resetHaSingletons();

Configuration configuration = new Configuration(false);

configuration.set("ranger.service.name", "ri");
configuration.set("ri.server.ha.enabled", "false");

StubRangerHAInitializer initializer = new StubRangerHAInitializer();

initializer.init(configuration);

Assertions.assertNotNull(initializer.readServiceState());
Assertions.assertNotNull(initializer.readCuratorFactory());
Assertions.assertNotNull(initializer.readActiveInstanceState());
Assertions.assertSame(initializer.readServiceState(), ServiceState.getInstance(configuration));
Assertions.assertSame(initializer.readCuratorFactory(), CuratorFactory.getInstance(configuration));
Assertions.assertTrue(initializer.readActiveInstanceState() instanceof ActiveInstanceState);
}

@Test
void testNoArgConstructor_positive_buildsInstance() {
StubRangerHAInitializer initializer = new StubRangerHAInitializer();

Assertions.assertNotNull(initializer);
}

@Test
void testRangerHaInitializer_classLoadsAndDeclaresStopMethod() throws Exception {
Assertions.assertNotNull(RangerHAInitializer.class.getDeclaredMethod("stop"));
Assertions.assertNotNull(RangerHAInitializer.class.getDeclaredMethod("init", Configuration.class));
}

@Test
void testStop_positive_invokesSubclassImplementation() {
StubRangerHAInitializer initializer = new StubRangerHAInitializer();

Assertions.assertDoesNotThrow(initializer::stop);
}

private static void resetHaSingletons() throws Exception {
resetStaticField(ServiceState.class, "instance");
resetStaticField(CuratorFactory.class, "instance");
}

private static void resetStaticField(Class<?> clazz, String fieldName) throws Exception {
Field field = clazz.getDeclaredField(fieldName);

field.setAccessible(true);
field.set(null, null);
}

private static final class StubRangerHAInitializer extends RangerHAInitializer {
private StubRangerHAInitializer() {
}

private StubRangerHAInitializer(Configuration configuration) {
super(configuration);
}

@Override
public void stop() {
}

private ServiceState readServiceState() {
return serviceState;
}

private CuratorFactory readCuratorFactory() {
return curatorFactory;
}

private ActiveInstanceState readActiveInstanceState() {
return activeInstanceState;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.ranger.ha.annotation;

import org.apache.ranger.ha.service.ServiceManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* @generated by Cursor
* @description : Unit Test cases for HAService
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestHAService {
@Test
void testTypeMarker_negative_notPresentOnUnannotatedClass() {
Assertions.assertNull(String.class.getAnnotation(HAService.class));
}

@Test
void testTypeMarker_positive_presentOnServiceManager() {
Assertions.assertNotNull(ServiceManager.class.getAnnotation(HAService.class));
}
}
Loading
Loading