GetRunningProcessesAction extends AbstractRunnerAction { @Override public void perform() { project = appContext.getCurrentProject(); if (project == null) { return; } startCheckingNewProcesses(); Unmarshallable<List<ApplicationProcessDescriptor>> unmarshallable = dtoUnmarshallerFactory .newListUnmarshaller(ApplicationProcessDescriptor.class); AsyncRequestCallback<List<ApplicationProcessDescriptor>> callback = callbackBuilderProvider .get() .unmarshaller(unmarshallable) .success(new SuccessCallback<List<ApplicationProcessDescriptor>>() { @Override public void onSuccess(List<ApplicationProcessDescriptor> result) { if (result.isEmpty()) { return; } propertiesContainer.setVisible(true); for (ApplicationProcessDescriptor processDescriptor : result) { if (isNewOrRunningProcess(processDescriptor)) { prepareRunnerWithRunningApp(processDescriptor); } } } }) .failure(new FailureCallback() { @Override public void onFailure(@NotNull Throwable reason) { Log.error(GetRunningProcessesAction.class, reason); } }) .build(); service.getRunningProcesses(project.getProjectDescription().getPath(), callback); } @Inject  GetRunningProcessesAction(NotificationManager notificationManager,
                                     RunnerServiceClient service,
                                     DtoUnmarshallerFactory dtoUnmarshallerFactory,
                                     AppContext appContext,
                                     RunnerLocalizationConstant locale,
                                     Provider<AsyncCallbackBuilder<List<ApplicationProcessDescriptor>>> callbackBuilderProvider,
                                     WebSocketUtil webSocketUtil,
                                     RunnerActionFactory actionFactory,
                                     RunnerManagerPresenter runnerManagerPresenter,
                                     PropertiesContainer propertiesContainer,
                                     @Named("workspaceId") String workspaceId); @Override void perform(); @Override void stop();  }
