use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class GrokProcessorGetActionTests method testRequest.
public void testRequest() throws Exception {
GrokProcessorGetAction.Request request = new GrokProcessorGetAction.Request(false);
BytesStreamOutput out = new BytesStreamOutput();
request.writeTo(out);
StreamInput streamInput = out.bytes().streamInput();
GrokProcessorGetAction.Request otherRequest = new GrokProcessorGetAction.Request(streamInput);
assertThat(otherRequest.validate(), nullValue());
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class DebugTests method testPainlessExplainErrorSerialization.
/**
* {@link PainlessExplainError} doesn't serialize but the headers still make it.
*/
public void testPainlessExplainErrorSerialization() throws IOException {
Map<String, Object> params = singletonMap("a", "jumped over the moon");
ScriptException e = expectThrows(ScriptException.class, () -> exec("Debug.explain(params.a)", params, true));
assertEquals(singletonList("jumped over the moon"), e.getMetadata("opensearch.to_string"));
assertEquals(singletonList("java.lang.String"), e.getMetadata("opensearch.java_class"));
assertEquals(singletonList("java.lang.String"), e.getMetadata("opensearch.painless_class"));
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeException(e);
try (StreamInput in = out.bytes().streamInput()) {
OpenSearchException read = (ScriptException) in.readException();
assertEquals(singletonList("jumped over the moon"), read.getMetadata("opensearch.to_string"));
assertEquals(singletonList("java.lang.String"), read.getMetadata("opensearch.java_class"));
assertEquals(singletonList("java.lang.String"), read.getMetadata("opensearch.painless_class"));
}
}
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testImplicitlySetDefaultScriptLang.
public void testImplicitlySetDefaultScriptLang() throws Exception {
addQueryFieldMappings();
XContentBuilder query = jsonBuilder();
query.startObject();
query.startObject("script");
if (randomBoolean()) {
query.field("script", "return true");
} else {
query.startObject("script");
query.field("source", "return true");
query.endObject();
}
query.endObject();
query.endObject();
ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
// Query builder's content is stored via BinaryFieldMapper, which has a custom encoding
// to encode multiple binary values into a single binary doc values field.
// This is the reason we need to first need to read the number of values and
// then the length of the field value in bytes.
input.readVInt();
input.readVInt();
ScriptQueryBuilder queryBuilder = (ScriptQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
assertEquals(Script.DEFAULT_SCRIPT_LANG, queryBuilder.script().getLang());
}
}
query = jsonBuilder();
query.startObject();
query.startObject("function_score");
query.startArray("functions");
query.startObject();
query.startObject("script_score");
if (randomBoolean()) {
query.field("script", "return true");
} else {
query.startObject("script");
query.field("source", "return true");
query.endObject();
}
query.endObject();
query.endObject();
query.endArray();
query.endObject();
query.endObject();
doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
input.readVInt();
input.readVInt();
FunctionScoreQueryBuilder queryBuilder = (FunctionScoreQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) queryBuilder.filterFunctionBuilders()[0].getScoreFunction();
assertEquals(Script.DEFAULT_SCRIPT_LANG, function.getScript().getLang());
}
}
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class PainlessExecuteRequestTests method testFromXContent.
// Testing XContent serialization manually here, because the xContentType field in ContextSetup determines
// how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType
// is not known and accessable when the test request instance is created in the xcontent serialization framework.
// Changing that is a big change. Writing a custom xcontent test here is the best option for now, because as far
// as I know this request class is the only case where this is a problem.
public final void testFromXContent() throws Exception {
for (int i = 0; i < 20; i++) {
PainlessExecuteAction.Request testInstance = createTestInstance();
ContextSetup contextSetup = testInstance.getContextSetup();
XContent xContent = randomFrom(XContentType.values()).xContent();
if (contextSetup != null && contextSetup.getXContentType() != null) {
xContent = contextSetup.getXContentType().xContent();
}
try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
builder.value(testInstance);
StreamInput instanceInput = BytesReference.bytes(builder).streamInput();
try (XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, instanceInput)) {
PainlessExecuteAction.Request result = PainlessExecuteAction.Request.parse(parser);
assertThat(result, equalTo(testInstance));
}
}
}
}
use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.
the class RoundTripTests method toInputByteStream.
private StreamInput toInputByteStream(Version version, Writeable example) throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(version);
example.writeTo(out);
StreamInput in = out.bytes().streamInput();
in.setVersion(version);
return in;
}
Aggregations