Skip to content

Commit

Permalink
Add coverage to eventbus '@ConsumeEvent' annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo gonzalez granados committed Jan 19, 2023
1 parent c0cecb4 commit 7da0d4b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static io.quarkus.ts.security.vertx.Application.AUTH.NO_SECURE;
import static io.quarkus.ts.security.vertx.Application.AUTH.SECURE;

import java.time.Duration;
import java.util.UUID;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
Expand All @@ -17,7 +20,11 @@
import io.quarkus.ts.security.vertx.handlers.BladeRunnerHandler;
import io.quarkus.ts.security.vertx.handlers.JWTHandler;
import io.quarkus.ts.security.vertx.handlers.ReplicantHandler;
import io.quarkus.ts.security.vertx.model.HelloEvent;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.Multi;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.web.Route;
Expand All @@ -29,10 +36,13 @@
import io.vertx.ext.web.handler.LoggerHandler;

@ApplicationScoped
public class Application {
public class Application extends CommonApplication<HelloEvent> {

private static final Logger LOG = Logger.getLogger(Application.class);

@Inject
EventBus eventBus;

@ConfigProperty(name = "app.name")
public String serviceName;

Expand Down Expand Up @@ -66,8 +76,12 @@ void init(@Observes Router router) {
}

void onStart(@Observes StartupEvent ev) {

LOG.info(String.format("Application %s starting...", serviceName));

Multi.createFrom().ticks().every(Duration.ofMillis(2000))
.subscribe().with((Long tick) -> eventBus.publish(ADDRESS, new HelloEvent(UUID.randomUUID().toString())));

addRoute(HttpMethod.POST, "/bladeRunner", SECURE, rc -> bladeRunner.upsertBladeRunner(rc));
addRoute(HttpMethod.GET, "/bladeRunner/:id", SECURE, rc -> bladeRunner.getBladeRunnerById(rc));
addRoute(HttpMethod.GET, "/bladeRunner", SECURE, rc -> bladeRunner.getAllBladeRunner(rc));
Expand Down Expand Up @@ -98,4 +112,10 @@ private void addRoute(HttpMethod method, String path, AUTH authEnabled, Handler<

route.handler(handler).failureHandler(rc -> failureHandler.handler(rc));
}

@Override
@ConsumeEvent(ADDRESS)
public void consumeEventBusEvent(HelloEvent event) {
LOG.infof("Consuming generated HelloEvent at starting point. Msg value: %s", event.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.ts.security.vertx;

public abstract class CommonApplication<T> {

public static final String ADDRESS = "greeting";

public abstract void consumeEventBusEvent(T event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.ts.security.vertx.model;

public class HelloEvent {

final String message;

public HelloEvent(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import static org.hamcrest.Matchers.is;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -47,4 +52,16 @@ public void deleteBladeRunner() {
.then()
.statusCode(404);
}

@Tag("QUARKUS-2746 ")
@Test
public void verifyConsumeEventAnnotation() {
List<String> actualLogs = app.getLogs();
List<String> helloEvents = actualLogs.stream()
.filter(l -> l.contains("Consuming generated HelloEvent at starting point"))
.collect(Collectors.toList());

Assertions.assertTrue(new HashSet<>(helloEvents).size() == helloEvents.size(),
"@ConsumeEvent annotation should be invoked once per event");
}
}

0 comments on commit 7da0d4b

Please sign in to comment.