@Test public void shouldReadNewEventsOnly() throws InterruptedException { BlockingQueue<PublishedEvent> publishedEvents = new LinkedBlockingDeque<>(); CdcProcessor<PublishedEvent> cdcProcessor = createCdcProcessor(); cdcProcessor.start(publishedEvent -> { publishedEvents.add(publishedEvent); onEventSent(publishedEvent); }); String accountCreatedEventData = generateAccountCreatedEvent(); EntityIdVersionAndEventIds entityIdVersionAndEventIds = saveEvent(accountCreatedEventData); waitForEvent(publishedEvents, entityIdVersionAndEventIds.getEntityVersion(), LocalDateTime.now().plusSeconds(10), accountCreatedEventData); cdcProcessor.stop(); publishedEvents.clear(); cdcProcessor.start(publishedEvent -> { publishedEvents.add(publishedEvent); onEventSent(publishedEvent); }); List<String> excludedIds = entityIdVersionAndEventIds.getEventIds().stream().map(Int128::asString).collect(Collectors.toList()); accountCreatedEventData = generateAccountCreatedEvent(); entityIdVersionAndEventIds = updateEvent(entityIdVersionAndEventIds.getEntityId(), entityIdVersionAndEventIds.getEntityVersion(), accountCreatedEventData); waitForEventExcluding(publishedEvents, entityIdVersionAndEventIds.getEntityVersion(), LocalDateTime.now().plusSeconds(10), accountCreatedEventData, excludedIds); cdcProcessor.stop(); }
@Test public void shouldReadUnprocessedEventsAfterStartup() throws InterruptedException { BlockingQueue<PublishedEvent> publishedEvents = new LinkedBlockingDeque<>(); String accountCreatedEventData = generateAccountCreatedEvent(); EntityIdVersionAndEventIds entityIdVersionAndEventIds = saveEvent(accountCreatedEventData); CdcProcessor<PublishedEvent> cdcProcessor = createCdcProcessor(); cdcProcessor.start(publishedEvents::add); waitForEvent(publishedEvents, entityIdVersionAndEventIds.getEntityVersion(), LocalDateTime.now().plusSeconds(20), accountCreatedEventData); cdcProcessor.stop(); }
@Test public void shouldReadPublishedEvent() throws InterruptedException { BlockingQueue<PublishedEvent> publishedEvents = new LinkedBlockingDeque<>(); CdcProcessor<PublishedEvent> cdcProcessor = createCdcProcessor(); cdcProcessor.start(publishedEvent -> { publishedEvents.add(publishedEvent); onEventSent(publishedEvent); }); String accountCreatedEventData = generateAccountCreatedEvent(); EntityIdVersionAndEventIds entityIdVersionAndEventIds = saveEvent(accountCreatedEventData); waitForEvent(publishedEvents, entityIdVersionAndEventIds.getEntityVersion(), LocalDateTime.now().plusSeconds(10), accountCreatedEventData); cdcProcessor.stop(); } }
@Test public void shouldGetEvents() throws InterruptedException { MySqlBinaryLogClient<PublishedEvent> mySqlBinaryLogClient = makeMySqlBinaryLogClient(); try { BlockingQueue<PublishedEvent> publishedEvents = new LinkedBlockingDeque<>(); mySqlBinaryLogClient.start(Optional.empty(), publishedEvents::add); String accountCreatedEventData = generateAccountCreatedEvent(); EntityIdVersionAndEventIds saveResult = saveEvent(accountCreatedEventData); String accountDebitedEventData = generateAccountDebitedEvent(); EntityIdVersionAndEventIds updateResult = updateEvent(saveResult.getEntityId(), saveResult.getEntityVersion(), accountDebitedEventData); // Wait for 10 seconds LocalDateTime deadline = LocalDateTime.now().plusSeconds(10); waitForEvent(publishedEvents, saveResult.getEntityVersion(), deadline, accountCreatedEventData); waitForEvent(publishedEvents, updateResult.getEntityVersion(), deadline, accountDebitedEventData); } finally { mySqlBinaryLogClient.stop(); } }
@Test public void shouldSaveAndLoadSnapshot() { EntityIdVersionAndEventIds eidv = eventStore.save(aggregateType, singletonList(new EventTypeAndData("MyEventType", "{}", Optional.empty())), Optional.of(new AggregateCrudSaveOptions().withEventContext(ectx))); EntityIdVersionAndEventIds updateResult = eventStore.update( new EntityIdAndType(eidv.getEntityId(), aggregateType), eidv.getEntityVersion(), singletonList(new EventTypeAndData("MyEventType", "{}", Optional.empty())), Optional.of(new AggregateCrudUpdateOptions().withSnapshot(new SerializedSnapshot("X", "Y")))); LoadedEvents findResult = eventStore.find(aggregateType, eidv.getEntityId(), Optional.of(new AggregateCrudFindOptions())); assertTrue(findResult.getSnapshot().isPresent()); assertTrue(findResult.getEvents().isEmpty()); }
@Test @PactVerification(fragment="create") public void shouldCreate() throws URISyntaxException, ExecutionException, InterruptedException { EntityIdVersionAndEventIds saveResult = save().get(); assertEquals(RequestResponseJsonObjects.ENTITY_ID, saveResult.getEntityId()); assertEquals(new Int128(1,2), saveResult.getEntityVersion()); //assertEquals(Collections.singletonList(new Int128(3,4)), saveResult.getEventIds()); }
@Test @PactVerification(fragment="update") public void shouldUpdate() throws URISyntaxException, ExecutionException, InterruptedException { EntityIdVersionAndEventIds updateResult = update().get(); assertEquals(RequestResponseJsonObjects.ENTITY_ID, updateResult.getEntityId()); assertEquals(new Int128(1,2), updateResult.getEntityVersion()); //assertEquals(Collections.singletonList(new Int128(3,4)), updateResult.getEventIds()); }
@Test @PactVerification(fragment="createWithId") public void shouldCreateWithId() throws URISyntaxException, ExecutionException, InterruptedException { EntityIdVersionAndEventIds saveResult = saveWithId().get(); assertEquals(RequestResponseJsonObjects.createId, saveResult.getEntityId()); assertEquals(new Int128(1,2), saveResult.getEntityVersion()); //assertEquals(Collections.singletonList(new Int128(3,4)), saveResult.getEventIds()); }
@Test @PactVerification(fragment="updateWithTriggeringEvent") public void shouldUpdateWithTriggeringEvent() throws URISyntaxException, ExecutionException, InterruptedException { EntityIdVersionAndEventIds updateResult = updateWithEventContext().get(); assertEquals(RequestResponseJsonObjects.ENTITY_ID, updateResult.getEntityId()); assertEquals(new Int128(1,2), updateResult.getEntityVersion()); //assertEquals(Collections.singletonList(new Int128(3,4)), updateResult.getEventIds()); }
@Test public void shouldGetEvents() throws InterruptedException{ PostgresWalClient<PublishedEvent> postgresWalClient = new PostgresWalClient<>(postgresWalMessageParser, dataSourceURL, dbUserName, dbPassword, eventuateConfigurationProperties.getBinlogConnectionTimeoutInMilliseconds(), eventuateConfigurationProperties.getMaxAttemptsForBinlogConnection(), eventuateConfigurationProperties.getPostgresWalIntervalInMilliseconds(), eventuateConfigurationProperties.getPostgresReplicationStatusIntervalInMilliseconds(), eventuateConfigurationProperties.getPostgresReplicationSlotName()); BlockingQueue<PublishedEvent> publishedEvents = new LinkedBlockingDeque<>(); postgresWalClient.start(Optional.empty(), publishedEvents::add); String accountCreatedEventData = generateAccountCreatedEvent(); EntityIdVersionAndEventIds saveResult = saveEvent(accountCreatedEventData); String accountDebitedEventData = generateAccountDebitedEvent(); EntityIdVersionAndEventIds updateResult = updateEvent(saveResult.getEntityId(), saveResult.getEntityVersion(), accountDebitedEventData); // Wait for 10 seconds LocalDateTime deadline = LocalDateTime.now().plusSeconds(10); waitForEvent(publishedEvents, saveResult.getEntityVersion(), deadline, accountCreatedEventData); waitForEvent(publishedEvents, updateResult.getEntityVersion(), deadline, accountDebitedEventData); postgresWalClient.stop(); }
@Test public void shouldSaveAndLoadEventMetadata() throws InterruptedException { String saveMetadata = "MyMetaData"; String updateMetadata = "MyMetaDataForUpdate"; LinkedBlockingQueue<SerializedEvent> events = new LinkedBlockingQueue<>(); aggregateEvents.subscribe("shouldSaveAndLoadEventMetadata", singletonMap(aggregateType, singleton("MyEventType")), new SubscriberOptions(), se -> { events.add(se); return new CompletableFuture<Object>(); }); EntityIdVersionAndEventIds eidv = eventStore.save(aggregateType, singletonList(new EventTypeAndData("MyEventType", "{}", Optional.of(saveMetadata))), Optional.empty()); LoadedEvents findResult = eventStore.find(aggregateType, eidv.getEntityId(), Optional.of(new AggregateCrudFindOptions())); assertEquals(Optional.of(saveMetadata), findResult.getEvents().get(0).getMetadata()); EntityIdVersionAndEventIds updateResult = eventStore.update( new EntityIdAndType(eidv.getEntityId(), aggregateType), eidv.getEntityVersion(), singletonList(new EventTypeAndData("MyEventType", "{}", Optional.of(updateMetadata))), Optional.empty()); LoadedEvents findResult2 = eventStore.find(aggregateType, eidv.getEntityId(), Optional.of(new AggregateCrudFindOptions())); assertEquals(Optional.of(saveMetadata), findResult2.getEvents().get(0).getMetadata()); assertEquals(Optional.of(updateMetadata), findResult2.getEvents().get(1).getMetadata()); assertContainsEventWithMetadata(eidv.getEventIds().get(0), saveMetadata, events); assertContainsEventWithMetadata(updateResult.getEventIds().get(0), updateMetadata, events); }
Int128 eventId = saveResult.getEntityVersion(); String eventData = accountCreatedEventData;
@Test public void testUpdate() { EventTypeAndData eventTypeAndData = new EventTypeAndData(testEventType, testEventData, Optional.empty()); SaveUpdateResult saveUpdateResult = eventuateJdbcAccess.save(testAggregate, Collections.singletonList(eventTypeAndData), Optional.empty()); EntityIdAndType entityIdAndType = new EntityIdAndType(saveUpdateResult.getEntityIdVersionAndEventIds().getEntityId(), testAggregate); eventTypeAndData = new EventTypeAndData("testEventType2", "testEventData2", Optional.empty()); eventuateJdbcAccess.update(entityIdAndType, saveUpdateResult.getEntityIdVersionAndEventIds().getEntityVersion(), Collections.singletonList(eventTypeAndData), Optional.of(new AggregateCrudUpdateOptions(Optional.empty(), Optional.of(new SerializedSnapshot("", ""))))); List<Map<String, Object>> events = jdbcTemplate.queryForList(readAllEventsSql()); Assert.assertEquals(2, events.size()); List<Map<String, Object>> entities = jdbcTemplate.queryForList(readAllEntitiesSql()); Assert.assertEquals(1, entities.size()); List<Map<String, Object>> snapshots = jdbcTemplate.queryForList(readAllSnapshots()); Assert.assertEquals(1, snapshots.size()); LoadedEvents loadedEvents = eventuateJdbcAccess.find(testAggregate, saveUpdateResult.getEntityIdVersionAndEventIds().getEntityId(), Optional.empty()); Assert.assertTrue(loadedEvents.getSnapshot().isPresent()); }