Skip to content

Commit

Permalink
Changes after code review searchbox-io#224
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekbp committed Jul 26, 2015
1 parent 40898ac commit 281de3e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
50 changes: 38 additions & 12 deletions jest-common/src/main/java/io/searchbox/core/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@
import io.searchbox.action.AbstractMultiIndexActionBuilder;
import io.searchbox.action.AbstractMultiTypeActionBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
* @author Bartosz Polnik
*/
public class Cat extends AbstractAction<CatResult> {
private final static String PATH_TO_RESULT = "result";
public Cat(AbstractAction.Builder<Cat, ? extends Builder> builder) {
private final String operationPath;

protected <T extends AbstractAction.Builder<Cat, ? extends Builder> & CatBuilder> Cat(T builder) {
super(builder);
this.operationPath = builder.operationPath();
setURI(buildURI());
}

Cat() {
@Override
protected String buildURI() {
String uriSuffix = super.buildURI();
return this.operationPath + (uriSuffix.isEmpty() ? "" : "/") + uriSuffix;
}

@Override
Expand Down Expand Up @@ -62,31 +70,49 @@ public boolean equals(Object obj) {
.isEquals();
}

public static class IndicesBuilder extends AbstractMultiTypeActionBuilder<Cat, IndicesBuilder> {
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(super.hashCode())
.toHashCode();
}

public static class IndicesBuilder extends AbstractMultiTypeActionBuilder<Cat, IndicesBuilder> implements CatBuilder {
private static final String operationPath = "_cat/indices";

public IndicesBuilder() {
setHeader("content-type", "application/json");
}

@Override
public Cat build() {
Cat cat = new Cat(this);
String uriSuffix = cat.buildURI();
cat.setURI("_cat/indices" + (uriSuffix.isEmpty() ? "" : "/") + uriSuffix);
return cat;
return new Cat(this);
}

@Override
public String operationPath() {
return operationPath;
}
}

public static class AliasesBuilder extends AbstractMultiIndexActionBuilder<Cat, AliasesBuilder> {
public static class AliasesBuilder extends AbstractMultiIndexActionBuilder<Cat, AliasesBuilder> implements CatBuilder {
private static final String operationPath = "_cat/aliases";
public AliasesBuilder() {
setHeader("content-type", "application/json");
}

@Override
public Cat build() {
Cat cat = new Cat(this);
String uriSuffix = cat.buildURI();
cat.setURI("_cat/aliases" + (uriSuffix.isEmpty() ? "" : "/") + uriSuffix);
return cat;
return new Cat(this);
}

@Override
public String operationPath() {
return operationPath;
}
}

private interface CatBuilder {
String operationPath();
}
}
5 changes: 5 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/CatResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* @author Bartosz Polnik
*/
public class CatResult extends JestResult {

public CatResult(CatResult catResult) {
super(catResult);
}

public CatResult(Gson gson) {
super(gson);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import static org.junit.Assert.assertArrayEquals;

/**
* @author Bartosz Polnik
*/
public class CatResultTest {
private final String EXAMPLE_RESPONSE_TWO_ROWS = "[\n" +
" {\n" +
Expand Down Expand Up @@ -36,7 +39,7 @@ public void shouldReturnEmptyArrayOnNoJsonObject() {

@Test
public void shouldReturnArrayWithColumnNamesAndSingleResult() {
Cat cat = new Cat();
Cat cat = new Cat.IndicesBuilder().build();
String reasonPhase = StringUtils.EMPTY;
CatResult catResult = cat.createNewElasticSearchResult(EXAMPLE_RESPONSE_SINGLE_ROWS, 200, reasonPhase, new Gson());

Expand All @@ -48,7 +51,7 @@ public void shouldReturnArrayWithColumnNamesAndSingleResult() {

@Test
public void shouldReturnArrayWithTwoResultsEventWhenColumnsWereReordered() {
Cat cat = new Cat();
Cat cat = new Cat.IndicesBuilder().build();
String reasonPhase = StringUtils.EMPTY;
CatResult catResult = cat.createNewElasticSearchResult(EXAMPLE_RESPONSE_TWO_ROWS, 200, reasonPhase, new Gson());

Expand Down
12 changes: 8 additions & 4 deletions jest/src/test/java/io/searchbox/core/CatIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numDataNodes = 1)
public class CatIntegrationTest extends AbstractIntegrationTest {
final static String INDEX = "twitter";
final static String ALIAS = "twitterAlias";
final static String INDEX2 = "twitter2";
final static String INDEX = "catintegrationindex";
final static String ALIAS = "catintegrationslias";
final static String INDEX2 = "catintegrationindex2";

@Test
public void shouldReturnEmptyPlainTextForIndices() throws IOException {
Expand All @@ -25,8 +25,9 @@ public void shouldReturnEmptyPlainTextForIndices() throws IOException {
}

@Test
public void shouldPropertyMapSingleResult() throws IOException {
public void shouldProperlyMapSingleResult() throws IOException {
createIndex(INDEX);
ensureSearchable(INDEX);

CatResult result = client.execute(new Cat.IndicesBuilder().setParameter("h", "index,docs.count").build());
assertArrayEquals(new String[][]{
Expand All @@ -38,6 +39,7 @@ public void shouldPropertyMapSingleResult() throws IOException {
@Test
public void shouldFilterResultsToASingleIndex() throws IOException {
createIndex(INDEX, INDEX2);
ensureSearchable(INDEX, INDEX2);

CatResult result = client.execute(new Cat.IndicesBuilder().setParameter("h", "index,docs.count").addIndex(INDEX2).build());
assertArrayEquals(new String[][]{
Expand All @@ -49,6 +51,7 @@ public void shouldFilterResultsToASingleIndex() throws IOException {
@Test
public void shouldDisplayAliasForSingleResult() throws IOException {
createIndex(INDEX);
ensureSearchable(INDEX);
IndicesAliasesAction.INSTANCE.newRequestBuilder(client().admin().indices()).addAlias(INDEX, ALIAS).get();

CatResult result = client.execute(new Cat.AliasesBuilder().setParameter("h", "alias,index").build());
Expand All @@ -61,6 +64,7 @@ public void shouldDisplayAliasForSingleResult() throws IOException {
@Test
public void shouldChangeOrderOfColumnsByspecifyingParameters() throws IOException {
createIndex(INDEX);
ensureSearchable(INDEX);
IndicesAliasesAction.INSTANCE.newRequestBuilder(client().admin().indices()).addAlias(INDEX, ALIAS).get();

CatResult result = client.execute(new Cat.AliasesBuilder().setParameter("h", "index,alias").build());
Expand Down

0 comments on commit 281de3e

Please sign in to comment.