Search in sources :

Example 61 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class IndexingIT method buildNodeAndVersions.

private Nodes buildNodeAndVersions() throws IOException {
    Response response = client().performRequest(new Request("GET", "_nodes"));
    ObjectPath objectPath = ObjectPath.createFromResponse(response);
    Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
    Nodes nodes = new Nodes();
    for (String id : nodesAsMap.keySet()) {
        nodes.add(new Node(id, objectPath.evaluate("nodes." + id + ".name"), Version.fromString(objectPath.evaluate("nodes." + id + ".version")), HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address"))));
    }
    response = client().performRequest(new Request("GET", "_cluster/state"));
    nodes.setMasterNodeId(ObjectPath.createFromResponse(response).evaluate("master_node"));
    return nodes;
}
Also used : Response(org.opensearch.client.Response) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) Request(org.opensearch.client.Request)

Example 62 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class ExceptionIT method testOpensearchException.

public void testOpensearchException() throws Exception {
    logClusterNodes();
    Request request = new Request("GET", "/no_such_index");
    for (Node node : client().getNodes()) {
        try {
            client().setNodes(Collections.singletonList(node));
            logger.info("node: {}", node.getHost());
            client().performRequest(request);
            fail();
        } catch (ResponseException e) {
            logger.debug(e.getMessage());
            Response response = e.getResponse();
            assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode());
            assertEquals("no_such_index", ObjectPath.createFromResponse(response).evaluate("error.index"));
        }
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) Node(org.opensearch.client.Node) Request(org.opensearch.client.Request)

Example 63 with Response

use of org.opensearch.client.Response in project ml-commons by opensearch-project.

the class MLCommonsRestTestCase method ingestModelData.

protected Response ingestModelData() throws IOException {
    Response trainModelResponse = TestHelper.makeRequest(client(), "POST", "_plugins/_ml/_train/sample_algo", null, TestHelper.toHttpEntity(trainModelDataJson()), null);
    HttpEntity entity = trainModelResponse.getEntity();
    assertNotNull(trainModelResponse);
    return trainModelResponse;
}
Also used : Response(org.opensearch.client.Response) HttpEntity(org.apache.http.HttpEntity)

Example 64 with Response

use of org.opensearch.client.Response in project ml-commons by opensearch-project.

the class MLCommonsRestTestCase method indexExistsWithAdminClient.

// Utility fn for checking if an index exists. Should only be used when not allowed in a regular context
// (e.g., checking existence of system indices)
protected static boolean indexExistsWithAdminClient(String indexName) throws IOException {
    Request request = new Request("HEAD", "/" + indexName);
    Response response = adminClient().performRequest(request);
    return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request)

Example 65 with Response

use of org.opensearch.client.Response in project ml-commons by opensearch-project.

the class MLCommonsRestTestCase method validateStats.

protected void validateStats(FunctionName functionName, ActionName actionName, int expectedMinimumTotalFailureCount, int expectedMinimumTotalAlgoFailureCount, int expectedMinimumTotalRequestCount, int expectedMinimumTotalAlgoRequestCount) throws IOException {
    Response statsResponse = TestHelper.makeRequest(client(), "GET", "_plugins/_ml/stats", null, "", null);
    HttpEntity entity = statsResponse.getEntity();
    assertNotNull(statsResponse);
    String entityString = TestHelper.httpEntityToString(entity);
    Map<String, Object> map = gson.fromJson(entityString, Map.class);
    int totalFailureCount = 0;
    int totalAlgoFailureCount = 0;
    int totalRequestCount = 0;
    int totalAlgoRequestCount = 0;
    for (String key : map.keySet()) {
        Map<String, Object> nodeStatsMap = (Map<String, Object>) map.get(key);
        if (nodeStatsMap.containsKey(ML_TOTAL_FAILURE_COUNT)) {
            totalFailureCount += (Double) nodeStatsMap.get(ML_TOTAL_FAILURE_COUNT);
        }
        String failureCountStat = StatNames.failureCountStat(functionName, actionName);
        if (nodeStatsMap.containsKey(failureCountStat)) {
            totalAlgoFailureCount += (Double) nodeStatsMap.get(failureCountStat);
        }
        if (nodeStatsMap.containsKey(ML_TOTAL_REQUEST_COUNT)) {
            totalRequestCount += (Double) nodeStatsMap.get(ML_TOTAL_REQUEST_COUNT);
        }
        String requestCountStat = StatNames.requestCountStat(functionName, actionName);
        if (nodeStatsMap.containsKey(requestCountStat)) {
            totalAlgoRequestCount += (Double) nodeStatsMap.get(requestCountStat);
        }
    }
    assertTrue(totalFailureCount >= expectedMinimumTotalFailureCount);
    assertTrue(totalAlgoFailureCount >= expectedMinimumTotalAlgoFailureCount);
    assertTrue(totalRequestCount >= expectedMinimumTotalRequestCount);
    assertTrue(totalAlgoRequestCount >= expectedMinimumTotalAlgoRequestCount);
}
Also used : Response(org.opensearch.client.Response) HttpEntity(org.apache.http.HttpEntity) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7