Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Added Search unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
edu committed Jul 11, 2017
1 parent 8c66a29 commit d886394
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
Expand Up @@ -16,6 +16,7 @@
package com.stratio.cassandra.lucene.search;

import com.google.common.base.MoreObjects;
import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.IndexPagingState;
import com.stratio.cassandra.lucene.schema.Schema;
import com.stratio.cassandra.lucene.search.condition.Condition;
Expand Down Expand Up @@ -44,8 +45,8 @@ public class Search {

protected static final Logger logger = LoggerFactory.getLogger(Search.class);

private static final boolean DEFAULT_FORCE_REFRESH = false;
private static final int DEFAULT_SKIP = 0;
static final boolean DEFAULT_FORCE_REFRESH = false;
static final int DEFAULT_SKIP = 0;

/** The mandatory conditions not participating in scoring. */
public final List<Condition> filter;
Expand All @@ -63,7 +64,7 @@ public class Search {
private final IndexPagingState paging;

/** Firsts rows to skip. */
private final Integer skip;
private int skip;

/**
* Constructor using the specified querying, filtering, sorting and refresh options.
Expand All @@ -86,6 +87,10 @@ public Search(List<Condition> filter,
this.sort = sort == null ? Collections.EMPTY_LIST : sort;
this.paging = paging;
this.refresh = refresh == null ? DEFAULT_FORCE_REFRESH : refresh;
if ((skip != null) && (skip < 0)) {
throw new IndexException("skip must be positive.");
}

this.skip = skip == null ? DEFAULT_SKIP : skip;
}

Expand Down Expand Up @@ -139,7 +144,7 @@ public boolean isEmpty() {
*
* @return the number of rows to skip
*/
public Integer getSkip() {
public int getSkip() {
return skip;
}

Expand Down
Expand Up @@ -228,7 +228,7 @@ class IndexQueryHandler extends QueryHandler with Logging {
if (data != null) data.close()
}
}

}

/** Companion object for [[IndexQueryHandler]]. */
Expand Down
Expand Up @@ -16,9 +16,9 @@
package com.stratio.cassandra.lucene.search;

import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.common.JsonSerializer;
import com.stratio.cassandra.lucene.search.condition.builder.ConditionBuilder;
import com.stratio.cassandra.lucene.search.sort.builder.SimpleSortFieldBuilder;
import com.stratio.cassandra.lucene.common.JsonSerializer;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -41,18 +41,32 @@ public void testBuild() throws IOException {
SimpleSortFieldBuilder sort2 = field("field4");
SearchBuilder builder = new SearchBuilder().filter(filter)
.query(query)
.sort(sort1, sort2);
String json = builder.toJson();
.sort(sort1, sort2)
.skip(25);
String json = "{" +
"filter:[{type:\"match\",field:\"field1\",value:\"value2\"}]," +
"query:[{type:\"match\",field:\"field2\",value:\"value2\"}]," +
"sort:[{type:\"simple\",field:\"field3\",reverse:false},{type:\"simple\",field:\"field4\",reverse:false}]," +
"refresh:false," +
"skip:25" +
"}";
assertEquals("JSON serialization is wrong", json, JsonSerializer.toString(builder));
}

@Test
public void testJson() {
SearchBuilder searchBuilder = search().filter(match("field1", "value1"))
.query(match("field2", "value2"))
.sort(field("field"));
String json = searchBuilder.toJson();
assertEquals("JSON serialization is wrong", json, SearchBuilder.fromJson(json).toJson());
.sort(field("field"))
.skip(10);
String json = "{" +
"filter:[{type:\"match\",field:\"field1\",value:\"value1\"}]," +
"query:[{type:\"match\",field:\"field2\",value:\"value2\"}]," +
"sort:[{type:\"simple\",field:\"field\",reverse:false}]," +
"refresh:false," +
"skip:10" +
"}";
assertEquals("JSON serialization is wrong", json, searchBuilder.toJson());
}

@Test(expected = IndexException.class)
Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.stratio.cassandra.lucene.search;

import com.google.common.collect.Sets;
import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.schema.Schema;
import com.stratio.cassandra.lucene.search.condition.builder.MatchConditionBuilder;
import com.stratio.cassandra.lucene.search.sort.builder.SortFieldBuilder;
Expand All @@ -25,6 +26,8 @@

import static com.stratio.cassandra.lucene.schema.SchemaBuilders.schema;
import static com.stratio.cassandra.lucene.schema.SchemaBuilders.stringMapper;
import static com.stratio.cassandra.lucene.search.Search.DEFAULT_FORCE_REFRESH;
import static com.stratio.cassandra.lucene.search.Search.DEFAULT_SKIP;
import static com.stratio.cassandra.lucene.search.SearchBuilders.*;
import static org.junit.Assert.*;

Expand All @@ -39,16 +42,38 @@ public class SearchTest {
@Test
public void testBuilderEmpty() {
Search search = search().build();
assertFalse("Default refresh is not set", search.refresh());
assertEquals("Default refresh is not set", DEFAULT_FORCE_REFRESH, search.refresh());
assertEquals("Default refresh is not set", new Long(DEFAULT_SKIP), new Long(search.getSkip()));
}

@Test
public void testBuilder() {
assertTrue("Refresh is not set", search().filter(MATCH)
.query(MATCH)
.sort(FIELD)
.refresh(true)
.build().refresh());
Search search = search().filter(MATCH)
.query(MATCH)
.sort(FIELD)
.refresh(true)
.skip(10)
.build();
assertEquals("Refresh is not set", true, search.refresh());
assertEquals("Skip is not set", 10, search.getSkip());
assertEquals("Skip is not set", true, search.useSkip());
}

@Test
public void testFailingBuilder() {
try {
search().filter(MATCH)
.query(MATCH)
.sort(FIELD)
.refresh(true)
.skip(-10)
.build();
fail("Incorrect Search builder with negative skip should throw a IndexException but does not");
} catch (IndexException e) {
String expectedMessage = "skip must be positive.";
String receivedMessage = e.getMessage();
assertEquals("Exception message should be '" + expectedMessage + "'but it is: '" + receivedMessage + "'", expectedMessage, receivedMessage);
}
}

@Test
Expand Down

0 comments on commit d886394

Please sign in to comment.