Search in sources :

Example 6 with GoPluginApiResponse

use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.

the class DefaultPluginManagerTest method shouldSubmitPluginApiRequestToGivenPlugin.

@Test
public void shouldSubmitPluginApiRequestToGivenPlugin() throws Exception {
    GoPluginApiRequest request = mock(GoPluginApiRequest.class);
    GoPluginApiResponse expectedResponse = mock(GoPluginApiResponse.class);
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPlugin.handle(request)).thenReturn(expectedResponse);
    ArgumentCaptor<PluginAwareDefaultGoApplicationAccessor> captor = ArgumentCaptor.forClass(PluginAwareDefaultGoApplicationAccessor.class);
    doNothing().when(goPlugin).initializeGoApplicationAccessor(captor.capture());
    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("plugin-id"), any(ActionWithReturn.class));
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
    GoPluginApiResponse actualResponse = pluginManager.submitTo("plugin-id", request);
    assertThat(actualResponse, is(expectedResponse));
    PluginAwareDefaultGoApplicationAccessor accessor = captor.getValue();
    assertThat(accessor.pluginDescriptor(), is(descriptor));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.Test)

Example 7 with GoPluginApiResponse

use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.

the class PluginController method handlePluginInteractRequest.

@RequestMapping(value = "/plugin/interact/{pluginId}/{requestName}", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE })
public void handlePluginInteractRequest(@PathVariable String pluginId, @PathVariable String requestName, HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!isAuthPlugin(pluginId)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter().println("Plugin interact endpoint is enabled only for Authentication Plugins");
        return;
    }
    if (isRestrictedRequestName(requestName)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter().println(String.format("Plugin interact for '%s' requestName is disallowed.", requestName));
        return;
    }
    DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(null, null, requestName);
    apiRequest.setRequestParams(getParameterMap(request));
    addRequestHeaders(request, apiRequest);
    try {
        GoPluginApiResponse pluginApiResponse = pluginManager.submitTo(pluginId, apiRequest);
        if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            renderPluginResponse(pluginApiResponse, response);
            return;
        }
        if (DefaultGoApiResponse.REDIRECT_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            String location = "";
            if (hasValueFor(pluginApiResponse, "Location")) {
                location = pluginApiResponse.responseHeaders().get("Location");
            }
            response.sendRedirect(location);
            return;
        }
    } catch (Exception e) {
    // handle
    }
    throw new RuntimeException("render error page");
}
Also used : DefaultGoPluginApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with GoPluginApiResponse

use of com.thoughtworks.go.plugin.api.response.GoPluginApiResponse in project gocd by gocd.

the class JsonBasedTaskExtensionHandler_V1Test method shouldConstructExecutionResultFromSuccessfulExecutionResponse.

@Test
public void shouldConstructExecutionResultFromSuccessfulExecutionResponse() {
    GoPluginApiResponse response = mock(GoPluginApiResponse.class);
    when(response.responseBody()).thenReturn("{\"success\":true,\"message\":\"message1\"}");
    ExecutionResult result = new JsonBasedTaskExtensionHandler_V1().toExecutionResult(response.responseBody());
    assertThat(result.isSuccessful(), is(true));
    assertThat(result.getMessagesForDisplay(), is("message1"));
}
Also used : ExecutionResult(com.thoughtworks.go.plugin.api.response.execution.ExecutionResult) 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