Skip to content

Commit

Permalink
iluwatar#113 Event Driven Architecture
Browse files Browse the repository at this point in the history
Adds unit test to assert and verify pattern behaviour
  • Loading branch information
cfarrugia committed Nov 28, 2015
1 parent e1c0731 commit 3ad3602
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
7 changes: 6 additions & 1 deletion event-driven-architecture/pom.xml
Expand Up @@ -18,6 +18,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -39,4 +39,12 @@ public void registerChannel(Class<? extends Event> contentType,
public void dispatch(Event 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;
}
}
Expand Up @@ -15,7 +15,6 @@ public UserCreatedEvent(User user) {
this.user = user;
}


public User getUser() {
return user;
}
Expand Down
47 changes: 47 additions & 0 deletions event-driven-architecture/src/test/java/EventDrivenTest.java
@@ -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);

}
}

0 comments on commit 3ad3602

Please sign in to comment.