CheckHealthStatusAction extends AbstractRunnerAction { @Override public void perform(@NotNull final Runner runner) { project = appContext.getCurrentProject(); if (runner.getApplicationURL() == null) { return; } changeAppAliveTimer = timerFactory.newInstance(new TimerFactory.TimerCallBack() { @Override public void onRun() { presenter.update(runner); String projectName = project.getProjectDescription().getName(); String notificationMessage = locale.applicationMaybeStarted(projectName); notification.update(notificationMessage, WARNING, FINISHED, null, true); consoleContainer.printWarn(runner, notificationMessage); } }); changeAppAliveTimer.schedule(THIRTY_SEC.getValue()); webSocketChannel = APP_HEALTH_CHANNEL + runner.getProcessId(); final String projectName = project.getProjectDescription().getName(); final String notificationMessage = locale.applicationStarted(projectName); runnerHealthHandler = new SubscriptionHandler<String>(new StringUnmarshallerWS()) { @Override protected void onMessageReceived(String result) { JSONObject jsonObject = JSONParser.parseStrict(result).isObject(); if(!jsonObjectIsValid(jsonObject)) { return; } changeAppAliveTimer.cancel(); runner.setStatus(Runner.Status.DONE); presenter.update(runner); notification.update(notificationMessage, INFO, FINISHED, null, true); consoleContainer.printInfo(runner, notificationMessage); stop(); } @Override protected void onErrorReceived(Throwable exception) { Log.error(getClass(), exception); } }; webSocketUtil.subscribeHandler(webSocketChannel, runnerHealthHandler); } @Inject  CheckHealthStatusAction(AppContext appContext,
                                   RunnerLocalizationConstant locale,
                                   RunnerManagerPresenter presenter,
                                   WebSocketUtil webSocketUtil,
                                   ConsoleContainer consoleContainer,
                                   TimerFactory timerFactory,
                                   @NotNull @Assisted Notification notification); @Override void perform(@NotNull final Runner runner); @Override void stop();  }
@Test public void shouldPerformWhenApplicationURLIsNull() { when(runner.getApplicationURL()).thenReturn(null); checkHealthStatusAction.perform(runner); verify(appContext).getCurrentProject(); verifyNoMoreInteractions(appContext, locale, presenter, webSocketUtil, consoleContainer, timerFactory); }
@Test public void shouldCheckPerformActionBeforeSendIntoServer() { when(timerFactory.newInstance(any(TimerFactory.TimerCallBack.class))).thenReturn(changeAppAliveTimer); checkHealthStatusAction.perform(runner); verify(appContext).getCurrentProject(); verify(runner).getApplicationURL(); }
@Test public void shouldPerformOperationBeforeSendDataToServer() throws Exception { checkHealthStatusAction.perform(runner); verify(appContext).getCurrentProject(); verify(runner).getApplicationURL(); verify(timerFactory).newInstance(changeAppAliveTimerCaptor.capture()); TimerFactory.TimerCallBack timerCallBack = changeAppAliveTimerCaptor.getValue(); timerCallBack.onRun(); verify(presenter).update(runner); verify(notification).update(NOTIFICATION_MESSAGE, WARNING, FINISHED, null, true); verify(consoleContainer).printWarn(runner, NOTIFICATION_MESSAGE); verify(changeAppAliveTimer).schedule(TimeInterval.THIRTY_SEC.getValue()); verify(runner).getProcessId(); verify(webSocketUtil).subscribeHandler(eq(WEB_SOCKET_CHANNEL), runnerHealthHandlerCaptor.capture()); SubscriptionHandler<String> runnerHealthHandler = runnerHealthHandlerCaptor.getValue(); TestUtil.invokeMethodByName(runnerHealthHandler, "onMessageReceived", RESULT); verify(webSocketUtil).subscribeHandler(WEB_SOCKET_CHANNEL, runnerHealthHandler); }