Skip to content

Commit

Permalink
Add KiwiMongoIndexes (#437)
Browse files Browse the repository at this point in the history
* 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
sleberknight authored Nov 12, 2020
1 parent ad7687b commit 3f9c82c
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 34 deletions.
52 changes: 52 additions & 0 deletions src/main/java/org/kiwiproject/spring/data/KiwiMongoIndexes.java
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)));
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import static java.util.stream.Collectors.toSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.kiwiproject.spring.util.MongoTestHelpers.mongoConnectionString;
import static org.kiwiproject.spring.util.MongoTestHelpers.startInMemoryMongoServer;

import de.bwaldvogel.mongo.MongoServer;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
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.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
Expand All @@ -35,24 +32,15 @@
@DisplayName("MongoRepositoryContext")
class MongoRepositoryContextTest {

private static MongoServer mongoServer;
@RegisterExtension
static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();

private MongoRepositoryContext mongoRepositoryContext;

@BeforeAll
static void beforeAll() {
mongoServer = startInMemoryMongoServer();
}

@AfterAll
static void afterAll() {
mongoServer.shutdownNow();
}

@BeforeEach
void setUp() {
String mongoUri = mongoConnectionString(mongoServer);
mongoRepositoryContext = new MongoRepositoryContext(mongoUri);
var connectionString = MONGO_SERVER_EXTENSION.getConnectionString();
mongoRepositoryContext = new MongoRepositoryContext(connectionString);
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,27 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.kiwiproject.spring.util.MongoTestHelpers.newMongoTemplate;
import static org.kiwiproject.spring.util.MongoTestHelpers.startInMemoryMongoServer;

import de.bwaldvogel.mongo.MongoServer;
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.junit.jupiter.api.extension.RegisterExtension;
import org.kiwiproject.junit.jupiter.MongoServerExtension;
import org.springframework.data.mongodb.core.MongoTemplate;

@DisplayName("KiwiMongoConverters")
class KiwiMongoConvertersTest {

private static MongoServer mongoServer;
@RegisterExtension
static final MongoServerExtension MONGO_SERVER_EXTENSION = new MongoServerExtension();

private MongoTemplate mongoTemplate;

@BeforeAll
static void beforeAll() {
mongoServer = startInMemoryMongoServer();
}

@AfterAll
static void afterAll() {
mongoServer.shutdownNow();
}

@BeforeEach
void setUp() {
mongoTemplate = newMongoTemplate(mongoServer);
mongoTemplate = newMongoTemplate(MONGO_SERVER_EXTENSION.getMongoServer());
}

@Nested
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/org/kiwiproject/spring/data/KiwiMongoIndexesTest.java
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;
}

}

0 comments on commit 3f9c82c

Please sign in to comment.