Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mp911de/a07cd9275769fb1141909d265ad5bd5d to your computer and use it in GitHub Desktop.

Select an option

Save mp911de/a07cd9275769fb1141909d265ad5bd5d to your computer and use it in GitHub Desktop.
Testcase to demonstrate the DATAMONGO-1393 issue
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<mongo:db-factory dbname="validation" />
<mongo:mapping-converter base-package="org.springframework.data.mongodb.core" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingConverter" />
</bean>
</beans>
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.mapping.event;
import javax.validation.ConstraintViolationException;
import javax.validation.constraints.NotNull;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Getter;
import lombok.Setter;
/**
* Integration test case to explore the {@link ValidatingMongoEventListener} behavior when saving {@link DBRef}.
*
* @author Mark Paluch
* @see DATAMONGO-1393
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ValidatingMongoEventListenerLazyDbRefTests {
@Autowired MongoTemplate mongoTemplate;
@Before
public void setUp() {
mongoTemplate.dropCollection(TypeWithReferences.class);
mongoTemplate.dropCollection(WithAnnotatedFieldsOnly.class);
mongoTemplate.dropCollection(WithAnnotatedProperties.class);
}
/**
* Fails because the underlying lazy proxy has uninitialized instance fields.
*/
@Test
public void validationShouldPassUsingAnnotatedFields() {
WithAnnotatedFieldsOnly withAnnotatedFields = new WithAnnotatedFieldsOnly();
withAnnotatedFields.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedFields);
mongoTemplate.save(rootObject.fields);
}
/**
* Fails because of a different reason than {@link WithAnnotatedFieldsOnly#setName(String)}, see
* {@link #validationShouldPassUsingAnnotatedFields}
*/
@Test(expected = ConstraintViolationException.class)
public void validationShouldFailUsingAnnotatedFields() {
WithAnnotatedFieldsOnly withAnnotatedFields = new WithAnnotatedFieldsOnly();
withAnnotatedFields.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedFields);
rootObject.fields.name = null;
mongoTemplate.save(rootObject.fields);
}
/**
* Fails because the underlying lazy proxy has uninitialized instance fields and the validator does not use the
* accessors.
*/
@Test
public void validationShouldPassUsingAnnotatedFieldsHavingAccessors() {
WithAnnotatedFieldsHavingAccessors withAnnotatedFields = new WithAnnotatedFieldsHavingAccessors();
withAnnotatedFields.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedFields);
mongoTemplate.save(rootObject.fieldsHavingAcessors);
}
/**
* Fails because of a different reason than {@link WithAnnotatedFieldsHavingAccessors#setName(String)}, see
* {@link #validationShouldPassUsingAnnotatedFieldsHavingAccessors}
*/
@Test(expected = ConstraintViolationException.class)
public void validationShouldFailUsingAnnotatedFieldsHavingAccessors() {
WithAnnotatedFieldsHavingAccessors withAnnotatedFields = new WithAnnotatedFieldsHavingAccessors();
withAnnotatedFields.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedFields);
rootObject.fieldsHavingAcessors.setName(null);
mongoTemplate.save(rootObject.fieldsHavingAcessors);
}
/**
* All good here.
*/
@Test
public void validationShouldPassUsingAnnotatedProperties() {
WithAnnotatedProperties withAnnotatedProperties = new WithAnnotatedProperties();
withAnnotatedProperties.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedProperties);
mongoTemplate.save(rootObject.properties);
}
/**
* All good here.
*/
@Test(expected = ConstraintViolationException.class)
public void validationShouldFailUsingAnnotatedProperties() {
WithAnnotatedProperties withAnnotatedProperties = new WithAnnotatedProperties();
withAnnotatedProperties.name = "Homer";
TypeWithReferences rootObject = createAndLoadTypeWithReferences(withAnnotatedProperties);
rootObject.properties.setName(null);
mongoTemplate.save(rootObject.properties);
}
private TypeWithReferences createAndLoadTypeWithReferences(WithAnnotatedFieldsOnly withAnnotatedFields) {
TypeWithReferences rootObject = new TypeWithReferences();
rootObject.fields = withAnnotatedFields;
mongoTemplate.insert(withAnnotatedFields);
return createAndLoadTypeWithReferences(rootObject);
}
private TypeWithReferences createAndLoadTypeWithReferences(WithAnnotatedFieldsHavingAccessors havingAccessors) {
TypeWithReferences rootObject = new TypeWithReferences();
rootObject.fieldsHavingAcessors = havingAccessors;
mongoTemplate.insert(havingAccessors);
return createAndLoadTypeWithReferences(rootObject);
}
private TypeWithReferences createAndLoadTypeWithReferences(WithAnnotatedProperties properties) {
TypeWithReferences rootObject = new TypeWithReferences();
rootObject.properties = properties;
mongoTemplate.insert(properties);
return createAndLoadTypeWithReferences(rootObject);
}
private TypeWithReferences createAndLoadTypeWithReferences(TypeWithReferences rootObject) {
mongoTemplate.insert(rootObject);
return mongoTemplate.findById(rootObject.getId(), TypeWithReferences.class);
}
@Getter
@Setter
public static class TypeWithReferences {
ObjectId id;
@DBRef(lazy = true) WithAnnotatedFieldsOnly fields;
@DBRef(lazy = true) WithAnnotatedFieldsHavingAccessors fieldsHavingAcessors;
@DBRef(lazy = true) WithAnnotatedProperties properties;
}
public static class WithAnnotatedFieldsOnly {
ObjectId id;
@NotNull String name;
}
public static class WithAnnotatedFieldsHavingAccessors {
ObjectId id;
@NotNull String name;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class WithAnnotatedProperties {
ObjectId id;
String name;
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment