From 544b364dd4cb286e26c6276a3dc85a014aa4eab1 Mon Sep 17 00:00:00 2001 From: Ben Antony Date: Wed, 9 Feb 2022 20:03:07 +0100 Subject: [PATCH] Improve example --- README.md | 2 +- docs/asyncapi-example.yaml | 78 ++++++++++++++++++ j2asyncapi-example/pom.xml | 58 +++++++++++++ .../example/ExampleApplication.java | 57 +++++++++++++ .../example/AsyncApiGeneratorTest.java | 81 +++++++++++++++++++ pom.xml | 1 + 6 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 docs/asyncapi-example.yaml create mode 100644 j2asyncapi-example/pom.xml create mode 100644 j2asyncapi-example/src/main/java/lu/greenhalos/j2asyncapi/example/ExampleApplication.java create mode 100644 j2asyncapi-example/src/test/java/lu/greenhalos/j2asyncapi/example/AsyncApiGeneratorTest.java diff --git a/README.md b/README.md index 0b90f37..565affb 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ A lib which helps you generate `asyncapi.yaml` from your Java code based on anno ``` -2. Copy [this test](./j2asyncapi-processor/src/test/java/lu/greenhalos/j2asyncapi/annoations/WriteToFileTest.java) +2. Copy [this test](./j2asyncapi-example/src/test/java/lu/greenhalos/j2asyncapi/example/AsyncApiGeneratorTest.java) from this Repository to your code base 3. Change `ExampleBaseApplication.class` with a valid base class of yours e.g. your SpringBootApplication class with the main method. diff --git a/docs/asyncapi-example.yaml b/docs/asyncapi-example.yaml new file mode 100644 index 0000000..2ae8f47 --- /dev/null +++ b/docs/asyncapi-example.yaml @@ -0,0 +1,78 @@ +asyncapi: "2.0.0" +info: + title: "Application API" + version: "0.1.0" + contact: + name: "Fancy Team" + url: "https://greenhalos.lu" + email: "asyncapi@greenhalos.lu" +servers: + test: + url: "http://rabbitmq" + protocol: "amqp" + description: "RabbitMQ Server" +channels: + exchange/routing.key: + description: "Description explaining exactly what happens here" + subscribe: + message: + $ref: "#/components/messages/l.g.j.e.ExampleApplication$ExamplePublisherMessage" + publish: + message: + $ref: "#/components/messages/l.g.j.e.ExampleApplication$ExampleListenerMessage" +components: + schemas: + j.l.Integer-decfea64: + examples: + - 42 + - 352 + type: "integer" + format: "int32" + j.l.String-38af4fe9: + examples: + - "Lorem" + - "ipsum" + type: "string" + j.l.String-931073f3: + examples: + - "EUR" + - "USD" + - "CHF" + type: "string" + j.m.BigDecimal-dbc8e12d: + examples: + - 42.42 + - 352.01 + type: "number" + format: "float" + l.g.j.e.ExampleApplication$ExampleListenerMessage: + title: "ExampleListenerMessage" + properties: + amount: + $ref: "#/components/schemas/j.m.BigDecimal-dbc8e12d" + currency: + $ref: "#/components/schemas/j.l.String-931073f3" + id: + $ref: "#/components/schemas/l.g.j.e.ExampleApplication$ObjectRepresentingAnId" + l.g.j.e.ExampleApplication$ExamplePublisherMessage: + title: "ExamplePublisherMessage" + properties: + amount: + $ref: "#/components/schemas/j.m.BigDecimal-dbc8e12d" + currency: + $ref: "#/components/schemas/j.l.Integer-decfea64" + l.g.j.e.ExampleApplication$ObjectRepresentingAnId: + title: "ObjectRepresentingAnId" + properties: + value: + $ref: "#/components/schemas/j.l.String-38af4fe9" + messages: + l.g.j.e.ExampleApplication$ExampleListenerMessage: + payload: + $ref: "#/components/schemas/l.g.j.e.ExampleApplication$ExampleListenerMessage" + title: "ExampleListenerMessage" + l.g.j.e.ExampleApplication$ExamplePublisherMessage: + payload: + $ref: "#/components/schemas/l.g.j.e.ExampleApplication$ExamplePublisherMessage" + title: "ExamplePublisherMessage" + description: "this is a message which gets published" diff --git a/j2asyncapi-example/pom.xml b/j2asyncapi-example/pom.xml new file mode 100644 index 0000000..965383a --- /dev/null +++ b/j2asyncapi-example/pom.xml @@ -0,0 +1,58 @@ + + + + j2asyncapi + lu.greenhalos + 1.0-SNAPSHOT + + 4.0.0 + + j2asyncapi-example + + + 11 + 11 + + + + + lu.greenhalos + j2asyncapi-annotation + 1.0-SNAPSHOT + + + + lu.greenhalos + j2asyncapi-processor + 1.0-SNAPSHOT + test + + + + org.junit.jupiter + junit-jupiter + test + + + + commons-io + commons-io + test + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + test + + + + org.apache.maven.plugins + maven-surefire-plugin + test + + + + diff --git a/j2asyncapi-example/src/main/java/lu/greenhalos/j2asyncapi/example/ExampleApplication.java b/j2asyncapi-example/src/main/java/lu/greenhalos/j2asyncapi/example/ExampleApplication.java new file mode 100644 index 0000000..0666b07 --- /dev/null +++ b/j2asyncapi-example/src/main/java/lu/greenhalos/j2asyncapi/example/ExampleApplication.java @@ -0,0 +1,57 @@ +package lu.greenhalos.j2asyncapi.example; + +import lu.greenhalos.j2asyncapi.annotations.AsyncApi; + +import java.math.BigDecimal; + +import static lu.greenhalos.j2asyncapi.annotations.AsyncApi.Type.LISTENER; +import static lu.greenhalos.j2asyncapi.annotations.AsyncApi.Type.PUBLISHER; + + +/** + * @author Ben Antony - antony@greenhalos.lu + */ +public class ExampleApplication { + + @AsyncApi( + type = PUBLISHER, exchange = "exchange", routingKey = "routing.key", payload = ExamplePublisherMessage.class + ) + public void publish() { + + System.out.println("publish the message ExampleListenerMessage"); + } + + + @AsyncApi( + type = LISTENER, exchange = "exchange", routingKey = "routing.key", payload = ExampleListenerMessage.class, + description = "Description explaining exactly what happens here" + ) + public void on(ExampleListenerMessage message) { + } + + @AsyncApi.Message(description = "this is a message which gets published") + public static class ExamplePublisherMessage { + + public BigDecimal amount; + @AsyncApi.Field(type = Integer.class) + public String currency; + } + + public static class ExampleListenerMessage { + + public BigDecimal amount; + @AsyncApi.Field(examples = { "EUR", "USD", "CHF" }) + public String currency; + public ObjectRepresentingAnId id; + } + + public static class ObjectRepresentingAnId { + + private final String value; + + public ObjectRepresentingAnId(String value) { + + this.value = value; + } + } +} diff --git a/j2asyncapi-example/src/test/java/lu/greenhalos/j2asyncapi/example/AsyncApiGeneratorTest.java b/j2asyncapi-example/src/test/java/lu/greenhalos/j2asyncapi/example/AsyncApiGeneratorTest.java new file mode 100644 index 0000000..1c9ba93 --- /dev/null +++ b/j2asyncapi-example/src/test/java/lu/greenhalos/j2asyncapi/example/AsyncApiGeneratorTest.java @@ -0,0 +1,81 @@ +package lu.greenhalos.j2asyncapi.example; + +import com.asyncapi.v2.model.AsyncAPI; +import com.asyncapi.v2.model.info.Contact; +import com.asyncapi.v2.model.info.Info; +import com.asyncapi.v2.model.server.Server; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; + +import lu.greenhalos.j2asyncapi.annoations.AsyncApiProcessor; +import lu.greenhalos.j2asyncapi.core.Config; + +import org.apache.commons.io.FileUtils; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; + +import java.util.Map; + + +/** + * @author Ben Antony - antony@greenhalos.lu + */ +public class AsyncApiGeneratorTest { + + private static final String DOCS_TARGET = "../docs/asyncapi-example.yaml"; + + @Test + void generate() throws IOException { + + var asyncAPI = new AsyncAPI(); + asyncAPI.setInfo(info()); + asyncAPI.setServers(servers()); + + var config = Config.builder() + .withAsyncApi(asyncAPI) + .build(); + + AsyncApiProcessor.process(ExampleApplication.class, config); + writeToFile(asyncAPI); + } + + + private Map servers() { + + return Map.of("test", server()); + } + + + private Server server() { + + var server = new Server(); + server.setUrl("http://rabbitmq"); + server.setDescription("RabbitMQ Server"); + server.setProtocol("amqp"); + + return server; + } + + + private static Info info() { + + return new Info("Application API", "0.1.0", null, null, + new Contact("Fancy Team", "https://greenhalos.lu", "asyncapi@greenhalos.lu"), null); + } + + + private static void writeToFile(AsyncAPI asyncAPI) throws IOException { + + var objectMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + + var bytes = objectMapper.writeValueAsBytes(asyncAPI); + FileUtils.writeByteArrayToFile(new File(DOCS_TARGET), bytes); + } +} diff --git a/pom.xml b/pom.xml index 442347f..db1d396 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ j2asyncapi-annotation j2asyncapi-processor + j2asyncapi-example