Skip to content

Commit

Permalink
fix: Fixed core tests (#540)
Browse files Browse the repository at this point in the history
* fix: Fixed core tests

Fixed the failing tests after migrating to AsyncAPI v3

* fix: Fixed AMQP integration

The AMQP was adding both 'exchange' & 'queue' keys, but it should only include the
one related with the binding type.

Also, deliveryMode can be ONLY 1 or 2, the
default value 0 which was used in different places is wrong.

* fix: Fixed AMQP integration

The AMQP was adding both 'exchange' & 'queue' keys, but it should only include the
one related with the binding type.

Also, deliveryMode can be ONLY 1 or 2, the
default value 0 which was used in different places is wrong.
  • Loading branch information
ctasada authored Jan 14, 2024
1 parent ed0514e commit 397163e
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AMQPBindingTest {
private static final DefaultAsyncApiSerializer serializer = new DefaultAsyncApiSerializer();

@Test
void shouldSerializeAMQPChannelBinding() throws IOException {
void shouldSerializeAMQPChannelBindingQueue() throws IOException {

var asyncapi = AsyncAPI.builder()
.channels(Map.of(
Expand All @@ -30,13 +30,36 @@ void shouldSerializeAMQPChannelBinding() throws IOException {
.bindings(Map.of(
"amqp",
AMQPChannelBinding.builder()
.is(AMQPChannelType.ROUTING_KEY)
.is(AMQPChannelType.QUEUE)
.queue(AMQPChannelQueueProperties.builder()
.name("my-queue-name")
.durable(true)
.exclusive(true)
.autoDelete(false)
.build())
.build()))
.build()))
.build();

// Uses https://github.com/asyncapi/bindings/blob/master/amqp/README.md#example
var example = ClasspathUtil.parseYamlFile("/v3/bindings/amqp/amqp-channel-queue.yaml");
assertThatJson(serializer.toJsonString(asyncapi))
.whenIgnoringPaths("asyncapi", "operations")
.isEqualTo(example);
}

@Test
void shouldSerializeAMQPChannelBindingRouting() throws IOException {

var asyncapi = AsyncAPI.builder()
.channels(Map.of(
"userSignup",
ChannelObject.builder()
.address("user/signup")
.bindings(Map.of(
"amqp",
AMQPChannelBinding.builder()
.is(AMQPChannelType.ROUTING_KEY)
.exchange(AMQPChannelExchangeProperties.builder()
.name("myExchange")
.type(AMQPChannelExchangeType.TOPIC)
Expand All @@ -48,7 +71,7 @@ void shouldSerializeAMQPChannelBinding() throws IOException {
.build();

// Uses https://github.com/asyncapi/bindings/blob/master/amqp/README.md#example
var example = ClasspathUtil.parseYamlFile("/v3/bindings/amqp/amqp-channel.yaml");
var example = ClasspathUtil.parseYamlFile("/v3/bindings/amqp/amqp-channel-routing.yaml");
assertThatJson(serializer.toJsonString(asyncapi))
.whenIgnoringPaths("asyncapi", "operations")
.isEqualTo(example);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
channels:
userSignup:
address: 'user/signup'
bindings:
amqp:
is: queue
queue:
name: my-queue-name
durable: true
exclusive: true
autoDelete: false
vhost: /
bindingVersion: 0.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ channels:
bindings:
amqp:
is: routingKey
queue:
name: my-queue-name
durable: true
exclusive: true
autoDelete: false
vhost: /
exchange:
name: myExchange
type: topic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,15 @@ void scan_componentHasOnlyDeclaredMethods(Class<?> clazz) {
Map<String, ChannelObject> actualChannels = channelScanner.scanChannels();

// Then the returned collection contains the channel with the actual method, excluding type erased methods
var messagePayload = MessagePayload.of(MultiFormatSchema.builder()
.schema(SchemaReference.fromSchema(String.class.getSimpleName()))
.build());
MessageObject message = MessageObject.builder()
.messageId(String.class.getName())
.name(String.class.getName())
.title(String.class.getSimpleName())
.description(null)
// .payload(PayloadReference.fromModelName(String.class.getSimpleName())) FIXME
// .schemaFormat("application/vnd.oai.openapi+json;version=3.0.0") FIXME
.payload(messagePayload)
.headers(
MessageHeaders.of(MessageReference.fromSchema(AsyncHeaders.NOT_DOCUMENTED.getSchemaName())))
.bindings(EMPTY_MAP)
Expand All @@ -418,11 +420,10 @@ void scan_componentHasOnlyDeclaredMethods(Class<?> clazz) {
.description("test channel operation description")
.title("test-channel_publish")
.bindings(EMPTY_MAP)
// .message(message) FIXME
.build();

ChannelObject expectedChannel = ChannelObject.builder()
.bindings(null) /*.publish(operation) FIXME*/
.bindings(null)
.messages(Map.of(message.getMessageId(), message))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.amqp.AMQPMessageBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.amqp.AMQPOperationBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.ChannelObject;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageHeaders;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageObject;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessagePayload;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageReference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.SchemaReference;
import io.github.stavshamir.springwolf.schemas.SchemasService;
Expand Down Expand Up @@ -91,8 +93,7 @@ void scan_componentHasTestListenerMethods() {
.name(String.class.getName())
.title(String.class.getSimpleName())
.payload(payload)
// FIXME
// .headers(MessageHeaders.of(MessageReference.fromSchema(AsyncHeaders.NOT_DOCUMENTED.getSchemaName())))
.headers(MessageHeaders.of(MessageReference.fromSchema(AsyncHeaders.NOT_DOCUMENTED.getSchemaName())))
.bindings(defaultMessageBinding)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ void scan_componentHasTestListenerMethods_multiplePayloads() {

ChannelObject expectedChannelItem2 = ChannelObject.builder()
.bindings(defaultChannelBinding)
.messages(Map.of(messageString.getMessageId(), messageSimpleFoo))
.messages(Map.of(messageString.getMessageId(), messageString))
.build();

assertThat(channels)
.containsExactly(
.containsExactlyInAnyOrder(
Map.entry(TestBindingFactory.CHANNEL, expectedChannelItem),
Map.entry(TestBindingFactory.CHANNEL, expectedChannelItem2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,18 @@ void scan_componentHasMultipleTestListenerMethods() {
.toList();

// then
MessagePayload payload = MessagePayload.of(MultiFormatSchema.builder()
MessagePayload stringPayload = MessagePayload.of(MultiFormatSchema.builder()
.schema(SchemaReference.fromSchema(String.class.getSimpleName()))
.build());
MessagePayload simpleFooPayload = MessagePayload.of(MultiFormatSchema.builder()
.schema(SchemaReference.fromSchema(SimpleFoo.class.getSimpleName()))
.build());

MessageObject stringMessage = MessageObject.builder()
.messageId(String.class.getName())
.name(String.class.getName())
.title(String.class.getSimpleName())
.payload(payload)
.payload(stringPayload)
.headers(MessageHeaders.of(
MessageReference.fromSchema(AsyncHeadersNotDocumented.NOT_DOCUMENTED.getSchemaName())))
.bindings(defaultMessageBinding)
Expand All @@ -141,19 +144,24 @@ void scan_componentHasMultipleTestListenerMethods() {
.messageId(SimpleFoo.class.getName())
.name(SimpleFoo.class.getName())
.title(SimpleFoo.class.getSimpleName())
.payload(payload)
.payload(simpleFooPayload)
.headers(MessageHeaders.of(
MessageReference.fromSchema(AsyncHeadersNotDocumented.NOT_DOCUMENTED.getSchemaName())))
.bindings(defaultMessageBinding)
.build();

ChannelObject expectedChannelItem = ChannelObject.builder()
ChannelObject methodChannel = ChannelObject.builder()
.bindings(defaultChannelBinding)
.messages(Map.of(stringMessage.getMessageId(), stringMessage))
.build();
ChannelObject anotherMethodChannel = ChannelObject.builder()
.bindings(defaultChannelBinding)
.messages(Map.of(
stringMessage.getMessageId(), stringMessage, simpleFooMessage.getMessageId(), simpleFooMessage))
.messages(Map.of(simpleFooMessage.getMessageId(), simpleFooMessage))
.build();

assertThat(channels).containsExactly(Map.entry(CHANNEL, expectedChannelItem));
assertThat(channels)
.containsExactlyInAnyOrderElementsOf(
List.of(Map.entry(CHANNEL, methodChannel), Map.entry(CHANNEL, anotherMethodChannel)));
}

private static class ClassWithMultipleTestListenerAnnotation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@
"bindings": {
"amqp": {
"is": "queue",
"exchange": {
"name": "",
"type": "direct",
"durable": true,
"autoDelete": false,
"vhost": "/"
},
"queue": {
"name": "another-queue",
"durable": false,
Expand Down Expand Up @@ -114,13 +107,6 @@
"bindings": {
"amqp": {
"is": "queue",
"exchange": {
"name": "",
"type": "direct",
"durable": true,
"autoDelete": false,
"vhost": "/"
},
"queue": {
"name": "example-queue",
"durable": false,
Expand Down Expand Up @@ -163,13 +149,6 @@
"autoDelete": false,
"vhost": "/"
},
"queue": {
"name": "example-bindings-queue",
"durable": false,
"exclusive": true,
"autoDelete": true,
"vhost": "/"
},
"bindingVersion": "0.3.0"
}
}
Expand Down Expand Up @@ -223,13 +202,6 @@
"autoDelete": false,
"vhost": "/"
},
"queue": {
"name": "multi-payload-queue",
"durable": true,
"exclusive": false,
"autoDelete": false,
"vhost": "/"
},
"bindingVersion": "0.3.0"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
@Override
public Optional<ProcessedMessageBinding> process(Method method) {
return Arrays.stream(method.getAnnotations())
.filter(annotation -> annotation instanceof AmqpAsyncOperationBinding)
.map(annotation -> (AmqpAsyncOperationBinding) annotation)
.filter(AmqpAsyncOperationBinding.class::isInstance)
.map(AmqpAsyncOperationBinding.class::cast)
.findAny()
.map(this::mapToMessageBinding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ public static Map<String, ChannelBinding> buildChannelBinding(
RabbitListener annotation, StringValueResolver resolver, RabbitListenerUtilContext context) {
AMQPChannelBinding.AMQPChannelBindingBuilder channelBinding = AMQPChannelBinding.builder();

channelBinding.queue(buildQueueProperties(annotation, resolver, context));

String exchangeName = getExchangeName(annotation, resolver, context);
channelBinding.exchange(buildExchangeProperties(annotation, exchangeName, context));
if (exchangeName.isEmpty()) {
channelBinding.is(AMQPChannelType.QUEUE);
channelBinding.queue(buildQueueProperties(annotation, resolver, context));
} else {
channelBinding.is(AMQPChannelType.ROUTING_KEY);
channelBinding.exchange(buildExchangeProperties(annotation, exchangeName, context));
}

return Map.of("amqp", channelBinding.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void mapToOperationBindingTest() throws NoSuchMethodException {
.isEqualTo(AMQPOperationBinding.builder()
.cc(List.of())
.priority(0)
.deliveryMode(0)
.deliveryMode(1)
.mandatory(false)
.timestamp(false)
.ack(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ void buildChannelBinding(Class<?> clazz) {
assertEquals(
AMQPChannelBinding.builder()
.is(AMQPChannelType.QUEUE)
.exchange(AMQPChannelExchangeProperties.builder()
.name("")
.type(AMQPChannelExchangeType.DIRECT)
.durable(true)
.autoDelete(false)
.vhost("/")
.build())
.queue(AMQPChannelQueueProperties.builder()
.name("queue-1")
.durable(true)
Expand Down Expand Up @@ -177,13 +170,6 @@ void buildChannelBinding() {
.durable(true)
.autoDelete(false)
.build())
.queue(AMQPChannelQueueProperties.builder()
.name("queue-1")
.durable(true)
.autoDelete(false)
.exclusive(false)
.vhost("/")
.build())
.build(),
channelBinding.get("amqp"));
}
Expand Down Expand Up @@ -267,13 +253,6 @@ void buildChannelBinding() {
.durable(true)
.autoDelete(false)
.build())
.queue(AMQPChannelQueueProperties.builder()
.name("queue-1")
.durable(true)
.autoDelete(false)
.exclusive(false)
.vhost("/")
.build())
.build(),
channelBinding.get("amqp"));
}
Expand Down Expand Up @@ -374,13 +353,6 @@ void buildChannelBinding() {
.durable(false)
.autoDelete(true)
.build())
.queue(AMQPChannelQueueProperties.builder()
.name("queue-1")
.durable(false)
.autoDelete(true)
.exclusive(true)
.vhost("/")
.build())
.build(),
channelBinding.get("amqp"));
}
Expand Down

0 comments on commit 397163e

Please sign in to comment.