-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issues/120 - Data field DataType to Events to allow for easy identifi…
…cation of data payload for both raw and pojo events
- Loading branch information
Showing
8 changed files
with
150 additions
and
7 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
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(); | ||
} | ||
} |
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
src/test/java/com/casper/sdk/service/impl/event/PojoEventTest.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,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
30
src/test/java/com/casper/sdk/service/impl/event/RawEventTest.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,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)); | ||
} | ||
} |