|
35 | 35 | import org.springframework.dao.DataIntegrityViolationException; |
36 | 36 | import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; |
37 | 37 | import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions; |
| 38 | +import org.springframework.data.mongodb.core.mapping.Encrypted; |
38 | 39 | import org.springframework.data.mongodb.core.mapping.Field; |
39 | 40 | import org.springframework.data.mongodb.core.query.Criteria; |
| 41 | +import org.springframework.data.mongodb.core.schema.MongoJsonSchema; |
40 | 42 | import org.springframework.data.mongodb.test.util.Client; |
41 | 43 | import org.springframework.data.mongodb.test.util.MongoClientExtension; |
42 | 44 | import org.springframework.lang.Nullable; |
|
48 | 50 |
|
49 | 51 | /** |
50 | 52 | * 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}. |
53 | 56 | * |
54 | 57 | * @author Andreas Zink |
55 | 58 | * @author Christoph Strobl |
| 59 | + * @author Julia Lee |
56 | 60 | */ |
57 | 61 | @ExtendWith({ MongoClientExtension.class, SpringExtension.class }) |
58 | 62 | public class MongoTemplateValidationTests { |
@@ -188,6 +192,20 @@ public void mapsDocumentValidatorFieldsCorrectly() { |
188 | 192 | assertThat(getValidatorInfo(COLLECTION_NAME)).isEqualTo(new Document("customName", new Document("$type", "bool"))); |
189 | 193 | } |
190 | 194 |
|
| 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 | + |
191 | 209 | private Document getCollectionOptions(String collectionName) { |
192 | 210 | return getCollectionInfo(collectionName).get("options", Document.class); |
193 | 211 | } |
@@ -222,4 +240,10 @@ static class SimpleBean { |
222 | 240 | private @Nullable Integer rangedInteger; |
223 | 241 | private @Field("customName") Object customFieldName; |
224 | 242 | } |
| 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 | + } |
225 | 249 | } |
0 commit comments