@Test public void performShouldNotBeCompletedIfProjectIsNull() { reset(logsAction, actionFactory); when(appContext.getCurrentProject()).thenReturn(null); getRunningProcessesAction.perform(); verify(appContext).getCurrentProject(); verifyNoMoreInteractions(notificationManager, service, dtoUnmarshallerFactory, appContext, locale, callbackBuilderProvider, webSocketUtil, actionFactory, runnerManagerPresenter); }
@Test public void shouldPerformSuccessfullyCheckingAndSuccessPreparingRunnerWhenStatusIsRunning() throws Exception { when(processDescriptor.getStatus()).thenReturn(ApplicationStatus.RUNNING); getRunningProcessesAction.perform(); verify(appContext).getCurrentProject(); verifyCreationChannel(); verify(webSocketUtil).subscribeHandler(anyString(), handlerArgumentCaptor.capture()); SubscriptionHandler<ApplicationProcessDescriptor> processStartedHandler = handlerArgumentCaptor.getValue(); Method method = SubscriptionHandler.class.getDeclaredMethod("onMessageReceived", Object.class); method.setAccessible(true); method.invoke(processStartedHandler, processDescriptor); verify(runnerManagerPresenter).isRunnerExist(1234567890L); verify(processDescriptor).getProcessId(); verify(processDescriptor).getStatus(); verify(runnerManagerPresenter).addRunner(processDescriptor); verify(logsAction).perform(runner); verify(locale).projectRunningNow(PROJECT_NAME); verify(project, times(3)).getProjectDescription(); verify(projectDescriptor).getName(); verifyShowNotification(); verify(webSocketUtil).subscribeHandler(CHANNEL, processStartedHandler); }
@Test public void shouldPerformWithSuccessfullyCheckingAndSuccessPreparingRunnerWithRunningAppWhenStatusIsNEW() throws Exception { when(processDescriptor.getStatus()).thenReturn(ApplicationStatus.NEW); getRunningProcessesAction.perform(); verify(appContext).getCurrentProject(); verifyCreationChannel(); verify(webSocketUtil).subscribeHandler(anyString(), handlerArgumentCaptor.capture()); SubscriptionHandler<ApplicationProcessDescriptor> processStartedHandler = handlerArgumentCaptor.getValue(); Method method = SubscriptionHandler.class.getDeclaredMethod("onMessageReceived", Object.class); method.setAccessible(true); method.invoke(processStartedHandler, processDescriptor); verify(runnerManagerPresenter).isRunnerExist(1234567890L); verify(processDescriptor).getStatus(); verify(runnerManagerPresenter).addRunner(processDescriptor); verify(logsAction).perform(runner); verify(notificationManager).showNotification(notificationCaptor.capture()); Notification notification = notificationCaptor.getValue(); assertThat(notification.getMessage(), is(MESSAGE)); assertThat(notification.isImportant(), is(true)); assertThat(notification.isInfo(), is(true)); verify(webSocketUtil).subscribeHandler(CHANNEL, processStartedHandler); runnerAndApplicationShouldBePrepared(); }
@Test public void shouldPerformWithSuccessfullyCheckingAndSuccessPreparingRunnerWhenStatusIsNotNewOrRunning() throws Exception { reset(logsAction); when(processDescriptor.getStatus()).thenReturn(null); getRunningProcessesAction.perform(); verify(appContext).getCurrentProject(); verifyCreationChannel(); verify(webSocketUtil).subscribeHandler(anyString(), handlerArgumentCaptor.capture()); SubscriptionHandler<ApplicationProcessDescriptor> processStartedHandler = handlerArgumentCaptor.getValue(); TestUtil.invokeMethodByName(processStartedHandler, "onMessageReceived", processDescriptor); verify(runnerManagerPresenter).isRunnerExist(1234567890L); verify(processDescriptor).getProcessId(); verify(processDescriptor).getStatus(); verifyNoMoreInteractions(runnerManagerPresenter, logsAction, notificationManager, locale); verify(webSocketUtil).subscribeHandler(CHANNEL, processStartedHandler); }
@Test public void shouldPerformWithSuccessfullyCheckingAndSuccessPreparingRunnerWhenRunnerIsExist() throws Exception { reset(logsAction); when(runnerManagerPresenter.isRunnerExist(1234567890L)).thenReturn(true); when(processDescriptor.getStatus()).thenReturn(ApplicationStatus.RUNNING); getRunningProcessesAction.perform(); verify(appContext).getCurrentProject(); verifyCreationChannel(); verify(webSocketUtil).subscribeHandler(anyString(), handlerArgumentCaptor.capture()); SubscriptionHandler<ApplicationProcessDescriptor> processStartedHandler = handlerArgumentCaptor.getValue(); TestUtil.invokeMethodByName(processStartedHandler, "onMessageReceived", processDescriptor); verify(runnerManagerPresenter).isRunnerExist(1234567890L); verify(processDescriptor).getProcessId(); verifyNoMoreInteractions(runnerManagerPresenter, logsAction, notificationManager, locale); verify(webSocketUtil).subscribeHandler(CHANNEL, processStartedHandler); runnerAndApplicationShouldBePrepared(); verify(webSocketUtil).subscribeHandler(CHANNEL, processStartedHandler); }
@Test public void runnerAndApplicationShouldNotBePreparedBecauseDescriptorsArrayIsEmpty() { result = new ArrayList<>(); reset(notificationManager,logsAction); getRunningProcessesAction.perform(); verify(asyncCallbackBuilder).success(successCallBackCaptor.capture()); SuccessCallback<List<ApplicationProcessDescriptor>> successCallback = successCallBackCaptor.getValue(); successCallback.onSuccess(result); verify(service).getRunningProcesses(PATH_TO_PROJECT, callback); verifyNoMoreInteractions(service, propertiesContainer, notificationManager, logsAction); }