SOLR-11631: fix Solrj tests

This commit is contained in:
Steve Rowe 2018-01-09 11:55:30 -05:00
parent 2432bdebe3
commit e3f3cdd085
1 changed files with 44 additions and 32 deletions

View File

@ -28,7 +28,9 @@ import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.solr.api.ApiBag;
import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.schema.AnalyzerDefinition; import org.apache.solr.client.solrj.request.schema.AnalyzerDefinition;
import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition; import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition;
import org.apache.solr.client.solrj.request.schema.SchemaRequest; import org.apache.solr.client.solrj.request.schema.SchemaRequest;
@ -37,6 +39,8 @@ import org.apache.solr.client.solrj.response.schema.FieldTypeRepresentation;
import org.apache.solr.client.solrj.response.schema.SchemaRepresentation; import org.apache.solr.client.solrj.response.schema.SchemaRepresentation;
import org.apache.solr.client.solrj.response.schema.SchemaResponse; import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.util.RestTestBase; import org.apache.solr.util.RestTestBase;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After; import org.junit.After;
@ -57,6 +61,15 @@ public class SchemaTest extends RestTestBase {
assertEquals("Response contained errors: " + schemaResponse.toString(), 0, schemaResponse.getStatus()); assertEquals("Response contained errors: " + schemaResponse.toString(), 0, schemaResponse.getStatus());
assertNull("Response contained errors: " + schemaResponse.toString(), schemaResponse.getResponse().get("errors")); assertNull("Response contained errors: " + schemaResponse.toString(), schemaResponse.getResponse().get("errors"));
} }
private static void assertFailedSchemaResponse(ThrowingRunnable runnable, String expectedErrorMessage) {
HttpSolrClient.RemoteExecutionException e = expectThrows(HttpSolrClient.RemoteExecutionException.class, runnable);
SimpleOrderedMap errorMap = (SimpleOrderedMap)e.getMetaData().get("error");
assertEquals("org.apache.solr.api.ApiBag$ExceptionWithErrObject",
((NamedList)errorMap.get("metadata")).get("error-class"));
List details = (List)errorMap.get("details");
assertTrue(((List)((Map)details.get(0)).get("errorMessages")).get(0).toString().contains(expectedErrorMessage));
}
private static void createStoredStringField(String fieldName, SolrClient solrClient) throws Exception { private static void createStoredStringField(String fieldName, SolrClient solrClient) throws Exception {
Map<String, Object> fieldAttributes = new LinkedHashMap<>(); Map<String, Object> fieldAttributes = new LinkedHashMap<>();
@ -283,16 +296,16 @@ public class SchemaTest extends RestTestBase {
@Test @Test
public void addFieldShouldntBeCalledTwiceWithTheSameName() throws Exception { public void addFieldShouldntBeCalledTwiceWithTheSameName() throws Exception {
Map<String, Object> fieldAttributes = new LinkedHashMap<>(); Map<String, Object> fieldAttributes = new LinkedHashMap<>();
fieldAttributes.put("name", "failureField"); String fieldName = "failureField";
fieldAttributes.put("name", fieldName);
fieldAttributes.put("type", "string"); fieldAttributes.put("type", "string");
SchemaRequest.AddField addFieldUpdateSchemaRequest = SchemaRequest.AddField addFieldUpdateSchemaRequest =
new SchemaRequest.AddField(fieldAttributes); new SchemaRequest.AddField(fieldAttributes);
SchemaResponse.UpdateResponse addFieldFirstResponse = addFieldUpdateSchemaRequest.process(getSolrClient()); SchemaResponse.UpdateResponse addFieldFirstResponse = addFieldUpdateSchemaRequest.process(getSolrClient());
assertValidSchemaResponse(addFieldFirstResponse); assertValidSchemaResponse(addFieldFirstResponse);
SchemaResponse.UpdateResponse addFieldSecondResponse = addFieldUpdateSchemaRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> addFieldUpdateSchemaRequest.process(getSolrClient()),
assertEquals(0, addFieldSecondResponse.getStatus()); "Field '" + fieldName + "' already exists.");
assertNotNull(addFieldSecondResponse.getResponse().get("errors"));
} }
@Test @Test
@ -326,11 +339,11 @@ public class SchemaTest extends RestTestBase {
} }
@Test @Test
public void deletingAFieldThatDoesntExistInTheSchemaShouldFail() throws Exception { public void deletingAFieldThatDoesntExistInTheSchemaShouldFail() {
SchemaRequest.DeleteField deleteFieldRequest = String fieldName = "fieldToBeDeleted";
new SchemaRequest.DeleteField("fieldToBeDeleted"); SchemaRequest.DeleteField deleteFieldRequest = new SchemaRequest.DeleteField(fieldName);
SchemaResponse.UpdateResponse deleteFieldResponse = deleteFieldRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> deleteFieldRequest.process(getSolrClient()),
assertNotNull(deleteFieldResponse.getResponse().get("errors")); "The field '" + fieldName + "' is not present in this schema, and so cannot be deleted.");
} }
@Test @Test
@ -406,7 +419,8 @@ public class SchemaTest extends RestTestBase {
@Test @Test
public void addDynamicFieldShouldntBeCalledTwiceWithTheSameName() throws Exception { public void addDynamicFieldShouldntBeCalledTwiceWithTheSameName() throws Exception {
Map<String, Object> fieldAttributes = new LinkedHashMap<>(); Map<String, Object> fieldAttributes = new LinkedHashMap<>();
fieldAttributes.put("name", "*_failure"); String dynamicFieldName = "*_failure";
fieldAttributes.put("name", dynamicFieldName);
fieldAttributes.put("type", "string"); fieldAttributes.put("type", "string");
SchemaRequest.AddDynamicField addDFieldUpdateSchemaRequest = SchemaRequest.AddDynamicField addDFieldUpdateSchemaRequest =
new SchemaRequest.AddDynamicField(fieldAttributes); new SchemaRequest.AddDynamicField(fieldAttributes);
@ -414,9 +428,8 @@ public class SchemaTest extends RestTestBase {
SchemaResponse.UpdateResponse addDFieldFirstResponse = addDFieldUpdateSchemaRequest.process(client); SchemaResponse.UpdateResponse addDFieldFirstResponse = addDFieldUpdateSchemaRequest.process(client);
assertValidSchemaResponse(addDFieldFirstResponse); assertValidSchemaResponse(addDFieldFirstResponse);
SchemaResponse.UpdateResponse addDFieldSecondResponse = addDFieldUpdateSchemaRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> addDFieldUpdateSchemaRequest.process(getSolrClient()),
assertEquals(0, addDFieldSecondResponse.getStatus()); "[schema.xml] Duplicate DynamicField definition for '" + dynamicFieldName + "'");
assertNotNull(addDFieldSecondResponse.getResponse().get("errors"));
} }
@Test @Test
@ -453,10 +466,10 @@ public class SchemaTest extends RestTestBase {
@Test @Test
public void deletingADynamicFieldThatDoesntExistInTheSchemaShouldFail() throws Exception { public void deletingADynamicFieldThatDoesntExistInTheSchemaShouldFail() throws Exception {
SchemaRequest.DeleteDynamicField deleteDynamicFieldRequest = String dynamicFieldName = "*_notexists";
new SchemaRequest.DeleteDynamicField("*_notexists"); SchemaRequest.DeleteDynamicField deleteDynamicFieldRequest = new SchemaRequest.DeleteDynamicField(dynamicFieldName);
SchemaResponse.UpdateResponse deleteDynamicFieldResponse = deleteDynamicFieldRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> deleteDynamicFieldRequest.process(getSolrClient()),
assertNotNull(deleteDynamicFieldResponse.getResponse().get("errors")); "The dynamic field '" + dynamicFieldName + "' is not present in this schema, and so cannot be deleted.");
} }
@Test @Test
@ -635,7 +648,8 @@ public class SchemaTest extends RestTestBase {
@Test @Test
public void addFieldTypeShouldntBeCalledTwiceWithTheSameName() throws Exception { public void addFieldTypeShouldntBeCalledTwiceWithTheSameName() throws Exception {
Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>(); Map<String, Object> fieldTypeAttributes = new LinkedHashMap<>();
fieldTypeAttributes.put("name", "failureInt"); String fieldName = "failureInt";
fieldTypeAttributes.put("name", fieldName);
fieldTypeAttributes.put("class", RANDOMIZED_NUMERIC_FIELDTYPES.get(Integer.class)); fieldTypeAttributes.put("class", RANDOMIZED_NUMERIC_FIELDTYPES.get(Integer.class));
fieldTypeAttributes.put("omitNorms", true); fieldTypeAttributes.put("omitNorms", true);
fieldTypeAttributes.put("positionIncrementGap", 0); fieldTypeAttributes.put("positionIncrementGap", 0);
@ -646,9 +660,8 @@ public class SchemaTest extends RestTestBase {
SchemaResponse.UpdateResponse addFieldTypeFirstResponse = addFieldTypeRequest.process(getSolrClient()); SchemaResponse.UpdateResponse addFieldTypeFirstResponse = addFieldTypeRequest.process(getSolrClient());
assertValidSchemaResponse(addFieldTypeFirstResponse); assertValidSchemaResponse(addFieldTypeFirstResponse);
SchemaResponse.UpdateResponse addFieldTypeSecondResponse = addFieldTypeRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> addFieldTypeRequest.process(getSolrClient()),
assertEquals(0, addFieldTypeSecondResponse.getStatus()); "Field type '" + fieldName + "' already exists.");
assertNotNull(addFieldTypeSecondResponse.getResponse().get("errors"));
} }
@Test @Test
@ -689,10 +702,10 @@ public class SchemaTest extends RestTestBase {
@Test @Test
public void deletingAFieldTypeThatDoesntExistInTheSchemaShouldFail() throws Exception { public void deletingAFieldTypeThatDoesntExistInTheSchemaShouldFail() throws Exception {
SchemaRequest.DeleteFieldType deleteFieldTypeRequest = String fieldType = "fieldTypeToBeDeleted";
new SchemaRequest.DeleteFieldType("fieldTypeToBeDeleted"); SchemaRequest.DeleteFieldType deleteFieldTypeRequest = new SchemaRequest.DeleteFieldType(fieldType);
SchemaResponse.UpdateResponse deleteFieldResponse = deleteFieldTypeRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> deleteFieldTypeRequest.process(getSolrClient()),
assertNotNull(deleteFieldResponse.getResponse().get("errors")); "The field type '" + fieldType + "' is not present in this schema, and so cannot be deleted.");
} }
@Test @Test
@ -800,11 +813,10 @@ public class SchemaTest extends RestTestBase {
String srcFieldName = "srcnotexist"; String srcFieldName = "srcnotexist";
String destFieldName1 = "destNotExist1", destFieldName2 = "destNotExist2"; String destFieldName1 = "destNotExist1", destFieldName2 = "destNotExist2";
SchemaRequest.AddCopyField addCopyFieldRequest = SchemaRequest.AddCopyField addCopyFieldRequest
new SchemaRequest.AddCopyField(srcFieldName, = new SchemaRequest.AddCopyField(srcFieldName, Arrays.asList(destFieldName1, destFieldName2));
Arrays.asList(destFieldName1, destFieldName2)); assertFailedSchemaResponse(() -> addCopyFieldRequest.process(getSolrClient()),
SchemaResponse.UpdateResponse addCopyFieldResponse = addCopyFieldRequest.process(getSolrClient()); "copyField source :'" + srcFieldName + "' is not a glob and doesn't match any explicit field or dynamicField.");
assertNotNull(addCopyFieldResponse.getResponse().get("errors"));
} }
@Test @Test
@ -838,8 +850,8 @@ public class SchemaTest extends RestTestBase {
SchemaRequest.DeleteCopyField deleteCopyFieldsRequest = SchemaRequest.DeleteCopyField deleteCopyFieldsRequest =
new SchemaRequest.DeleteCopyField(srcFieldName, new SchemaRequest.DeleteCopyField(srcFieldName,
Arrays.asList(destFieldName1, destFieldName2)); Arrays.asList(destFieldName1, destFieldName2));
SchemaResponse.UpdateResponse deleteCopyFieldResponse = deleteCopyFieldsRequest.process(getSolrClient()); assertFailedSchemaResponse(() -> deleteCopyFieldsRequest.process(getSolrClient()),
assertNotNull(deleteCopyFieldResponse.getResponse().get("errors")); "Copy field directive not found: '" + srcFieldName + "' -> '" + destFieldName1 + "'");
} }
@Test @Test