-
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 KiwiMongoIndexes and test * Extract common MongoServer test code into MongoServerExtension, which is a JUnit Jupiter extension * Retrofit MongoRepositoryContextTest and KiwiMongoConvertersTest with MongoServerExtension Fixes #411
- Loading branch information
1 parent
ad7687b
commit 3f9c82c
Showing
5 changed files
with
195 additions
and
34 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/org/kiwiproject/spring/data/KiwiMongoIndexes.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,52 @@ | ||
package org.kiwiproject.spring.data; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.index.Index; | ||
import org.springframework.data.mongodb.core.index.IndexDefinition; | ||
|
||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Utilities related to Mongo indexes. | ||
*/ | ||
@UtilityClass | ||
public class KiwiMongoIndexes { | ||
|
||
/** | ||
* Ensure indexes exist for the given collection (defined by {@code clazz}), sort direction, and properties. | ||
* | ||
* @param mongoTemplate the MongoTemplate instance | ||
* @param clazz the entity/document class representing the mapped MongoDB collection | ||
* @param direction the sort direction for all specified properties | ||
* @param properties the properties for which to add an index | ||
* @see org.springframework.data.mongodb.core.index.IndexOperations#ensureIndex(IndexDefinition) | ||
*/ | ||
public static void ensureIndices(MongoTemplate mongoTemplate, | ||
Class<?> clazz, | ||
Sort.Direction direction, | ||
String... properties) { | ||
|
||
Stream.of(properties).forEach(prop -> | ||
mongoTemplate.indexOps(clazz).ensureIndex(new Index(prop, direction))); | ||
} | ||
|
||
/** | ||
* Ensure indexes exist for the given collection, sort direction, and properties. | ||
* | ||
* @param mongoTemplate the MongoTemplate instance | ||
* @param collectionName the MongoDB collection name | ||
* @param direction the sort direction for all specified properties | ||
* @param properties the properties for which to add an index | ||
* @see org.springframework.data.mongodb.core.index.IndexOperations#ensureIndex(IndexDefinition) | ||
*/ | ||
public static void ensureIndices(MongoTemplate mongoTemplate, | ||
String collectionName, | ||
Sort.Direction direction, | ||
String... properties) { | ||
|
||
Stream.of(properties).forEach(prop -> | ||
mongoTemplate.indexOps(collectionName).ensureIndex(new Index(prop, direction))); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/org/kiwiproject/junit/jupiter/MongoServerExtension.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,30 @@ | ||
package org.kiwiproject.junit.jupiter; | ||
|
||
import static org.kiwiproject.spring.util.MongoTestHelpers.mongoConnectionString; | ||
import static org.kiwiproject.spring.util.MongoTestHelpers.startInMemoryMongoServer; | ||
|
||
import de.bwaldvogel.mongo.MongoServer; | ||
import lombok.Getter; | ||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
public class MongoServerExtension implements BeforeAllCallback, AfterAllCallback { | ||
|
||
@Getter | ||
private MongoServer mongoServer; | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) { | ||
mongoServer = startInMemoryMongoServer(); | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext context) { | ||
mongoServer.shutdownNow(); | ||
} | ||
|
||
public String getConnectionString() { | ||
return mongoConnectionString(mongoServer); | ||
} | ||
} |
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
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
102 changes: 102 additions & 0 deletions
102
src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.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,102 @@ | ||
package org.kiwiproject.spring.data; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.tuple; | ||
import static org.kiwiproject.spring.util.MongoTestHelpers.newMongoTemplate; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
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.junit.jupiter.api.extension.RegisterExtension; | ||
import org.kiwiproject.junit.jupiter.MongoServerExtension; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.index.IndexInfo; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.util.Collection; | ||
|
||
@DisplayName("KiwiMongoIndexes") | ||
class KiwiMongoIndexesTest { | ||
|
||
@RegisterExtension | ||
static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension(); | ||
|
||
private static final String PEOPLE_COLLECTION_NAME = "people"; | ||
|
||
private MongoTemplate mongoTemplate; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mongoTemplate = newMongoTemplate(MONGO_SERVER_EXTENSION.getMongoServer()); | ||
|
||
mongoTemplate.createCollection(PEOPLE_COLLECTION_NAME); | ||
} | ||
|
||
@Nested | ||
class EnsureIndices { | ||
|
||
private String[] properties; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
properties = new String[]{"lastName", "zipCode"}; | ||
} | ||
|
||
@Test | ||
void shouldAcceptDocumentClass() { | ||
KiwiMongoIndexes.ensureIndices(mongoTemplate, Person.class, Sort.Direction.ASC, properties); | ||
|
||
assertIndices(Sort.Direction.ASC); | ||
} | ||
|
||
@Test | ||
void shouldAcceptCollectionName() { | ||
KiwiMongoIndexes.ensureIndices(mongoTemplate, PEOPLE_COLLECTION_NAME, Sort.Direction.DESC, properties); | ||
|
||
assertIndices(Sort.Direction.DESC); | ||
} | ||
|
||
private void assertIndices(Sort.Direction direction) { | ||
var indexInfos = mongoTemplate.indexOps(PEOPLE_COLLECTION_NAME).getIndexInfo(); | ||
|
||
var indexFields = indexInfos.stream() | ||
.map(IndexInfo::getIndexFields) | ||
.flatMap(Collection::stream) | ||
.collect(toList()); | ||
|
||
assertThat(indexFields) | ||
.extracting("key", "direction") | ||
.contains( | ||
tuple("lastName", direction), | ||
tuple("zipCode", direction) | ||
); | ||
} | ||
} | ||
|
||
@Document(collection = PEOPLE_COLLECTION_NAME) | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
private static class Person { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String firstName; | ||
private String lastName; | ||
|
||
private String street1; | ||
private String street2; | ||
private String city; | ||
private String state; | ||
private String zipCode; | ||
} | ||
|
||
} |