Skip to content

Commit b06e8d5

Browse files
committed
Add integration test to ensure schema validation fails when domain type property values are not encrypted as expected.
Closes #4454 Original Pull Request: #4455
1 parent bb65692 commit b06e8d5

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateValidationTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import org.springframework.dao.DataIntegrityViolationException;
3636
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
3737
import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
38+
import org.springframework.data.mongodb.core.mapping.Encrypted;
3839
import org.springframework.data.mongodb.core.mapping.Field;
3940
import org.springframework.data.mongodb.core.query.Criteria;
41+
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
4042
import org.springframework.data.mongodb.test.util.Client;
4143
import org.springframework.data.mongodb.test.util.MongoClientExtension;
4244
import org.springframework.lang.Nullable;
@@ -48,11 +50,13 @@
4850

4951
/**
5052
* Integration tests for {@link CollectionOptions#validation(ValidationOptions)} using
51-
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator} and
52-
* {@link org.springframework.data.mongodb.core.validation.DocumentValidator}.
53+
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator},
54+
* {@link org.springframework.data.mongodb.core.validation.DocumentValidator} and
55+
* {@link org.springframework.data.mongodb.core.validation.JsonSchemaValidator}.
5356
*
5457
* @author Andreas Zink
5558
* @author Christoph Strobl
59+
* @author Julia Lee
5660
*/
5761
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
5862
public class MongoTemplateValidationTests {
@@ -188,6 +192,20 @@ public void mapsDocumentValidatorFieldsCorrectly() {
188192
assertThat(getValidatorInfo(COLLECTION_NAME)).isEqualTo(new Document("customName", new Document("$type", "bool")));
189193
}
190194

195+
@Test // GH-4454
196+
public void failsJsonSchemaValidationForEncryptedDomainEntityProperty() {
197+
198+
MongoJsonSchema schema = MongoJsonSchemaCreator.create().createSchemaFor(BeanWithEncryptedDomainEntity.class);
199+
template.createCollection(COLLECTION_NAME, CollectionOptions.empty().schema(schema));
200+
201+
BeanWithEncryptedDomainEntity person = new BeanWithEncryptedDomainEntity();
202+
person.encryptedDomainEntity = new SimpleBean("some string", 100, null);
203+
204+
assertThatExceptionOfType(DataIntegrityViolationException.class)
205+
.isThrownBy(() -> template.save(person))
206+
.withMessageContaining("Document failed validation");
207+
}
208+
191209
private Document getCollectionOptions(String collectionName) {
192210
return getCollectionInfo(collectionName).get("options", Document.class);
193211
}
@@ -222,4 +240,10 @@ static class SimpleBean {
222240
private @Nullable Integer rangedInteger;
223241
private @Field("customName") Object customFieldName;
224242
}
243+
244+
@org.springframework.data.mongodb.core.mapping.Document(collection = COLLECTION_NAME)
245+
@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
246+
static class BeanWithEncryptedDomainEntity {
247+
@Encrypted SimpleBean encryptedDomainEntity;
248+
}
225249
}

0 commit comments

Comments
 (0)