2020package org .elasticsearch .action .search ;
2121
2222import org .elasticsearch .action .search .SearchResponse .InternalSearchResponse ;
23- import org .elasticsearch .common .bytes . BytesReference ;
23+ import org .elasticsearch .common .Strings ;
2424import org .elasticsearch .common .text .Text ;
2525import org .elasticsearch .common .xcontent .ToXContent ;
2626import org .elasticsearch .common .xcontent .XContentBuilder ;
4444
4545public class SearchResponseTests extends ESTestCase {
4646
47- public static SearchResponse createTestItem () {
47+ public static SearchResponse createTestItem (boolean withFailures ) {
4848 InternalSearchHits hits = InternalSearchHitsTests .createTestItem ();
4949 boolean timedOut = randomBoolean ();
5050 Boolean terminatedEarly = randomBoolean () ? null : randomBoolean ();
51- int tookInMillis = randomIntBetween ( 0 , 1000 );
52- int successfulShards = tookInMillis ;
51+ long tookInMillis = randomNonNegativeLong ( );
52+ int successfulShards = randomInt () ;
5353 int totalShards = randomInt ();
54- int numFailures = randomIntBetween (0 , 3 ) ;
54+ int numFailures = withFailures ? randomIntBetween (1 , 5 ) : 0 ;
5555 ShardSearchFailure [] failures = new ShardSearchFailure [numFailures ];
5656 for (int i = 0 ; i < numFailures ; i ++) {
5757 failures [i ] = ShardSearchFailureTests .createTestItem ();
@@ -65,7 +65,8 @@ public static SearchResponse createTestItem() {
6565 }
6666
6767 public void testFromXContent () throws IOException {
68- SearchResponse response = createTestItem ();
68+ // the "_shard/total/failures" section makes if impossible to directly compare xContent, so we omit it here
69+ SearchResponse response = createTestItem (false );
6970 XContentType xcontentType = randomFrom (XContentType .values ());
7071 XContentBuilder builder = XContentFactory .contentBuilder (xcontentType );
7172 builder = response .toXContent (builder , ToXContent .EMPTY_PARAMS );
@@ -74,33 +75,39 @@ public void testFromXContent() throws IOException {
7475 XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
7576 SearchResponse parsed = SearchResponse .fromXContent (parser );
7677
77- // the "_shard/total/failures" section makes if impossible to directly compare xContent, because
78- // the failures in the parsed SearchResponse are wrapped in an extra ElasticSearchException on the client side.
79- // Because of this we compare the "top level" fields for equality and the subsections xContent equivalence independently
80- assertEquals (response .getScrollId (), parsed .getScrollId ());
81- assertEquals (response .getTookInMillis (), parsed .getTookInMillis ());
82- assertEquals (response .getTook (), parsed .getTook ());
83- assertEquals (response .isTimedOut (), parsed .isTimedOut ());
84- assertEquals (response .isTerminatedEarly (), parsed .isTerminatedEarly ());
85- assertEquals (response .getFailedShards (), parsed .getFailedShards ());
86- assertEquals (response .getSuccessfulShards (), parsed .getSuccessfulShards ());
87- assertEquals (response .getTotalShards (), parsed .getTotalShards ());
78+ assertToXContentEquivalent (builder .bytes (), toXContent (parsed , xcontentType ), xcontentType );
79+ assertEquals (XContentParser .Token .END_OBJECT , parser .currentToken ());
80+ assertNull (parser .nextToken ());
81+ }
82+
83+ /**
84+ * The "_shard/total/failures" section makes if impossible to directly compare xContent, because
85+ * the failures in the parsed SearchResponse are wrapped in an extra ElasticSearchException on the client side.
86+ * Because of this, in this special test case we compare the "top level" fields for equality
87+ * and the subsections xContent equivalence independently
88+ */
89+ public void testFromXContentWithFailures () throws IOException {
90+ SearchResponse response = createTestItem (true );
91+ XContentType xcontentType = randomFrom (XContentType .values ());
92+ XContentBuilder builder = XContentFactory .contentBuilder (xcontentType );
93+ builder = response .toXContent (builder , ToXContent .EMPTY_PARAMS );
8894
89- // compare the "hits" section by comparing xContent equivalence
90- assertToXContentEquivalent (toXContent (response .getHits (), xcontentType ), toXContent (parsed .getHits (), xcontentType ),
91- xcontentType );
95+ XContentParser parser = createParser (builder );
96+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser ::getTokenLocation );
97+ SearchResponse parsed = SearchResponse .fromXContent (parser );
98+ // check that we at least get the same number of shardFailures
99+ assertEquals (response .getShardFailures ().length , parsed .getShardFailures ().length );
92100 assertEquals (XContentParser .Token .END_OBJECT , parser .currentToken ());
93101 assertNull (parser .nextToken ());
94102 }
95103
96- public void testToXContent () throws IOException {
104+ public void testToXContent () {
97105 InternalSearchHit hit = new InternalSearchHit (1 , "id1" , new Text ("type" ), Collections .emptyMap ());
98106 hit .score (2.0f );
99107 InternalSearchHit [] hits = new InternalSearchHit [] { hit };
100108 SearchResponse response = new SearchResponse (
101109 new InternalSearchResponse (new InternalSearchHits (hits , 100 , 1.5f ), null , null , null , false , null ), null , 0 , 0 , 0 ,
102110 new ShardSearchFailure [0 ]);
103- BytesReference xContent = toXContent (response , XContentType .JSON );
104111 assertEquals (
105112 "{\" took\" :0,"
106113 + "\" timed_out\" :false,"
@@ -114,7 +121,7 @@ public void testToXContent() throws IOException {
114121 + "\" max_score\" :1.5,"
115122 + "\" hits\" :[{\" _type\" :\" type\" ,\" _id\" :\" id1\" ,\" _score\" :2.0}]"
116123 + "}"
117- + "}" , xContent . utf8ToString ( ));
124+ + "}" , Strings . toString ( response ));
118125 }
119126
120127}
0 commit comments