Java Code Examples for com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException

The following examples show how to use com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void shouldContainFilePatternInResponseToGetConfigurationRequest() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest getConfigRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "go.plugin-settings.get-configuration");

    GoPluginApiResponse response = plugin.handle(getConfigRequest);
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    JsonElement pattern = responseJsonObject.get("file_pattern");
    assertNotNull(pattern);
    JsonObject patternAsJsonObject = pattern.getAsJsonObject();
    assertThat(patternAsJsonObject.get("display-name").getAsString(), is("Go YAML files pattern"));
    assertThat(patternAsJsonObject.get("default-value").getAsString(), is("**/*.gocd.yaml,**/*.gocd.yml"));
    assertThat(patternAsJsonObject.get("required").getAsBoolean(), is(false));
    assertThat(patternAsJsonObject.get("secure").getAsBoolean(), is(false));
    assertThat(patternAsJsonObject.get("display-order").getAsInt(), is(0));
}
 
Example 2
@Test
public void shouldContainValidFieldsInResponseMessage() throws UnhandledRequestTypeException {
    GoApiResponse settingsResponse = DefaultGoApiResponse.success("{}");
    when(goAccessor.submit(any(GoApiRequest.class))).thenReturn(settingsResponse);

    GoPluginApiResponse response = parseAndGetResponseForDir(tempDir.getRoot());

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    final JsonParser parser = new JsonParser();
    JsonElement responseObj = parser.parse(response.responseBody());
    assertTrue(responseObj.isJsonObject());
    JsonObject obj = responseObj.getAsJsonObject();
    assertTrue(obj.has("errors"));
    assertTrue(obj.has("pipelines"));
    assertTrue(obj.has("environments"));
    assertTrue(obj.has("target_version"));
}
 
Example 3
Source Project: gocd-plugins   Source File: NessusScanTask.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRequestTypeException {
    switch (request.requestName()) {
        case "configuration":
        case "go.plugin-settings.get-configuration":
            return handleGetConfigRequest();
        case "validate":
        case "go.plugin-settings.validate-configuration":
            return handleValidation();
        case "execute":
            return handleTaskExecution(request);
        case "view":
        case "go.plugin-settings.get-view":
            return handleTaskView();
        default:
            throw new UnhandledRequestTypeException(request.requestName());
    }
}
 
Example 4
Source Project: gocd   Source File: DummyClassWithLocalInnerClass.java    License: Apache License 2.0 6 votes vote down vote up
public void foo() {
    @Extension
    class DummyInnerClassWithExtension implements GoPlugin {
        @Override
        public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationAccessor) {
            throw new UnsupportedOperationException();
        }

        @Override
        public GoPluginApiResponse handle(GoPluginApiRequest requestMessage) throws UnhandledRequestTypeException {
            throw new UnsupportedOperationException();
        }

        @Override
        public GoPluginIdentifier pluginIdentifier() {
            throw new UnsupportedOperationException();
        }
    }
}
 
Example 5
Source Project: gocd   Source File: DummyClassProvidingAnonymousClass.java    License: Apache License 2.0 6 votes vote down vote up
public static GoPlugin getAnonymousClass() {
    return new GoPlugin() {
        @Override
        public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationAccessor) {
        }

        @Override
        public GoPluginApiResponse handle(GoPluginApiRequest requestMessage) throws UnhandledRequestTypeException {
            return null;
        }

        @Override
        public GoPluginIdentifier pluginIdentifier() {
            return null;
        }
    };
}
 
Example 6
Source Project: script-executor-task   Source File: GoPluginImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    if (goPluginApiRequest.requestName().equals(REQUEST_CONFIGURATION) || goPluginApiRequest.requestName().equals(REQUEST_CONFIGURATION_2)) {
        return handleConfiguration();
    } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATION) || goPluginApiRequest.requestName().equals(REQUEST_VALIDATION_2)) {
        return handleValidation(goPluginApiRequest);
    } else if (goPluginApiRequest.requestName().equals(REQUEST_TASK_VIEW) || goPluginApiRequest.requestName().equals(REQUEST_TASK_VIEW_2)) {
        try {
            return handleView();
        } catch (IOException e) {
            String message = "Failed to find template: " + e.getMessage();
            return renderJSON(500, message);
        }
    } else if (goPluginApiRequest.requestName().equals(REQUEST_EXECUTION)) {
        return handleExecute(goPluginApiRequest);
    }
    return null;
}
 
