|
| 1 | +package org.testcontainers.nats; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | +import org.testcontainers.utility.DockerImageName; |
| 6 | + |
| 7 | +/** |
| 8 | + * Testcontainers implementation for NATS. |
| 9 | + * <p> |
| 10 | + * Supported image: {@code nats} |
| 11 | + * <p> |
| 12 | + * Exposed ports: |
| 13 | + * <ul> |
| 14 | + * <li>4222 (Client connections)</li> |
| 15 | + * <li>6222 (Cluster/Route connections)</li> |
| 16 | + * <li>8222 (HTTP management/monitoring)</li> |
| 17 | + * </ul> |
| 18 | + */ |
| 19 | +public class NatsContainer extends GenericContainer<NatsContainer> { |
| 20 | + |
| 21 | + /** |
| 22 | + * Default port for NATS client connections. |
| 23 | + */ |
| 24 | + public static final int DEFAULT_NATS_CLIENT_PORT = 4222; |
| 25 | + |
| 26 | + /** |
| 27 | + * Default port for NATS cluster/route connections. |
| 28 | + */ |
| 29 | + public static final int DEFAULT_NATS_ROUTING_PORT = 6222; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default port for NATS HTTP monitoring. |
| 33 | + */ |
| 34 | + public static final int DEFAULT_NATS_HTTP_MONITORING_PORT = 8222; |
| 35 | + |
| 36 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("nats"); |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a NATS container using a specific docker image name. |
| 40 | + * |
| 41 | + * @param dockerImageName The docker image to use. |
| 42 | + */ |
| 43 | + public NatsContainer(String dockerImageName) { |
| 44 | + this(DockerImageName.parse(dockerImageName)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a NATS container using a specific docker image. |
| 49 | + * |
| 50 | + * @param dockerImageName The docker image to use. |
| 51 | + */ |
| 52 | + public NatsContainer(DockerImageName dockerImageName) { |
| 53 | + super(dockerImageName); |
| 54 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 55 | + |
| 56 | + withExposedPorts( |
| 57 | + DEFAULT_NATS_CLIENT_PORT, |
| 58 | + DEFAULT_NATS_ROUTING_PORT, |
| 59 | + DEFAULT_NATS_HTTP_MONITORING_PORT); |
| 60 | + waitingFor(Wait.forLogMessage(".*Server is ready.*", 1)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Gets the port for client connections. |
| 65 | + * |
| 66 | + * @return The mapped port for client connections. |
| 67 | + */ |
| 68 | + public Integer getClientPort() { |
| 69 | + return getMappedPort(DEFAULT_NATS_CLIENT_PORT); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Gets the port for cluster/route connections. |
| 74 | + * |
| 75 | + * @return The mapped port for routing connections. |
| 76 | + */ |
| 77 | + public Integer getRoutingPort() { |
| 78 | + return getMappedPort(DEFAULT_NATS_ROUTING_PORT); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Gets the port for HTTP monitoring. |
| 83 | + * |
| 84 | + * @return The mapped port for HTTP monitoring. |
| 85 | + */ |
| 86 | + public Integer getHttpMonitoringPort() { |
| 87 | + return getMappedPort(DEFAULT_NATS_HTTP_MONITORING_PORT); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the NATS connection URL. |
| 92 | + * |
| 93 | + * @return NATS URL for client connections in the format nats://host:port |
| 94 | + */ |
| 95 | + public String getConnectionUrl() { |
| 96 | + return String.format("nats://%s:%d", getHost(), getClientPort()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets the NATS monitoring endpoint URL. |
| 101 | + * |
| 102 | + * @return HTTP URL for monitoring endpoint in the format http://host:port |
| 103 | + */ |
| 104 | + public String getHttpMonitoringUrl() { |
| 105 | + return String.format("http://%s:%d", getHost(), getHttpMonitoringPort()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Enables JetStream for the NATS server. |
| 110 | + * |
| 111 | + * @return This container instance |
| 112 | + */ |
| 113 | + public NatsContainer withJetStream() { |
| 114 | + withCommand("--jetstream"); |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Configures authentication with username and password. |
| 120 | + * |
| 121 | + * @param username The username for authentication |
| 122 | + * @param password The password for authentication |
| 123 | + * @return This container instance |
| 124 | + */ |
| 125 | + public NatsContainer withAuth(String username, String password) { |
| 126 | + withCommand("--user", username, "--pass", password); |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Enables debug logging for the NATS server. |
| 132 | + * |
| 133 | + * @return This container instance |
| 134 | + */ |
| 135 | + public NatsContainer withDebug() { |
| 136 | + withCommand("-D"); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Enables protocol tracing for the NATS server. |
| 142 | + * |
| 143 | + * @return This container instance |
| 144 | + */ |
| 145 | + public NatsContainer withProtocolTracing() { |
| 146 | + withCommand("-V"); |
| 147 | + return this; |
| 148 | + } |
| 149 | +} |
0 commit comments