Cleaning up some tests by using assertHitCount assertion

This commit is contained in:
Alexander Reelsen 2013-06-10 16:56:35 +02:00
parent 21945e5060
commit 9323e677bd
3 changed files with 44 additions and 42 deletions

View File

@ -25,6 +25,7 @@ import java.util.Arrays;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.search.SearchHit;
@ -43,6 +44,10 @@ public class ElasticsearchAssertions {
assertThat(searchResponse.getHits().totalHits(), is(expectedHitCount));
}
public static void assertHitCount(CountResponse countResponse, long expectedHitCount) {
assertThat(countResponse.getCount(), is(expectedHitCount));
}
public static void assertFirstHit(SearchResponse searchResponse, Matcher<SearchHit> matcher) {
assertSearchHit(searchResponse, 1, matcher);
}

View File

@ -19,8 +19,6 @@
package org.elasticsearch.test.integration.gateway.local;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
@ -98,7 +96,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
.startArray("appAccountIds").value(14).endArray().endObject()).execute().actionGet();
node1.client().admin().indices().prepareRefresh().execute().actionGet();
assertThat(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet(), 2);
closeNode("node1");
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@ -112,7 +110,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
node1.client().admin().indices().prepareRefresh().execute().actionGet();
CountResponse count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
assertNoFailures(count);
assertThat(count.getCount(), equalTo(2l));
assertHitCount(count, 2);
closeNode("node1");
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@ -126,7 +124,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
node1.client().admin().indices().prepareRefresh().execute().actionGet();
count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
assertNoFailures(count);
assertThat(count.getCount(), equalTo(2l));
assertHitCount(count, 2);
}
@Test
@ -148,10 +146,10 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
node1.client().admin().indices().prepareRefresh().execute().actionGet();
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
}
closeNode("node1");
@ -164,10 +162,10 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
}
closeNode("node1");
@ -180,10 +178,10 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet().getCount(), equalTo(1l));
assertThat(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet().getCount(), equalTo(1l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value1")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("field", "value2")).execute().actionGet(), 1);
assertHitCount(node1.client().prepareCount().setQuery(termQuery("num", 179)).execute().actionGet(), 1);
}
}
@ -199,7 +197,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
node1.client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field", "value2").endObject()).execute().actionGet();
node1.client().admin().indices().prepareRefresh().execute().actionGet();
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
closeNode("node1");
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@ -211,7 +209,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
}
closeNode("node1");
@ -224,7 +222,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
}
}
@ -250,7 +248,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
}
logger.info("--> closing nodes");
@ -271,7 +269,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
}
}
@ -297,7 +295,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 2);
}
logger.info("--> closing first node, and indexing more data to the second node");
@ -307,7 +305,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
node2.client().admin().indices().prepareRefresh().execute().actionGet();
for (int i = 0; i < 10; i++) {
assertThat(node2.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(3l));
assertHitCount(node2.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 3);
}
logger.info("--> add some metadata, additional type and template");
@ -340,7 +338,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
for (int i = 0; i < 10; i++) {
assertThat(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(3l));
assertHitCount(node1.client().prepareCount().setQuery(matchAllQuery()).execute().actionGet(), 3);
}
ClusterState state = node1.client().admin().cluster().prepareState().execute().actionGet().getState();
@ -459,7 +457,7 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(health.isTimedOut(), equalTo(false));
assertThat(client("node2").admin().indices().prepareExists("test").execute().actionGet().isExists(), equalTo(true));
assertThat(client("node2").prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet().getCount(), equalTo(1l));
assertHitCount(client("node2").prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(), 1);
}
}

View File

@ -24,7 +24,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.elasticsearch.test.hamcrest.ElasticsearchMatchers.SearchHitHasIdMatcher;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import org.apache.lucene.util.English;
@ -81,9 +80,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.setRescoreWindow(5).execute().actionGet();
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
assertThirdHit(searchResponse, hasId("3"));
searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(MatchQueryBuilder.Operator.OR))
@ -91,9 +90,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.setRescoreWindow(5).execute().actionGet();
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
assertThirdHit(searchResponse, hasId("3"));
}
@Test
@ -138,9 +137,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
assertThat(searchResponse.getHits().hits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
assertFirstHit(searchResponse, hasId("2"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("3"));
searchResponse = client()
.prepareSearch()
@ -154,9 +153,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
assertThat(searchResponse.getHits().hits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
assertFirstHit(searchResponse, hasId("2"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("3"));
}
private static final void assertEquivalent(SearchResponse plain, SearchResponse rescored) {
@ -289,9 +288,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.setQueryWeight(0.5f).setRescoreQueryWeight(0.4f)).setRescoreWindow(5).setExplain(true).execute()
.actionGet();
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("2"));
assertThirdHit(searchResponse, hasId("3"));
for (int i = 0; i < 3; i++) {
assertThat(searchResponse.getHits().getAt(i).explanation(), notNullValue());