Search in sources :

Example 1 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult in project gocd by gocd.

the class JsonBasedTaskExecutorTest method shouldExecuteAndReturnSuccessfulExecutionResultTaskThroughPlugin.

@Test
public void shouldExecuteAndReturnSuccessfulExecutionResultTaskThroughPlugin() {
    when(pluginManager.submitTo(eq(pluginId), any(GoPluginApiRequest.class))).thenReturn(response);
    when(handler.toExecutionResult(response.responseBody())).thenReturn(ExecutionResult.success("message1"));
    ExecutionResult result = new JsonBasedTaskExecutor(pluginId, pluginRequestHelper, handlerHashMap).execute(config(), context);
    assertThat(result.isSuccessful(), is(true));
    assertThat(result.getMessagesForDisplay(), is("message1"));
    ArgumentCaptor<GoPluginApiRequest> argument = ArgumentCaptor.forClass(GoPluginApiRequest.class);
    verify(pluginManager).submitTo(eq(pluginId), argument.capture());
    assertThat(argument.getValue().extension(), is(TaskExtension.TASK_EXTENSION));
    assertThat(argument.getValue().extensionVersion(), is(extensionVersion));
    assertThat(argument.getValue().requestName(), is(TaskExtension.EXECUTION_REQUEST));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) Test(org.junit.Test)

Example 2 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult in project gocd by gocd.

the class JsonBasedTaskExecutorTest method shouldExecuteAndReturnFailureExecutionResultTaskThroughPlugin.

@Test
public void shouldExecuteAndReturnFailureExecutionResultTaskThroughPlugin() {
    when(pluginManager.submitTo(eq(pluginId), any(GoPluginApiRequest.class))).thenReturn(response);
    when(handler.toExecutionResult(response.responseBody())).thenReturn(ExecutionResult.failure("error1"));
    ExecutionResult result = new JsonBasedTaskExecutor(pluginId, pluginRequestHelper, handlerHashMap).execute(config(), context);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.getMessagesForDisplay(), is("error1"));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) Test(org.junit.Test)

Example 3 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult in project gocd by gocd.

the class JsonBasedTaskExtensionHandler_V1Test method shouldConstructExecutionResultFromFailureExecutionResponse.

@Test
public void shouldConstructExecutionResultFromFailureExecutionResponse() {
    GoPluginApiResponse response = mock(GoPluginApiResponse.class);
    when(response.responseBody()).thenReturn("{\"success\":false,\"message\":\"error1\"}");
    ExecutionResult result = new JsonBasedTaskExtensionHandler_V1().toExecutionResult(response.responseBody());
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.getMessagesForDisplay(), is("error1"));
}
Also used : ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Example 4 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult 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 5 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult in project gocd by gocd.

the class JsonBasedTaskExtensionHandler_V1 method toExecutionResult.

@Override
public ExecutionResult toExecutionResult(String responseBody) {
    ExecutionResult executionResult = new ExecutionResult();
    ArrayList<String> exceptions = new ArrayList<>();
    try {
        Map result = (Map) new GsonBuilder().create().fromJson(responseBody, Object.class);
        if (!(result.containsKey("success") && result.get("success") instanceof Boolean)) {
            exceptions.add("The Json for Execution Result must contain a not-null 'success' field of type Boolean");
        }
        if (result.containsKey("message") && (!(result.get("message") instanceof String))) {
            exceptions.add("If the 'message' key is present in the Json for Execution Result, it must contain a not-null message of type String");
        }
        if (!exceptions.isEmpty()) {
            throw new RuntimeException(ListUtil.join(exceptions));
        }
        if ((Boolean) result.get("success")) {
            executionResult.withSuccessMessages((String) result.get("message"));
        } else {
            executionResult.withErrorMessages((String) result.get("message"));
        }
        return executionResult;
    } catch (Exception e) {
        LOGGER.error(String.format("Error occurred while converting the Json to Execution Result. Error: %s. The Json received was '%s'.", e.getMessage(), responseBody));
        throw new RuntimeException(String.format("Error occurred while converting the Json to Execution Result. Error: %s.", e.getMessage()));
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ExecutionResult (com.thoughtworks.go.plugin.api.response.execution.ExecutionResult)9 Test (org.junit.Test)8 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)3 BuildLogElement (com.thoughtworks.go.domain.BuildLogElement)3 ActionWithReturn (com.thoughtworks.go.plugin.infra.ActionWithReturn)3 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)3 DefaultGoPublisher (com.thoughtworks.go.work.DefaultGoPublisher)3 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)2 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)2 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 CruiseControlException (com.thoughtworks.go.util.command.CruiseControlException)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 GsonBuilder (com.google.gson.GsonBuilder)1 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1