-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from jijisv/master
GenericEvent to trigger/log arbitrary events
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 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
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
40 changes: 40 additions & 0 deletions
40
micro-events/src/main/java/com/aol/micro/server/events/GenericEvent.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,40 @@ | ||
package com.aol.micro.server.events; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class GenericEvent<T> { | ||
|
||
private GenericEventData<T> data; | ||
|
||
public static <T> GenericEvent<T> trigger(String name, EventBus bus) { | ||
return trigger(name, bus, null, new String[]{}); | ||
} | ||
|
||
public static <T> GenericEvent<T> trigger(String name, EventBus bus, String[] subTypes) { | ||
return trigger(name, bus, null, subTypes); | ||
} | ||
|
||
public static <T> GenericEvent<T> trigger(String name, EventBus bus, T data, String[] subTypes) { | ||
GenericEvent<T> event = new GenericEvent<>(GenericEventData.<T>builder() | ||
.name(name) | ||
.data(data) | ||
.subTypes(subTypes) | ||
.build()); | ||
bus.post(event); | ||
return event; | ||
} | ||
|
||
@AllArgsConstructor | ||
@Builder | ||
@Getter | ||
public static class GenericEventData<T> extends BaseEventInfo { | ||
private final String name; | ||
private final String[] subTypes; | ||
private final T data; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
micro-events/src/test/java/com/aol/micro/server/events/GenericEventTest.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,47 @@ | ||
package com.aol.micro.server.events; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class GenericEventTest { | ||
|
||
@Mock | ||
EventBus bus; | ||
|
||
@Test | ||
public void trigger() throws Exception { | ||
GenericEvent<Object> event = GenericEvent.trigger("some-event", bus); | ||
Mockito.verify(bus).post(event); | ||
} | ||
|
||
@Test | ||
public void trigger1() throws Exception { | ||
GenericEvent<Object> event = GenericEvent.trigger("some-event", bus, new String[] {"start", "phase1"}); | ||
Mockito.verify(bus).post(event); | ||
} | ||
|
||
@Test | ||
public void trigger2() throws Exception { | ||
GenericEvent<String> event = GenericEvent.trigger("some-event", bus, "data", new String[]{"finish", "terminate"}); | ||
Mockito.verify(bus).post(event); | ||
} | ||
|
||
@Test | ||
public void getData() throws Exception { | ||
GenericEvent<Integer> event = GenericEvent.trigger("some-event", bus, 10, new String[]{"finish", "terminate"}); | ||
GenericEvent.GenericEventData<Integer> eventData = event.getData(); | ||
assertEquals("some-event", eventData.getName()); | ||
assertEquals(Integer.valueOf(10), eventData.getData()); | ||
assertNotNull(eventData.getSubTypes()); | ||
assertEquals(eventData.getSubTypes()[0], "finish"); | ||
assertEquals(eventData.getSubTypes()[1], "terminate"); | ||
} | ||
|
||
} |