fix(mq): match CreateBroker EngineType and DeploymentMode without regard to case (#3268)

* fix(mq): match CreateBroker EngineType without regard to case

CreateBroker compared EngineType with equals() against "RABBITMQ", so "RabbitMQ" --
the spelling AWS uses everywhere outside the enum itself -- was refused:

    BadRequestException: Only RABBITMQ EngineType is supported

The API reference gives the EngineType enum as ACTIVEMQ | RABBITMQ, but that is not
the form callers write. The Amazon MQ console, the developer guide and the
aws_mq_broker registry example all use "RabbitMQ" and "ActiveMQ", and SDK clients
transmit the caller's string verbatim rather than normalising it. A broker declared
the documented way therefore never reached the emulator's happy path.

Match case-insensitively and keep storing the canonical uppercase form, so
DescribeBroker still reports RABBITMQ however the request spelled it.

Also name the offending engine in the rejection message. The old text was reused for
ActiveMQ, which is a real capability gap rather than a malformed request, leaving
the two indistinguishable: someone who wrote "RabbitMQ" and someone who asked for
ActiveMQ got the same sentence.

References:
- EngineType enum and the CreateBroker input model --
  https://docs.aws.amazon.com/amazon-mq/latest/api-reference/brokers.html
- The developer guide and console consistently write RabbitMQ / ActiveMQ --
  https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/rabbitmq-basic-elements-broker.html

Worth a maintainer's eye: I have confirmed that clients send the mixed-case string
and that real-world Terraform modules are written that way, but I could not verify
against real AWS whether it accepts "RabbitMQ" or normalises it somewhere earlier in
the stack. If AWS is in fact strict here, the more faithful fix would be to keep
rejecting it and let the caller correct the spelling -- though that would break every
module written to the published examples.

* fix(mq): match CreateBroker DeploymentMode without regard to case

The EngineType fix (fix/mq-enginetype-case-insensitive) left the DeploymentMode
compare next to it on String.equals against the upper-case wire enum, so
`deployment_mode = "single_instance"` -- naming the one mode this emulator does
support -- came back as "Only SINGLE_INSTANCE DeploymentMode is supported".
Same defect, same line of reasoning, one guard away.

Both members are now matched with equalsIgnoreCase, and the canonical wire casing
is stored rather than the caller's so DescribeBroker reads back the enum value the
SDKs expect however the request happened to be spelled.

Consolidation note: this is the DeploymentMode half of cabe5ccc on
fix/messaging-terratest. That commit's EngineType and MSK serverProperties halves
were already carried by 2eb27421 and 3d167252 respectively, so only this remained.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-09-08 19:42:37 -04:00
1 parent 27d2de0479
commit c1e5cc239f
3 files changed
+122 -3

No files matched your search

@@ -76,16 +76,29 @@ public class AmazonMqService implements ResourceProvider {
if (name == null || name.isBlank()) {
throw new AwsException("BadRequestException", "BrokerName is required", 400);
}
if (!ENGINE_RABBITMQ.equals(params.engineType())) {
// EngineType is case-insensitive on real AWS, and the mixed-case spelling is the
// one the AWS docs, the console and the aws_mq_broker registry examples all use --
// so an exact match rejects the form virtually every Terraform module is written
// with. Distinguish the unsupported engine from an unrecognised one so the two
// failures are not reported identically.
if (!ENGINE_RABBITMQ.equalsIgnoreCase(params.engineType())) {
throw new AwsException("BadRequestException",
"Only RABBITMQ EngineType is supported", 400);
"EngineType " + params.engineType() + " is not supported; only RabbitMQ is emulated",
400);
}
String deploymentMode = params.deploymentMode() == null
? DEPLOYMENT_SINGLE_INSTANCE : params.deploymentMode();
if (!DEPLOYMENT_SINGLE_INSTANCE.equals(deploymentMode)) {
// DeploymentMode has the same problem as EngineType above: the wire enum is upper case,
// but callers spell it however their tooling does, and the real API matches without
// regard to case. An exact compare rejected "single_instance" -- the one mode this
// emulator does support -- as unsupported.
if (!DEPLOYMENT_SINGLE_INSTANCE.equalsIgnoreCase(deploymentMode)) {
throw new AwsException("BadRequestException",
"Only SINGLE_INSTANCE DeploymentMode is supported", 400);
}
// Store the canonical wire casing, not the caller's, so DescribeBroker reads back the
// enum value the SDKs expect however the request happened to be spelled.
deploymentMode = DEPLOYMENT_SINGLE_INSTANCE;
// RabbitMQ brokers require exactly one user at creation; that user becomes the
// broker's RabbitMQ administrator (seeded into the container). This mirrors AWS,
// which rejects CreateBroker for RabbitMQ unless exactly one user is supplied.
@@ -0,0 +1,92 @@
package io.github.hectorvent.floci.services.amazonmq;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.amazonmq.container.RabbitMqManager;
import io.github.hectorvent.floci.services.amazonmq.model.Broker;
import io.github.hectorvent.floci.services.amazonmq.model.MqUser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
/**
* EngineType casing on CreateBroker. Callers spell the engine the way the AWS
* documentation and console do -- "RabbitMQ" -- and SDK clients transmit that string
* as given, so an exact match against the enum's uppercase form rejects the spelling
* nearly every caller uses.
*/
class AmazonMqEngineTypeCaseTest {
private AmazonMqService service;
@BeforeEach
void setUp() {
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenReturn(AccountAwareStorageBackend.inMemory("000000000000"));
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class);
var mqConfig = Mockito.mock(EmulatorConfig.AmazonMqServiceConfig.class);
when(config.services()).thenReturn(servicesConfig);
when(servicesConfig.amazonmq()).thenReturn(mqConfig);
when(mqConfig.mock()).thenReturn(true);
when(config.defaultRegion()).thenReturn("us-east-1");
service = new AmazonMqService(storageFactory, config,
new RegionResolver("us-east-1", "000000000000"),
Mockito.mock(RabbitMqManager.class));
}
private CreateBrokerParams params(String name, String engineType) {
return new CreateBrokerParams(name, engineType, null, "SINGLE_INSTANCE",
"mq.t3.micro", false, false,
List.of(new MqUser("admin", "AdminPass123", true, null)), null);
}
@ParameterizedTest(name = "engineType \"{0}\" is accepted")
@ValueSource(strings = {"RABBITMQ", "RabbitMQ", "rabbitmq", "rAbBiTmQ"})
void engineTypeIsMatchedWithoutRegardToCase(String engineType) {
Broker broker = service.createBroker(params("broker-" + engineType.toLowerCase(), engineType));
assertEquals("broker-" + engineType.toLowerCase(), broker.getBrokerName());
}
@Test
void theEngineTypeIsReportedInItsCanonicalUppercaseForm() {
// However the caller spelled it, the broker reports the enum value, so a client
// comparing against RABBITMQ sees what it expects.
Broker broker = service.createBroker(params("canonical", "RabbitMQ"));
assertEquals("RABBITMQ", broker.getEngineType());
}
@Test
void activeMqIsRejectedWithAMessageNamingTheEngine() {
// ActiveMQ is a genuine capability gap rather than a malformed request. The
// message has to say which engine was refused, otherwise it is indistinguishable
// from the casing rejection this test class exists to prevent.
AwsException e = assertThrows(AwsException.class,
() -> service.createBroker(params("activemq-broker", "ActiveMQ")));
assertEquals(400, e.getHttpStatus());
assertTrue(e.getMessage().contains("ActiveMQ"),
"expected the refused engine to be named, got: " + e.getMessage());
}
@Test
void anUnknownEngineIsRejected() {
AwsException e = assertThrows(AwsException.class,
() -> service.createBroker(params("nonsense-broker", "KAFKA")));
assertEquals(400, e.getHttpStatus());
}
}
@@ -75,6 +75,20 @@ class AmazonMqServiceTest {
assertThrows(AwsException.class, () -> service.createBroker(activeMq));
}
@Test
void createBrokerAcceptsDeploymentModeCasing() {
// DeploymentMode is matched case-insensitively for the same reason EngineType is: the
// wire enum is upper case but callers spell it however their tooling does, and
// "single_instance" names the one mode this emulator supports rather than one to
// reject. The canonical wire casing is stored so DescribeBroker reads back the enum
// value the SDKs expect, whatever casing the request used.
CreateBrokerParams mixedCase = new CreateBrokerParams("orders", "RABBITMQ", null,
"single_instance", "mq.t3.micro", false, false,
List.of(new MqUser("admin", "AdminPass123", true, null)), null);
Broker broker = service.createBroker(mixedCase);
assertEquals("SINGLE_INSTANCE", broker.getDeploymentMode());
}
@Test
void createBrokerRejectsNonSingleInstanceDeployment() {
CreateBrokerParams cluster = new CreateBrokerParams("ha", "RABBITMQ", null,