-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added validation for adding subscribers, and refactored test class for eventmanager. Removed unused getname method for eventchannel, and added validator ruleset for valid week.
- Loading branch information
Showing
5 changed files
with
152 additions
and
73 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
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
185 changes: 131 additions & 54 deletions
185
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventManagerTest.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 |
|---|---|---|
| @@ -1,99 +1,176 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewData; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewEnum; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class EventManagerTest { | ||
| private enum TestEventTypes implements EventChannel { | ||
| /** | ||
| * Test event type 1. | ||
| * */ | ||
| TEST_TYPE_1, | ||
|
|
||
| /** | ||
| * Test event type 2. | ||
| * */ | ||
| TEST_TYPE_2, | ||
|
|
||
| /** | ||
| * Test event type 3 (Used by both subscribers). | ||
| * */ | ||
| TEST_TYPE_3; | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return this.name(); | ||
| } | ||
| } | ||
|
|
||
| private GenericEventPublisher testEventPublisher; | ||
| private GenericEventPublisher testEventPublisher2; | ||
| private GenericEventPublisher testEventPublisher3; | ||
|
|
||
| private GenericEventSubscriber testEventSubscriber; | ||
| private GenericEventSubscriber testEventSubscriber2; | ||
|
|
||
| private EventManager testEventManager; | ||
| private SampleEventSubscriber sampleEventSubscriber1; | ||
| private SampleEventSubscriber sampleEventSubscriber2; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| testEventManager = new EventManager(); | ||
|
|
||
| testEventSubscriber = new GenericEventSubscriber(); | ||
| testEventSubscriber2 = new GenericEventSubscriber(); | ||
| sampleEventSubscriber1 = new SampleEventSubscriber(); | ||
| sampleEventSubscriber2 = new SampleEventSubscriber(); | ||
|
|
||
| testEventManager.addSubscriber(sampleEventSubscriber1, TestEventTypes.TEST_TYPE_1); | ||
| testEventManager.addSubscriber(sampleEventSubscriber1, TestEventTypes.TEST_TYPE_3); | ||
|
|
||
| testEventManager.addSubscriber(sampleEventSubscriber2, TestEventTypes.TEST_TYPE_2); | ||
| testEventManager.addSubscriber(sampleEventSubscriber2, TestEventTypes.TEST_TYPE_3); | ||
| } | ||
|
|
||
| testEventPublisher = new GenericEventPublisher(testEventManager, TestEventTypes.TEST_TYPE_1); | ||
| testEventPublisher2 = new GenericEventPublisher(testEventManager, TestEventTypes.TEST_TYPE_2); | ||
| testEventPublisher3 = new GenericEventPublisher(testEventManager, TestEventTypes.TEST_TYPE_3); | ||
| @Test | ||
| void addingSubscriberNullParametersThrowsException() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> testEventManager.addSubscriber( | ||
| null, | ||
| TestEventTypes.TEST_TYPE_2)); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, | ||
| () -> testEventManager.addSubscriber( | ||
| sampleEventSubscriber1, | ||
| null)); | ||
|
|
||
| // Will only throw exception if sample event subscriber 1 is duplicate. | ||
| assertDoesNotThrow( | ||
| () -> testEventManager.addSubscriber( | ||
| sampleEventSubscriber1, | ||
| TestEventTypes.TEST_TYPE_2)); | ||
|
|
||
| testEventManager.addSubscriber(testEventSubscriber, TestEventTypes.TEST_TYPE_1); | ||
| testEventManager.addSubscriber(testEventSubscriber2, TestEventTypes.TEST_TYPE_2); | ||
| } | ||
|
|
||
| @Test | ||
| void addingDuplicateSubscriberForSameChannelThrowsException() { | ||
| assertDoesNotThrow( | ||
| () -> testEventManager.addSubscriber( | ||
| sampleEventSubscriber1, | ||
| TestEventTypes.TEST_TYPE_2)); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, | ||
| () -> testEventManager.addSubscriber( | ||
| sampleEventSubscriber1, | ||
| TestEventTypes.TEST_TYPE_1)); | ||
| } | ||
|
|
||
| @Test | ||
| void firedEventCaughtByCorrectSubscriber() { | ||
| assertFalse(testEventSubscriber.invokedEvent); | ||
| testEventPublisher.fireEvent(); | ||
| assertTrue(testEventSubscriber.invokedEvent); | ||
| String dataToSend = "Data for type 1"; | ||
|
|
||
| assertFalse(sampleEventSubscriber1.getInvoked()); | ||
| assertNull(sampleEventSubscriber1.getLastReceivedData()); | ||
|
|
||
| EventData<String> eventData = new EventData<>( | ||
| TestEventTypes.TEST_TYPE_1, | ||
| dataToSend | ||
| ); | ||
| testEventManager.invokeEvent(eventData); | ||
|
|
||
| assertTrue(sampleEventSubscriber1.getInvoked()); | ||
| assertEquals(dataToSend, sampleEventSubscriber1.getLastReceivedData()); | ||
| } | ||
|
|
||
| @Test | ||
| void firedEventNotCaughtByIncorrectSubscriber() { | ||
| assertFalse(testEventSubscriber.invokedEvent); | ||
| testEventPublisher2.fireEvent(); | ||
| assertFalse(testEventSubscriber.invokedEvent); | ||
| String dataToSend = "Data for type 1"; | ||
|
|
||
| assertFalse(sampleEventSubscriber1.getInvoked()); | ||
| assertNull(sampleEventSubscriber1.getLastReceivedData()); | ||
| assertFalse(sampleEventSubscriber2.getInvoked()); | ||
| assertNull(sampleEventSubscriber2.getLastReceivedData()); | ||
|
|
||
| EventData<String> eventData = new EventData<>( | ||
| TestEventTypes.TEST_TYPE_2, | ||
| dataToSend | ||
| ); | ||
| testEventManager.invokeEvent(eventData); | ||
|
|
||
| assertFalse(sampleEventSubscriber1.getInvoked()); | ||
| assertNull(sampleEventSubscriber1.getLastReceivedData()); | ||
|
|
||
| assertTrue(sampleEventSubscriber2.getInvoked()); | ||
| assertEquals(dataToSend, sampleEventSubscriber2.getLastReceivedData()); | ||
| } | ||
|
|
||
| @Test | ||
| void firedEventThrowsErrorWhenNoSubscribers() { | ||
| assertFalse(testEventSubscriber.invokedEvent); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| testEventPublisher3.fireEvent(); | ||
| }); | ||
| assertFalse(testEventSubscriber.invokedEvent); | ||
| } | ||
| void firedEventsWithMultipleSubscribersCaughtByAllSubscribers() { | ||
| String dataToSend = "Data for type 1"; | ||
|
|
||
| private class GenericEventPublisher implements EventPublisher { | ||
| assertFalse(sampleEventSubscriber1.getInvoked()); | ||
| assertNull(sampleEventSubscriber1.getLastReceivedData()); | ||
| assertFalse(sampleEventSubscriber2.getInvoked()); | ||
| assertNull(sampleEventSubscriber2.getLastReceivedData()); | ||
|
|
||
| private final ViewData viewData; | ||
| private final EventData<ViewData> eventData; | ||
| private final EventManager eventManager; | ||
| EventData<String> eventData = new EventData<>( | ||
| TestEventTypes.TEST_TYPE_3, | ||
| dataToSend | ||
| ); | ||
| testEventManager.invokeEvent(eventData); | ||
|
|
||
| public GenericEventPublisher(final EventManager eventManager, final TestEventTypes eventType) { | ||
| viewData = new ViewData(ViewEnum.IN_GAME); | ||
| eventData = new EventData<ViewData>(eventType, viewData); | ||
| this.eventManager = eventManager; | ||
| } | ||
| assertTrue(sampleEventSubscriber1.getInvoked()); | ||
| assertEquals(dataToSend, sampleEventSubscriber1.getLastReceivedData()); | ||
|
|
||
| public void fireEvent() { | ||
| invoke(eventData, eventManager); | ||
| } | ||
| assertTrue(sampleEventSubscriber2.getInvoked()); | ||
| assertEquals(dataToSend, sampleEventSubscriber2.getLastReceivedData()); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> void invoke(EventData<T> data, EventManager eventManager) { | ||
| eventManager.invokeEvent(data); | ||
| } | ||
| @Test | ||
| void firedEventThrowsErrorWhenDataIsNull() { | ||
|
|
||
| EventData<String> invalidEventData = new EventData<>( | ||
| TestEventTypes.TEST_TYPE_1, | ||
| null | ||
| ); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, | ||
| () -> testEventManager.invokeEvent(null)); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, | ||
| () -> testEventManager.invokeEvent(invalidEventData)); | ||
| } | ||
|
|
||
| private class GenericEventSubscriber implements EventSubscriber { | ||
| public boolean invokedEvent = false; | ||
| private static class SampleEventSubscriber implements EventSubscriber { | ||
| private boolean invokedEvent = false; | ||
| private Object lastReceivedData = null; | ||
|
|
||
| @Override | ||
| public <T> void handleEvent(EventData<T> data) { | ||
| public <T> void handleEvent(final EventData<T> data) { | ||
| invokedEvent = true; | ||
| lastReceivedData = data.data(); | ||
| } | ||
|
|
||
| private boolean getInvoked() { | ||
| return invokedEvent; | ||
| } | ||
|
|
||
| private Object getLastReceivedData() { | ||
| return lastReceivedData; | ||
| } | ||
| } | ||
| } |