I´m trying create a Integration test with qpid embedded memory broker.My idea is to create a component where developers don't have to do any configuration, I want to know if there is a way to create queues, exchanges and bindings automatically from the JSON configuration file. Below show java classes and JSON file configuration.
JSON File configuration: json_config.json
{
"name": "Embedded Broker",
"modelVersion": "8.0",
"authenticationproviders": [
{
"name": "hardcoded",
"type": "Plain",
"secureOnlyMechanisms": [],
"users": [
{
"name": "test",
"password": "test",
"type": "managed"
}
]
}
],
"ports": [
{
"name": "AMQP",
"port": "${qpid.amqp_port}",
"bindingAddress": "127.0.0.1",
"protocols": [
"AMQP_0_9_1"
],
"authenticationProvider": "hardcoded",
"virtualhostaliases": [
{
"name": "defaultAlias",
"type": "defaultAlias"
},{
"name": "nameAlias",
"type": "nameAlias"
}
]
}
],
"virtualhostnodes": [
{
"name": "localhost",
"type": "Memory",
"defaultVirtualHostNode": "true",
"virtualHostInitialConfiguration" : "{\"type\": \"Memory\",\"context\":{},\"name\":\"localhost\",\"nodeAutoCreationPolicies\":[{\"pattern\":\"[a-z]*.*.queue.test\",\"createdOnPublish\" : true,\"createdOnConsume\" : true,\"nodeType\":\"Queue\" },{\"pattern\":\"[a-z]*.*.exchange.test\",\"createdOnPublish\" : true,\"createdOnConsume\" : false,\"nodeType\":\"exchange\" }]}"
}
]
}
In above JSON file, I want declare a queue, exchange and binding in virtualhost part
EmbeddedInMemoryQpidBroker.java
public class EmbeddedInMemoryQpidBroker {
private static final String DEFAULT_INITIAL_CONFIGURATION_LOCATION = "rabbit/json_config.json";
private SystemLauncher systemLauncher;
public EmbeddedInMemoryQpidBroker() {
this.systemLauncher = new SystemLauncher();
}
public void start() throws Exception {
this.systemLauncher.startup(createSystemConfig());
}
public void shutdown() {
this.systemLauncher.shutdown();
}
private Map<String, Object> createSystemConfig() throws IllegalConfigurationException {
Map<String, Object> attributes = new HashMap<>();
URL initialConfigUrl = EmbeddedInMemoryQpidBroker.class.getClassLoader()
.getResource(DEFAULT_INITIAL_CONFIGURATION_LOCATION);
if (initialConfigUrl == null) {
throw new IllegalConfigurationException(
"Configuration location '" + DEFAULT_INITIAL_CONFIGURATION_LOCATION + "' not found");
}
attributes.put(SystemConfig.TYPE, "Memory");
attributes.put(SystemConfig.INITIAL_CONFIGURATION_LOCATION, initialConfigUrl.toExternalForm());
attributes.put(SystemConfig.STARTUP_LOGGED_TO_SYSTEM_OUT, true);
return attributes;
}
}
Test class
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AuthorizationApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = { "test-rabbit" })
public class StartAuthorizationMessageProducerIntegrationTest {
private static EmbeddedInMemoryQpidBroker brokerStarter;
/* CONSTANTS POPULATION */
private static String HOST = "localhost";
private static String USER = "test";
private static String PASS = "test";
private static String USER_BAD = "user_mal";
private StartAuthorizationMessage authorizationMessagePopulated = new StartAuthorizationMessage("/qpid", "1234",
"externalId", "CODE");
/* CONSTANTS POPULATION */
@Autowired
@Qualifier("startAuthorizationMessageProducer")
public StartAuthorizationMessageProducer messageProducer;
@Autowired
public RabbitAdmin rabbitAdmin;
private String queueName = "queue.test";
private String exchangeName = "exchange.test";
private String routingKey = "routing.test";
@BeforeClass
public static void startup() throws Exception {
brokerStarter = new EmbeddedInMemoryQpidBroker();
brokerStarter.start();
}
@AfterClass
public static void tearDown() throws Exception {
brokerStarter.shutdown();
;
}
@Test
public void givenQpidBrokerAndAGoodConnectionThenConnectionIsOpen() throws Exception {
// given
Connection connection = null;
ConnectionFactory connectionFactory = this.getConnectionFactory(HOST, USER, PASS);
// when
connection = connectionFactory.newConnection();
// then
assertTrue(connection.isOpen());
}
@Test
public void givenQpidBrokerAndAGoodConnectionWhenPutAMessaggerThenMessageIsQueue() throws Exception {
// given
StartAuthorizationMessage messageReturned;
createQueue();
createExchange();
createBinding();
// when
messageProducer.sendMessage(authorizationMessagePopulated, null);
// then
assertTrue(countMessages() > 0);
messageReturned = getMessage();
assertTrue(messageReturned != null);
assertTrue(messageReturned.getPath().equals(authorizationMessagePopulated.getPath()));
assertTrue(messageReturned.getId().equals(authorizationMessagePopulated.getId()));
assertTrue(messageReturned.getExternalAuthorizationId()
.equals(authorizationMessagePopulated.getExternalAuthorizationId()));
assertTrue(messageReturned.getOperationCode().equals(authorizationMessagePopulated.getOperationCode()));
assertTrue(countMessages() == 0);
}
@Test(expected = Exception.class)
public void givenQpidBrokerAndABadConnectionThenConnectionIsClose() throws Exception {
// given
Connection connection;
ConnectionFactory connectionFactory = this.getConnectionFactory(HOST, USER_BAD, PASS);
// when
connection = connectionFactory.newConnection();
// then
// exception throws
}
/* Support Methods */
private StartAuthorizationMessage getMessage() throws Exception {
StartAuthorizationMessage startAuthorizationMessage;
Message messageAMQP = rabbitAdmin.getRabbitTemplate().receive(queueName);
String strMessage = new String(messageAMQP.getBody());
JSONReader jsonReader = new JSONReader();
HashMap jsonValues = (HashMap)jsonReader.read(strMessage);
startAuthorizationMessage = new StartAuthorizationMessage((String)jsonValues.get("path"),
(String)jsonValues.get("id"), (String)jsonValues.get("externalAuthorizationId"),
(String)jsonValues.get("operationCode"));
return startAuthorizationMessage;
}
private int countMessages() throws Exception {
Properties rabbitProperties = rabbitAdmin.getQueueProperties(queueName);
return (int)rabbitProperties.get(RabbitAdmin.QUEUE_MESSAGE_COUNT);
}
private void createQueue() throws Exception {
Queue queue = new Queue(queueName);
rabbitAdmin.declareQueue(queue);
}
private void createExchange() {
rabbitAdmin.declareExchange(new DirectExchange(exchangeName));
}
private void createBinding() {
Binding declaredBinding = new Binding(queueName, DestinationType.QUEUE,
exchangeName, routingKey, null);
rabbitAdmin.declareBinding(declaredBinding);
}
private ConnectionFactory getConnectionFactory(String host, String userName, String password) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setUsername(userName);
connectionFactory.setPassword(password);
return connectionFactory;
}
/* Support Methods */
}
In test class I´m declare queue, exchange and binding with RabbitAdmin Class, but I want declare it in a JSON config file.
Thank´s for your help.