use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.
the class TaskExtensionTest method shouldValidateTask.
@Test
public void shouldValidateTask() {
GoPluginApiResponse response = mock(GoPluginApiResponse.class);
TaskExtension jsonBasedTaskExtension = new TaskExtension(pluginManager);
TaskConfig taskConfig = mock(TaskConfig.class);
when(response.responseCode()).thenReturn(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE);
when(pluginManager.isPluginOfType(TaskExtension.TASK_EXTENSION, pluginId)).thenReturn(true);
when(response.responseBody()).thenReturn("{\"errors\":{\"key\":\"error\"}}");
when(pluginManager.submitTo(eq(pluginId), any(GoPluginApiRequest.class))).thenReturn(response);
ValidationResult validationResult = jsonBasedTaskExtension.validate(pluginId, taskConfig);
verify(pluginManager).submitTo(eq(pluginId), any(GoPluginApiRequest.class));
assertFalse(validationResult.isSuccessful());
assertEquals(validationResult.getErrors().get(0).getKey(), "key");
assertEquals(validationResult.getErrors().get(0).getMessage(), "error");
}
use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse 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.GoPluginApiResponse in project gocd by gocd.
the class DefaultPluginManagerTest method shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch.
@Test
public void shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch() throws Exception {
final String pluginThatDoesNotImplement = "plugin-that-does-not-implement";
GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier("another-extension-type", asList("1.0"));
final GoPlugin goPlugin = mock(GoPlugin.class);
final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginThatDoesNotImplement)).thenReturn(true);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[2];
return action.execute(goPlugin, descriptor);
}
}).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
assertFalse(pluginManager.isPluginOfType("extension-type", pluginThatDoesNotImplement));
verify(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
}
use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.
the class PluginRequestHelper method submitRequest.
public <T> T submitRequest(String pluginId, String requestName, PluginInteractionCallback<T> pluginInteractionCallback) {
if (!pluginManager.isPluginOfType(extensionName, pluginId)) {
throw new PluginNotFoundException(format("Did not find '%s' plugin with id '%s'. Looks like plugin is missing", extensionName, pluginId));
}
try {
String resolvedExtensionVersion = pluginManager.resolveExtensionVersion(pluginId, goSupportedVersions);
DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(extensionName, resolvedExtensionVersion, requestName);
apiRequest.setRequestBody(pluginInteractionCallback.requestBody(resolvedExtensionVersion));
apiRequest.setRequestParams(pluginInteractionCallback.requestParams(resolvedExtensionVersion));
GoPluginApiResponse response = pluginManager.submitTo(pluginId, apiRequest);
if (response == null) {
throw new RuntimeException("The plugin sent a null response");
}
if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == response.responseCode()) {
return pluginInteractionCallback.onSuccess(response.responseBody(), resolvedExtensionVersion);
}
throw new RuntimeException(format("The plugin sent a response that could not be understood by Go. Plugin returned with code '%s' and the following response: '%s'", response.responseCode(), response.responseBody()));
} catch (Exception e) {
throw new RuntimeException(format("Interaction with plugin with id '%s' implementing '%s' extension failed while requesting for '%s'. Reason: [%s]", pluginId, extensionName, requestName, e.getMessage()), e);
}
}
use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.
the class DefaultPluginManagerTest method shouldReturnTrueIfPluginIsOfGivenExtensionWhenReferenceFoundAndExtensionMatch.
@Test
public void shouldReturnTrueIfPluginIsOfGivenExtensionWhenReferenceFoundAndExtensionMatch() throws Exception {
String pluginId = "plugin-id";
GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier("sample-extension", asList("1.0"));
final GoPlugin goPlugin = mock(GoPlugin.class);
final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginId)).thenReturn(true);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[2];
return action.execute(goPlugin, descriptor);
}
}).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginId), any(ActionWithReturn.class));
when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
assertTrue(pluginManager.isPluginOfType("sample-extension", pluginId));
}
Aggregations