Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Add tests for the AxonServerConnectionManager
Add tests for the AxonServerHealthIndicator
Add tests for the AxonServerActuatorAutoConfiguration

#1964
  • Loading branch information
smcvb committed Feb 24, 2022
1 parent 980bdf4 commit a9fafdc
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,9 +29,7 @@
import org.axonframework.axonserver.connector.util.TcpUtil;
import org.axonframework.common.AxonConfigurationException;
import org.axonframework.config.TagsConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -41,11 +39,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.axonframework.axonserver.connector.utils.AssertUtils.assertWithin;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit tests for {@link AxonServerConnectionManager}.
Expand Down Expand Up @@ -313,6 +307,31 @@ void testDisconnectSingleConnection() {
assertEquals(1, secondNode.getPlatformService().getNumberOfCompletedStreams());
}

@Test
void testConnectionsReturnsConnectionStatus() {
AxonServerConnectionManager testSubject = AxonServerConnectionManager.builder()
.axonServerConfiguration(testConfig)
.build();

AxonServerConnection channelOne = testSubject.getConnection(TEST_CONTEXT);
AxonServerConnection channelTwo = testSubject.getConnection("some-other-context");

assertWithin(250, TimeUnit.MILLISECONDS, () -> {
assertTrue(channelOne.isReady());
assertTrue(channelTwo.isReady());
});

Map<String, Boolean> results = testSubject.connections();

Boolean testContextConnection = results.get(TEST_CONTEXT);
assertNotNull(testContextConnection);
assertTrue(testContextConnection);

Boolean someOtherContextConnection = results.get("some-other-context");
assertNotNull(someOtherContextConnection);
assertTrue(someOtherContextConnection);
}

@Test
void testBuildWithNullAxonServerConfigurationThrowsAxonConfigurationException() {
AxonServerConnectionManager.Builder builderTestSubject = AxonServerConnectionManager.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AxonServerHealthIndicator extends AbstractHealthIndicator {
/**
* Constructs this health indicator, extracting health information from the given {@code connectionManager}.
*
* @param connectionManager The Axon Server CONNECTION manager to extract health information from.
* @param connectionManager The Axon Server connection manager to extract health information from.
*/
public AxonServerHealthIndicator(AxonServerConnectionManager connectionManager) {
this.connectionManager = connectionManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed 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.axonframework.actuator;

import org.axonframework.axonserver.connector.AxonServerConnectionManager;
import org.junit.jupiter.api.*;
import org.springframework.boot.actuate.health.Health;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Test class validating the {@link AxonServerHealthIndicator}.
*
* @author Steven van Beelen
*/
class AxonServerHealthIndicatorTest {

@Test
void testDoHealthCheck() {
String testContextOne = "context-one";
String testContextTwo = "context-two";
String expectedDetailsContextOne = testContextOne + ".connection.active";
String expectedDetailsContextTwo = testContextTwo + ".connection.active";

Map<String, Boolean> testConnections = new HashMap<>();
testConnections.put(testContextOne, true);
testConnections.put(testContextTwo, false);

AxonServerConnectionManager connectionManager = mock(AxonServerConnectionManager.class);
when(connectionManager.connections()).thenReturn(testConnections);

AxonServerHealthIndicator testSubject = new AxonServerHealthIndicator(connectionManager);

Health.Builder healthBuilder = new Health.Builder();

testSubject.doHealthCheck(healthBuilder);

Health result = healthBuilder.build();
Map<String, Object> details = result.getDetails();
assertFalse(details.isEmpty());
assertEquals(2, details.size());
Object detailsContextOne = details.get(expectedDetailsContextOne);
assertNotNull(detailsContextOne);
assertTrue((Boolean) detailsContextOne);
Object detailsContextTwo = details.get(expectedDetailsContextTwo);
assertNotNull(detailsContextTwo);
assertFalse((Boolean) detailsContextTwo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed 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.axonframework.springboot.autoconfig;

import org.axonframework.actuator.AxonServerHealthIndicator;
import org.junit.jupiter.api.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.test.context.ContextConfiguration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test class validating the {@link AxonServerActuatorAutoConfiguration}.
*
* @author Steven van Beelen
*/
class AxonServerActuatorAutoConfigurationTest {

private ApplicationContextRunner testApplicationContext;

@BeforeEach
void setUp() {
testApplicationContext = new ApplicationContextRunner();
}

@Test
void testAxonServerHealthIndicatorIsNotCreatedForAxonServerDisabled() {
testApplicationContext.withUserConfiguration(TestContext.class)
.withPropertyValues("axon.axonserver.enabled:false")
.run(context -> assertThat(context).doesNotHaveBean(AxonServerHealthIndicator.class));
}

@Test
void testAxonServerHealthIndicatorIsCreated() {
testApplicationContext.withUserConfiguration(TestContext.class)
.withPropertyValues("axon.axonserver.enabled:true")
.run(context -> assertThat(context).hasSingleBean(AxonServerHealthIndicator.class));
}

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

}
}

0 comments on commit a9fafdc

Please sign in to comment.