GetLogsAction extends AbstractRunnerAction { @Override public void perform(@NotNull final Runner runner) { eventLogger.log(this); CurrentProject project = appContext.getCurrentProject(); if (project == null) { return; } final Link viewLogsLink = runner.getLogUrl(); if (viewLogsLink == null) { return; } presenter.setActive(); AsyncRequestCallback<String> callback = callbackBuilderProvider .get() .unmarshaller(new StringUnmarshaller()) .success(new SuccessCallback<String>() { @Override public void onSuccess(String result) { consoleContainer.print(runner, result); } }) .failure(new FailureCallback() { @Override public void onFailure(@NotNull Throwable reason) { runnerUtil.showError(runner, constant.applicationLogsFailed(), reason); } }) .build(); service.getLogs(viewLogsLink, callback); } @Inject  GetLogsAction(RunnerServiceClient service,
                         AppContext appContext,
                         Provider<AsyncCallbackBuilder<String>> callbackBuilderProvider,
                         RunnerLocalizationConstant constant,
                         RunnerUtil runnerUtil,
                         ConsoleContainer consoleContainer,
                         RunnerManagerPresenter runnerManagerPresenter,
                         AnalyticsEventLogger eventLogger); @Override void perform(@NotNull final Runner runner);  }
@Test public void nothingShouldHappenWhenACurrentProjectIsEmpty() throws Exception { action.perform(runner); verify(eventLogger).log(action); verify(runnerUtil, never()).showError(any(Runner.class), anyString(), any(Throwable.class)); verify(runnerManagerPresenter, never()).setActive(); verify(service, never()).getLogs(any(Link.class), Matchers.<AsyncRequestCallback<String>>anyObject()); }
@Test public void errorShouldBeShownWhenLogUrlIsEmpty() throws Exception { when(constant.applicationLogsFailed()).thenReturn(SOME_TEXT); when(appContext.getCurrentProject()).thenReturn(project); action.perform(runner); verify(eventLogger).log(action); verify(runnerManagerPresenter, never()).setActive(); verify(service, never()).getLogs(any(Link.class), Matchers.<AsyncRequestCallback<String>>anyObject()); }
@Test public void requestShouldBeExecutedWhenAllIsOK() throws Exception { when(appContext.getCurrentProject()).thenReturn(project); Link viewLogsLink = mock(Link.class); when(runner.getLogUrl()).thenReturn(viewLogsLink); action.perform(runner); verify(eventLogger).log(action); verify(runnerUtil, never()).showError(any(Runner.class), anyString(), any(Throwable.class)); verify(runnerManagerPresenter).setActive(); verify(service).getLogs(eq(viewLogsLink), Matchers.<AsyncRequestCallback<String>>anyObject()); }
@Test public void successCallbackShouldBeExecutedWhenRequestIsSuccessful() throws Exception { when(appContext.getCurrentProject()).thenReturn(project); Link viewLogsLink = mock(Link.class); when(runner.getLogUrl()).thenReturn(viewLogsLink); action.perform(runner); verify(eventLogger).log(action); verify(callbackBuilder).success(successCallbackCaptor.capture()); SuccessCallback<String> successCallback = successCallbackCaptor.getValue(); successCallback.onSuccess(SOME_TEXT); verify(consoleContainer).print(runner, SOME_TEXT); }
@Test public void failedCallbackShouldBeExecutedWhenRequestIsFailed() throws Exception { Throwable throwable = mock(Throwable.class); when(appContext.getCurrentProject()).thenReturn(project); Link viewLogsLink = mock(Link.class); when(runner.getLogUrl()).thenReturn(viewLogsLink); when(constant.applicationLogsFailed()).thenReturn(SOME_TEXT); action.perform(runner); verify(eventLogger).log(action); verify(callbackBuilder).failure(failureCallbackCaptor.capture()); FailureCallback failureCallback = failureCallbackCaptor.getValue(); failureCallback.onFailure(throwable); verify(runnerUtil).showError(runner, SOME_TEXT, throwable); }