Search in sources :

Example 6 with ExecutionResult

use of com.thoughtworks.go.plugin.api.response.execution.ExecutionResult 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 7 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(StringUtils.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("Error occurred while converting the Json to Execution Result. Error: {}. The Json received was '{}'.", 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)7 Test (org.junit.Test)6 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)2 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)2 GsonBuilder (com.google.gson.GsonBuilder)1 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)1 ActionWithReturn (com.thoughtworks.go.plugin.infra.ActionWithReturn)1 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)1 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)1 DefaultGoPublisher (com.thoughtworks.go.work.DefaultGoPublisher)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1