Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
62 changes: 62 additions & 0 deletions docs/modules/axonserver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Axon Server Containers

Testcontainers can be used to automatically instantiate and manage both [Axon Server SE](https://axoniq.io/product-overview/axon-server) and [Axon Server EE](https://axoniq.io/product-overview/axon-server-enterprise) containers.
It uses the official [docker images](https://hub.docker.com/u/axoniq) provided by AxonIQ.

## Benefits

* Running a single node Axon Server SE/EE with just one line of code

## Example

### Axon Server Standard Edition (SE)

Create an `AxonServerSEContainer` to use in your tests.

```java
final AxonServerSEContainer axonServerSEContainer =
new AxonServerSEContainer(DockerImageName.parse("axoniq/axonserver:4.4.12"));
```

This version is the simplest one and also included some utils methods to get the Axon Server Address. The only out of the box configuration provided is the `devMode` flag:
* `withDevMode` where you can specify if you want dev-mode to be enabled or not. Default is `false`

### Axon Server Enterprise Edition (EE)

Create an `AxonServerEEContainer` to use in your tests.

```java
final AxonServerEEContainer axonServerEEContainer =
new AxonServerEEContainer(DockerImageName.parse("axoniq/axonserver-enterprise:4.5.9-dev"));
```

This version is more complex and provides additional configuration listed below:
* `withLicense` where you can provide a path to your license file
* `withAutoCluster` where you can provide a path to your auto-cluster file
* `withConfiguration` where you can provide a path to your `axonserver.properties` file
* `withAxonServerName` where you can provide Axon Server's name
* `withAxonServerHostname` where you can provide Axon Server's hostname
* `withAxonServerInternalHostname` where you can provide Axon Server's internal hostname

It also includes some utils methods to get the Axon Server Address.

### Configuration

For an extensive list of environment variables you can use, please check the [official docs](https://docs.axoniq.io/reference-guide/v/master/axon-server/administration/admin-configuration/configuration#configuration-properties).

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:

```groovy tab='Gradle'
testImplementation "org.testcontainers:axonserver:{{latest_version}}"
```

```xml tab='Maven'
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>axonserver</artifactId>
<version>{{latest_version}}</version>
<scope>test</scope>
</dependency>
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ nav:
- modules/databases/postgres.md
- modules/databases/presto.md
- modules/databases/trino.md
- modules/axonserver.md
- modules/azure.md
- modules/docker_compose.md
- modules/elasticsearch.md
Expand Down
5 changes: 5 additions & 0 deletions modules/axonserver/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description = "Testcontainers :: AxonServer"

dependencies {
api project(':testcontainers')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.testcontainers.containers;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import java.util.Optional;

/**
* Constructs a single node AxonServer Enterprise Edition (EE) for testing.
*/
@Slf4j
public class AxonServerEEContainer<SELF extends AxonServerEEContainer<SELF>> extends GenericContainer<SELF> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("axoniq/axonserver-enterprise");
private static final int AXON_SERVER_HTTP_PORT = 8024;
private static final int AXON_SERVER_GRPC_PORT = 8124;

private static final String WAIT_FOR_LOG_MESSAGE = ".*Started AxonServer.*";

private static final String LICENCE_DEFAULT_LOCATION = "/axonserver/config/axoniq.license";
private static final String CONFIGURATION_DEFAULT_LOCATION = "/axonserver/config/axonserver.properties";
private static final String CLUSTER_TEMPLATE_DEFAULT_LOCATION = "/axonserver/cluster-template.yml";

private static final String AXONIQ_LICENSE = "AXONIQ_LICENSE";
private static final String AXONIQ_AXONSERVER_NAME = "AXONIQ_AXONSERVER_NAME";
private static final String AXONIQ_AXONSERVER_INTERNAL_HOSTNAME = "AXONIQ_AXONSERVER_INTERNAL_HOSTNAME";
private static final String AXONIQ_AXONSERVER_HOSTNAME = "AXONIQ_AXONSERVER_HOSTNAME";

private static final String AXON_SERVER_ADDRESS_TEMPLATE = "%s:%s";

private String licensePath;
private String configurationPath;
private String clusterTemplatePath;
private String axonServerName;
private String axonServerInternalHostname;
private String axonServerHostname;

public AxonServerEEContainer(@NonNull final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public AxonServerEEContainer(final DockerImageName dockerImageName) {
super(dockerImageName);

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

withExposedPorts(AXON_SERVER_HTTP_PORT, AXON_SERVER_GRPC_PORT);
waitingFor(Wait.forLogMessage(WAIT_FOR_LOG_MESSAGE, 1));
withEnv(AXONIQ_LICENSE, LICENCE_DEFAULT_LOCATION);
}

@Override
protected void configure() {
optionallyMapResourceParameterAsVolume(LICENCE_DEFAULT_LOCATION, licensePath);
optionallyMapResourceParameterAsVolume(CONFIGURATION_DEFAULT_LOCATION, configurationPath);
optionallyMapResourceParameterAsVolume(CLUSTER_TEMPLATE_DEFAULT_LOCATION, clusterTemplatePath);
withOptionalEnv(AXONIQ_AXONSERVER_NAME, axonServerName);
withOptionalEnv(AXONIQ_AXONSERVER_HOSTNAME, axonServerHostname);
withOptionalEnv(AXONIQ_AXONSERVER_INTERNAL_HOSTNAME, axonServerInternalHostname);
}

/**
* Map (effectively replace) directory in Docker with the content of resourceLocation if resource location is not
* null
* <p>
* Protected to allow for changing implementation by extending the class
*
* @param pathNameInContainer path in docker
* @param resourceLocation relative classpath to resource
*/
protected void optionallyMapResourceParameterAsVolume(String pathNameInContainer, String resourceLocation) {
Optional.ofNullable(resourceLocation)
.map(MountableFile::forClasspathResource)
.ifPresent(mountableFile -> withCopyFileToContainer(mountableFile, pathNameInContainer));
}

/**
* Set an environment value if the value is present.
* <p>
* Protected to allow for changing implementation by extending the class
*
* @param key environment key value, usually a constant
* @param value environment value to be set
*/
protected void withOptionalEnv(String key, String value) {
Optional.ofNullable(value)
.ifPresent(v -> withEnv(key, value));
}

/**
* Initialize AxonServer EE with a given license.
*/
public SELF withLicense(String licensePath) {
this.licensePath = licensePath;
return self();
}

/**
* Initialize AxonServer EE with a given configuration file.
*/
public SELF withConfiguration(String configurationPath) {
this.configurationPath = configurationPath;
return self();
}

/**
* Initialize AxonServer EE with a given cluster template configuration file.
*/
public SELF withClusterTemplate(String clusterTemplatePath) {
this.clusterTemplatePath = clusterTemplatePath;
return self();
}

/**
* Initialize AxonServer EE with a given Axon Server Name.
*/
public SELF withAxonServerName(String axonServerName) {
this.axonServerName = axonServerName;
return self();
}

/**
* Initialize AxonServer EE with a given Axon Server Internal Hostname.
*/
public SELF withAxonServerInternalHostname(String axonServerInternalHostname) {
this.axonServerInternalHostname = axonServerInternalHostname;
return self();
}

/**
* Initialize AxonServer EE with a given Axon Server Hostname.
*/
public SELF withAxonServerHostname(String axonServerHostname) {
this.axonServerHostname = axonServerHostname;
return self();
}

public Integer getGrpcPort() {
return this.getMappedPort(AXON_SERVER_GRPC_PORT);
}

public String getIPAddress() {
return this.getContainerIpAddress();
}

public String getAxonServerAddress() {
return String.format(AXON_SERVER_ADDRESS_TEMPLATE,
this.getContainerIpAddress(),
this.getMappedPort(AXON_SERVER_GRPC_PORT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.testcontainers.containers;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

/**
* Constructs a single node AxonServer Standard Edition (SE) for testing.
*/
@Slf4j
public class AxonServerSEContainer<SELF extends AxonServerSEContainer<SELF>> extends GenericContainer<SELF> {

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("axoniq/axonserver");
private static final int AXON_SERVER_HTTP_PORT = 8024;
private static final int AXON_SERVER_GRPC_PORT = 8124;

private static final String WAIT_FOR_LOG_MESSAGE = ".*Started AxonServer.*";

private static final String AXONIQ_AXONSERVER_DEVMODE_ENABLED = "AXONIQ_AXONSERVER_DEVMODE_ENABLED";

private static final String AXON_SERVER_ADDRESS_TEMPLATE = "%s:%s";

private boolean devMode;

public AxonServerSEContainer(@NonNull final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public AxonServerSEContainer(final DockerImageName dockerImageName) {
super(dockerImageName);

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

withExposedPorts(AXON_SERVER_HTTP_PORT, AXON_SERVER_GRPC_PORT);
waitingFor(Wait.forLogMessage(WAIT_FOR_LOG_MESSAGE, 1));
}

@Override
protected void configure() {
withEnv(AXONIQ_AXONSERVER_DEVMODE_ENABLED, String.valueOf(devMode));
}

/**
* Initialize AxonServer EE with a given license.
*/
public SELF withDevMode(boolean devMode) {
this.devMode = devMode;
return self();
}

public Integer getGrpcPort() {
return this.getMappedPort(AXON_SERVER_GRPC_PORT);
}

public String getIPAddress() {
return this.getContainerIpAddress();
}

public String getAxonServerAddress() {
return String.format(AXON_SERVER_ADDRESS_TEMPLATE,
this.getContainerIpAddress(),
this.getMappedPort(AXON_SERVER_GRPC_PORT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.testcontainers.containers;

import org.junit.*;
import org.testcontainers.utility.DockerImageName;


public class AxonServerEEContainerTest {

@Test
public void supportsAxonServer_4_5_X() {
try (
final AxonServerEEContainer axonServerEEContainer =
new AxonServerEEContainer(DockerImageName.parse("axoniq/axonserver-enterprise:4.5.9-dev"))
) {
axonServerEEContainer.start();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.testcontainers.containers;

import org.junit.*;
import org.testcontainers.utility.DockerImageName;


public class AxonServerSEContainerTest {

@Test
public void supportsAxonServer_4_4_X() {
try (
final AxonServerSEContainer axonServerSEContainer =
new AxonServerSEContainer(DockerImageName.parse("axoniq/axonserver:4.4.12"))
) {
axonServerSEContainer.start();
}
}

@Test
public void supportsAxonServer_4_5_X() {
try (
final AxonServerSEContainer axonServerSEContainer =
new AxonServerSEContainer(DockerImageName.parse("axoniq/axonserver:4.5.8"))
) {
axonServerSEContainer.start();
}
}
}
16 changes: 16 additions & 0 deletions modules/axonserver/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="DEBUG"/>
</configuration>