Replace java amqp10 with javascript one

This commit is contained in:
Marcial Rosales 2024-11-27 10:40:23 +01:00
parent 167913743c
commit 0ba194ae53
8 changed files with 120 additions and 257 deletions

View File

@ -7,9 +7,6 @@ COPY package.json package.json
FROM base as test
RUN npm install
RUN mkdir -p /code/amqp10-roundtriptest
COPY amqp10-roundtriptest /code/amqp10-roundtriptest
RUN mvn -f /code/amqp10-roundtriptest package
ENTRYPOINT [ "npm" ]
CMD [ "" ]

View File

@ -1,103 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rabbitmq.amqp1_0</groupId>
<artifactId>amqp10-roundtriptest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>amqp10-roundtriptest</name>
<url>https://www.rabbitmq.com</url>
<properties>
<junit.jupiter.version>5.9.3</junit.jupiter.version>
<qpid-jms-client.version>2.3.0</qpid-jms-client.version>
<logback.version>1.2.13</logback.version>
<spotless.version>2.24.0</spotless.version>
<google-java-format.version>1.17.0</google-java-format.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>${qpid-jms-client.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.rabbitmq.amqp1_0.RoundTripTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.rabbitmq.amqp1_0.RoundTripTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<java>
<googleJavaFormat>
<version>${google-java-format.version}</version>
<style>GOOGLE</style>
</googleJavaFormat>
</java>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,16 +0,0 @@
#!/usr/bin/env bash
SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
env | grep RABBITMQ
if [[ -f "/code/amqp10-roundtriptest" ]]; then
echo "Running amqp10-roundtriptest inside mocha-test docker image ..."
java -jar /code/amqp10-roundtriptest-1.0-SNAPSHOT-jar-with-dependencies.jar $@
else
if [[ ! -f "${SCRIPT}/target/amqp10-roundtriptest-1.0-SNAPSHOT-jar-with-dependencies.jar" ]]; then
echo "Building amqp10-roundtriptest jar ..."
mvn -f amqp10-roundtriptest package $@
fi
echo "Running amqp10-roundtriptest jar ..."
java -jar ${SCRIPT}/target/amqp10-roundtriptest-1.0-SNAPSHOT-jar-with-dependencies.jar $@
fi

View File

@ -1,96 +0,0 @@
// vim:sw=4:et:
package com.rabbitmq.amqp1_0;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import jakarta.jms.*;
import java.util.*;
import javax.naming.Context;
/** Unit test for simple App. */
public class RoundTripTest {
public static String getEnv(String property, String defaultValue) {
return System.getenv(property) == null ? defaultValue : System.getenv(property);
}
public static String getEnv(String property) {
String value = System.getenv(property);
if (value == null) {
throw new IllegalArgumentException("Missing env variable " + property);
}
return value;
}
public static void main(String args[]) throws Exception {
String hostname = getEnv("RABBITMQ_HOSTNAME", "localhost");
String port = getEnv("RABBITMQ_AMQP_PORT", "5672");
String scheme = getEnv("RABBITMQ_AMQP_SCHEME", "amqp");
String uri = scheme + "://" + hostname + ":" + port;
String username = args.length > 0 ? args[0] : getEnv("RABBITMQ_AMQP_USERNAME", "guest");
String password = args.length > 1 ? args[1] : getEnv("RABBITMQ_AMQP_PASSWORD", "guest");
boolean usemtls = Boolean.parseBoolean(getEnv("AMQP_USE_MTLS", "false"));
if ("amqps".equals(scheme)) {
List<String> connectionParams = new ArrayList<String>();
String certsLocation = getEnv("RABBITMQ_CERTS");
connectionParams.add("transport.trustStoreLocation=" + certsLocation + "/truststore.jks");
connectionParams.add("transport.trustStorePassword=foobar");
connectionParams.add("transport.verifyHost=true");
connectionParams.add("transport.trustAll=true");
if (usemtls) {
connectionParams.add("amqp.saslMechanisms=EXTERNAL");
connectionParams.add("transport.keyStoreLocation=" + certsLocation + "/client_rabbitmq.jks");
connectionParams.add("transport.keyStorePassword=foobar");
connectionParams.add("transport.keyAlias=client-rabbitmq-tls");
}
if (!connectionParams.isEmpty()) {
uri = uri + "?" + String.join("&", connectionParams);
System.out.println("Using AMQP URI " + uri);
}
}
assertNotNull(uri);
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
env.put("connectionfactory.myFactoryLookup", uri);
env.put("queue.myQueueLookup", "my-queue");
env.put("jms.sendTimeout", 5);
env.put("jms.requestTimeout", 5);
javax.naming.Context context = new javax.naming.InitialContext(env);
ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
Destination queue = (Destination) context.lookup("myQueueLookup");
try (Connection connection =
createConnection(factory, usemtls, username, password)) {
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
MessageConsumer messageConsumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello world!");
messageProducer.send(
message,
DeliveryMode.NON_PERSISTENT,
Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE);
TextMessage receivedMessage = (TextMessage) messageConsumer.receive(2000L);
assertEquals(message.getText(), receivedMessage.getText());
}
}
private static Connection createConnection(ConnectionFactory factory,
boolean usemtls, String username, String password) throws jakarta.jms.JMSException {
if (usemtls) {
return factory.createConnection();
}
return factory.createConnection(username, password);
}
}

View File

@ -6,7 +6,6 @@
"scripts": {
"fakeportal": "node fakeportal/app.js",
"fakeproxy": "node fakeportal/proxy.js",
"amqp10_roundtriptest": "eval $(cat $ENV_FILE ) && amqp10-roundtriptest/run",
"test": " eval $(cat $ENV_FILE ) && mocha --recursive --trace-warnings --timeout 40000"
},
"keywords": [],

72
selenium/test/amqp.js Normal file
View File

@ -0,0 +1,72 @@
var container = require('rhea') // https://github.com/amqp/rhea
var fs = require('fs');
var path = require('path');
function getAmqpConnectionOptions() {
return {
'host': process.env.RABBITMQ_HOSTNAME || 'rabbitmq',
'port': process.env.RABBITMQ_AMQP_PORT || 5672,
'username' : process.env.RABBITMQ_AMQP_USERNAME || 'guest',
'password' : process.env.RABBITMQ_AMQP_PASSWORD || 'guest',
'id': "selenium-connection-id",
'container_id': "selenium-container-id"
}
}
function getAmqpsConnectionOptions() {
let options = getAmqpConnectionOptions()
let useMtls = process.env.AMQP_USE_MTLS || false
if (useMtls) {
options['enable_sasl_external'] = true
}
options['transport'] = 'tls'
let certsLocation = getEnv("RABBITMQ_CERTS");
options['key'] = fs.readFileSync(path.resolve(certsLocation,'client_rabbitmq_key.pem'))
options['cert'] = fs.readFileSync(path.resolve(certsLocation,'client_rabbitmq_certificate.pem'))
options['ca'] = fs.readFileSync(path.resolve(certsLocation,'ca_rabbitmq_certificate.pem'))
}
function getConnectionOptions() {
switch(process.env.RABBITMQ_AMQP_SCHEME || 'amqp'){
case 'amqp':
return getAmqpConnectionOptions()
case 'amqps':
return getAmqpsConnectionOptions()
}
}
module.exports = {
open: () => {
let promise = new Promise((resolve, reject) => {
container.on('connection_open', function(context) {
resolve()
})
})
let connection = container.connect(getConnectionOptions())
let receiver = connection.open_receiver({
source: 'examples',
target: 'receiver-target',
name: 'receiver-link'
})
let sender = connection.open_sender({
target: 'examples',
source: 'sender-source',
name: 'sender-link'
})
return {
'connection': connection,
'promise' : promise,
'receiver' : receiver,
'sender' : sender
}
},
close: (connection) => {
if (connection != null) {
connection.close()
}
},
once: (event, callback) => {
container.once(event, callback)
},
on: (event, callback) => {
container.on(event, callback)
}
}

View File

@ -1,7 +1,21 @@
const assert = require('assert')
const { tokenFor, openIdConfiguration } = require('../utils')
const { reset, expectUser, expectVhost, expectResource, allow, verifyAll } = require('../mock_http_backend')
const {execSync} = require('child_process')
const { open: openAmqp, once: onceAmqp, on: onAmqp, close: closeAmqp } = require('../amqp')
var receivedAmqpMessageCount = 0
var untilConnectionEstablished = new Promise((resolve, reject) => {
onAmqp('connection_open', function(context) {
resolve()
})
})
onAmqp('message', function (context) {
receivedAmqpMessageCount++
})
onceAmqp('sendable', function (context) {
context.sender.send({body:'first message'})
})
const profiles = process.env.PROFILES || ""
var backends = ""
@ -15,10 +29,8 @@ describe('Having AMQP 1.0 protocol enabled and the following auth_backends: ' +
let expectations = []
let username = process.env.RABBITMQ_AMQP_USERNAME
let password = process.env.RABBITMQ_AMQP_PASSWORD
let usemtls = process.env.AMQP_USE_MTLS
let amqpClientCommand = "npm run amqp10_roundtriptest" +
(usemtls ? "" : " " + username + " " + password)
let amqp;
before(function () {
if (backends.includes("http") && username.includes("http")) {
reset()
@ -39,13 +51,29 @@ describe('Having AMQP 1.0 protocol enabled and the following auth_backends: ' +
}
})
it('can open an AMQP 1.0 connection', function () {
console.log(execSync(amqpClientCommand).toString())
it('can open an AMQP 1.0 connection', async function () {
amqp = openAmqp()
await untilConnectionEstablished
var untilMessageReceived = new Promise((resolve, reject) => {
onAmqp('message', function(context) {
resolve()
})
})
amqp.sender.send({body:'second message'})
await untilMessageReceived
assert.equal(2, receivedAmqpMessageCount)
})
after(function () {
if ( backends.includes("http") ) {
verifyAll(expectations)
if ( backends.includes("http") ) {
verifyAll(expectations)
}
try {
if (amqp != null) {
closeAmqp(amqp.connection)
}
} catch (error) {
console.error("Failed to close amqp10 connection due to " + error);
}
})
})

View File

@ -1,6 +1,7 @@
const { By, Key, until, Builder } = require('selenium-webdriver')
require('chromedriver')
const assert = require('assert')
const { open: openAmqp, once: onceAmqp, on: onAmqp, close: closeAmqp } = require('../../amqp')
const { buildDriver, goToHome, captureScreensFor, teardown, delay } = require('../../utils')
const LoginPage = require('../../pageobjects/LoginPage')
@ -8,18 +9,17 @@ const OverviewPage = require('../../pageobjects/OverviewPage')
const ConnectionsPage = require('../../pageobjects/ConnectionsPage')
const ConnectionPage = require('../../pageobjects/ConnectionPage')
var container = require('rhea') // https://github.com/amqp/rhea
var receivedAmqpMessageCount = 0
var untilConnectionEstablished = new Promise((resolve, reject) => {
container.on('connection_open', function(context) {
onAmqp('connection_open', function(context) {
resolve()
})
})
container.on('message', function (context) {
onAmqp('message', function (context) {
receivedAmqpMessageCount++
})
container.once('sendable', function (context) {
onceAmqp('sendable', function (context) {
context.sender.send({body:'first message'})
})
@ -28,7 +28,7 @@ describe('Given an amqp10 connection opened, listed and clicked on it', function
let captureScreen
let connectionsPage
let connectionPage
let connection
let amqp
before(async function () {
driver = buildDriver()
@ -41,24 +41,8 @@ describe('Given an amqp10 connection opened, listed and clicked on it', function
await login.login('monitoring-only', 'guest')
await overview.isLoaded()
connection = container.connect(
{'host': process.env.RABBITMQ_HOSTNAME || 'rabbitmq',
'port': process.env.RABBITMQ_AMQP_PORT || 5672,
'username' : process.env.RABBITMQ_AMQP_USERNAME || 'guest',
'password' : process.env.RABBITMQ_AMQP_PASSWORD || 'guest',
'id': "selenium-connection-id",
'container_id': "selenium-container-id"
})
connection.open_receiver({
source: 'examples',
target: 'receiver-target',
name: 'receiver-link'
})
sender = connection.open_sender({
target: 'examples',
source: 'sender-source',
name: 'sender-link'
})
amqp = openAmqp()
await untilConnectionEstablished
await overview.clickOnConnectionsTab()
await connectionsPage.isLoaded()
@ -108,11 +92,11 @@ describe('Given an amqp10 connection opened, listed and clicked on it', function
it('display live link information', async function () {
var untilMessageReceived = new Promise((resolve, reject) => {
container.on('message', function(context) {
onAmqp('message', function(context) {
resolve()
})
})
sender.send({body:'second message'})
amqp.sender.send({body:'second message'})
await untilMessageReceived
assert.equal(2, receivedAmqpMessageCount)
@ -121,16 +105,14 @@ describe('Given an amqp10 connection opened, listed and clicked on it', function
let incomingLink = connectionPage.getIncomingLinkInfo(sessions.incoming_links, 0)
assert.equal(2, incomingLink.deliveryCount)
//console.log("incomingLink: " + JSON.stringify(incomingLink))
//console.log("outgoingLink: " + JSON.stringify(outgoingLink))
})
after(async function () {
await teardown(driver, this, captureScreen)
try {
if (connection != null) {
connection.close()
if (amqp != null) {
closeAmqp(amqp.connection)
}
} catch (error) {
console.error("Failed to close amqp10 connection due to " + error);