forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for some failure conditions
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
server/src/test/java/org/elasticsearch/common/xcontent/ChunkedToXContentBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |