Skip to content

Commit 9323e67

Browse files
committed
Cleaning up some tests by using assertHitCount assertion
1 parent 21945e5 commit 9323e67

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.lucene.search.BooleanQuery;
2727
import org.apache.lucene.search.Query;
28+
import org.elasticsearch.action.count.CountResponse;
2829
import org.elasticsearch.action.search.SearchResponse;
2930
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
3031
import org.elasticsearch.search.SearchHit;
@@ -43,6 +44,10 @@ public static void assertHitCount(SearchResponse searchResponse, long expectedHi
4344
assertThat(searchResponse.getHits().totalHits(), is(expectedHitCount));
4445
}
4546

47+
public static void assertHitCount(CountResponse countResponse, long expectedHitCount) {
48+
assertThat(countResponse.getCount(), is(expectedHitCount));
49+
}
50+
4651
public static void assertFirstHit(SearchResponse searchResponse, Matcher<SearchHit> matcher) {
4752
assertSearchHit(searchResponse, 1, matcher);
4853
}

src/test/java/org/elasticsearch/test/integration/gateway/local/SimpleRecoveryLocalGatewayTests.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.test.integration.gateway.local;
2121

22-
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
23-
2422
import org.elasticsearch.action.count.CountResponse;
2523

2624
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
@@ -98,7 +96,7 @@ public void testX() throws Exception {
9896
.startArray("appAccountIds").value(14).endArray().endObject()).execute().actionGet();
9997

10098
node1.client().admin().indices().prepareRefresh().execute().actionGet();
101-
assertThat(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet().getCount(), equalTo(2l));
99+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet(), 2);
102100

