|
33 | 33 | import org.springframework.dao.DataIntegrityViolationException;
|
34 | 34 | import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
35 | 35 | import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
|
| 36 | +import org.springframework.data.mongodb.core.mapping.Encrypted; |
36 | 37 | import org.springframework.data.mongodb.core.mapping.Field;
|
37 | 38 | import org.springframework.data.mongodb.core.query.Criteria;
|
| 39 | +import org.springframework.data.mongodb.core.schema.MongoJsonSchema; |
38 | 40 | import org.springframework.data.mongodb.test.util.Client;
|
39 | 41 | import org.springframework.data.mongodb.test.util.MongoClientExtension;
|
40 | 42 | import org.springframework.lang.Nullable;
|
|
46 | 48 |
|
47 | 49 | /**
|
48 | 50 | * Integration tests for {@link CollectionOptions#validation(ValidationOptions)} using
|
49 |
| - * {@link org.springframework.data.mongodb.core.validation.CriteriaValidator} and |
50 |
| - * {@link org.springframework.data.mongodb.core.validation.DocumentValidator}. |
| 51 | + * {@link org.springframework.data.mongodb.core.validation.CriteriaValidator}, |
| 52 | + * {@link org.springframework.data.mongodb.core.validation.DocumentValidator} and |
| 53 | + * {@link org.springframework.data.mongodb.core.validation.JsonSchemaValidator}. |
51 | 54 | *
|
52 | 55 | * @author Andreas Zink
|
53 | 56 | * @author Christoph Strobl
|
| 57 | + * @author Julia Lee |
54 | 58 | */
|
55 | 59 | @ExtendWith({ MongoClientExtension.class, SpringExtension.class })
|
56 | 60 | public class MongoTemplateValidationTests {
|
@@ -186,6 +190,20 @@ public void mapsDocumentValidatorFieldsCorrectly() {
|
186 | 190 | assertThat(getValidatorInfo(COLLECTION_NAME)).isEqualTo(new Document("customName", new Document("$type", "bool")));
|
187 | 191 | }
|
188 | 192 |
|
| 193 | + @Test // GH-4454 |
| 194 | + public void failsJsonSchemaValidationForEncryptedDomainEntityProperty() { |
| 195 | + |
| 196 | + MongoJsonSchema schema = MongoJsonSchemaCreator.create().createSchemaFor(BeanWithEncryptedDomainEntity.class); |
| 197 | + template.createCollection(COLLECTION_NAME, CollectionOptions.empty().schema(schema)); |
| 198 | + |
| 199 | + BeanWithEncryptedDomainEntity person = new BeanWithEncryptedDomainEntity(); |
| 200 | + person.encryptedDomainEntity = new SimpleBean("some string", 100, null); |
| 201 | + |
| 202 | + assertThatExceptionOfType(DataIntegrityViolationException.class) |
| 203 | + .isThrownBy(() -> template.save(person)) |
| 204 | + .withMessageContaining("Document failed validation"); |
| 205 | + } |
| 206 | + |
189 | 207 | private Document getCollectionOptions(String collectionName) {
|
190 | 208 | return getCollectionInfo(collectionName).get("options", Document.class);
|
191 | 209 | }
|
@@ -271,4 +289,10 @@ public String toString() {
|
271 | 289 | return "MongoTemplateValidationTests.SimpleBean(nonNullString=" + this.getNonNullString() + ", rangedInteger=" + this.getRangedInteger() + ", customFieldName=" + this.getCustomFieldName() + ")";
|
272 | 290 | }
|
273 | 291 | }
|
| 292 | + |
| 293 | + @org.springframework.data.mongodb.core.mapping.Document(collection = COLLECTION_NAME) |
| 294 | + @Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic") |
| 295 | + static class BeanWithEncryptedDomainEntity { |
| 296 | + @Encrypted SimpleBean encryptedDomainEntity; |
| 297 | + } |
274 | 298 | }
|
0 commit comments