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