Skip to content

Commit

Permalink
Add tests for GetSourceRequest, GetSourceResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
timoninmaxim committed Jan 20, 2020
1 parent 3c3ed1e commit 7e7341c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.GetSourceRequest;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.indices.AnalyzeRequest;
Expand Down Expand Up @@ -160,6 +161,10 @@ public void testSourceExistsWithType() throws IOException {
doTestSourceExists((index, id) -> new GetRequest(index, id));
}

public void testGetSource() throws IOException {
doTestGetSource((index, id) -> new GetSourceRequest(index, id));
}

private static void doTestSourceExists(BiFunction<String, String, GetRequest> requestFunction) throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
Expand Down Expand Up @@ -198,6 +203,44 @@ private static void doTestSourceExists(BiFunction<String, String, GetRequest> re
assertNull(request.getEntity());
}

private static void doTestGetSource(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
final GetSourceRequest getRequest = requestFunction.apply(index, id);

Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
String preference = randomAlphaOfLengthBetween(3, 10);
getRequest.preference(preference);
expectedParams.put("preference", preference);
}
if (randomBoolean()) {
String routing = randomAlphaOfLengthBetween(3, 10);
getRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
boolean realtime = randomBoolean();
getRequest.realtime(realtime);
if (realtime == false) {
expectedParams.put("realtime", "false");
}
}
if (randomBoolean()) {
boolean refresh = randomBoolean();
getRequest.refresh(refresh);
if (refresh) {
expectedParams.put("refresh", "true");
}
}
Request request = RequestConverters.getSource(getRequest);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals("/" + index + "/_source/" + id, request.getEndpoint());

assertEquals(expectedParams, request.getParameters());
assertNull(request.getEntity());
}

public void testMultiGet() throws IOException {
Map<String, String> expectedParams = new HashMap<>();
MultiGetRequest multiGetRequest = new MultiGetRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.core;

import org.elasticsearch.client.AbstractResponseTestCase;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;

public final class GetSourceResponseTests extends
AbstractResponseTestCase<GetSourceResponseTests.SourceOnlyResponse, GetSourceResponse> {

static class SourceOnlyResponse implements ToXContent {

private final BytesReference source;

SourceOnlyResponse(BytesReference source) {
this.source = source;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// this implementation copied from RestGetSourceAction.RestGetSourceResponseListener::buildResponse
try (InputStream stream = source.streamInput()) {
builder.rawValue(stream, XContentHelper.xContentType(source));
}
return builder;
}
}

@Override
protected SourceOnlyResponse createServerTestInstance(XContentType xContentType) {
BytesReference source = new BytesArray("{\"field\":\"value\"}");
return new SourceOnlyResponse(source);
}

@Override
protected GetSourceResponse doParseToClientInstance(XContentParser parser) throws IOException {
return GetSourceResponse.fromXContent(parser);
}

@Override
protected void assertInstances(SourceOnlyResponse serverTestInstance, GetSourceResponse clientInstance) {
Map<String, Object> expected = new HashMap<>();
expected.put("field", "value");

assertThat(clientInstance.getSource(), equalTo(serverTestInstance.source));
assertThat(clientInstance.getSource(), equalTo(expected));
}
}

0 comments on commit 7e7341c

Please sign in to comment.