Example 7
Source Project: gocd-s3-artifacts   Source File: S3PackageMaterialPoller.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    if (goPluginApiRequest.requestName().equals(REQUEST_REPOSITORY_CONFIGURATION)) {
        return handleRepositoryConfiguration();
    } else if (goPluginApiRequest.requestName().equals(REQUEST_PACKAGE_CONFIGURATION)) {
        return handlePackageConfiguration();
    } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATE_REPOSITORY_CONFIGURATION)) {
        return handleRepositoryValidation(goPluginApiRequest);
    } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATE_PACKAGE_CONFIGURATION)) {
        return handlePackageValidation();
    } else if (goPluginApiRequest.requestName().equals(REQUEST_CHECK_REPOSITORY_CONNECTION)) {
        return handleRepositoryCheckConnection(goPluginApiRequest);
    } else if (goPluginApiRequest.requestName().equals(REQUEST_CHECK_PACKAGE_CONNECTION)) {
        return handlePackageCheckConnection(goPluginApiRequest);
    } else if (goPluginApiRequest.requestName().equals(REQUEST_LATEST_REVISION)) {
        return handleGetLatestRevision(goPluginApiRequest);
    } else if (goPluginApiRequest.requestName().equals(REQUEST_LATEST_REVISION_SINCE)) {
        return handleLatestRevisionSince(goPluginApiRequest);
    }
    return null;
}
 
Example 8
Source Project: go-plugins   Source File: InvalidXmlPlugin.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    return new GoPluginApiResponse() {
        @Override
        public int responseCode() {
            return 200;
        }

        @Override
        public Map<String, String> responseHeaders() {
            return null;
        }

        @Override
        public String responseBody() {
            return "{}";
        }
    };
}
 
Example 9
Source Project: go-plugins   Source File: PluginWithMultipleLoadMethods.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    return new GoPluginApiResponse() {
        @Override
        public int responseCode() {
            return 200;
        }

        @Override
        public Map<String, String> responseHeaders() {
            return null;
        }

        @Override
        public String responseBody() {
            return "{}";
        }
    };
}
 
Example 10
Source Project: go-plugins   Source File: PluginWithInvalidId.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    return new GoPluginApiResponse() {
        @Override
        public int responseCode() {
            return 200;
        }

        @Override
        public Map<String, String> responseHeaders() {
            return null;
        }

        @Override
        public String responseBody() {
            return "{}";
        }
    };
}
 
Example 11
Source Project: go-plugins   Source File: Plugin2.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {

    if ("configuration".equals(goPluginApiRequest.requestName())) {
        HashMap<String, Object> config = new HashMap<>();

        HashMap<String, Object> url = new HashMap<>();
        url.put("display-order", "0");
        url.put("display-name", "Url");
        url.put("required", true);
        config.put("Url", url);
        return DefaultGoPluginApiResponse.success(new GsonBuilder().create().toJson(config));
    } else if ("view".equals(goPluginApiRequest.requestName())) {
        return getViewRequest();
    }
    throw new UnhandledRequestTypeException(goPluginApiRequest.requestName());
}
 
Example 12
Source Project: gocd   Source File: DummyClassProvidingAnonymousClass.java    License: Apache License 2.0 6 votes vote down vote up
public static GoPlugin getAnonymousClass() {
    return new GoPlugin() {
        @Override
        public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationAccessor) {
        }

        @Override
        public GoPluginApiResponse handle(GoPluginApiRequest requestMessage) throws UnhandledRequestTypeException {
            return null;
        }

        @Override
        public GoPluginIdentifier pluginIdentifier() {
            return null;
        }
    };
}
 
Example 13
@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) {
    try {
        switch (Request.fromString(request.requestName())) {
            case REQUEST_GET_PLUGIN_ICON:
                return new GetPluginIconExecutor().execute();
            case REQUEST_GET_PLUGIN_CAPABILITIES:
                return new GetCapabilitiesExecutor().execute();
            case REQUEST_STORE_CONFIG_METADATA:
                return new GetArtifactStoreConfigMetadataExecutor().execute();
            case REQUEST_STORE_CONFIG_VIEW:
                return new GetArtifactStoreViewExecutor().execute();
            case REQUEST_STORE_CONFIG_VALIDATE:
                return new ValidateArtifactStoreConfigExecutor(request).execute();
            case REQUEST_PUBLISH_ARTIFACT_METADATA:
                return new GetPublishArtifactConfigMetadataExecutor().execute();
            case REQUEST_PUBLISH_ARTIFACT_VIEW:
                return new GetPublishArtifactViewExecutor().execute();
            case REQUEST_PUBLISH_ARTIFACT_VALIDATE:
                return new ValidatePublishArtifactConfigExecutor(request).execute();
            case REQUEST_FETCH_ARTIFACT_METADATA:
                return new GetFetchArtifactMetadataExecutor().execute();
            case REQUEST_FETCH_ARTIFACT_VIEW:
                return new GetFetchArtifactViewExecutor().execute();
            case REQUEST_FETCH_ARTIFACT_VALIDATE:
                return new ValidateFetchArtifactConfigExecutor(request).execute();
            case REQUEST_PUBLISH_ARTIFACT:
                return new PublishArtifactExecutor(request, consoleLogger).execute();
            case REQUEST_FETCH_ARTIFACT:
                return new FetchArtifactExecutor(request, consoleLogger).execute();
            default:
                throw new UnhandledRequestTypeException(request.requestName());
        }
    } catch (Exception e) {
        LOG.error("Error while executing request " + request.requestName(), e);
        throw new RuntimeException(e);
    }
}
 
Example 14
Source Project: gocd-yaml-config-plugin   Source File: YamlConfigPlugin.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRequestTypeException {
    String requestName = request.requestName();

    switch (requestName) {
        case REQ_PLUGIN_SETTINGS_GET_CONFIGURATION:
            return handleGetPluginSettingsConfiguration();
        case REQ_CONFIG_FILES:
            return handleGetConfigFiles(request);
        case REQ_PLUGIN_SETTINGS_GET_VIEW:
            try {
                return handleGetPluginSettingsView();
            } catch (IOException e) {
                return error(gson.toJson(format("Failed to find template: %s", e.getMessage())));
            }
        case REQ_PLUGIN_SETTINGS_VALIDATE_CONFIGURATION:
            return handleValidatePluginSettingsConfiguration();
        case REQ_PARSE_CONTENT:
            return handleParseContentRequest(request);
        case REQ_PARSE_DIRECTORY:
            ensureConfigured();
            return handleParseDirectoryRequest(request);
        case REQ_PIPELINE_EXPORT:
            return handlePipelineExportRequest(request);
        case REQ_GET_CAPABILITIES:
            return success(gson.toJson(new Capabilities()));
        case REQ_PLUGIN_SETTINGS_CHANGED:
            configurePlugin(PluginSettings.fromJson(request.requestBody()));
            return new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, "");
        case REQ_GET_ICON:
            return handleGetIconRequest();
        default:
            throw new UnhandledRequestTypeException(requestName);
    }
}
 
Example 15
@Test
public void shouldRespondSuccessToGetConfigurationRequest() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest getConfigRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "go.plugin-settings.get-configuration");

    GoPluginApiResponse response = plugin.handle(getConfigRequest);
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
}
 
Example 16
@Test
public void shouldRespondSuccessToGetViewRequest() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest getConfigRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "go.plugin-settings.get-view");

    GoPluginApiResponse response = plugin.handle(getConfigRequest);
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
}
 
Example 17
@Test
public void shouldRespondSuccessToValidateConfigRequest() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest validateRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "go.plugin-settings.validate-configuration");

    GoPluginApiResponse response = plugin.handle(validateRequest);
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
}
 
Example 18
@Test
public void shouldRespondSuccessToParseDirectoryRequestWhenEmpty() throws UnhandledRequestTypeException {
    GoPluginApiResponse response = parseAndGetResponseForDir(tempDir.getRoot());

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    assertNoError(responseJsonObject);
}
 
Example 19
@Test
public void shouldRespondSuccessToParseDirectoryRequestWhenSimpleCaseFile() throws UnhandledRequestTypeException, IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("simple"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    assertNoError(responseJsonObject);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(1));
    JsonObject expected = (JsonObject) readJsonObject("examples.out/simple.gocd.json");
    assertThat(responseJsonObject, is(new JsonObjectMatcher(expected)));
}
 
Example 20
@Test
public void shouldRespondSuccessToParseDirectoryRequestWhenRichCaseFile() throws UnhandledRequestTypeException, IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("rich"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    assertNoError(responseJsonObject);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(1));
    JsonObject expected = (JsonObject) readJsonObject("examples.out/rich.gocd.json");
    assertThat(responseJsonObject, is(new JsonObjectMatcher(expected)));
}
 
