From 4a3c00219f2df60cb965364cb3ae68f460f36873 Mon Sep 17 00:00:00 2001 From: meywood Date: Thu, 11 Aug 2022 12:22:43 +0100 Subject: [PATCH] issues/120 - Data field DataType to Events to allow for easy identification of data payload for both raw and pojo events --- .../com/casper/sdk/model/event/DataType.java | 58 +++++++++++++++++++ .../com/casper/sdk/model/event/Event.java | 7 +++ .../sdk/service/impl/event/AbstractEvent.java | 4 +- .../sdk/service/impl/event/EventBuilder.java | 2 +- .../sdk/service/impl/event/PojoEvent.java | 8 ++- .../sdk/service/impl/event/RawEvent.java | 15 ++++- .../sdk/service/impl/event/PojoEventTest.java | 33 +++++++++++ .../sdk/service/impl/event/RawEventTest.java | 30 ++++++++++ 8 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/casper/sdk/model/event/DataType.java create mode 100644 src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.java create mode 100644 src/test/java/com/casper/sdk/service/impl/event/RawEventTest.java diff --git a/src/main/java/com/casper/sdk/model/event/DataType.java b/src/main/java/com/casper/sdk/model/event/DataType.java new file mode 100644 index 000000000..0dde7effa --- /dev/null +++ b/src/main/java/com/casper/sdk/model/event/DataType.java @@ -0,0 +1,58 @@ +package com.casper.sdk.model.event; + +import com.casper.sdk.model.event.blockadded.BlockAdded; +import com.casper.sdk.model.event.deployaccepted.DeployAccepted; +import com.casper.sdk.model.event.deployexpired.DeployExpired; +import com.casper.sdk.model.event.deployprocessed.DeployProcessed; +import com.casper.sdk.model.event.fault.Fault; +import com.casper.sdk.model.event.finalitysignature.FinalitySignature; +import com.casper.sdk.model.event.shutdown.Shutdown; +import com.casper.sdk.model.event.step.Step; +import com.casper.sdk.model.event.version.ApiVersion; + +/** + * The enums of the allowable data type key names + * + * @author ian@meywood.com + */ +public enum DataType { + + API_VERSION(ApiVersion.class), + BLOCK_ADDED(BlockAdded.class), + DEPLOY_ACCEPTED(DeployAccepted.class), + DEPLOY_EXPIRED(DeployExpired.class), + DEPLOY_PROCESSED(DeployProcessed.class), + FAULT(Fault.class), + FINALITY_SIGNATURE(FinalitySignature.class), + SHUTDOWN(Shutdown.class), + STEP(Step.class); + + + private Class dataType; + + DataType(Class dataType) { + this.dataType = dataType; + } + + public static DataType of(final Class dataTypeClass) { + for (DataType dataType : DataType.values()) { + if (dataType.dataType.equals(dataTypeClass)) { + return dataType; + } + } + return null; + } + + public static DataType of(final String dataTypeSimpleClassName) { + for (DataType dataType : DataType.values()) { + if (dataType.dataType.getSimpleName().equals(dataTypeSimpleClassName)) { + return dataType; + } + } + return null; + } + + public String getDataTypeName() { + return this.dataType.getSimpleName(); + } +} diff --git a/src/main/java/com/casper/sdk/model/event/Event.java b/src/main/java/com/casper/sdk/model/event/Event.java index d5235b2b2..9cfc54590 100644 --- a/src/main/java/com/casper/sdk/model/event/Event.java +++ b/src/main/java/com/casper/sdk/model/event/Event.java @@ -23,6 +23,13 @@ public interface Event { */ EventType getEventType(); + /** + * The key name of the data field. + * + * @return the key name of the data field + */ + DataType getDataType(); + /** * The event payload a JSON string or Pojo * diff --git a/src/main/java/com/casper/sdk/service/impl/event/AbstractEvent.java b/src/main/java/com/casper/sdk/service/impl/event/AbstractEvent.java index 6a7451250..7a96b55f1 100644 --- a/src/main/java/com/casper/sdk/service/impl/event/AbstractEvent.java +++ b/src/main/java/com/casper/sdk/service/impl/event/AbstractEvent.java @@ -1,10 +1,10 @@ package com.casper.sdk.service.impl.event; +import com.casper.sdk.model.event.DataType; import com.casper.sdk.model.event.Event; import com.casper.sdk.model.event.EventType; import lombok.*; -import java.util.Objects; import java.util.Optional; /** @@ -21,6 +21,8 @@ abstract class AbstractEvent implements Event { /** The type of event RAW or POJO */ private final EventType eventType; + /** The type of the data field */ + private final DataType dataType; /** The source node of the event */ private final String source; /** The ID of the event */ diff --git a/src/main/java/com/casper/sdk/service/impl/event/EventBuilder.java b/src/main/java/com/casper/sdk/service/impl/event/EventBuilder.java index 6113fcb6a..3469a4ee2 100644 --- a/src/main/java/com/casper/sdk/service/impl/event/EventBuilder.java +++ b/src/main/java/com/casper/sdk/service/impl/event/EventBuilder.java @@ -87,7 +87,7 @@ T buildEvent() { final T event; if (this.eventTarget == EventTarget.RAW) { //noinspection unchecked - event = (T) new RawEvent(eventType, source, data, id); + event = (T) new RawEvent(eventType, source, id, data); } else if (eventTarget == EventTarget.POJO) { //noinspection unchecked event = (T) buildPojoEvent(); diff --git a/src/main/java/com/casper/sdk/service/impl/event/PojoEvent.java b/src/main/java/com/casper/sdk/service/impl/event/PojoEvent.java index 44692dece..61f0f40de 100644 --- a/src/main/java/com/casper/sdk/service/impl/event/PojoEvent.java +++ b/src/main/java/com/casper/sdk/service/impl/event/PojoEvent.java @@ -1,5 +1,6 @@ package com.casper.sdk.service.impl.event; +import com.casper.sdk.model.event.DataType; import com.casper.sdk.model.event.EventType; import com.casper.sdk.model.event.EventData; import com.casper.sdk.model.event.EventTarget; @@ -11,10 +12,13 @@ * @param the types of the expected EventData * @author ian@meywood.com */ - final class PojoEvent extends AbstractEvent { PojoEvent(final EventType eventType, final String source, final Long id, T data) { - super(eventType, source, id, data); + super(eventType, getDataType(data), source, id, data); + } + + private static DataType getDataType(final Object data) { + return DataType.of(data.getClass()); } } diff --git a/src/main/java/com/casper/sdk/service/impl/event/RawEvent.java b/src/main/java/com/casper/sdk/service/impl/event/RawEvent.java index 737e2e8cc..4f883a922 100644 --- a/src/main/java/com/casper/sdk/service/impl/event/RawEvent.java +++ b/src/main/java/com/casper/sdk/service/impl/event/RawEvent.java @@ -1,7 +1,8 @@ package com.casper.sdk.service.impl.event; -import com.casper.sdk.model.event.EventType; +import com.casper.sdk.model.event.DataType; import com.casper.sdk.model.event.EventTarget; +import com.casper.sdk.model.event.EventType; /** * An event that contains the event data as raw JSON without the 'data:' prefix is returned from the event service when @@ -11,7 +12,15 @@ */ final class RawEvent extends AbstractEvent { - RawEvent(final EventType eventType, final String source, final String data, final Long id) { - super(eventType, source, id, data); + RawEvent(final EventType eventType, final String source, final Long id, final String data) { + super(eventType, getDataType(data), source, id, data); + } + + private static DataType getDataType(final String data) { + final int start = data.indexOf(':'); + final int end = data.indexOf(':', start + 1); + final String dataTypeName = data.substring(start + 3, end - 1); + return DataType.of(dataTypeName); } } + diff --git a/src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.java b/src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.java new file mode 100644 index 000000000..09b1ee897 --- /dev/null +++ b/src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.java @@ -0,0 +1,33 @@ +package com.casper.sdk.service.impl.event; + +import com.casper.sdk.model.common.Digest; +import com.casper.sdk.model.event.DataType; +import com.casper.sdk.model.event.EventType; +import com.casper.sdk.model.event.deployexpired.DeployExpired; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +/** + * @author ian@meywood.com + */ +class PojoEventTest { + + @Test + void pojoEvent() { + + final String source = "http://localhost:9999"; + final DeployExpired data = new DeployExpired(new Digest("bb878bcf8827649f070c487800a95c35be3eb2e83b5447921675040cea38af1c")); + + final PojoEvent rawEvent = new PojoEvent<>(EventType.MAIN, source, 2L, data); + + assertThat(rawEvent.getEventType(), is(EventType.MAIN)); + assertThat(rawEvent.getSource(), is(source)); + assertThat(rawEvent.getId().isPresent(), is(true)); + assertThat(rawEvent.getId().get(), is(2L)); + assertThat(rawEvent.getData(), is(data)); + assertThat(rawEvent.getDataType(), is(DataType.DEPLOY_EXPIRED)); + + } +} \ No newline at end of file diff --git a/src/test/java/com/casper/sdk/service/impl/event/RawEventTest.java b/src/test/java/com/casper/sdk/service/impl/event/RawEventTest.java new file mode 100644 index 000000000..c50e052f1 --- /dev/null +++ b/src/test/java/com/casper/sdk/service/impl/event/RawEventTest.java @@ -0,0 +1,30 @@ +package com.casper.sdk.service.impl.event; + +import com.casper.sdk.model.event.DataType; +import com.casper.sdk.model.event.EventType; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +/** + * @author ian@meywood.com + */ +class RawEventTest { + + @Test + void rawEvent() { + + final String source = "http://localhost:9999"; + final String data = "data:{\"ApiVersion\":\"1.0.0\"}"; + + final RawEvent rawEvent = new RawEvent(EventType.MAIN, source, 1L, data); + + assertThat(rawEvent.getEventType(), is(EventType.MAIN)); + assertThat(rawEvent.getSource(), is(source)); + assertThat(rawEvent.getId().isPresent(), is(true)); + assertThat(rawEvent.getId().get(), is(1L)); + assertThat(rawEvent.getData(), is(data)); + assertThat(rawEvent.getDataType(), is(DataType.API_VERSION)); + } +} \ No newline at end of file