use of org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper in project spring-data-mongodb by spring-projects.
the class ReactiveMongoJsonSchemaTests method writeSchemaViaTemplate.
// DATAMONGO-1835
@Test
public void writeSchemaViaTemplate() {
MongoJsonSchema schema = //
MongoJsonSchema.builder().required("firstname", //
"lastname").properties(//
JsonSchemaProperty.string("firstname").possibleValues("luke", "han").maxLength(10), //
JsonSchemaProperty.object("address").properties(JsonSchemaProperty.string("postCode").minLength(4).maxLength(5))).build();
template.createCollection(Person.class, CollectionOptions.empty().schema(schema)).as(StepVerifier::create).expectNextCount(1).verifyComplete();
Document $jsonSchema = new MongoJsonSchemaMapper(template.getConverter()).mapSchema(schema.toDocument(), Person.class);
Document fromDb = readSchemaFromDatabase("persons");
assertThat(fromDb).isEqualTo($jsonSchema);
}
use of org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper in project spring-data-mongodb by spring-projects.
the class MongoJsonSchemaTests method nonMappedSchema.
// DATAMONGO-1835
@Test
public void nonMappedSchema() {
MongoJsonSchema schema = //
MongoJsonSchema.builder().required("firstname", //
"lastname").properties(//
JsonSchemaProperty.string("firstname").possibleValues("luke", "han").maxLength(10), //
JsonSchemaProperty.object("address").properties(JsonSchemaProperty.string("postCode").minLength(4).maxLength(5))).build();
template.createCollection("persons", CollectionOptions.empty().schema(schema));
Document fromDb = readSchemaFromDatabase("persons");
assertThat(fromDb).isNotEqualTo(new MongoJsonSchemaMapper(template.getConverter()).mapSchema(schema.toDocument(), Person.class));
}
use of org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper in project spring-data-mongodb by spring-projects.
the class MongoJsonSchemaTests method writeSchemaManually.
// DATAMONGO-1835
@Test
public void writeSchemaManually() {
MongoJsonSchema schema = //
MongoJsonSchema.builder().required("firstname", //
"lastname").properties(//
JsonSchemaProperty.string("firstname").possibleValues("luke", "han").maxLength(10), //
JsonSchemaProperty.object("address").properties(JsonSchemaProperty.string("postCode").minLength(4).maxLength(5))).build();
Document $jsonSchema = new MongoJsonSchemaMapper(template.getConverter()).mapSchema(schema.toDocument(), Person.class);
ValidationOptions options = new ValidationOptions();
options.validationLevel(ValidationLevel.MODERATE);
options.validationAction(ValidationAction.ERROR);
options.validator($jsonSchema);
CreateCollectionOptions cco = new CreateCollectionOptions();
cco.validationOptions(options);
MongoDatabase db = template.getDb();
db.createCollection("persons", cco);
Document fromDb = readSchemaFromDatabase("persons");
assertThat(fromDb).isEqualTo($jsonSchema);
}
use of org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper in project spring-data-mongodb by spring-projects.
the class MongoJsonSchemaTests method writeSchemaViaTemplate.
// DATAMONGO-1835
@Test
public void writeSchemaViaTemplate() {
MongoJsonSchema schema = //
MongoJsonSchema.builder().required("firstname", //
"lastname").properties(//
JsonSchemaProperty.string("firstname").possibleValues("luke", "han").maxLength(10), //
JsonSchemaProperty.object("address").properties(JsonSchemaProperty.string("postCode").minLength(4).maxLength(5))).build();
template.createCollection(Person.class, CollectionOptions.empty().schema(schema));
Document $jsonSchema = new MongoJsonSchemaMapper(template.getConverter()).mapSchema(schema.toDocument(), Person.class);
Document fromDb = readSchemaFromDatabase("persons");
assertThat(fromDb).isEqualTo($jsonSchema);
}
Aggregations