Skip to content

Commit

Permalink
Merge pull request #258 from EventStore/expose-building-json-payload-…
Browse files Browse the repository at this point in the history
…event-with-raw-bytes

Expose building a JSON payload event with raw bytes
  • Loading branch information
YoEight authored Dec 13, 2023
2 parents 680f1cb + 9af963b commit 6b87c3d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public byte[] getUserMetadata() {
* @return an event data builder.
* @param <A> a type that can be serialized in JSON.
*/
@Deprecated
public static <A> EventDataBuilder builderAsJson(String eventType, A eventData) {
return builderAsJson(null, eventType, eventData);
}
Expand All @@ -78,6 +79,27 @@ public static <A> EventDataBuilder builderAsJson(UUID eventId, String eventType,
return EventDataBuilder.json(eventId, eventType, eventData);
}

/**
* Configures an event data builder to host a JSON payload.
* @param eventType event's type.
* @param eventData event's payload.
* @return an event data builder.
*/
public static EventDataBuilder builderAsJson(String eventType, byte[] eventData) {
return EventDataBuilder.json(eventType, eventData);
}

/**
* Configures an event data builder to host a JSON payload.
* @param eventId event's id.
* @param eventType event's type.
* @param eventData event's payload.
* @return an event data builder.
*/
public static EventDataBuilder builderAsJson(UUID eventId, String eventType, byte[] eventData) {
return EventDataBuilder.json(eventId, eventType, eventData);
}

/**
* Configures an event data builder to host a binary payload.
* @param eventType event's type.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eventstore.dbclient.persistentsubscriptions;

import com.eventstore.dbclient.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -138,6 +139,7 @@ default void testReplayParkedMessages() throws Throwable {
Exceptions exceptions = new Exceptions().registerGoAwayError();
EventStoreDBPersistentSubscriptionsClient client = EventStoreDBPersistentSubscriptionsClient.from(getDatabase().defaultClient());
final EventStoreDBClient streamClient = getDatabase().defaultClient();
final JsonMapper jsonMapper = new JsonMapper();
final String streamName = generateName();
final String groupName = generateName();

Expand Down Expand Up @@ -169,7 +171,7 @@ public void onEvent(PersistentSubscription subscription, int retryCount, Resolve
}).get();

for (int i = 0; i < 2; i++) {
EventData data = EventData.builderAsJson("foobar", new Foo()).build();
EventData data = EventData.builderAsJson("foobar", jsonMapper.writeValueAsBytes(new Foo())).build();
streamClient.appendToStream(streamName, data).get();
}

Expand All @@ -196,6 +198,7 @@ default void testReplayParkedMessagesToAll() throws Throwable {
Exceptions exceptions = new Exceptions().registerGoAwayError();
EventStoreDBPersistentSubscriptionsClient client = EventStoreDBPersistentSubscriptionsClient.from(getDatabase().defaultClient());
final EventStoreDBClient streamClient = getDatabase().defaultClient();
final JsonMapper jsonMapper = new JsonMapper();
String streamName = generateName();
String groupName = generateName();

Expand Down Expand Up @@ -228,7 +231,7 @@ public void onEvent(PersistentSubscription subscription, int retryCount, Resolve
}).get();

for (int i = 0; i < 2; i++) {
EventData data = EventData.builderAsJson("foobar", new Foo()).build();
EventData data = EventData.builderAsJson("foobar", jsonMapper.writeValueAsBytes(new Foo())).build();
streamClient.appendToStream(streamName, data).get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eventstore.dbclient.persistentsubscriptions;

import com.eventstore.dbclient.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -14,11 +15,12 @@ default void testSubscribePersistentSub() throws Throwable {
String streamName = generateName();
EventStoreDBPersistentSubscriptionsClient client = EventStoreDBPersistentSubscriptionsClient.from(getDatabase().defaultClient());
EventStoreDBClient streamsClient = getDatabase().defaultClient();
JsonMapper jsonMapper = new JsonMapper();

flaky(10, exceptions, () -> client.createToStream(streamName, "aGroup")
.get());

EventDataBuilder builder = EventData.builderAsJson("foobar", new Foo());
EventDataBuilder builder = EventData.builderAsJson("foobar", jsonMapper.writeValueAsBytes(new Foo()));

streamsClient.appendToStream(streamName, builder.build(), builder.build(), builder.build())
.get();
Expand Down Expand Up @@ -66,11 +68,12 @@ default void testSubscribePersistentSubToAll() throws Throwable {
String streamName = generateName();
EventStoreDBPersistentSubscriptionsClient client = EventStoreDBPersistentSubscriptionsClient.from(getDatabase().defaultClient());
EventStoreDBClient streamsClient = getDatabase().defaultClient();
final JsonMapper jsonMapper = new JsonMapper();

flaky(10, exceptions, () -> client.createToAll("aGroup")
.get());

EventDataBuilder builder = EventData.builderAsJson("foobar", new Foo());
EventDataBuilder builder = EventData.builderAsJson("foobar", jsonMapper.writeValueAsBytes(new Foo()));

streamsClient.appendToStream(streamName, builder.build(), builder.build(), builder.build())
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import com.eventstore.dbclient.*;
import com.eventstore.dbclient.samples.TestEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

public class QuickStart {
public static void Run() throws ConnectionStringParsingException, ExecutionException, InterruptedException {
public static void Run() throws ConnectionStringParsingException, ExecutionException, InterruptedException, JsonProcessingException {
// region createClient
EventStoreDBClientSettings settings = EventStoreDBConnectionString.parseOrThrow("{connectionString}");
EventStoreDBClient client = EventStoreDBClient.create(settings);
// endregion createClient

// region createEvent
TestEvent event = new TestEvent();
JsonMapper jsonMapper = new JsonMapper();
event.setId(UUID.randomUUID().toString());
event.setImportantData("I wrote my first event!");

EventData eventData = EventData
.builderAsJson("TestEvent", event)
.builderAsJson("TestEvent", jsonMapper.writeValueAsBytes(event))
.build();
// endregion createEvent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Objects;
import java.util.UUID;

public interface AppendTests extends ConnectionAware {
Expand All @@ -17,8 +16,9 @@ default void testAppendSingleEventNoStream() throws Throwable {
final String eventType = "TestEvent";
final String eventId = "38fffbc2-339e-11ea-8c7b-784f43837872";
final byte[] eventMetaData = new byte[]{0xd, 0xe, 0xa, 0xd};
final JsonMapper jsonMapper = new JsonMapper();

EventData event = EventData.builderAsJson(eventType, new Foo())
EventData event = EventData.builderAsJson(eventType, jsonMapper.writeValueAsBytes(new Foo()))
.metadataAsBytes(eventMetaData)
.eventId(UUID.fromString(eventId))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eventstore.dbclient.streams;

import com.eventstore.dbclient.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -142,7 +143,11 @@ default void testCancellingSubscriptionShouldNotRaiseAnException() throws Throwa
String streamName = generateName();
String eventType = generateName();

client.appendToStream(streamName, EventData.builderAsJson(eventType, "Hello World").build()).get(60, TimeUnit.SECONDS);
client.appendToStream(
streamName,
EventData.builderAsJson(eventType, "Hello World".getBytes())
.build()
).get(60, TimeUnit.SECONDS);

Subscription sub = client.subscribeToAll(new SubscriptionListener() {
@Override
Expand Down

0 comments on commit 6b87c3d

Please sign in to comment.