-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KiwiMongoConverters utility class (#435)
* Add KiwiMongoConverters and test * Add Spring Data MongoDB to the POM. Since our service code is currently all using the 2.x series, we are only updating to the latest 2.x version, which is 2.2.11.RELEASE as of 2020-11-12 * Add mongo-java-server as test scope dependency Fixes #410
- Loading branch information
1 parent
a4745ab
commit 7f09f1e
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/org/kiwiproject/spring/data/KiwiMongoConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.kiwiproject.spring.data; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.common.collect.Lists; | ||
import lombok.experimental.UtilityClass; | ||
import org.bson.BsonUndefined; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.CustomConversions; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; | ||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions; | ||
|
||
/** | ||
* A few utilities related to Spring Data Mongo and custom {@link Converter}s. | ||
*/ | ||
@UtilityClass | ||
public class KiwiMongoConverters { | ||
|
||
/** | ||
* Create a new {@link BsonUndefinedToNullStringConverter}. | ||
* | ||
* @return new BsonUndefinedToNullStringConverter instance | ||
*/ | ||
public static Converter<BsonUndefined, String> newBsonUndefinedToNullObjectConverter() { | ||
return new BsonUndefinedToNullStringConverter(); | ||
} | ||
|
||
/** | ||
* Adds one or more custom converters to a {@link MongoTemplate} instance. | ||
* <p> | ||
* The MongoTemplate is assumed to have a {@link Converter} of type {@link MappingMongoConverter}. | ||
* | ||
* @param template the MongoTemplate | ||
* @param converters the converters to add to MongoTemplate's existing converter | ||
* @throws IllegalArgumentException if the given MongoTemplate's {@link Converter} is <em>not</em> | ||
* a {@link MappingMongoConverter} | ||
* @see org.springframework.data.mongodb.core.convert.AbstractMongoConverter#setCustomConversions(CustomConversions) | ||
*/ | ||
public static MongoTemplate addCustomConverters(MongoTemplate template, Converter<?, ?>... converters) { | ||
checkArgument(template.getConverter() instanceof MappingMongoConverter, | ||
"Currently a MappingMongoConverter is required as the MongoTemplate's converter"); | ||
|
||
var converter = (MappingMongoConverter) template.getConverter(); | ||
converter.setCustomConversions(new MongoCustomConversions(Lists.newArrayList(converters))); | ||
converter.afterPropertiesSet(); | ||
|
||
return template; | ||
} | ||
|
||
/** | ||
* A {@link Converter} that maps from the JS 'undefined' type to a 'null' value. | ||
*/ | ||
public static class BsonUndefinedToNullStringConverter implements Converter<BsonUndefined, String> { | ||
|
||
@SuppressWarnings("NullableProblems") | ||
@Override | ||
public String convert(BsonUndefined bsonUndefined) { | ||
return null; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/org/kiwiproject/spring/data/KiwiMongoConvertersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.kiwiproject.spring.data; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.kiwiproject.base.KiwiStrings.f; | ||
|
||
import de.bwaldvogel.mongo.MongoServer; | ||
import de.bwaldvogel.mongo.backend.memory.MemoryBackend; | ||
import org.bson.BsonUndefined; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory; | ||
|
||
@DisplayName("KiwiMongoConverters") | ||
class KiwiMongoConvertersTest { | ||
|
||
private static MongoServer mongoServer; | ||
|
||
private MongoTemplate mongoTemplate; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
mongoServer = startInMemoryMongoServer(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
mongoServer.shutdownNow(); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
var addr = mongoServer.getLocalAddress(); | ||
var connectionString = f("mongodb://{}:{}/kiwi_mongo_converters_test", addr.getHostName(), addr.getPort()); | ||
var factory = new SimpleMongoClientDbFactory(connectionString); | ||
mongoTemplate = new MongoTemplate(factory); | ||
} | ||
|
||
@Nested | ||
class AddCustomConverters { | ||
|
||
@Test | ||
void shouldAddConverters() { | ||
var converter = KiwiMongoConverters.newBsonUndefinedToNullObjectConverter(); | ||
KiwiMongoConverters.addCustomConverters(mongoTemplate, converter); | ||
|
||
var bsonUndefined = new BsonUndefined(); | ||
var result = mongoTemplate.getConverter().getConversionService().convert(bsonUndefined, String.class); | ||
assertThat(result).isNull(); | ||
} | ||
} | ||
|
||
@Nested | ||
class BsonUndefinedToNullStringConverter { | ||
|
||
@Test | ||
void shouldConvertToNull() { | ||
var converter = KiwiMongoConverters.newBsonUndefinedToNullObjectConverter(); | ||
assertThat(converter.convert(new BsonUndefined())).isNull(); | ||
} | ||
} | ||
|
||
|
||
private static MongoServer startInMemoryMongoServer() { | ||
var mongoServer = new MongoServer(new MemoryBackend()); | ||
mongoServer.bind(); | ||
return mongoServer; | ||
} | ||
|
||
} |