Minor cleanup with tests
Added converter on saving in db user
This commit is contained in:
parent
3e4f69fb74
commit
236b808d05
|
@ -0,0 +1,12 @@
|
||||||
|
package org.baeldung.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface CascadeSave {
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,16 @@
|
||||||
package org.baeldung.config;
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
import org.baeldung.converter.UserWriterConverter;
|
||||||
import org.baeldung.event.CascadingMongoEventListener;
|
import org.baeldung.event.CascadingMongoEventListener;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||||
|
import org.springframework.data.mongodb.core.convert.CustomConversions;
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||||
|
|
||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
|
@ -13,6 +20,8 @@ import com.mongodb.MongoClient;
|
||||||
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
||||||
public class MongoConfig extends AbstractMongoConfiguration {
|
public class MongoConfig extends AbstractMongoConfiguration {
|
||||||
|
|
||||||
|
private List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getDatabaseName() {
|
protected String getDatabaseName() {
|
||||||
return "test";
|
return "test";
|
||||||
|
@ -32,4 +41,10 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
||||||
public CascadingMongoEventListener cascadingMongoEventListener(){
|
public CascadingMongoEventListener cascadingMongoEventListener(){
|
||||||
return new CascadingMongoEventListener();
|
return new CascadingMongoEventListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CustomConversions customConversions() {
|
||||||
|
converters.add(new UserWriterConverter());
|
||||||
|
return new CustomConversions(converters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.baeldung.converter;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.baeldung.model.User;
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DBObject;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserWriterConverter implements Converter<User, DBObject> {
|
||||||
|
@Override
|
||||||
|
public DBObject convert(User user) {
|
||||||
|
DBObject dbObject = new BasicDBObject();
|
||||||
|
dbObject.put("name", user.getName());
|
||||||
|
dbObject.put("age", user.getAge());
|
||||||
|
if (user.getEmailAddress() != null) {
|
||||||
|
DBObject emailDbObject = new BasicDBObject();
|
||||||
|
emailDbObject.put("value", user.getEmailAddress().getValue());
|
||||||
|
dbObject.put("email", emailDbObject);
|
||||||
|
}
|
||||||
|
dbObject.removeField("_class");
|
||||||
|
return dbObject;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.baeldung.event;
|
||||||
|
|
||||||
|
import org.baeldung.annotation.CascadeSave;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
public class CascadingMongoEventListener extends AbstractMongoEventListener<Object> {
|
||||||
|
@Autowired
|
||||||
|
private MongoOperations mongoOperations;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBeforeConvert(final Object source) {
|
||||||
|
ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
|
||||||
|
|
||||||
|
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
ReflectionUtils.makeAccessible(field);
|
||||||
|
|
||||||
|
if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
|
||||||
|
final Object fieldValue = field.get(source);
|
||||||
|
|
||||||
|
if (fieldValue != null) {
|
||||||
|
DbRefFieldCallback callback = new DbRefFieldCallback();
|
||||||
|
|
||||||
|
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
|
||||||
|
|
||||||
|
mongoOperations.save(fieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package org.baeldung.event;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
public class DbRefFieldCallback implements ReflectionUtils.FieldCallback {
|
||||||
|
private boolean idFound;
|
||||||
|
|
||||||
|
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
ReflectionUtils.makeAccessible(field);
|
||||||
|
|
||||||
|
if (field.isAnnotationPresent(Id.class)) {
|
||||||
|
idFound = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIdFound() {
|
||||||
|
return idFound;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package org.baeldung.model;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
@Document
|
||||||
|
public class EmailAddress {
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public EmailAddress(){
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmailAddress(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,11 +20,21 @@ public class User {
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private Integer age;
|
private Integer age;
|
||||||
|
|
||||||
@DBRef
|
@DBRef
|
||||||
@Field("email")
|
@Field("email")
|
||||||
@CascadeSave
|
@CascadeSave
|
||||||
|
|
||||||
private EmailAddress emailAddress;
|
private EmailAddress emailAddress;
|
||||||
|
|
||||||
|
public User(){
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User(String name, Integer age, String value){
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.emailAddress = new EmailAddress(value);
|
||||||
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|
|
@ -34,7 +34,9 @@ public class MongoTemplateQueryIntegrationTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void testSetup() {
|
public void testSetup() {
|
||||||
mongoTemplate.createCollection(User.class);
|
if (!mongoTemplate.collectionExists(User.class)) {
|
||||||
|
mongoTemplate.createCollection(User.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@ -148,7 +150,7 @@ public class MongoTemplateQueryIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExisted() {
|
public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName("Brendan");
|
user.setName("Brendan");
|
||||||
EmailAddress emailAddress = new EmailAddress();
|
EmailAddress emailAddress = new EmailAddress();
|
||||||
|
|
|
@ -17,7 +17,9 @@ public class BaseQueryIntegrationTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void testSetup() {
|
public void testSetup() {
|
||||||
mongoOps.createCollection(User.class);
|
if (!mongoOps.collectionExists(User.class)) {
|
||||||
|
mongoOps.createCollection(User.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
|
@ -34,7 +34,9 @@ public class UserRepositoryIntegrationTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void testSetup() {
|
public void testSetup() {
|
||||||
mongoOps.createCollection(User.class);
|
if (!mongoOps.collectionExists(User.class)) {
|
||||||
|
mongoOps.createCollection(User.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@ -67,12 +69,11 @@ public class UserRepositoryIntegrationTest {
|
||||||
mongoOps.insert(user);
|
mongoOps.insert(user);
|
||||||
|
|
||||||
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class);
|
||||||
final String id = user.getId();
|
|
||||||
|
|
||||||
user.setName("Jim");
|
user.setName("Jim");
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim"));
|
assertThat(mongoOps.findAll(User.class).size(), is(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue