Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/amqp handle routing key #317

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions springwolf-examples/springwolf-amqp-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
1. Copy the `docker-compose.yml` file to your machine.
2. Run `$ docker-compose up`.
3. Visit `localhost:8080/springwolf/asyncapi-ui.html` or try the API: `$ curl localhost:8080/springwolf/docs`.
4. RabbitMQ Management: `http://localhost:15672` using `guest:guest` as login
2 changes: 2 additions & 0 deletions springwolf-examples/springwolf-amqp-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
testImplementation "com.vaadin.external.google:android-json:${androidJsonVersion}"

testImplementation "org.assertj:assertj-core:${assertjCoreVersion}"
testImplementation "org.awaitility:awaitility:${awaitilityVersion}"
testImplementation "org.mockito:mockito-core:${mockitoCoreVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"

testImplementation "org.springframework.boot:spring-boot-test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Queue anotherQueue() {

@Bean
public Queue exampleBindingsQueue() {
return new Queue("example-bindings-queue", false);
return new Queue("example-bindings-queue", false, true, true);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.github.stavshamir.springwolf.example.amqp.dtos.AnotherPayloadDto;
import io.github.stavshamir.springwolf.example.amqp.dtos.ExamplePayloadDto;
import io.github.stavshamir.springwolf.example.amqp.producers.ExampleProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
Expand All @@ -12,11 +14,15 @@

@Service
@Slf4j
@RequiredArgsConstructor
public class ExampleConsumer {

private final ExampleProducer exampleProducer;

@RabbitListener(queues = "example-queue")
public void receiveExamplePayload(ExamplePayloadDto payload) {
log.info("Received new message in example-queue: {}", payload.toString());
exampleProducer.sendMessage(payload);
}

@RabbitListener(queues = "another-queue")
Expand All @@ -27,8 +33,14 @@ public void receiveAnotherPayload(AnotherPayloadDto payload) {
@RabbitListener(
bindings = {
@QueueBinding(
exchange = @Exchange(name = "name", type = ExchangeTypes.TOPIC),
value = @Queue(name = "example-bindings-queue"))
exchange = @Exchange(name = "example-bindings-exchange-name", type = ExchangeTypes.TOPIC),
value =
@Queue(
name = "example-bindings-queue",
durable = "false",
exclusive = "true",
autoDelete = "true"),
key = "example-topic-routing-key")
})
public void bindingsExample(AnotherPayloadDto payload) {
log.info("Received new message in example-bindings-queue: {}", payload.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
package io.github.stavshamir.springwolf.example.amqp.dtos;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.NOT_REQUIRED;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Schema(description = "Another payload model")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AnotherPayloadDto {

@Schema(description = "Foo field", example = "bar", requiredMode = NOT_REQUIRED)
private String foo;

@Schema(description = "Example field", requiredMode = REQUIRED)
private ExamplePayloadDto example;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public ExamplePayloadDto getExample() {
return example;
}

public void setExample(ExamplePayloadDto example) {
this.example = example;
}

@Override
public String toString() {
return "AnotherPayloadDto{" + "foo='" + foo + '\'' + ", example=" + example + '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.github.stavshamir.springwolf.example.amqp.dtos;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Schema(description = "Example payload model")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExamplePayloadDto {
@Schema(description = "Some string field", example = "some string value", requiredMode = REQUIRED)
private String someString;
Expand All @@ -15,41 +21,9 @@ public class ExamplePayloadDto {
@Schema(description = "Some enum field", example = "FOO2", requiredMode = REQUIRED)
private ExampleEnum someEnum;

public String getSomeString() {
return someString;
}

public void setSomeString(String someString) {
this.someString = someString;
}

public long getSomeLong() {
return someLong;
}

public void setSomeLong(long someLong) {
this.someLong = someLong;
}

public ExampleEnum getSomeEnum() {
return someEnum;
}

public void setSomeEnum(ExampleEnum someEnum) {
this.someEnum = someEnum;
}

enum ExampleEnum {
public enum ExampleEnum {
FOO1,
FOO2,
FOO3
}

@Override
public String toString() {
return "ExamplePayloadDto{" + "someString='"
+ someString + '\'' + ", someLong="
+ someLong + ", someEnum="
+ someEnum + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AmqpAsyncOperationBinding;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncOperation;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.AsyncPublisher;
import io.github.stavshamir.springwolf.example.amqp.dtos.AnotherPayloadDto;
import io.github.stavshamir.springwolf.example.amqp.dtos.ExamplePayloadDto;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ExampleProducer {
private final RabbitTemplate rabbitTemplate;

@AsyncPublisher(
operation =
Expand All @@ -17,5 +22,7 @@ public class ExampleProducer {
@AmqpAsyncOperationBinding()
public void sendMessage(ExamplePayloadDto msg) {
// send
AnotherPayloadDto dto = new AnotherPayloadDto("fooValue", msg);
rabbitTemplate.convertAndSend("example-topic-exchange", "example-topic-routing-key", dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.stavshamir.springwolf.example.amqp;

import io.github.stavshamir.springwolf.example.amqp.consumers.ExampleConsumer;
import io.github.stavshamir.springwolf.example.amqp.dtos.ExamplePayloadDto;
import io.github.stavshamir.springwolf.producer.SpringwolfAmqpProducer;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.amqp.AmqpIOException;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;

import static io.github.stavshamir.springwolf.example.amqp.dtos.ExamplePayloadDto.ExampleEnum.FOO1;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

/**
* While the assertion of this test is identical to ApiIntegrationTests,
* the setup uses a full docker-compose context with a real sqs instance.
*/
@SpringBootTest(
classes = {SpringwolfAmqpExampleApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
@DirtiesContext
@TestMethodOrder(OrderAnnotation.class)
@TestPropertySource(properties = {"spring.rabbitmq.host=localhost"})
// @Ignore("Uncomment this line if you have issues running this test on your local machine.")
public class ProducerIntegrationWithDockerIntegrationTest {

@Autowired
SpringwolfAmqpProducer springwolfAmqpProducer;

@SpyBean
ExampleConsumer exampleConsumer;

@Container
public static DockerComposeContainer<?> environment =
new DockerComposeContainer<>(new File("docker-compose.yml")).withServices("amqp");

@Test
@Order(1)
void verifyAmqpIsAvailable() {
ConnectionFactory factory = new CachingConnectionFactory("localhost");

await().atMost(60, SECONDS).ignoreException(AmqpIOException.class).untilAsserted(() -> assertThat(
factory.createConnection().isOpen())
.isTrue());
}

@Test
@Order(2)
void producerCanUseSpringwolfConfigurationToSendMessage() {
// given
ExamplePayloadDto payload = new ExamplePayloadDto();
payload.setSomeString("foo");
payload.setSomeLong(5);
payload.setSomeEnum(FOO1);

// when
springwolfAmqpProducer.send("example-queue", payload);

// then
verify(exampleConsumer, timeout(10000)).receiveExamplePayload(payload);
}
}
Loading