001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.schema;
017:
018: import java.util.Collection;
019:
020: import org.apache.solr.core.SolrCore;
021: import org.apache.solr.schema.SchemaField;
022: import org.apache.solr.util.AbstractSolrTestCase;
023:
024: /**
025: *
026: * @author Greg Ludington
027: */
028: public class RequiredFieldsTest extends AbstractSolrTestCase {
029:
030: @Override
031: public String getSchemaFile() {
032: return "schema-required-fields.xml";
033: }
034:
035: @Override
036: public String getSolrConfigFile() {
037: return "solrconfig.xml";
038: }
039:
040: @Override
041: public void setUp() throws Exception {
042: super .setUp();
043: }
044:
045: @Override
046: public void tearDown() throws Exception {
047: super .tearDown();
048: }
049:
050: public void testRequiredFieldsConfig() {
051:
052: SolrCore core = SolrCore.getSolrCore();
053: IndexSchema schema = core.getSchema();
054: SchemaField uniqueKey = schema.getUniqueKeyField();
055:
056: // Make sure the uniqueKey is required
057: assertTrue(uniqueKey.isRequired());
058: assertTrue(schema.getRequiredFields().contains(uniqueKey));
059:
060: // we specified one required field, but all devault valued fields are also required
061: Collection<SchemaField> requiredFields = schema
062: .getRequiredFields();
063: int numDefaultFields = schema.getFieldsWithDefaultValue()
064: .size();
065: assertEquals(numDefaultFields + 1 + 1, requiredFields.size()); // also the uniqueKey
066: }
067:
068: public void testRequiredFieldsSingleAdd() {
069: // Add a single document
070: assertU("adding document", adoc("id", "529", "name",
071: "document with id, name, and subject", "field_t",
072: "what's inside?", "subject", "info"));
073: assertU(commit());
074:
075: // Check it it is in the index
076: assertQ("should find one", req("id:529"),
077: "//result[@numFound=1]");
078:
079: // Add another document without the required subject field, which
080: // has a configured defaultValue of "Stuff"
081: assertU("adding a doc without field w/ configured default",
082: adoc("id", "530", "name", "document with id and name",
083: "field_t", "what's inside?"));
084: assertU(commit());
085:
086: // Add another document without a subject, which has a default in schema
087: String subjectDefault = SolrCore.getSolrCore().getSchema()
088: .getField("subject").getDefaultValue();
089: assertNotNull("subject has no default value", subjectDefault);
090: assertQ("should find one with subject=" + subjectDefault,
091: req("id:530 subject:" + subjectDefault),
092: "//result[@numFound=1]");
093:
094: // Add another document without a required name, which has no default
095: assertNull(SolrCore.getSolrCore().getSchema().getField("name")
096: .getDefaultValue());
097: assertFailedU("adding doc without required field", adoc("id",
098: "531", "subject", "no name document", "field_t",
099: "what's inside?"));
100: assertU(commit());
101:
102: // Check to make sure this submission did not succeed
103: assertQ("should not find any", req("id:531"),
104: "//result[@numFound=0]");
105: }
106:
107: public void testAddMultipleDocumentsWithErrors() {
108: //Add three documents at once to make sure the baseline succeeds
109: assertU("adding 3 documents", "<add>"
110: + doc("id", "601", "name", "multiad one", "field_t",
111: "what's inside?", "subject", "info")
112: + doc("id", "602", "name", "multiad two", "field_t",
113: "what's inside?", "subject", "info")
114: + doc("id", "603", "name", "multiad three", "field_t",
115: "what's inside?", "subject", "info") + "</add>");
116: assertU(commit());
117:
118: // Check that they are in the index
119: assertQ("should find three", req("name:multiad"),
120: "//result[@numFound=3]");
121:
122: // Add three documents at once, with the middle one missing a field that has a default
123: assertU(
124: "adding 3 docs, with 2nd one missing a field that has a default value",
125: "<add>"
126: + doc("id", "601", "name",
127: "nosubject batch one", "field_t",
128: "what's inside?", "subject", "info")
129: + doc("id", "602", "name",
130: "nosubject batch two", "field_t",
131: "what's inside?")
132: + doc("id", "603", "name",
133: "nosubject batch three", "field_t",
134: "what's inside?", "subject", "info")
135: + "</add>");
136: assertU(commit());
137:
138: // Since the missing field had a devault value,
139: // All three should have made it into the index
140: assertQ("should find three", req("name:nosubject"),
141: "//result[@numFound=3]");
142:
143: // Add three documents at once, with the middle with a bad field definition,
144: // to establish the baselinie behavior for errors in a multi-ad submission
145: assertFailedU(
146: "adding 3 documents, with 2nd one with undefined field",
147: "<add>"
148: + doc("id", "801", "name", "baddef batch one",
149: "field_t", "what's inside?", "subject",
150: "info")
151: + doc("id", "802", "field_t", "name",
152: "baddef batch two", "what's inside?",
153: "subject", "info", "GaRbAgeFiElD",
154: "garbage")
155: + doc("id", "803", "name",
156: "baddef batch three", "field_t",
157: "what's inside?", "subject", "info")
158: + "</add>");
159: assertU(commit());
160:
161: // Check that only docs before the error should be in the index
162: assertQ("should find one", req("name:baddef"),
163: "//result[@numFound=1]");
164:
165: // Add three documents at once, with the middle one missing a required field that has no default
166: assertFailedU(
167: "adding 3 docs, with 2nd one missing required field",
168: "<add>"
169: + doc("id", "701", "name", "noname batch one",
170: "field_t", "what's inside?", "subject",
171: "info")
172: + doc("id", "702", "field_t", "what's inside?",
173: "subject", "info")
174: + doc("id", "703", "name",
175: "noname batch batch three", "field_t",
176: "what's inside?", "subject", "info")
177: + "</add>");
178:
179: assertU(commit());
180:
181: // Check that only docs before the error should be in the index
182: assertQ("should find one", req("name:noname"),
183: "//result[@numFound=1]");
184: }
185: }
|