Add integration test to ensure schema validation fails when domain type property values are not encrypted as expected.
Closes #4454 Original Pull Request: #4455
This commit is contained in:
@@ -35,8 +35,10 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
||||||
import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
|
import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.Encrypted;
|
||||||
import org.springframework.data.mongodb.core.mapping.Field;
|
import org.springframework.data.mongodb.core.mapping.Field;
|
||||||
import org.springframework.data.mongodb.core.query.Criteria;
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
|
||||||
import org.springframework.data.mongodb.test.util.Client;
|
import org.springframework.data.mongodb.test.util.Client;
|
||||||
import org.springframework.data.mongodb.test.util.MongoClientExtension;
|
import org.springframework.data.mongodb.test.util.MongoClientExtension;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
@@ -48,11 +50,13 @@ import com.mongodb.client.model.ValidationLevel;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@link CollectionOptions#validation(ValidationOptions)} using
|
* Integration tests for {@link CollectionOptions#validation(ValidationOptions)} using
|
||||||
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator} and
|
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator},
|
||||||
* {@link org.springframework.data.mongodb.core.validation.DocumentValidator}.
|
* {@link org.springframework.data.mongodb.core.validation.DocumentValidator} and
|
||||||
|
* {@link org.springframework.data.mongodb.core.validation.JsonSchemaValidator}.
|
||||||
*
|
*
|
||||||
* @author Andreas Zink
|
* @author Andreas Zink
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
|
* @author Julia Lee
|
||||||
*/
|
*/
|
||||||
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
|
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
|
||||||
public class MongoTemplateValidationTests {
|
public class MongoTemplateValidationTests {
|
||||||
@@ -188,6 +192,20 @@ public class MongoTemplateValidationTests {
|
|||||||
assertThat(getValidatorInfo(COLLECTION_NAME)).isEqualTo(new Document("customName", new Document("$type", "bool")));
|
assertThat(getValidatorInfo(COLLECTION_NAME)).isEqualTo(new Document("customName", new Document("$type", "bool")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // GH-4454
|
||||||
|
public void failsJsonSchemaValidationForEncryptedDomainEntityProperty() {
|
||||||
|
|
||||||
|
MongoJsonSchema schema = MongoJsonSchemaCreator.create().createSchemaFor(BeanWithEncryptedDomainEntity.class);
|
||||||
|
template.createCollection(COLLECTION_NAME, CollectionOptions.empty().schema(schema));
|
||||||
|
|
||||||
|
BeanWithEncryptedDomainEntity person = new BeanWithEncryptedDomainEntity();
|
||||||
|
person.encryptedDomainEntity = new SimpleBean("some string", 100, null);
|
||||||
|
|
||||||
|
assertThatExceptionOfType(DataIntegrityViolationException.class)
|
||||||
|
.isThrownBy(() -> template.save(person))
|
||||||
|
.withMessageContaining("Document failed validation");
|
||||||
|
}
|
||||||
|
|
||||||
private Document getCollectionOptions(String collectionName) {
|
private Document getCollectionOptions(String collectionName) {
|
||||||
return getCollectionInfo(collectionName).get("options", Document.class);
|
return getCollectionInfo(collectionName).get("options", Document.class);
|
||||||
}
|
}
|
||||||
@@ -222,4 +240,10 @@ public class MongoTemplateValidationTests {
|
|||||||
private @Nullable Integer rangedInteger;
|
private @Nullable Integer rangedInteger;
|
||||||
private @Field("customName") Object customFieldName;
|
private @Field("customName") Object customFieldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@org.springframework.data.mongodb.core.mapping.Document(collection = COLLECTION_NAME)
|
||||||
|
@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
|
||||||
|
static class BeanWithEncryptedDomainEntity {
|
||||||
|
@Encrypted SimpleBean encryptedDomainEntity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user