Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rev of the Event work with an EventTest

  • Loading branch information...
commit 5374520e26b93c08d3af8456d52a6b7fe4a788de 1 parent ef733f3
Bill Graham authored
View
47 common/src/main/java/com/twitter/ambrose/model/Event.java
@@ -15,6 +15,11 @@
*/
package com.twitter.ambrose.model;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.JsonTypeName;
import com.twitter.ambrose.util.JSONUtil;
import java.io.IOException;
@@ -31,6 +36,12 @@
*
* @author billg
*/
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+@JsonTypeName("default")
+@JsonSubTypes({
+ @JsonSubTypes.Type(value=com.twitter.ambrose.model.Event.class, name="default"),
+ @JsonSubTypes.Type(value=com.twitter.ambrose.model.Event.JobStartedEvent.class, name="job_started"),
+})
public class Event<T> {
private static AtomicInteger NEXT_ID = new AtomicInteger();
@@ -45,6 +56,8 @@
private Type type;
private T payload;
+ protected Event(Type type) { this.type = type; }
+
private Event(Type type, T payload) {
this.id = NEXT_ID.incrementAndGet();
this.timestamp = System.currentTimeMillis();
@@ -62,16 +75,14 @@ public Event(int eventId, long timestamp, Type type, T payload) {
public long getTimestamp() { return timestamp; }
public int getId() { return id; }
public Type getType() { return type; }
- public Object getPayload() { return payload; }
+ public T getPayload() { return payload; }
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws IOException {
- String json = JSONUtil.readFile("pig/src/main/resources/web/data/small-events.json");
- List<Event> events = JSONUtil.toObject(json, new TypeReference<List<Event>>() { });
- for (Event event : events) {
- // useful if we need to read a file, add a field, output and re-generate
- }
- JSONUtil.writeJson("pig/src/main/resources/web/data/small-events.json2", events);
+ public String toJson() throws IOException {
+ return JSONUtil.toJson(this);
+ }
+
+ public static Event<?> fromJson(String json) throws IOException {
+ return JSONUtil.toObject(json, new TypeReference<Event<?>>() { });
}
/**
@@ -100,14 +111,20 @@ public static Event create(Type type, Object data) {
}
}
+ @JsonTypeName("job_started")
public static class JobStartedEvent extends Event<DAGNode<? extends Job>> {
+ @JsonCreator
+ public JobStartedEvent() { super(Type.JOB_STARTED); }
+
public JobStartedEvent(DAGNode<? extends Job> eventData) {
super(Type.JOB_STARTED, eventData);
}
}
public static class JobProgressEvent extends Event<DAGNode<? extends Job>> {
- public JobProgressEvent(DAGNode<? extends Job> eventData) {
+
+
+ public JobProgressEvent(@JsonProperty("payload") DAGNode<? extends Job> eventData) {
super(Type.JOB_PROGRESS, eventData);
}
}
@@ -129,4 +146,14 @@ public WorkflowProgressEvent(Map<WorkflowProgressField, String> eventData) {
super(Type.WORKFLOW_PROGRESS, eventData);
}
}
+
+ @SuppressWarnings("unchecked")
+ public static void main(String[] args) throws IOException {
+ String json = JSONUtil.readFile("pig/src/main/resources/web/data/small-events.json");
+ List<Event> events = JSONUtil.toObject(json, new TypeReference<List<Event>>() { });
+ for (Event event : events) {
+ // useful if we need to read a file, add a field, output and re-generate
+ }
+ JSONUtil.writeJson("pig/src/main/resources/web/data/small-events.json2", events);
+ }
}
View
41 common/src/test/java/com/twitter/ambrose/model/EventTest.java
@@ -0,0 +1,41 @@
+package com.twitter.ambrose.model;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for {@link EventTest}.
+ */
+public class EventTest {
+
+ private void testRoundTrip(Event expected) throws IOException {
+ String asJson = expected.toJson();
+ Event asEventAgain = Event.fromJson(asJson);
+ assertEquals(expected.getId(), asEventAgain.getId());
+ assertEquals(expected.getType(), asEventAgain.getType());
+ assertEquals(expected.getTimestamp(), asEventAgain.getTimestamp());
+ assertTrue(asEventAgain.getPayload() instanceof DAGNode);
+ assertEquals(expected.getPayload(), asEventAgain.getPayload());
+ ModelTestUtils.assertJobEquals((Job)expected.getPayload(), (Job)asEventAgain.getPayload());
+ }
+
+ @Test
+ public void testRoundTrip() throws IOException {
+ Map<String, Number> metrics = new HashMap<String, Number>();
+ metrics.put("somemetric", 6);
+ Properties properties = new Properties();
+ properties.setProperty("someprop", "propvalue");
+ Job job = new Job("scope-123", metrics, properties);
+
+ DAGNode<Job> node = new DAGNode<Job>("dag name", job);
+
+ testRoundTrip(new Event.JobStartedEvent(node));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.