Skip to content

Commit

Permalink
issues/120 - Data field DataType to Events to allow for easy identifi…
Browse files Browse the repository at this point in the history
…cation of data payload for both raw and pojo events
  • Loading branch information
meywood committed Aug 11, 2022
1 parent 9500fda commit 4a3c002
Showing 8 changed files with 150 additions and 7 deletions.
58 changes: 58 additions & 0 deletions src/main/java/com/casper/sdk/model/event/DataType.java
Original file line number Diff line number Diff line change
@@ -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<? extends EventData> dataType;

DataType(Class<? extends EventData> 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();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/casper/sdk/model/event/Event.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,13 @@ public interface Event<T> {
*/
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
*
Original file line number Diff line number Diff line change
@@ -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<T> implements Event<T> {

/** 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 */
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ <T extends Event> 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();
Original file line number Diff line number Diff line change
@@ -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 <T> the types of the expected EventData
* @author ian@meywood.com
*/

final class PojoEvent<T extends EventData> extends AbstractEvent<T> {

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());
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/casper/sdk/service/impl/event/RawEvent.java
Original file line number Diff line number Diff line change
@@ -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<String> {

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);
}
}

33 changes: 33 additions & 0 deletions src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.java
Original file line number Diff line number Diff line change
@@ -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<DeployExpired> 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));

}
}
30 changes: 30 additions & 0 deletions src/test/java/com/casper/sdk/service/impl/event/RawEventTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 4a3c002

Please sign in to comment.