103101
closeNode("node1");
104102
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@@ -112,7 +110,7 @@ public void testX() throws Exception {
112110
node1.client().admin().indices().prepareRefresh().execute().actionGet();
113111
CountResponse count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
114112
assertNoFailures(count);
115-
assertThat(count.getCount(), equalTo(2l));
113+
assertHitCount(count, 2);
116114

117115
closeNode("node1");
118116
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@@ -126,7 +124,7 @@ public void testX() throws Exception {
126124
node1.client().admin().indices().prepareRefresh().execute().actionGet();
127125
count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
128126
assertNoFailures(count);
129-
assertThat(count.getCount(), equalTo(2l));
127+
assertHitCount(count, 2);
130128
}
131129

132130
@Test
@@ -148,10 +146,10 @@ public void testSingleNodeNoFlush() throws Exception {
148146

149147
node1.client().admin().indices().prepareRefresh().execute().actionGet();
150148
for (int i = 0; i < 10; i++) {
151-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
152-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
153-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
154-
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
149+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
150+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
151+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
152+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
155153
}
156154

157155
closeNode("node1");
@@ -164,10 +162,10 @@ public void testSingleNodeNoFlush() throws Exception {
164162
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
165163

166164
for (int i = 0; i < 10; i++) {
167-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
168-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
169-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
170-
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
165+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
166+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
167+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
168+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
171169
}
172170

173171
closeNode("node1");
@@ -180,10 +178,10 @@ public void testSingleNodeNoFlush() throws Exception {
180178
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
181179

182180
for (int i = 0; i < 10; i++) {
183-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
184-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
185-
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
186-
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
181+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
182+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
183+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
184+
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
187185
}
188186
}
189187

@@ -199,7 +197,7 @@ public void testSingleNodeWithFlush() throws Exception {
199197
node1.client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field", "value2").endObject()).execute().actionGet();
200198
node1.client().admin().indices().prepareRefresh().execute().actionGet();
201199

202-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
200+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
203201

204202
closeNode("node1");
205203
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@@ -211,7 +209,7 @@ public void testSingleNodeWithFlush() throws Exception {
211209
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
212210

213211
for (int i = 0; i < 10; i++) {
214-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
212+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
215213
}
216214

217215
closeNode("node1");
@@ -224,7 +222,7 @@ public void testSingleNodeWithFlush() throws Exception {
224222
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
225223

226224
for (int i = 0; i < 10; i++) {
227-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
225+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
228226
}
229227
}
230228

@@ -250,7 +248,7 @@ public void testTwoNodeFirstNodeCleared() throws Exception {
250248
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
251249

252250
for (int i = 0; i < 10; i++) {
253-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
251+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
254252
}
255253

256254
logger.info("--> closing nodes");
@@ -271,7 +269,7 @@ public void testTwoNodeFirstNodeCleared() throws Exception {
271269
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
272270

273271
for (int i = 0; i < 10; i++) {
274-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
272+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
275273
}
276274
}
277275

@@ -297,7 +295,7 @@ public void testLatestVersionLoaded() throws Exception {
297295
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
298296

299297
for (int i = 0; i < 10; i++) {
300-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
298+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
301299
}
302300

303301
logger.info("--> closing first node, and indexing more data to the second node");
@@ -307,7 +305,7 @@ public void testLatestVersionLoaded() throws Exception {
307305
node2.client().admin().indices().prepareRefresh().execute().actionGet();
308306

309307
for (int i = 0; i < 10; i++) {
310-
assertThat(node2.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(3l));
308+
assertHitCount(node2.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 3);
311309
}
312310

313311
logger.info("--> add some metadata, additional type and template");
@@ -340,7 +338,7 @@ public void testLatestVersionLoaded() throws Exception {
340338
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
341339

342340
for (int i = 0; i < 10; i++) {
343-
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(3l));
341+
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 3);
344342
}
345343

346344
ClusterState state = node1.client().admin().cluster().prepareState().execute().actionGet().getState();
@@ -459,7 +457,7 @@ public void testRecoveryDifferentNodeOrderStartup() throws Exception {
459457
assertThat(health.isTimedOut(), equalTo(false));
460458

461459
assertThat(client("node2").admin().indices().prepareExists("test").execute().actionGet().isExists(), equalTo(true));
462-
assertThat(client("node2").prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet().getCount(), equalTo(1l));
460+
assertHitCount(client("node2").prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(), 1);
463461
}
464462

465463
}

src/test/java/org/elasticsearch/test/integration/search/rescore/QueryRescorerTests.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.hamcrest.Matchers.equalTo;
2525
import static org.hamcrest.Matchers.notNullValue;
2626

27-
import static org.elasticsearch.test.hamcrest.ElasticsearchMatchers.SearchHitHasIdMatcher;
2827
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
2928

3029
import org.apache.lucene.util.English;
@@ -81,19 +80,19 @@ public void testRescorePhrase() throws Exception {
8180
.setRescoreWindow(5).execute().actionGet();
8281

8382
assertHitCount(searchResponse, 3);
84-
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
85-
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
86-
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
83+
assertFirstHit(searchResponse, hasId("1"));
84+
assertSecondHit(searchResponse, hasId("2"));
85+
assertThirdHit(searchResponse, hasId("3"));
8786

8887
searchResponse = client().prepareSearch()
8988
.setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(MatchQueryBuilder.Operator.OR))
9089
.setRescorer(RescoreBuilder.queryRescorer((QueryBuilders.matchPhraseQuery("field1", "the quick brown"))))
9190
.setRescoreWindow(5).execute().actionGet();
9291

9392
assertHitCount(searchResponse, 3);
94-
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
95-
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
96-
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
93+
assertFirstHit(searchResponse, hasId("1"));
94+
assertSecondHit(searchResponse, hasId("2"));
95+
assertThirdHit(searchResponse, hasId("3"));
9796
}
9897

9998
@Test
@@ -138,9 +137,9 @@ public void testMoreDocs() throws Exception {
138137

139138
assertThat(searchResponse.getHits().hits().length, equalTo(5));
140139
assertHitCount(searchResponse, 9);
141-
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
142-
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
143-
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
140+
assertFirstHit(searchResponse, hasId("2"));
141+
assertSecondHit(searchResponse, hasId("6"));
142+
assertThirdHit(searchResponse, hasId("3"));
144143

145144
searchResponse = client()
146145
.prepareSearch()
@@ -154,9 +153,9 @@ public void testMoreDocs() throws Exception {
154153

155154
assertThat(searchResponse.getHits().hits().length, equalTo(5));
156155
assertHitCount(searchResponse, 9);
157-
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
158-
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
159-
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
156+
assertFirstHit(searchResponse, hasId("2"));
157+
assertSecondHit(searchResponse, hasId("6"));
158+
assertThirdHit(searchResponse, hasId("3"));
160159
}
161160

162161
private static final void assertEquivalent(SearchResponse plain, SearchResponse rescored) {
@@ -289,9 +288,9 @@ public void testExplain() throws Exception {
289288
.setQueryWeight(0.5f).setRescoreQueryWeight(0.4f)).setRescoreWindow(5).setExplain(true).execute()
290289
.actionGet();
291290
assertHitCount(searchResponse, 3);
292-
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
293-
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
294-
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
291+
assertFirstHit(searchResponse, hasId("1"));
292+
assertSecondHit(searchResponse, hasId("2"));
293+
assertThirdHit(searchResponse, hasId("3"));
295294

296295
for (int i = 0; i < 3; i++) {
297296
assertThat(searchResponse.getHits().getAt(i).explanation(), notNullValue());

0 commit comments

Comments
 (0)