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.
Further conversions to ChunkedXContentBuilder (elastic#114237)
- Loading branch information
Showing
11 changed files
with
203 additions
and
282 deletions.
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
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
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
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
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
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
88 changes: 88 additions & 0 deletions
88
server/src/test/java/org/elasticsearch/common/xcontent/ChunkedToXContentBuilderTests.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,88 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.common.xcontent; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.collect.Iterators; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xcontent.ToXContent; | ||
|
||
import java.util.function.IntFunction; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class ChunkedToXContentBuilderTests extends ESTestCase { | ||
|
||
public void testFieldWithInnerChunkedObject() { | ||
|
||
ToXContent innerXContent = (b, p) -> { | ||
b.startObject(); | ||
b.field("field1", 10); | ||
b.field("field2", "aaa"); | ||
b.endObject(); | ||
return b; | ||
}; | ||
|
||
ToXContent outerXContent = (b, p) -> b.field("field3", 10).field("field4", innerXContent); | ||
|
||
String expectedContent = Strings.toString(outerXContent); | ||
|
||
ChunkedToXContentObject innerChunkedContent = params -> new ChunkedToXContentBuilder(params).object( | ||
o -> o.field("field1", 10).field("field2", "aaa") | ||
); | ||
|
||
ChunkedToXContent outerChunkedContent = params -> new ChunkedToXContentBuilder(params).field("field3", 10) | ||
.field("field4", innerChunkedContent); | ||
|
||
assertThat(Strings.toString(outerChunkedContent), equalTo(expectedContent)); | ||
} | ||
|
||
public void testFieldWithInnerChunkedArray() { | ||
|
||
ToXContent innerXContent = (b, p) -> { | ||
b.startArray(); | ||
b.value(10); | ||
b.value(20); | ||
b.endArray(); | ||
return b; | ||
}; | ||
|
||
ToXContent outerXContent = (b, p) -> b.field("field3", 10).field("field4", innerXContent); | ||
|
||
String expectedContent = Strings.toString(outerXContent); | ||
|
||
IntFunction<ToXContent> value = v -> (b, p) -> b.value(v); | ||
|
||
ChunkedToXContentObject innerChunkedContent = params -> new ChunkedToXContentBuilder(params).array( | ||
IntStream.of(10, 20).mapToObj(value).iterator() | ||
); | ||
|
||
ChunkedToXContent outerChunkedContent = params -> new ChunkedToXContentBuilder(params).field("field3", 10) | ||
.field("field4", innerChunkedContent); | ||
|
||
assertThat(Strings.toString(outerChunkedContent), equalTo(expectedContent)); | ||
} | ||
|
||
public void testFieldWithInnerChunkedField() { | ||
|
||
ToXContent innerXContent = (b, p) -> b.value(10); | ||
ToXContent outerXContent = (b, p) -> b.field("field3", 10).field("field4", innerXContent); | ||
|
||
String expectedContent = Strings.toString(outerXContent); | ||
|
||
ChunkedToXContentObject innerChunkedContent = params -> Iterators.single((b, p) -> b.value(10)); | ||
|
||
ChunkedToXContent outerChunkedContent = params -> new ChunkedToXContentBuilder(params).field("field3", 10) | ||
.field("field4", innerChunkedContent); | ||
|
||
assertThat(Strings.toString(outerChunkedContent), equalTo(expectedContent)); | ||
} | ||
} |
Oops, something went wrong.