Search in sources :

Example 76 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project elasticsearch by elastic.

the class DeprecationHttpIT method doTestDeprecationWarningsAppearInHeaders.

/**
     * Run a request that receives a predictably randomized number of deprecation warnings.
     * <p>
     * Re-running this back-to-back helps to ensure that warnings are not being maintained across requests.
     */
private void doTestDeprecationWarningsAppearInHeaders() throws IOException {
    final boolean useDeprecatedField = randomBoolean();
    final boolean useNonDeprecatedSetting = randomBoolean();
    // deprecated settings should also trigger a deprecation warning
    final List<Setting<Boolean>> settings = new ArrayList<>(3);
    settings.add(TEST_DEPRECATED_SETTING_TRUE1);
    if (randomBoolean()) {
        settings.add(TEST_DEPRECATED_SETTING_TRUE2);
    }
    if (useNonDeprecatedSetting) {
        settings.add(TEST_NOT_DEPRECATED_SETTING);
    }
    Collections.shuffle(settings, random());
    // trigger all deprecations
    Response response = getRestClient().performRequest("GET", "/_test_cluster/deprecated_settings", Collections.emptyMap(), buildSettingsRequest(settings, useDeprecatedField));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(OK.getStatus()));
    final List<String> deprecatedWarnings = getWarningHeaders(response.getHeaders());
    final List<Matcher<String>> headerMatchers = new ArrayList<>(4);
    headerMatchers.add(equalTo(TestDeprecationHeaderRestAction.DEPRECATED_ENDPOINT));
    if (useDeprecatedField) {
        headerMatchers.add(equalTo(TestDeprecationHeaderRestAction.DEPRECATED_USAGE));
    }
    for (Setting<?> setting : settings) {
        if (setting.isDeprecated()) {
            headerMatchers.add(equalTo("[" + setting.getKey() + "] setting was deprecated in Elasticsearch and will be removed in a future release! " + "See the breaking changes documentation for the next major version."));
        }
    }
    assertThat(deprecatedWarnings, hasSize(headerMatchers.size()));
    for (final String deprecatedWarning : deprecatedWarnings) {
        assertThat(deprecatedWarning, matches(WARNING_HEADER_PATTERN.pattern()));
    }
    final List<String> actualWarningValues = deprecatedWarnings.stream().map(DeprecationLogger::extractWarningValueFromWarningHeader).collect(Collectors.toList());
    for (Matcher<String> headerMatcher : headerMatchers) {
        assertThat(actualWarningValues, hasItem(headerMatcher));
    }
}
Also used : Response(org.elasticsearch.client.Response) Matcher(org.hamcrest.Matcher) Setting(org.elasticsearch.common.settings.Setting) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 77 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project elasticsearch by elastic.

the class IndexingIT method buildShards.

private List<Shard> buildShards(Nodes nodes, RestClient client) throws IOException {
    Response response = client.performRequest("GET", "test/_stats", singletonMap("level", "shards"));
    List<Object> shardStats = ObjectPath.createFromResponse(response).evaluate("indices.test.shards.0");
    ArrayList<Shard> shards = new ArrayList<>();
    for (Object shard : shardStats) {
        final String nodeId = ObjectPath.evaluate(shard, "routing.node");
        final Boolean primary = ObjectPath.evaluate(shard, "routing.primary");
        final Node node = nodes.getSafe(nodeId);
        final SeqNoStats seqNoStats;
        if (node.getVersion().onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)) {
            Integer maxSeqNo = ObjectPath.evaluate(shard, "seq_no.max_seq_no");
            Integer localCheckpoint = ObjectPath.evaluate(shard, "seq_no.local_checkpoint");
            Integer globalCheckpoint = ObjectPath.evaluate(shard, "seq_no.global_checkpoint");
            seqNoStats = new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint);
        } else {
            seqNoStats = null;
        }
        shards.add(new Shard(node, primary, seqNoStats));
    }
    return shards;
}
Also used : Response(org.elasticsearch.client.Response) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) ArrayList(java.util.ArrayList)

Example 78 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project elasticsearch by elastic.

the class IndexingIT method assertVersion.

private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
    final Response response = client().performRequest("GET", index + "/test/" + docId, Collections.singletonMap("preference", preference));
    assertOK(response);
    final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
    assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion));
}
Also used : Response(org.elasticsearch.client.Response)

Example 79 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project elasticsearch by elastic.

the class ClientYamlTestClient method callApi.

