use of com.thoughtworks.go.plugin.infra.ActionWithReturn in project gocd by gocd.
the class PluggableTaskBuilderTest method shouldPublishErrorMessageIfPluginThrowsAnException.
@Test
public void shouldPublishErrorMessageIfPluginThrowsAnException() throws CruiseControlException {
PluggableTask task = mock(PluggableTask.class);
when(task.getPluginConfiguration()).thenReturn(new PluginConfiguration());
PluggableTaskBuilder taskBuilder = new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, TEST_PLUGIN_ID, "test-directory") {
@Override
protected ExecutionResult executeTask(Task task, BuildLogElement buildLogElement, DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext) {
throw new RuntimeException("err");
}
};
when(pluginManager.doOn(eq(Task.class), eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenAnswer(new Answer<ExecutionResult>() {
@Override
public ExecutionResult answer(InvocationOnMock invocationOnMock) throws Throwable {
ActionWithReturn<Task, ExecutionResult> actionWithReturn = (ActionWithReturn<Task, ExecutionResult>) invocationOnMock.getArguments()[2];
return actionWithReturn.execute(mock(Task.class), pluginDescriptor);
}
});
try {
taskBuilder.build(buildLogElement, goPublisher, variableContext, taskExtension);
fail("expected exception to be thrown");
} catch (Exception e) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(goPublisher).consumeLine(captor.capture());
String error = "Error: err";
assertThat(captor.getValue(), is(error));
assertThat(e.getMessage(), is(new RuntimeException("err").toString()));
}
}
use of com.thoughtworks.go.plugin.infra.ActionWithReturn in project gocd by gocd.
the class TaskExtensionTest method shouldExecuteTheTask.
@Test
public void shouldExecuteTheTask() {
ActionWithReturn actionWithReturn = mock(ActionWithReturn.class);
when(actionWithReturn.execute(any(JsonBasedPluggableTask.class), any(GoPluginDescriptor.class))).thenReturn(ExecutionResult.success("yay"));
ExecutionResult executionResult = extension.execute(pluginId, actionWithReturn);
verify(actionWithReturn).execute(any(JsonBasedPluggableTask.class), any(GoPluginDescriptor.class));
assertThat(executionResult.getMessagesForDisplay(), is("yay"));
assertTrue(executionResult.isSuccessful());
}
use of com.thoughtworks.go.plugin.infra.ActionWithReturn in project gocd by gocd.
the class PluggableTaskBuilderTest method shouldPublishErrorMessageIfPluginReturnsAFailureResponse.
@Test
public void shouldPublishErrorMessageIfPluginReturnsAFailureResponse() throws CruiseControlException {
PluggableTask task = mock(PluggableTask.class);
when(task.getPluginConfiguration()).thenReturn(new PluginConfiguration());
PluggableTaskBuilder taskBuilder = new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, TEST_PLUGIN_ID, "test-directory") {
@Override
protected ExecutionResult executeTask(Task task, BuildLogElement buildLogElement, DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext) {
return ExecutionResult.failure("err");
}
};
when(pluginManager.doOn(eq(Task.class), eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenAnswer(new Answer<ExecutionResult>() {
@Override
public ExecutionResult answer(InvocationOnMock invocationOnMock) throws Throwable {
ActionWithReturn<Task, ExecutionResult> actionWithReturn = (ActionWithReturn<Task, ExecutionResult>) invocationOnMock.getArguments()[2];
return actionWithReturn.execute(mock(Task.class), pluginDescriptor);
}
});
try {
taskBuilder.build(buildLogElement, goPublisher, variableContext, taskExtension);
fail("expected exception to be thrown");
} catch (Exception e) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(goPublisher).consumeLine(captor.capture());
assertThat(captor.getValue(), is("err"));
assertThat(e.getMessage(), is("err"));
}
}
Aggregations