Skip to content

Commit b6322d7

Browse files
committed
DATAMONGO-374 - Added test case showing custom converters being used for updates.
1 parent 572c4bb commit b6322d7

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2012 the original author or authors.
33
*
4-
* Licensed under t
5-
import org.springframework.util.ResourceUtils;
6-
7-
import org.springframework.data.convert.EntityReader;
8-
he Apache License, Version 2.0 (the "License");
4+
* Licensed under the Apache License, Version 2.0 (the "License");
95
* you may not use this file except in compliance with the License.
106
* You may obtain a copy of the License at
117
*
12-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
139
*
1410
* Unless required by applicable law or agreed to in writing, software
1511
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -830,11 +826,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
830826

831827
DBObject queryObj = query == null ? new BasicDBObject()
832828
: mapper.getMappedObject(query.getQueryObject(), entity);
833-
DBObject updateObj = update.getUpdateObject();
834-
835-
for (String key : updateObj.keySet()) {
836-
updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key)));
837-
}
829+
DBObject updateObj = update == null ? new BasicDBObject() : mapper.getMappedObject(update.getUpdateObject(),
830+
entity);
838831

839832
if (LOGGER.isDebugEnabled()) {
840833
LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj + " in collection: "

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

+54-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.mockito.Matchers.*;
2021
import static org.mockito.Mockito.*;
2122

2223
import java.math.BigInteger;
24+
import java.util.Collections;
2325

2426
import org.bson.types.ObjectId;
2527
import org.junit.Before;
@@ -29,10 +31,16 @@
2931
import org.mockito.Mockito;
3032
import org.mockito.runners.MockitoJUnitRunner;
3133
import org.springframework.context.support.GenericApplicationContext;
34+
import org.springframework.core.convert.converter.Converter;
3235
import org.springframework.dao.DataAccessException;
3336
import org.springframework.dao.InvalidDataAccessApiUsageException;
3437
import org.springframework.data.annotation.Id;
38+
import org.springframework.data.mongodb.MongoDbFactory;
39+
import org.springframework.data.mongodb.core.convert.CustomConversions;
3540
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
41+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
42+
import org.springframework.data.mongodb.core.query.Query;
43+
import org.springframework.data.mongodb.core.query.Update;
3644
import org.springframework.test.util.ReflectionTestUtils;
3745

3846
import com.mongodb.DB;
@@ -51,20 +59,24 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
5159

5260
MongoTemplate template;
5361

62+
@Mock
63+
MongoDbFactory factory;
5464
@Mock
5565
Mongo mongo;
56-
5766
@Mock
5867
DB db;
59-
6068
@Mock
6169
DBCollection collection;
6270

71+
MappingMongoConverter converter;
72+
6373
@Before
6474
public void setUp() {
65-
this.template = new MongoTemplate(mongo, "database");
6675

67-
when(mongo.getDB("database")).thenReturn(db);
76+
this.converter = new MappingMongoConverter(factory, new MongoMappingContext());
77+
this.template = new MongoTemplate(factory, converter);
78+
79+
when(factory.getDb()).thenReturn(db);
6880
when(db.getCollection(Mockito.any(String.class))).thenReturn(collection);
6981
}
7082

@@ -126,6 +138,8 @@ public void storesEntityWithSetIdAlthoughNotAutogenerateable() {
126138
@Test
127139
public void autogeneratesIdForEntityWithAutogeneratableId() {
128140

141+
this.converter.afterPropertiesSet();
142+
129143
MongoTemplate template = spy(this.template);
130144
doReturn(new ObjectId()).when(template).saveDBObject(Mockito.any(String.class), Mockito.any(DBObject.class),
131145
Mockito.any(Class.class));
@@ -136,6 +150,27 @@ public void autogeneratesIdForEntityWithAutogeneratableId() {
136150
assertThat(entity.id, is(notNullValue()));
137151
}
138152

153+
/**
154+
* @see DATAMONGO-374
155+
*/
156+
@Test
157+
public void convertsUpdateConstraintsUsingConverters() {
158+
159+
CustomConversions conversions = new CustomConversions(Collections.singletonList(MyConverter.INSTANCE));
160+
this.converter.setCustomConversions(conversions);
161+
this.converter.afterPropertiesSet();
162+
163+
Query query = new Query();
164+
Update update = new Update().set("foo", new AutogenerateableId());
165+
166+
template.updateFirst(query, update, Wrapper.class);
167+
168+
QueryMapper queryMapper = new QueryMapper(converter);
169+
DBObject reference = queryMapper.getMappedObject(update.getUpdateObject(), null);
170+
171+
verify(collection, times(1)).update(Mockito.any(DBObject.class), eq(reference), anyBoolean(), anyBoolean());
172+
}
173+
139174
class AutogenerateableId {
140175

141176
@Id
@@ -148,6 +183,20 @@ class NotAutogenerateableId {
148183
Integer id;
149184
}
150185

186+
enum MyConverter implements Converter<AutogenerateableId, String> {
187+
188+
INSTANCE;
189+
190+
public String convert(AutogenerateableId source) {
191+
return source.toString();
192+
}
193+
}
194+
195+
class Wrapper {
196+
197+
AutogenerateableId foo;
198+
}
199+
151200
/**
152201
* Mocks out the {@link MongoTemplate#getDb()} method to return the {@link DB} mock instead of executing the actual
153202
* behaviour.

0 commit comments

Comments
 (0)