Search in sources :

Example 1 with ActionWithReturn

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()));
    }
}
Also used : PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) ArgumentCaptor(org.mockito.ArgumentCaptor) BuildLogElement(com.thoughtworks.go.domain.BuildLogElement) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException) DefaultGoPublisher(com.thoughtworks.go.work.DefaultGoPublisher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) ActionWithReturn(com.thoughtworks.go.plugin.infra.ActionWithReturn) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) Test(org.junit.Test)

Example 2 with ActionWithReturn

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());
}
Also used : ActionWithReturn(com.thoughtworks.go.plugin.infra.ActionWithReturn) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) Test(org.junit.Test)

Example 3 with ActionWithReturn

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"));
    }
}
Also used : PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) ArgumentCaptor(org.mockito.ArgumentCaptor) BuildLogElement(com.thoughtworks.go.domain.BuildLogElement) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException) DefaultGoPublisher(com.thoughtworks.go.work.DefaultGoPublisher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) ActionWithReturn(com.thoughtworks.go.plugin.infra.ActionWithReturn) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) Test(org.junit.Test)

Aggregations

ExecutionResult (com.thoughtworks.go.plugin.api.response.execution.ExecutionResult)3 ActionWithReturn (com.thoughtworks.go.plugin.infra.ActionWithReturn)3 Test (org.junit.Test)3 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)2 BuildLogElement (com.thoughtworks.go.domain.BuildLogElement)2 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)2 CruiseControlException (com.thoughtworks.go.util.command.CruiseControlException)2 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)2 DefaultGoPublisher (com.thoughtworks.go.work.DefaultGoPublisher)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)1