use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class SnapshotRequestsTests method testRestoreSnapshotRequestParsing.
public void testRestoreSnapshotRequestParsing() throws IOException {
RestoreSnapshotRequest request = new RestoreSnapshotRequest("test-repo", "test-snap");
XContentBuilder builder = jsonBuilder().startObject();
if (randomBoolean()) {
builder.field("indices", "foo,bar,baz");
} else {
builder.startArray("indices");
builder.value("foo");
builder.value("bar");
builder.value("baz");
builder.endArray();
}
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
if (indicesOptions.expandWildcardsClosed()) {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "all");
} else {
builder.field("expand_wildcards", "closed");
}
} else {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "open");
} else {
builder.field("expand_wildcards", "none");
}
}
builder.field("allow_no_indices", indicesOptions.allowNoIndices());
builder.field("rename_pattern", "rename-from");
builder.field("rename_replacement", "rename-to");
boolean partial = randomBoolean();
builder.field("partial", partial);
builder.startObject("settings").field("set1", "val1").endObject();
builder.startObject("index_settings").field("set1", "val2").endObject();
if (randomBoolean()) {
builder.field("ignore_index_settings", "set2,set3");
} else {
builder.startArray("ignore_index_settings");
builder.value("set2");
builder.value("set3");
builder.endArray();
}
BytesReference bytes = builder.endObject().bytes();
request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
assertEquals("test-repo", request.repository());
assertEquals("test-snap", request.snapshot());
assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
assertEquals("rename-from", request.renamePattern());
assertEquals("rename-to", request.renameReplacement());
assertEquals(partial, request.partial());
assertEquals("val1", request.settings().get("set1"));
assertArrayEquals(request.ignoreIndexSettings(), new String[] { "set2", "set3" });
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class SnapshotRequestsTests method testCreateSnapshotRequestParsing.
public void testCreateSnapshotRequestParsing() throws IOException {
CreateSnapshotRequest request = new CreateSnapshotRequest("test-repo", "test-snap");
XContentBuilder builder = jsonBuilder().startObject();
if (randomBoolean()) {
builder.field("indices", "foo,bar,baz");
} else {
builder.startArray("indices");
builder.value("foo");
builder.value("bar");
builder.value("baz");
builder.endArray();
}
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
if (indicesOptions.expandWildcardsClosed()) {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "all");
} else {
builder.field("expand_wildcards", "closed");
}
} else {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "open");
} else {
builder.field("expand_wildcards", "none");
}
}
builder.field("allow_no_indices", indicesOptions.allowNoIndices());
boolean partial = randomBoolean();
builder.field("partial", partial);
builder.startObject("settings").field("set1", "val1").endObject();
builder.startObject("index_settings").field("set1", "val2").endObject();
if (randomBoolean()) {
builder.field("ignore_index_settings", "set2,set3");
} else {
builder.startArray("ignore_index_settings");
builder.value("set2");
builder.value("set3");
builder.endArray();
}
BytesReference bytes = builder.endObject().bytes();
request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
assertEquals("test-repo", request.repository());
assertEquals("test-snap", request.snapshot());
assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
assertEquals(partial, request.partial());
assertEquals("val1", request.settings().get("set1"));
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class TaskIdTests method roundTrip.
private TaskId roundTrip(TaskId taskId, int expectedSize) throws IOException {
try (BytesStreamOutput out = new BytesStreamOutput()) {
taskId.writeTo(out);
BytesReference bytes = out.bytes();
assertEquals(expectedSize, bytes.length());
try (StreamInput in = bytes.streamInput()) {
return TaskId.readFromStream(in);
}
}
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class TCPTransportTests method testCompressRequest.
public void testCompressRequest() throws IOException {
final boolean compressed = randomBoolean();
final AtomicBoolean called = new AtomicBoolean(false);
Req request = new Req(randomRealisticUnicodeOfLengthBetween(10, 100));
ThreadPool threadPool = new TestThreadPool(TCPTransportTests.class.getName());
try {
TcpTransport transport = new TcpTransport("test", Settings.builder().put("transport.tcp.compress", compressed).build(), threadPool, new BigArrays(Settings.EMPTY, null), null, null, null) {
@Override
protected InetSocketAddress getLocalAddress(Object o) {
return null;
}
@Override
protected Object bind(String name, InetSocketAddress address) throws IOException {
return null;
}
@Override
protected void closeChannels(List channel) throws IOException {
}
@Override
protected void sendMessage(Object o, BytesReference reference, Runnable sendListener) throws IOException {
StreamInput streamIn = reference.streamInput();
streamIn.skip(TcpHeader.MARKER_BYTES_SIZE);
int len = streamIn.readInt();
long requestId = streamIn.readLong();
assertEquals(42, requestId);
byte status = streamIn.readByte();
Version version = Version.fromId(streamIn.readInt());
assertEquals(Version.CURRENT, version);
assertEquals(compressed, TransportStatus.isCompress(status));
called.compareAndSet(false, true);
if (compressed) {
final int bytesConsumed = TcpHeader.HEADER_SIZE;
streamIn = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed)).streamInput(streamIn);
}
threadPool.getThreadContext().readHeaders(streamIn);
assertEquals("foobar", streamIn.readString());
Req readReq = new Req("");
readReq.readFrom(streamIn);
assertEquals(request.value, readReq.value);
}
@Override
protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
return new NodeChannels(node, new Object[profile.getNumConnections()], profile);
}
@Override
protected boolean isOpen(Object o) {
return false;
}
@Override
public long serverOpen() {
return 0;
}
@Override
public NodeChannels getConnection(DiscoveryNode node) {
return new NodeChannels(node, new Object[MockTcpTransport.LIGHT_PROFILE.getNumConnections()], MockTcpTransport.LIGHT_PROFILE);
}
};
DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT);
Transport.Connection connection = transport.getConnection(node);
connection.sendRequest(42, "foobar", request, TransportRequestOptions.EMPTY);
assertTrue(called.get());
} finally {
ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
}
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class MustacheTests method testSizeAccessForCollectionsAndArrays.
public void testSizeAccessForCollectionsAndArrays() throws Exception {
String[] randomArrayValues = generateRandomStringArray(10, 20, false);
List<String> randomList = Arrays.asList(generateRandomStringArray(10, 20, false));
String template = "{{data.array.size}} {{data.list.size}}";
CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
Map<String, Object> data = new HashMap<>();
data.put("array", randomArrayValues);
data.put("list", randomList);
Map<String, Object> vars = new HashMap<>();
vars.put("data", data);
Object output = engine.executable(mustache, vars).run();
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
String expectedString = String.format(Locale.ROOT, "%s %s", randomArrayValues.length, randomList.size());
assertThat(bytes.utf8ToString(), equalTo(expectedString));
}
Aggregations