Skip to content

Commit

Permalink
#27 Improved eventuate-tram-examples-basic-event-subscriber tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cer committed Jan 10, 2024
1 parent 5c31d1b commit e73820a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.eventuate.tram.examples.basic.events.subscriber;
package io.eventuate.tram.examples.basic.events.subscriber.broker;

import io.eventuate.tram.events.subscriber.DomainEventEnvelope;
import io.eventuate.tram.examples.basic.events.domain.AccountDebited;
import io.eventuate.tram.examples.basic.events.publisher.EventPublisherConfiguration;
import io.eventuate.tram.examples.basic.events.publisher.PublishRequest;
import io.eventuate.tram.examples.basic.events.subscriber.EventSubscriberConfiguration;
import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumer;
import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumerConfiguration;
import io.eventuate.util.test.async.Eventually;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand All @@ -21,16 +22,14 @@
import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = EventSubscriberBrokerTest.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EventSubscriberBrokerTest {

@Configuration
@EnableAutoConfiguration
@Import({EventPublisherConfiguration.class, EventSubscriberConfiguration.class})
@Import({EventPublisherConfiguration.class, EventSubscriberConfiguration.class, AssertableAccountEventsConsumerConfiguration.class})
public static class Config {
}

Expand All @@ -43,10 +42,10 @@ public void setup() {
}

@Autowired
private AccountEventsConsumer accountEventsConsumer;
private AssertableAccountEventsConsumer accountEventsConsumer;

@Test
public void shouldPublishEvent() throws InterruptedException {
public void shouldPublishEvent() {

long accountId = System.currentTimeMillis();

Expand All @@ -59,18 +58,10 @@ public void shouldPublishEvent() throws InterruptedException {
.statusCode(200);

Eventually.eventually(100, 500, TimeUnit.MILLISECONDS, () -> {
assertEventPublished(Long.toString(accountId));
accountEventsConsumer.assertEventPublished(Long.toString(accountId));
});

}

public void assertEventPublished(String aggregateId) {
DomainEventEnvelope<AccountDebited> event;
event = accountEventsConsumer.getQueue().poll();
assertNotNull(event);
assertEquals(aggregateId, event.getAggregateId());
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.eventuate.tram.examples.basic.events.subscriber.common;

import io.eventuate.tram.events.subscriber.DomainEventEnvelope;
import io.eventuate.tram.examples.basic.events.domain.AccountDebited;
import io.eventuate.tram.examples.basic.events.subscriber.AccountEventsConsumer;

import java.util.concurrent.BlockingQueue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AssertableAccountEventsConsumer {


private final AccountEventsConsumer accountEventsConsumer;

public AssertableAccountEventsConsumer(AccountEventsConsumer accountEventsConsumer) {
this.accountEventsConsumer = accountEventsConsumer;
}


public BlockingQueue<DomainEventEnvelope<AccountDebited>> getQueue() {
return accountEventsConsumer.getQueue();
}

public void assertEventPublished(String aggregateId) {
DomainEventEnvelope<AccountDebited> event = getQueue().poll();
assertNotNull(event);
assertEquals(aggregateId, event.getAggregateId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.eventuate.tram.examples.basic.events.subscriber.common;

import io.eventuate.tram.examples.basic.events.subscriber.AccountEventsConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AssertableAccountEventsConsumerConfiguration {
@Bean
public AssertableAccountEventsConsumer assertableAccountEventsConsumer(AccountEventsConsumer accountEventsConsumer) {
return new AssertableAccountEventsConsumer(accountEventsConsumer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.eventuate.tram.examples.basic.events.subscriber.inmemory;

import io.eventuate.tram.examples.basic.events.publisher.EventPublisherConfiguration;
import io.eventuate.tram.examples.basic.events.publisher.PublishRequest;
import io.eventuate.tram.examples.basic.events.subscriber.EventSubscriberConfiguration;
import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumer;
import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumerConfiguration;
import io.eventuate.tram.spring.inmemory.TramInMemoryConfiguration;
import io.eventuate.util.test.async.Eventually;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.given;


@SpringBootTest(classes = EventSubscriberInMemoryTest.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EventSubscriberInMemoryTest {

@Configuration
@EnableAutoConfiguration
@Import({
EventPublisherConfiguration.class, EventSubscriberConfiguration.class,
TramInMemoryConfiguration.class,
AssertableAccountEventsConsumerConfiguration.class,
})
static class Config {
}

@LocalServerPort
private int port;

@BeforeEach
public void setup() {
RestAssured.port = port;
}


@Autowired
private AssertableAccountEventsConsumer accountEventsConsumer;

@Test
public void shouldPublishEvent() {

long accountId = System.currentTimeMillis();

given().when()
.log().all()
.body(new PublishRequest(accountId, 102L))
.contentType(ContentType.JSON)
.post("/publish")
.then()
.statusCode(200);

Eventually.eventually(100, 500, TimeUnit.MILLISECONDS, () -> {
accountEventsConsumer.assertEventPublished(Long.toString(accountId));
});

}


}

0 comments on commit e73820a

Please sign in to comment.