Skip to content

Commit

Permalink
Merge pull request #2414 from AxonFramework/enhancement/conditional_b…
Browse files Browse the repository at this point in the history
…ean_in_actuator_auto_conf

Added ConditionalOnMissingBean to AutoConfiguration
  • Loading branch information
smcvb committed Oct 3, 2022
2 parents bf5e238 + f86c587 commit 453b5d0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Expand Up @@ -19,8 +19,11 @@
import org.axonframework.actuator.axonserver.AxonServerHealthIndicator;
import org.axonframework.actuator.axonserver.AxonServerStatusAggregator;
import org.axonframework.axonserver.connector.AxonServerConnectionManager;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.SimpleStatusAggregator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -40,11 +43,12 @@
@ConditionalOnProperty(name = "axon.axonserver.enabled", matchIfMissing = true)
public class AxonServerActuatorAutoConfiguration {

@ConditionalOnMissingBean(AxonServerHealthIndicator.class)
@Bean
public AxonServerHealthIndicator axonServerHealthIndicator(AxonServerConnectionManager connectionManager) {
return new AxonServerHealthIndicator(connectionManager);
}

@ConditionalOnMissingBean(SimpleStatusAggregator.class)
@Bean
public AxonServerStatusAggregator axonServerStatusAggregator() {
return new AxonServerStatusAggregator();
Expand Down
Expand Up @@ -19,10 +19,13 @@
import org.axonframework.actuator.axonserver.AxonServerHealthIndicator;
import org.axonframework.actuator.axonserver.AxonServerStatusAggregator;
import org.axonframework.axonserver.connector.AxonServerConnectionManager;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.SimpleStatusAggregator;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -73,10 +76,65 @@ void serviceIsIgnoredIfLibraryIsNotPresent() {
});
}

@Test
void axonServerHealthIndicatorCanBeExchangedForOwnBeans() {

// Test autoconfig adds beans if we don't specify any
testApplicationContext.withUserConfiguration(TestContext.class)
.withPropertyValues("axon.axonserver.enabled:true")
.run(context -> {
assertThat(context).hasSingleBean(SimpleStatusAggregator.class);
assertThat(context).getBean(SimpleStatusAggregator.class)
.isNotOfAnyClassIn(CustomSimpleStatusAggregator.class);

assertThat(context).hasSingleBean(AxonServerHealthIndicator.class);
assertThat(context).getBeanNames(AxonServerHealthIndicator.class)
.isNotOfAnyClassIn(CustomAxonServerServerHealthIndicator.class);
});

// Test Autoconfig does not add beans if we specify them
testApplicationContext.withUserConfiguration(TestContextWithCustomBean.class)
.withPropertyValues("axon.axonserver.enabled:true")
.run(context -> {
//existence
assertThat(context).hasSingleBean(CustomSimpleStatusAggregator.class);
assertThat(context).hasSingleBean(CustomAxonServerServerHealthIndicator.class);

// access over supertype
assertThat(context).getBean(SimpleStatusAggregator.class)
.isOfAnyClassIn(CustomSimpleStatusAggregator.class);
assertThat(context).getBean(AxonServerHealthIndicator.class)
.isOfAnyClassIn(CustomAxonServerServerHealthIndicator.class);
});
}

@ContextConfiguration
@EnableAutoConfiguration
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
private static class TestContext {

}

@ContextConfiguration
@EnableAutoConfiguration
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
private static class TestContextWithCustomBean {
@Bean
public CustomSimpleStatusAggregator customAxonServerStatusAggregator(){
return new CustomSimpleStatusAggregator();
}
@Bean
public CustomAxonServerServerHealthIndicator customAxonServerServerHealthIndicator(AxonServerConnectionManager connectionManager){
return new CustomAxonServerServerHealthIndicator(connectionManager);
}
}

}
class CustomSimpleStatusAggregator extends SimpleStatusAggregator {

}
class CustomAxonServerServerHealthIndicator extends AxonServerHealthIndicator {
public CustomAxonServerServerHealthIndicator(AxonServerConnectionManager connectionManager) {
super(connectionManager);
}
}

0 comments on commit 453b5d0

Please sign in to comment.