Skip to content

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhalos committed Feb 9, 2022
1 parent 8fd81c9 commit 544b364
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A lib which helps you generate `asyncapi.yaml` from your Java code based on anno
</dependencies>
```

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.
Expand Down
78 changes: 78 additions & 0 deletions docs/asyncapi-example.yaml
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"
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"
58 changes: 58 additions & 0 deletions j2asyncapi-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>j2asyncapi</artifactId>
<groupId>lu.greenhalos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>j2asyncapi-example</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>lu.greenhalos</groupId>
<artifactId>j2asyncapi-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>lu.greenhalos</groupId>
<artifactId>j2asyncapi-processor</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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 - [email protected]
*/
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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 - [email protected]
*/
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<String, Server> 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", "[email protected]"), 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);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<modules>
<module>j2asyncapi-annotation</module>
<module>j2asyncapi-processor</module>
<module>j2asyncapi-example</module>
</modules>

<properties>
Expand Down

0 comments on commit 544b364

Please sign in to comment.