RobotAgentEventDispatcher { void runEventsLoop(final BufferedReader eventReader) throws IOException, RobotAgentEventsListenerException { try { for (final RobotAgentEventListener listener : eventsListeners) { listener.eventsProcessingAboutToStart(); } eventsLoop(eventReader); } finally { for (final RobotAgentEventListener listener : eventsListeners) { listener.eventsProcessingFinished(); } } } RobotAgentEventDispatcher(final AgentClient client, final RobotAgentEventListener... eventsListeners);   }
@Test public void exceptionIsRethrown_whenReaderThrowsIOException() throws Exception { final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null); final BufferedReader reader = mock(BufferedReader.class); when(reader.readLine()).thenThrow(IOException.class); assertThatIOException().isThrownBy(() -> dispatcher.runEventsLoop(reader)); }
@Test public void listenerIsNotNotified_whenThereAreNoEvents() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); dispatcher.runEventsLoop(readerFor("")); verify(listener).eventsProcessingAboutToStart(); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotNotified_whenThereIsOnlyNullEventRead() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); dispatcher.runEventsLoop(readerFor(toJson(null))); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotNotified_whenUnknownEventComes() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final String json = toJson(ImmutableMap.of("some_event", "val")); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotNotified_whenValidEventComesButListenerDoesNotHandleEventsAnymore() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(false); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final String json = toJson(ImmutableMap.of("pid", 1)); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void whenAtLeastOneListenerIsHandlingEvents_allAreNotifiedAboutIt() throws Exception { final RobotAgentEventListener listener1 = mock(RobotAgentEventListener.class); final RobotAgentEventListener listener2 = mock(RobotAgentEventListener.class); final RobotAgentEventListener listener3 = mock(RobotAgentEventListener.class); when(listener1.isHandlingEvents()).thenReturn(false); when(listener2.isHandlingEvents()).thenReturn(true); when(listener3.isHandlingEvents()).thenReturn(false); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener1, listener2, listener3); final String json = toJson(ImmutableMap.of("ready_to_start", 1)); dispatcher.runEventsLoop(readerFor(json)); verify(listener1).eventsProcessingAboutToStart(); verify(listener1, atLeast(0)).isHandlingEvents(); verify(listener1).handleAgentIsReadyToStart(new ReadyToStartEvent(null)); verify(listener1).eventsProcessingFinished(); verifyNoMoreInteractions(listener1); verify(listener2).eventsProcessingAboutToStart(); verify(listener2, atLeast(1)).isHandlingEvents(); verify(listener2).handleAgentIsReadyToStart(new ReadyToStartEvent(null)); verify(listener2).eventsProcessingFinished(); verifyNoMoreInteractions(listener2); verify(listener3).eventsProcessingAboutToStart(); verify(listener3, atLeast(0)).isHandlingEvents(); verify(listener3).handleAgentIsReadyToStart(new ReadyToStartEvent(null)); verify(listener3).eventsProcessingFinished(); verifyNoMoreInteractions(listener3); }
@Test public void listenerIsNotifiedAboutReadyToStartEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final String json = toJson(ImmutableMap.of("ready_to_start", 0)); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleAgentIsReadyToStart(new ReadyToStartEvent(null)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutVersionEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(mock(AgentClient.class), listener); final Map<String, Object> attributes = ImmutableMap.of("cmd_line", "cmdLine", "python", "py3", "robot", "1.2.3", "protocol", 2, "pid", 42); final String json = toJson(ImmutableMap.of("version", newArrayList(attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleVersions(new VersionsEvent(null, "cmdLine", "py3", "1.2.3", 2, Optional.of(42L))); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutSuiteStartEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = new HashMap<>(); attributes.put("source", "/a/b/suite.robot"); attributes.put("is_dir", false); attributes.put("suites", newArrayList("s1", "s2")); attributes.put("tests", newArrayList("t1", "t2")); attributes.put("child_paths", new ArrayList<>()); attributes.put("totaltests", 7); attributes.put("vars_scopes", newArrayList(ImmutableMap.of("a", newArrayList("t", 1, "global"), "b", newArrayList("t", 2, "local")))); final String json = toJson(ImmutableMap.of("start_suite", newArrayList("suite", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleSuiteStarted(new SuiteStartedEvent("suite", new URI("file: ExecutionMode.TESTS, 7, newArrayList("s1", "s2"), newArrayList("t1", "t2"), newArrayList(Optional.<URI> empty(), Optional.<URI> empty()))); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutSuiteEndEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.<String, Object> of("elapsedtime", 10, "message", "msg", "status", "PASS"); final String json = toJson(ImmutableMap.of("end_suite", newArrayList("suite", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleSuiteEnded(new SuiteEndedEvent("suite", 10, Status.PASS, "msg")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutTestStartEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.of("longname", "suite-a-b-test", "vars_scopes", newArrayList(ImmutableMap.of("a", newArrayList("t", 1, "test"), "b", newArrayList("t", 2, "local"))), "template", ""); final String json = toJson(ImmutableMap.of("start_test", newArrayList("test", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleTestStarted(new TestStartedEvent("test", "test", "suite-a-b-test", "")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutTestEndEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.<String, Object> of("longname", "suite-a-b-test", "elapsedtime", 10, "message", "msg", "status", "FAIL"); final String json = toJson(ImmutableMap.of("end_test", newArrayList("test", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleTestEnded(new TestEndedEvent("test", "test", "suite-a-b-test", 10, Status.FAIL, "msg")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutPreKeywordStartEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.<String, Object> of("kwname", "kw", "type", "Keyword", "libname", "lib", "vars_scopes", newArrayList(ImmutableMap.of("a", newArrayList("t", 1, "global"), "b", newArrayList("t", 2, "suite")))); final String json = toJson(ImmutableMap.of("pre_start_keyword", newArrayList("kw", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleKeywordAboutToStart(new KeywordStartedEvent("kw", "Keyword", "lib")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutKeywordStartEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.<String, Object> of("kwname", "kw", "type", "Keyword", "libname", "lib", "vars_scopes", newArrayList(ImmutableMap.of("a", newArrayList("t", 1, "global"), "b", newArrayList("t", 2, "suite")))); final String json = toJson(ImmutableMap.of("start_keyword", newArrayList("kw", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleKeywordStarted(new KeywordStartedEvent("kw", "Keyword", "lib")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutPreKeywordEndEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.of("kwname", "kw", "libname", "lib", "type", "Setup", "status", "PASS"); final String json = toJson(ImmutableMap.of("pre_end_keyword", newArrayList("_", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleKeywordAboutToEnd(new KeywordEndedEvent("lib", "kw", "Setup", Status.PASS)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutKeywordEndEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final Map<String, Object> attributes = ImmutableMap.of("kwname", "kw", "libname", "lib", "type", "Setup", "status", "PASS"); final String json = toJson(ImmutableMap.of("end_keyword", newArrayList("_", attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleKeywordEnded(new KeywordEndedEvent("lib", "kw", "Setup", Status.PASS)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutConditionResultEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final ImmutableMap<String, Boolean> attributes = ImmutableMap.of("result", Boolean.TRUE); final String json = toJson(ImmutableMap.of("condition_result", newArrayList(attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleConditionEvaluated(new ConditionEvaluatedEvent(true)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutConditionErrorEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final ImmutableMap<String, String> attributes = ImmutableMap.of("error", "Error evaluating condition"); final String json = toJson(ImmutableMap.of("condition_result", newArrayList(attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleConditionEvaluated(new ConditionEvaluatedEvent("Error evaluating condition")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutExpressionEvaluationResult() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final ImmutableMap<String, Object> attributes = ImmutableMap.of("id", 1, "type", "robot", "result", "123"); final String json = toJson(ImmutableMap.of("expression_result", newArrayList(attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleExpressionEvaluated(new ExpressionEvaluatedEvent(1, ExpressionType.ROBOT, "123", null)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutExpressionEvaluationError() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final ImmutableMap<String, Object> attributes = ImmutableMap.of("id", 1, "type", "robot", "error", "fail"); final String json = toJson(ImmutableMap.of("expression_result", newArrayList(attributes))); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleExpressionEvaluated(new ExpressionEvaluatedEvent(1, ExpressionType.ROBOT, null, "fail")); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutPausedEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final String json = toJson(ImmutableMap.of("paused", 0)); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handlePaused(new PausedEvent(null)); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }
@Test public void listenerIsNotifiedAboutClosedEvent() throws Exception { final RobotAgentEventListener listener = mock(RobotAgentEventListener.class); when(listener.isHandlingEvents()).thenReturn(true); final RobotAgentEventDispatcher dispatcher = new RobotAgentEventDispatcher(null, listener); final String json = toJson(ImmutableMap.of("close", 0)); dispatcher.runEventsLoop(readerFor(json)); verify(listener).eventsProcessingAboutToStart(); verify(listener, atLeast(1)).isHandlingEvents(); verify(listener).handleClosed(); verify(listener).eventsProcessingFinished(); verifyNoMoreInteractions(listener); }