Skip to content

Commit

Permalink
Added test base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Feb 28, 2018
1 parent bf2169f commit db62a9b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 76 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -30,6 +30,6 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
compile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
@@ -0,0 +1,145 @@
/*
* MongoConfiguration.java
*
* Created on 2018-02-27, 7:02
*/
package com.marcnuri.spring.mongo.customrepository;

import com.marcnuri.spring.mongo.customrepository.book.BookRepository;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.*;
import de.flapdoodle.embed.mongo.distribution.Version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.io.IOException;

/**
* Mongo Spring configuration that creates a MongoDB process listening in a random port
* and attaches the {@link Mongo} connection to this process.
* <p>
* Created by Marc Nuri <marc@marcnuri.com> on 2018-02-27.
*/
@Configuration
@EnableMongoRepositories(
basePackageClasses = {
BookRepository.class
})
@Import({MongoProperties.class, EmbeddedMongoProperties.class})
public class EmbeddedMongoConfiguration extends AbstractMongoConfiguration {

//**************************************************************************************************
// Fields
//**************************************************************************************************
private static final String DATABASE_NAME = "marcnuri_demo";

private Mongo mongo;
private final Environment environment;
private final MongoProperties properties;
private MongoClientOptions options;


//**************************************************************************************************
// Constructors
//**************************************************************************************************
@Autowired
public EmbeddedMongoConfiguration(Environment environment,
MongoProperties properties) {
this.environment = environment;
this.properties = properties;
}

//**************************************************************************************************
// Abstract Methods
//**************************************************************************************************

//**************************************************************************************************
// Overridden Methods
//**************************************************************************************************
@Override
protected String getDatabaseName() {
return DATABASE_NAME;
}

@Override
public Mongo mongo() throws Exception {
return mongo;
}

//**************************************************************************************************
// Other Methods
//**************************************************************************************************

@Bean(destroyMethod = "close")
@Primary
public MongoClient mongo(MongodProcess mongodProcess) throws Exception {
Net net = mongodProcess.getConfig().net();
properties.setHost(net.getServerAddress().getHostName());
properties.setPort(net.getPort());
return properties.createMongoClient(this.options, this.environment);

}

@Bean(destroyMethod = "stop")
public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException {
return mongodExecutable.start();
}

@Bean(destroyMethod = "stop")
public MongodExecutable mongodExecutable(MongodStarter mongodStarter, IMongodConfig mongodConfig) throws IOException {
return mongodStarter.prepare(mongodConfig);
}

@Bean
public IMongodConfig mongodConfig() throws IOException {
// https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/166
// Storage engine fixes slow runs on TeamCity
return new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.cmdOptions(
new MongoCmdOptionsBuilder()
.useStorageEngine("mmapv1")
.build()
)
.build();
}

@Bean
public MongodStarter mongodStarter() {
return MongodStarter.getDefaultInstance();
}

//**************************************************************************************************
// Getter/Setter Methods
//**************************************************************************************************
@Autowired(required = false)
public void setOptions(MongoClientOptions options) {
this.options = options;
}

@Autowired
public void setMongo(Mongo mongo) {
this.mongo = mongo;
}
//**************************************************************************************************
// Static Methods
//**************************************************************************************************

//**************************************************************************************************
// Inner Classes
//**************************************************************************************************

}

This file was deleted.

Expand Up @@ -7,7 +7,7 @@
/**
* Created by Marc Nuri <marc@marcnuri.com> on 2018-02-27.
*/
public interface BookRepository extends MongoRepository<Book, String>, BookReposiotryCustom {
public interface BookRepository extends MongoRepository<Book, String>, BookRepositoryCustom {

List<Book> findByTitleContainingOrderByTitle(String titleContains);

Expand Down
Expand Up @@ -5,7 +5,7 @@
/**
* Created by Marc Nuri <marc@marcnuri.com> on 2018-02-27.
*/
public interface BookReposiotryCustom {
public interface BookRepositoryCustom {

List<Book> query(DynamicQuery dynamicQuery);

Expand Down
Expand Up @@ -13,7 +13,7 @@
/**
* Created by Marc Nuri <marc@marcnuri.com> on 2018-02-27.
*/
public class BookRepositoryCustomImpl implements BookReposiotryCustom {
public class BookRepositoryImpl implements BookRepositoryCustom {

//**************************************************************************************************
// Fields
Expand All @@ -24,7 +24,7 @@ public class BookRepositoryCustomImpl implements BookReposiotryCustom {
// Constructors
//**************************************************************************************************
@Autowired
public BookRepositoryCustomImpl(MongoTemplate mongoTemplate) {
public BookRepositoryImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

Expand Down
@@ -0,0 +1,46 @@
/*
* BookRepositoryTest.java
*
* Created on 2018-02-28, 7:07
*/
package com.marcnuri.spring.mongo.customrepository.book;

import com.marcnuri.spring.mongo.customrepository.EmbeddedMongoConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Created by Marc Nuri <marc@marcnuri.com> on 2018-02-28.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
BookRepository.class
})
@EnableMongoRepositories()
@Import(EmbeddedMongoConfiguration.class)
public class BookRepositoryTest {

@Autowired
private BookRepository bookRepository;

@Before
public void setUp(){
bookRepository.deleteAll();
}

//**************************************************************************************************
// Constructors
//**************************************************************************************************
@Test
public void findByTitleContainingOrderByTitle_existingTitle_shouldReturnList() {
bookRepository.findByTitleContainingOrderByTitle("test");
}


}

0 comments on commit db62a9b

Please sign in to comment.