Skip to content

Commit

Permalink
Add tests for some failure conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoop committed Sep 9, 2024
1 parent a5a6a84 commit 7544b27
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public <T> ChunkedToXContentBuilder appendXContentFields(Map<String, ? extends T
return forEach(map.entrySet().iterator(), (e, b) -> b.field(e.getKey()).appendXContent(e.getValue()));
}

private ChunkedToXContentBuilder appendXContent(ToXContent xContent) {
ChunkedToXContentBuilder appendXContent(ToXContent xContent) {
if (xContent != ToXContent.EMPTY) {
addChunk(xContent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentType;

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

import static org.hamcrest.Matchers.sameInstance;

public class ChunkedToXContentBuilderTest extends ESTestCase {

private static ChunkedToXContentBuilder builder() {
return new ChunkedToXContentBuilder(ToXContent.EMPTY_PARAMS);
}

public void testCannotAddAfterRead() {
var builder = builder();
builder.hasNext();
expectThrows(AssertionError.class, builder::startObject);
}

public void testParamsPassedToNestedXContent() throws IOException {
ToXContent.Params params = new ToXContent.MapParams(Map.of());
var builder = new ChunkedToXContentBuilder(params);
builder.appendXContent((b, p) -> {assertThat(p, sameInstance(params)); return b;});
read(params, builder);
}

public void testOutOfOrderElementsThrows() {
expectThrows(IllegalStateException.class, () -> builder().endObject());
expectThrows(IllegalStateException.class, () -> builder().endArray());
expectThrows(IllegalStateException.class, () -> builder().startObject().endArray());
expectThrows(IllegalStateException.class, () -> builder().startArray().endObject());

expectThrows(IllegalStateException.class, builder().startObject()::hasNext);
expectThrows(IllegalStateException.class, builder().startArray()::hasNext);
expectThrows(IllegalStateException.class, builder().startObject().startObject().endObject()::hasNext);
expectThrows(IllegalStateException.class, builder().startArray().endArray().startArray()::hasNext);
}

private static void read(ChunkedToXContentBuilder builder) throws IOException {
XContentHelper.toXContent(p -> builder, XContentType.JSON, false);
}

private static void read(ToXContent.Params params, ChunkedToXContentBuilder builder) throws IOException {
XContentHelper.toXContent(p -> builder, XContentType.JSON, params, false);
}
}

0 comments on commit 7544b27

Please sign in to comment.