/**
     * Calls an api with the provided parameters and body
     */
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, HttpEntity entity, Map<String, String> headers) throws IOException {
    if ("raw".equals(apiName)) {
        // Raw requests are bit simpler....
        Map<String, String> queryStringParams = new HashMap<>(params);
        String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request");
        String path = "/" + Objects.requireNonNull(queryStringParams.remove("path"), "Path must be set to use raw request");
        // And everything else is a url parameter!
        try {
            Response response = restClient.performRequest(method, path, queryStringParams, entity);
            return new ClientYamlTestResponse(response);
        } catch (ResponseException e) {
            throw new ClientYamlTestResponseException(e);
        }
    }
    ClientYamlSuiteRestApi restApi = restApi(apiName);
    //divide params between ones that go within query string and ones that go within path
    Map<String, String> pathParts = new HashMap<>();
    Map<String, String> queryStringParams = new HashMap<>();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (restApi.getPathParts().contains(entry.getKey())) {
            pathParts.put(entry.getKey(), entry.getValue());
        } else {
            if (restApi.getParams().contains(entry.getKey()) || restSpec.isGlobalParameter(entry.getKey()) || restSpec.isClientParameter(entry.getKey())) {
                queryStringParams.put(entry.getKey(), entry.getValue());
            } else {
                throw new IllegalArgumentException("param [" + entry.getKey() + "] not supported in [" + restApi.getName() + "] " + "api");
            }
        }
    }
    List<String> supportedMethods = restApi.getSupportedMethods(pathParts.keySet());
    String requestMethod;
    if (entity != null) {
        if (!restApi.isBodySupported()) {
            throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api");
        }
        String contentType = entity.getContentType().getValue();
        //randomly test the GET with source param instead of GET/POST with body
        if (sendBodyAsSourceParam(supportedMethods, contentType)) {
            logger.debug("sending the request body as source param with GET method");
            queryStringParams.put("source", EntityUtils.toString(entity));
            queryStringParams.put("source_content_type", contentType);
            requestMethod = HttpGet.METHOD_NAME;
            entity = null;
        } else {
            requestMethod = RandomizedTest.randomFrom(supportedMethods);
        }
    } else {
        if (restApi.isBodyRequired()) {
            throw new IllegalArgumentException("body is required by [" + restApi.getName() + "] api");
        }
        requestMethod = RandomizedTest.randomFrom(supportedMethods);
    }
    //the rest path to use is randomized out of the matching ones (if more than one)
    ClientYamlSuiteRestPath restPath = RandomizedTest.randomFrom(restApi.getFinalPaths(pathParts));
    //Encode rules for path and query string parameters are different. We use URI to encode the path.
    //We need to encode each path part separately, as each one might contain slashes that need to be escaped, which needs to
    //be done manually.
    String requestPath;
    if (restPath.getPathParts().length == 0) {
        requestPath = "/";
    } else {
        StringBuilder finalPath = new StringBuilder();
        for (String pathPart : restPath.getPathParts()) {
            try {
                finalPath.append('/');
                // We append "/" to the path part to handle parts that start with - or other invalid characters
                URI uri = new URI(null, null, null, -1, "/" + pathPart, null, null);
                //manually escape any slash that each part may contain
                finalPath.append(uri.getRawPath().substring(1).replaceAll("/", "%2F"));
            } catch (URISyntaxException e) {
                throw new RuntimeException("unable to build uri", e);
            }
        }
        requestPath = finalPath.toString();
    }
    Header[] requestHeaders = new Header[headers.size()];
    int index = 0;
    for (Map.Entry<String, String> header : headers.entrySet()) {
        logger.info("Adding header {}\n with value {}", header.getKey(), header.getValue());
        requestHeaders[index++] = new BasicHeader(header.getKey(), header.getValue());
    }
    logger.debug("calling api [{}]", apiName);
    try {
        Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, entity, requestHeaders);
        return new ClientYamlTestResponse(response);
    } catch (ResponseException e) {
        throw new ClientYamlTestResponseException(e);
    }
}
Also used : HashMap(java.util.HashMap) ResponseException(org.elasticsearch.client.ResponseException) ClientYamlSuiteRestPath(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Response(org.elasticsearch.client.Response) ClientYamlSuiteRestApi(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HashMap(java.util.HashMap) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader)

Example 80 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project elasticsearch by elastic.

the class ESClientYamlSuiteTestCase method readVersionsFromCatNodes.

private static Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws IOException {
    // we simply go to the _cat/nodes API and parse all versions in the cluster
    Response response = restClient.performRequest("GET", "/_cat/nodes", Collections.singletonMap("h", "version,master"));
    ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
    String nodesCatResponse = restTestResponse.getBodyAsString();
    String[] split = nodesCatResponse.split("\n");
    Version version = null;
    Version masterVersion = null;
    for (String perNode : split) {
        final String[] versionAndMaster = perNode.split("\\s+");
        assert versionAndMaster.length == 2 : "invalid line: " + perNode + " length: " + versionAndMaster.length;
        final Version currentVersion = Version.fromString(versionAndMaster[0]);
        final boolean master = versionAndMaster[1].trim().equals("*");
        if (master) {
            assert masterVersion == null;
            masterVersion = currentVersion;
        }
        if (version == null) {
            version = currentVersion;
        } else if (version.onOrAfter(currentVersion)) {
            version = currentVersion;
        }
    }
    return new Tuple<>(version, masterVersion);
}
Also used : Response(org.elasticsearch.client.Response) Version(org.elasticsearch.Version) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

Response (org.elasticsearch.client.Response)111 Request (org.elasticsearch.client.Request)38 IOException (java.io.IOException)33 HttpEntity (org.apache.http.HttpEntity)24 NStringEntity (org.apache.http.nio.entity.NStringEntity)21 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Map (java.util.Map)14 BasicHeader (org.apache.http.message.BasicHeader)14 ResponseException (org.elasticsearch.client.ResponseException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 RestClient (org.elasticsearch.client.RestClient)11 ArrayList (java.util.ArrayList)10 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)10 StringEntity (org.apache.http.entity.StringEntity)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 InputStream (java.io.InputStream)8 JSONObject (org.json.simple.JSONObject)8 MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7