#113 Event Driven Architecture
Adds unit test to assert and verify pattern behaviour
This commit is contained in:
parent
e1c0731f7e
commit
3ad36020aa
@ -18,6 +18,11 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -39,4 +39,12 @@ public class EventDispatcher implements DynamicRouter<Event> {
|
|||||||
public void dispatch(Event content) {
|
public void dispatch(Event content) {
|
||||||
handlers.get(content.getClass()).dispatch(content);
|
handlers.get(content.getClass()).dispatch(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a map of registered event handlers.
|
||||||
|
* @return {@Map} of registered event handlers.
|
||||||
|
*/
|
||||||
|
public Map<Class<? extends Event>, Channel<?>> getHandlers() {
|
||||||
|
return handlers;
|
||||||
|
}
|
||||||
}
|
}
|
@ -15,7 +15,6 @@ public class UserCreatedEvent extends Event {
|
|||||||
this.user = user;
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public User getUser() {
|
public User getUser() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
47
event-driven-architecture/src/test/java/EventDrivenTest.java
Normal file
47
event-driven-architecture/src/test/java/EventDrivenTest.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import com.iluwatar.eda.EventDispatcher;
|
||||||
|
import com.iluwatar.eda.event.UserCreatedEvent;
|
||||||
|
import com.iluwatar.eda.event.UserUpdatedEvent;
|
||||||
|
import com.iluwatar.eda.handler.UserCreatedEventHandler;
|
||||||
|
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
|
||||||
|
import com.iluwatar.eda.model.User;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class EventDrivenTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEventDriverPattern(){
|
||||||
|
|
||||||
|
EventDispatcher dispatcher = spy(new EventDispatcher());
|
||||||
|
UserCreatedEventHandler userCreatedEventHandler = new UserCreatedEventHandler();
|
||||||
|
UserUpdatedEventHandler userUpdatedEventHandler = new UserUpdatedEventHandler();
|
||||||
|
dispatcher.registerChannel(UserCreatedEvent.class, userCreatedEventHandler);
|
||||||
|
dispatcher.registerChannel(UserUpdatedEvent.class, userUpdatedEventHandler);
|
||||||
|
|
||||||
|
assertEquals("Two handlers must be registered", 2, dispatcher.getHandlers().size());
|
||||||
|
assertEquals("UserCreatedEvent must return the UserCreatedEventHandler",
|
||||||
|
userCreatedEventHandler,
|
||||||
|
(UserCreatedEventHandler)dispatcher.getHandlers().get(UserCreatedEvent.class));
|
||||||
|
assertEquals("UserUpdatedEvent must be registered to the UserUpdatedEventHandler",
|
||||||
|
userUpdatedEventHandler,
|
||||||
|
(UserUpdatedEventHandler)dispatcher.getHandlers().get(UserUpdatedEvent.class));
|
||||||
|
|
||||||
|
User user = new User("iluwatar");
|
||||||
|
|
||||||
|
UserCreatedEvent userCreatedEvent = new UserCreatedEvent(user);
|
||||||
|
UserUpdatedEvent userUpdatedEvent = new UserUpdatedEvent(user);
|
||||||
|
dispatcher.dispatch(userCreatedEvent);
|
||||||
|
dispatcher.dispatch(userUpdatedEvent);
|
||||||
|
|
||||||
|
//verify that the events have been dispatched
|
||||||
|
verify(dispatcher).dispatch(userCreatedEvent);
|
||||||
|
verify(dispatcher).dispatch(userUpdatedEvent);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user