Example 21
@Test
public void shouldRespondSuccessWithErrorMessagesToParseDirectoryRequestWhenSimpleInvalidCaseFile() throws UnhandledRequestTypeException, IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("simple-invalid"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(0));
    assertFirstError(responseJsonObject, "Failed to parse pipeline pipe1; expected a hash of pipeline materials", "simple-invalid.gocd.yaml");
}
 
Example 22
@Test
public void shouldRespondSuccessWithErrorMessagesToParseDirectoryRequestWhenDuplicateKeysCaseFile() throws UnhandledRequestTypeException, IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("duplicate-materials"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(0));
    assertFirstError(responseJsonObject, "Line 9, column 20: Duplicate key found 'upstream'", "duplicate-materials.gocd.yaml");
}
 
Example 23
@Test
public void shouldRespondSuccessWithErrorMessagesToParseDirectoryRequestWhenParsingErrorCaseFile() throws UnhandledRequestTypeException, IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("invalid-materials"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(0));
    assertFirstError(responseJsonObject, "Error parsing YAML. : Line 21, column 0: Expected a 'block end' but found: scalar : ", "invalid-materials.gocd.yaml");
}
 
Example 24
@Test
public void shouldRespondBadRequestToParseDirectoryRequestWhenRequestBodyIsNull() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest parseDirectoryRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "parse-directory");
    String requestBody = null;
    parseDirectoryRequest.setRequestBody(requestBody);

    GoPluginApiResponse response = plugin.handle(parseDirectoryRequest);
    assertThat(response.responseCode(), is(DefaultGoPluginApiResponse.BAD_REQUEST));
}
 
Example 25
@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    switch(goPluginApiRequest.requestName()){
        case "Thread.currentThread.getContextClassLoader":
            return DefaultGoPluginApiResponse.success(Thread.currentThread().getContextClassLoader().getClass().getCanonicalName());
        case "this.getClass.getClassLoader":
            return DefaultGoPluginApiResponse.success(this.getClass().getClassLoader().getClass().getCanonicalName());
        default:
            throw new UnhandledRequestTypeException(goPluginApiRequest.requestName());
    }
}
 
Example 26
@Test
public void shouldRespondBadRequestToParseDirectoryRequestWhenRequestBodyIsNotJson() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest parseDirectoryRequest = new DefaultGoPluginApiRequest("configrepo", "1.0", "parse-directory");
    parseDirectoryRequest.setRequestBody("{bla");

    GoPluginApiResponse response = plugin.handle(parseDirectoryRequest);
    assertThat(response.responseCode(), is(DefaultGoPluginApiResponse.BAD_REQUEST));
}
 
Example 27
@Test
public void shouldConsumePluginSettingsOnConfigChangeRequest() throws UnhandledRequestTypeException {
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest("configrepo", "2.0", REQ_PLUGIN_SETTINGS_CHANGED);
    request.setRequestBody("{\"file_pattern\": \"*.foo.gocd.yaml\"}");

    assertEquals(DEFAULT_FILE_PATTERN, plugin.getFilePattern());
    GoPluginApiResponse response = plugin.handle(request);

    assertEquals("*.foo.gocd.yaml", plugin.getFilePattern());
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
}
 
Example 28
@Test
public void shouldRespondSuccessToParseDirectoryRequestWhenPluginHasConfiguration() throws UnhandledRequestTypeException {
    GoApiResponse settingsResponse = DefaultGoApiResponse.success("{}");
    when(goAccessor.submit(any(GoApiRequest.class))).thenReturn(settingsResponse);

    GoPluginApiResponse response = parseAndGetResponseForDir(tempDir.getRoot());

    verify(goAccessor, times(1)).submit(any(GoApiRequest.class));
    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
}
 
Example 29
@Test
public void shouldRespondSuccessToParseDirectoryRequestWhenAliasesCaseFile() throws UnhandledRequestTypeException,
        IOException {
    GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("aliases"));

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    JsonObject responseJsonObject = getJsonObjectFromResponse(response);
    assertNoError(responseJsonObject);
    JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray();
    assertThat(pipelines.size(), is(1));
    JsonObject expected = (JsonObject) readJsonObject("examples.out/aliases.gocd.json");
    assertThat(responseJsonObject, is(new JsonObjectMatcher(expected)));
}
 
Example 30
@Test
public void shouldRespondWithCapabilities() throws UnhandledRequestTypeException {
    String expected = new Gson().toJson(new Capabilities());
    DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest("configrepo", "2.0", "get-capabilities");

    GoPluginApiResponse response = plugin.handle(request);

    assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE));
    assertThat(response.responseBody(), is(expected));
}