Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.elasticsearch.action.Action;

import java.util.Collections;

public class FieldCapabilitiesAction extends Action<FieldCapabilitiesResponse> {

public static final FieldCapabilitiesAction INSTANCE = new FieldCapabilitiesAction();
Expand All @@ -32,6 +34,6 @@ private FieldCapabilitiesAction() {

@Override
public FieldCapabilitiesResponse newResponse() {
return new FieldCapabilitiesResponse();
return new FieldCapabilitiesResponse(Collections.emptyMap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this one for serialization? I would have changed the other call from the TransportAction, this is ok as-is I think, unless we want to completely get rid of the default constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oups, I fixed, thanks !

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -56,16 +57,15 @@ public class FieldCapabilitiesResponse extends ActionResponse implements ToXCont

private FieldCapabilitiesResponse(Map<String, Map<String, FieldCapabilities>> responseMap,
List<FieldCapabilitiesIndexResponse> indexResponses) {
this.responseMap = responseMap;
this.indexResponses = indexResponses;
this.responseMap = Objects.requireNonNull(responseMap);
this.indexResponses = Objects.requireNonNull(indexResponses);
}

/**
* Used for serialization
*/
FieldCapabilitiesResponse() {
this.responseMap = Collections.emptyMap();
this.indexResponses = Collections.emptyList();
this(Collections.emptyMap(), Collections.emptyList());
}

/**
Expand All @@ -82,6 +82,7 @@ public Map<String, Map<String, FieldCapabilities>> get() {
List<FieldCapabilitiesIndexResponse> getIndexResponses() {
return indexResponses;
}

/**
*
* Get the field capabilities per type for the provided {@code field}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
import org.elasticsearch.test.AbstractStreamableXContentTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLength;


public class FieldCapabilitiesResponseTests extends AbstractStreamableXContentTestCase<FieldCapabilitiesResponse> {

Expand All @@ -48,22 +52,46 @@ protected FieldCapabilitiesResponse createBlankInstance() {

@Override
protected FieldCapabilitiesResponse createTestInstance() {
Map<String, Map<String, FieldCapabilities>> responses = new HashMap<>();
if (randomBoolean()) {
// merged responses
Map<String, Map<String, FieldCapabilities>> responses = new HashMap<>();

String[] fields = generateRandomStringArray(5, 10, false, true);
assertNotNull(fields);

for (String field : fields) {
Map<String, FieldCapabilities> typesToCapabilities = new HashMap<>();
String[] types = generateRandomStringArray(5, 10, false, false);
assertNotNull(types);

for (String type : types) {
typesToCapabilities.put(type, FieldCapabilitiesTests.randomFieldCaps(field));
}
responses.put(field, typesToCapabilities);
}
return new FieldCapabilitiesResponse(responses);
} else {
// non-merged responses
List<FieldCapabilitiesIndexResponse> responses = new ArrayList<>();
int numResponse = randomIntBetween(0, 10);
for (int i = 0; i < numResponse; i++) {
responses.add(createRandomIndexResponse());
}
return new FieldCapabilitiesResponse(responses);
}
}


private FieldCapabilitiesIndexResponse createRandomIndexResponse() {
Map<String, FieldCapabilities> responses = new HashMap<>();

String[] fields = generateRandomStringArray(5, 10, false, true);
assertNotNull(fields);

for (String field : fields) {
Map<String, FieldCapabilities> typesToCapabilities = new HashMap<>();
String[] types = generateRandomStringArray(5, 10, false, false);
assertNotNull(types);

for (String type : types) {
typesToCapabilities.put(type, FieldCapabilitiesTests.randomFieldCaps(field));
}
responses.put(field, typesToCapabilities);
responses.put(field, FieldCapabilitiesTests.randomFieldCaps(field));
}
return new FieldCapabilitiesResponse(responses);
return new FieldCapabilitiesIndexResponse(randomAsciiLettersOfLength(10), responses);
}

@Override
Expand Down Expand Up @@ -138,6 +166,11 @@ public void testToXContent() throws IOException {
"}").replaceAll("\\s+", ""), generatedResponse);
}

public void testEmptyResponse() throws IOException {
FieldCapabilitiesResponse testInstance = new FieldCapabilitiesResponse();
assertSerialization(testInstance);
}

private static FieldCapabilitiesResponse createSimpleResponse() {
Map<String, FieldCapabilities> titleCapabilities = new HashMap<>();
titleCapabilities.put("text", new FieldCapabilities("title", "text", true, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,4 @@ public void testFieldAliasFilteringWithWildcard() {
assertEquals(1, response.get().size());
assertTrue(response.get().containsKey("distance"));
}

public void testEmptyIndexPattern() {
FieldCapabilitiesResponse response = client().prepareFieldCaps("empty_index_pattern*")
.setFields("*")
.execute().actionGet();
assertEquals(0, response.get().size());
}
}