Search in sources :

Example 1 with MainResponse

use of org.opensearch.action.main.MainResponse in project OpenSearch by opensearch-project.

the class CustomRestHighLevelClientTests method testCustomEndpointAsync.

public void testCustomEndpointAsync() throws Exception {
    final MainRequest request = new MainRequest();
    String nodeName = randomAlphaOfLengthBetween(1, 10);
    PlainActionFuture<MainResponse> future = PlainActionFuture.newFuture();
    restHighLevelClient.customAsync(request, optionsForNodeName(nodeName), future);
    assertEquals(nodeName, future.get().getNodeName());
    future = PlainActionFuture.newFuture();
    restHighLevelClient.customAndParseAsync(request, optionsForNodeName(nodeName), future);
    assertEquals(nodeName, future.get().getNodeName());
}
Also used : MainRequest(org.opensearch.action.main.MainRequest) MainResponse(org.opensearch.action.main.MainResponse)

Example 2 with MainResponse

use of org.opensearch.action.main.MainResponse in project OpenSearch by opensearch-project.

the class RestMainActionTests method testGetResponse.

public void testGetResponse() throws Exception {
    final String nodeName = "node1";
    final ClusterName clusterName = new ClusterName("cluster1");
    final String clusterUUID = randomAlphaOfLengthBetween(10, 20);
    final Version version = Version.CURRENT;
    final Build build = Build.CURRENT;
    final boolean prettyPrint = randomBoolean();
    final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build);
    XContentBuilder builder = JsonXContent.contentBuilder();
    Map<String, String> params = new HashMap<>();
    if (prettyPrint == false) {
        params.put("pretty", String.valueOf(prettyPrint));
    }
    RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
    assertNotNull(response);
    assertThat(response.status(), equalTo(RestStatus.OK));
    assertThat(response.content().length(), greaterThan(0));
    XContentBuilder responseBuilder = JsonXContent.contentBuilder();
    if (prettyPrint) {
        // do this to mimic what the rest layer does
        responseBuilder.prettyPrint().lfAtEnd();
    }
    mainResponse.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
    BytesReference xcontentBytes = BytesReference.bytes(responseBuilder);
    assertEquals(xcontentBytes, response.content());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) Version(org.opensearch.Version) MainResponse(org.opensearch.action.main.MainResponse) Build(org.opensearch.Build) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ClusterName(org.opensearch.cluster.ClusterName) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with MainResponse

use of org.opensearch.action.main.MainResponse in project OpenSearch by opensearch-project.

the class CustomRestHighLevelClientTests method mockPerformRequest.

/**
 * Mocks the synchronous request execution like if it was executed by OpenSearch.
 */
private Response mockPerformRequest(Request request) throws IOException {
    assertThat(request.getOptions().getHeaders(), hasSize(1));
    Header httpHeader = request.getOptions().getHeaders().get(0);
    final Response mockResponse = mock(Response.class);
    when(mockResponse.getHost()).thenReturn(new HttpHost("localhost", 9200));
    ProtocolVersion protocol = new ProtocolVersion("HTTP", 1, 1);
    when(mockResponse.getStatusLine()).thenReturn(new BasicStatusLine(protocol, 200, "OK"));
    MainResponse response = new MainResponse(httpHeader.getValue(), Version.CURRENT, ClusterName.DEFAULT, "_na", Build.CURRENT);
    BytesRef bytesRef = XContentHelper.toXContent(response, XContentType.JSON, false).toBytesRef();
    when(mockResponse.getEntity()).thenReturn(new NByteArrayEntity(bytesRef.bytes, ContentType.APPLICATION_JSON));
    RequestLine requestLine = new BasicRequestLine(HttpGet.METHOD_NAME, ENDPOINT, protocol);
    when(mockResponse.getRequestLine()).thenReturn(requestLine);
    return mockResponse;
}
Also used : MainResponse(org.opensearch.action.main.MainResponse) RequestLine(org.apache.http.RequestLine) BasicRequestLine(org.apache.http.message.BasicRequestLine) Header(org.apache.http.Header) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) MainResponse(org.opensearch.action.main.MainResponse) HttpHost(org.apache.http.HttpHost) BasicRequestLine(org.apache.http.message.BasicRequestLine) ProtocolVersion(org.apache.http.ProtocolVersion) BytesRef(org.apache.lucene.util.BytesRef) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 4 with MainResponse

use of org.opensearch.action.main.MainResponse in project OpenSearch by opensearch-project.

the class CustomRestHighLevelClientTests method testCustomEndpoint.

public void testCustomEndpoint() throws IOException {
    final MainRequest request = new MainRequest();
    String nodeName = randomAlphaOfLengthBetween(1, 10);
    MainResponse response = restHighLevelClient.custom(request, optionsForNodeName(nodeName));
    assertEquals(nodeName, response.getNodeName());
    response = restHighLevelClient.customAndParse(request, optionsForNodeName(nodeName));
    assertEquals(nodeName, response.getNodeName());
}
Also used : MainRequest(org.opensearch.action.main.MainRequest) MainResponse(org.opensearch.action.main.MainResponse)

Example 5 with MainResponse

use of org.opensearch.action.main.MainResponse in project OpenSearch by opensearch-project.

the class RestMainActionTests method testHeadResponse.

public void testHeadResponse() throws Exception {
    final String nodeName = "node1";
    final ClusterName clusterName = new ClusterName("cluster1");
    final String clusterUUID = randomAlphaOfLengthBetween(10, 20);
    final Version version = Version.CURRENT;
    final Build build = Build.CURRENT;
    final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build);
    XContentBuilder builder = JsonXContent.contentBuilder();
    RestRequest restRequest = new FakeRestRequest() {

        @Override
        public Method method() {
            return Method.HEAD;
        }
    };
    BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
    assertNotNull(response);
    assertThat(response.status(), equalTo(RestStatus.OK));
// the empty responses are handled in the HTTP layer so we do
// not assert on them here
}
Also used : RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) Version(org.opensearch.Version) MainResponse(org.opensearch.action.main.MainResponse) Build(org.opensearch.Build) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ClusterName(org.opensearch.cluster.ClusterName) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

MainResponse (org.opensearch.action.main.MainResponse)5 Build (org.opensearch.Build)2 Version (org.opensearch.Version)2 MainRequest (org.opensearch.action.main.MainRequest)2 ClusterName (org.opensearch.cluster.ClusterName)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 BytesRestResponse (org.opensearch.rest.BytesRestResponse)2 RestRequest (org.opensearch.rest.RestRequest)2 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)2 HashMap (java.util.HashMap)1 Header (org.apache.http.Header)1 HttpHost (org.apache.http.HttpHost)1 ProtocolVersion (org.apache.http.ProtocolVersion)1 RequestLine (org.apache.http.RequestLine)1 BasicRequestLine (org.apache.http.message.BasicRequestLine)1 BasicStatusLine (org.apache.http.message.BasicStatusLine)1 NByteArrayEntity (org.apache.http.nio.entity.NByteArrayEntity)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesReference (org.opensearch.common.bytes.BytesReference)1