Search in sources :

Example 1 with GoPluginApiResponse

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");
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) Test(org.junit.Test)

Example 2 with GoPluginApiResponse

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"));
}
Also used : ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Example 3 with GoPluginApiResponse

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));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Example 4 with GoPluginApiResponse

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);
    }
}
Also used : DefaultGoPluginApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)

Example 5 with GoPluginApiResponse

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));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Aggregations

GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)8 Test (org.junit.Test)6 GoPlugin (com.thoughtworks.go.plugin.api.GoPlugin)3 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 GoPluginIdentifier (com.thoughtworks.go.plugin.api.GoPluginIdentifier)2 DefaultGoPluginApiRequest (com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest)2 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)2 ExecutionResult (com.thoughtworks.go.plugin.api.response.execution.ExecutionResult)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)2 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)1 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)1 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)1 IOException (java.io.IOException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1