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());
}
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()));
}
}
Aggregations