-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27 Improved eventuate-tram-examples-basic-event-subscriber tests.
- Loading branch information
Showing
4 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...entuate/tram/examples/basic/events/subscriber/common/AssertableAccountEventsConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...examples/basic/events/subscriber/common/AssertableAccountEventsConsumerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...eventuate/tram/examples/basic/events/subscriber/inmemory/EventSubscriberInMemoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
|
||
} | ||
|
||
|
||
} |