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