Search in sources :

Example 1 with MongoJsonSchemaMapper

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);
}
Also used : MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) StepVerifier(reactor.test.StepVerifier) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 2 with MongoJsonSchemaMapper

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));
}
Also used : MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 3 with MongoJsonSchemaMapper

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);
}
Also used : MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 4 with MongoJsonSchemaMapper

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);
}
Also used : MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (org.bson.Document)4 Test (org.junit.jupiter.api.Test)4 MongoJsonSchemaMapper (org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper)4 MongoDatabase (com.mongodb.client.MongoDatabase)1 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)1 ValidationOptions (com.mongodb.client.model.ValidationOptions)1 StepVerifier (reactor.test.StepVerifier)1