Browse Source
Convert lineendings from CRLF to LF. Reduce validator implementations visibility to package. Extend Javadoc. Slight rewording in reference documentation. Original pull request: #525. Related pull request: #511. Related ticket: DATACMNS-1835.pull/527/head
10 changed files with 239 additions and 245 deletions
@ -1,75 +1,75 @@ |
|||||||
/* |
/* |
||||||
* Copyright 2018 the original author or authors. |
* Copyright 2018 the original author or authors. |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package org.springframework.data.mongodb.core.validation; |
package org.springframework.data.mongodb.core.validation; |
||||||
|
|
||||||
import lombok.AccessLevel; |
import lombok.AccessLevel; |
||||||
import lombok.EqualsAndHashCode; |
import lombok.EqualsAndHashCode; |
||||||
import lombok.RequiredArgsConstructor; |
import lombok.RequiredArgsConstructor; |
||||||
|
|
||||||
import org.bson.Document; |
import org.bson.Document; |
||||||
import org.springframework.data.mongodb.core.query.Criteria; |
import org.springframework.data.mongodb.core.query.Criteria; |
||||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition; |
import org.springframework.data.mongodb.core.query.CriteriaDefinition; |
||||||
import org.springframework.data.mongodb.core.query.SerializationUtils; |
import org.springframework.data.mongodb.core.query.SerializationUtils; |
||||||
import org.springframework.util.Assert; |
import org.springframework.util.Assert; |
||||||
|
|
||||||
/** |
/** |
||||||
* {@link Validator} implementation based on {@link CriteriaDefinition query expressions}. |
* {@link Validator} implementation based on {@link CriteriaDefinition query expressions}. |
||||||
* |
* |
||||||
* @author Andreas Zink |
* @author Andreas Zink |
||||||
* @author Christoph Strobl |
* @author Christoph Strobl |
||||||
* @since 2.1 |
* @since 2.1 |
||||||
* @see Criteria |
* @see Criteria |
||||||
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/#query-expressions">Schema Validation</a> |
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/#query-expressions">Schema Validation</a> |
||||||
*/ |
*/ |
||||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
||||||
@EqualsAndHashCode |
@EqualsAndHashCode |
||||||
public class CriteriaValidator implements Validator { |
class CriteriaValidator implements Validator { |
||||||
|
|
||||||
private final CriteriaDefinition criteria; |
private final CriteriaDefinition criteria; |
||||||
|
|
||||||
/** |
/** |
||||||
* Creates a new {@link Validator} object, which is basically setup of query operators, based on a |
* Creates a new {@link Validator} object, which is basically setup of query operators, based on a |
||||||
* {@link CriteriaDefinition} instance. |
* {@link CriteriaDefinition} instance. |
||||||
* |
* |
||||||
* @param criteria the criteria to build the {@code validator} from. Must not be {@literal null}. |
* @param criteria the criteria to build the {@code validator} from. Must not be {@literal null}. |
||||||
* @return new instance of {@link CriteriaValidator}. |
* @return new instance of {@link CriteriaValidator}. |
||||||
* @throws IllegalArgumentException when criteria is {@literal null}. |
* @throws IllegalArgumentException when criteria is {@literal null}. |
||||||
*/ |
*/ |
||||||
public static CriteriaValidator of(CriteriaDefinition criteria) { |
static CriteriaValidator of(CriteriaDefinition criteria) { |
||||||
|
|
||||||
Assert.notNull(criteria, "Criteria must not be null!"); |
Assert.notNull(criteria, "Criteria must not be null!"); |
||||||
return new CriteriaValidator(criteria); |
|
||||||
} |
return new CriteriaValidator(criteria); |
||||||
|
} |
||||||
/* |
|
||||||
* (non-Javadoc) |
/* |
||||||
* @see org.springframework.data.mongodb.core.validation.Validator#toDocument() |
* (non-Javadoc) |
||||||
*/ |
* @see org.springframework.data.mongodb.core.validation.Validator#toDocument() |
||||||
@Override |
*/ |
||||||
public Document toDocument() { |
@Override |
||||||
return criteria.getCriteriaObject(); |
public Document toDocument() { |
||||||
} |
return criteria.getCriteriaObject(); |
||||||
|
} |
||||||
/* |
|
||||||
* (non-Javadoc) |
/* |
||||||
* @see java.lang.Object#toString() |
* (non-Javadoc) |
||||||
*/ |
* @see java.lang.Object#toString() |
||||||
@Override |
*/ |
||||||
public String toString() { |
@Override |
||||||
return SerializationUtils.serializeToJsonSafely(toDocument()); |
public String toString() { |
||||||
} |
return SerializationUtils.serializeToJsonSafely(toDocument()); |
||||||
|
} |
||||||
} |
} |
||||||
|
|||||||
@ -1,81 +1,81 @@ |
|||||||
/* |
/* |
||||||
* Copyright 2018 the original author or authors. |
* Copyright 2018 the original author or authors. |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package org.springframework.data.mongodb.core.validation; |
package org.springframework.data.mongodb.core.validation; |
||||||
|
|
||||||
import org.bson.Document; |
import org.bson.Document; |
||||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition; |
import org.springframework.data.mongodb.core.query.CriteriaDefinition; |
||||||
import org.springframework.data.mongodb.core.schema.MongoJsonSchema; |
import org.springframework.data.mongodb.core.schema.MongoJsonSchema; |
||||||
import org.springframework.util.Assert; |
import org.springframework.util.Assert; |
||||||
|
|
||||||
/** |
/** |
||||||
* Provides a {@code validator} object to be used for collection validation via |
* Provides a {@code validator} object to be used for collection validation via |
||||||
* {@link org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions}. |
* {@link org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions}. |
||||||
* |
* |
||||||
* @author Andreas Zink |
* @author Andreas Zink |
||||||
* @author Christoph Strobl |
* @author Christoph Strobl |
||||||
* @since 2.1 |
* @since 2.1 |
||||||
* @see <a href="https://docs.mongodb.com/manual/reference/method/db.createCollection/">MongoDB Collection Options</a> |
* @see <a href="https://docs.mongodb.com/manual/reference/method/db.createCollection/">MongoDB Collection Options</a> |
||||||
*/ |
*/ |
||||||
public interface Validator { |
public interface Validator { |
||||||
|
|
||||||
/** |
/** |
||||||
* Get the {@link Document} containing the validation specific rules. The document may contain fields that may require |
* Get the {@link Document} containing the validation specific rules. The document may contain fields that may require |
||||||
* type and/or field name mapping. |
* type and/or field name mapping. |
||||||
* |
* |
||||||
* @return a MongoDB {@code validator} {@link Document}. Never {@literal null}. |
* @return a MongoDB {@code validator} {@link Document}. Never {@literal null}. |
||||||
*/ |
*/ |
||||||
Document toDocument(); |
Document toDocument(); |
||||||
|
|
||||||
/** |
/** |
||||||
* Creates a basic {@link Validator} checking documents against a given set of rules. |
* Creates a basic {@link Validator} checking documents against a given set of rules. |
||||||
* |
* |
||||||
* @param validationRules must not be {@literal null}. |
* @param validationRules must not be {@literal null}. |
||||||
* @return new instance of {@link Validator}. |
* @return new instance of {@link Validator}. |
||||||
* @throws IllegalArgumentException if validationRules is {@literal null}. |
* @throws IllegalArgumentException if validationRules is {@literal null}. |
||||||
*/ |
*/ |
||||||
static Validator document(Document validationRules) { |
static Validator document(Document validationRules) { |
||||||
|
|
||||||
Assert.notNull(validationRules, "ValidationRules must not be null!"); |
Assert.notNull(validationRules, "ValidationRules must not be null!"); |
||||||
return DocumentValidator.of(validationRules); |
return DocumentValidator.of(validationRules); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Creates a new {@link Validator} checking documents against the structure defined in {@link MongoJsonSchema}. |
* Creates a new {@link Validator} checking documents against the structure defined in {@link MongoJsonSchema}. |
||||||
* |
* |
||||||
* @param schema must not be {@literal null}. |
* @param schema must not be {@literal null}. |
||||||
* @return new instance of {@link Validator}. |
* @return new instance of {@link Validator}. |
||||||
* @throws IllegalArgumentException if schema is {@literal null}. |
* @throws IllegalArgumentException if schema is {@literal null}. |
||||||
*/ |
*/ |
||||||
static Validator schema(MongoJsonSchema schema) { |
static Validator schema(MongoJsonSchema schema) { |
||||||
|
|
||||||
Assert.notNull(schema, "Schema must not be null!"); |
Assert.notNull(schema, "Schema must not be null!"); |
||||||
return JsonSchemaValidator.of(schema); |
return JsonSchemaValidator.of(schema); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Creates a new {@link Validator} checking documents against a given query structure expressed by |
* Creates a new {@link Validator} checking documents against a given query structure expressed by |
||||||
* {@link CriteriaDefinition}. <br /> |
* {@link CriteriaDefinition}. <br /> |
||||||
* |
* |
||||||
* @param criteria must not be {@literal null}. |
* @param criteria must not be {@literal null}. |
||||||
* @return new instance of {@link Validator}. |
* @return new instance of {@link Validator}. |
||||||
* @throws IllegalArgumentException if criteria is {@literal null}. |
* @throws IllegalArgumentException if criteria is {@literal null}. |
||||||
*/ |
*/ |
||||||
static Validator criteria(CriteriaDefinition criteria) { |
static Validator criteria(CriteriaDefinition criteria) { |
||||||
|
|
||||||
Assert.notNull(criteria, "Criteria must not be null!"); |
Assert.notNull(criteria, "Criteria must not be null!"); |
||||||
return CriteriaValidator.of(criteria); |
return CriteriaValidator.of(criteria); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
@ -1,47 +1,47 @@ |
|||||||
/* |
/* |
||||||
* Copyright 2017 the original author or authors. |
* Copyright 2017 the original author or authors. |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package org.springframework.data.mongodb.core.validation; |
package org.springframework.data.mongodb.core.validation; |
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
import static org.assertj.core.api.Assertions.*; |
||||||
|
|
||||||
import org.bson.Document; |
import org.bson.Document; |
||||||
import org.junit.Test; |
import org.junit.Test; |
||||||
import org.springframework.data.mongodb.core.query.Criteria; |
import org.springframework.data.mongodb.core.query.Criteria; |
||||||
|
|
||||||
/** |
/** |
||||||
* Unit tests for {@link CriteriaValidator}. |
* Unit tests for {@link CriteriaValidator}. |
||||||
* |
* |
||||||
* @author Andreas Zink |
* @author Andreas Zink |
||||||
* @author Christoph Strobl |
* @author Christoph Strobl |
||||||
*/ |
*/ |
||||||
public class CriteriaValidatorUnitTests { |
public class CriteriaValidatorUnitTests { |
||||||
|
|
||||||
@Test // DATAMONGO-1322
|
@Test // DATAMONGO-1322
|
||||||
public void testSimpleCriteria() { |
public void testSimpleCriteria() { |
||||||
|
|
||||||
Criteria criteria = Criteria.where("nonNullString").ne(null).type(2).and("rangedInteger").type(16).gte(0).lte(122); |
Criteria criteria = Criteria.where("nonNullString").ne(null).type(2).and("rangedInteger").type(16).gte(0).lte(122); |
||||||
Document validator = CriteriaValidator.of(criteria).toDocument(); |
Document validator = CriteriaValidator.of(criteria).toDocument(); |
||||||
|
|
||||||
assertThat(validator.get("nonNullString")).isEqualTo(new Document("$ne", null).append("$type", 2)); |
assertThat(validator.get("nonNullString")).isEqualTo(new Document("$ne", null).append("$type", 2)); |
||||||
assertThat(validator.get("rangedInteger")) |
assertThat(validator.get("rangedInteger")) |
||||||
.isEqualTo(new Document("$type", 16).append("$gte", 0).append("$lte", 122)); |
.isEqualTo(new Document("$type", 16).append("$gte", 0).append("$lte", 122)); |
||||||
} |
} |
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1322
|
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1322
|
||||||
public void testFailOnNull() { |
public void testFailOnNull() { |
||||||
CriteriaValidator.of(null); |
CriteriaValidator.of(null); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue