Use Flapdoodle Embedded MongoDB for integration tests and samples
This commit is contained in:
committed by
Vedran Pavic
parent
3980be349b
commit
903cac492e
@@ -15,13 +15,21 @@
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
@@ -30,11 +38,15 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.data.AbstractITests;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link MongoOperationsSessionRepository} tests.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
abstract public class AbstractMongoRepositoryITests extends AbstractITests {
|
||||
|
||||
@@ -364,4 +376,22 @@ abstract public class AbstractMongoRepositoryITests extends AbstractITests {
|
||||
return this.changedContext.getAuthentication().getName();
|
||||
}
|
||||
|
||||
protected static class BaseConfig {
|
||||
|
||||
private int embeddedMongoPort = SocketUtils.findAvailableTcpPort();
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public MongodExecutable embeddedMongoServer() throws IOException {
|
||||
return MongoITestUtils.embeddedMongoServer(this.embeddedMongoPort);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@DependsOn("embeddedMongoServer")
|
||||
public MongoOperations mongoOperations() throws UnknownHostException {
|
||||
MongoClient mongo = new MongoClient("localhost", this.embeddedMongoPort);
|
||||
return new MongoTemplate(mongo, "test");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||
import de.flapdoodle.embed.mongo.config.IMongodConfig;
|
||||
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.Net;
|
||||
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||
import de.flapdoodle.embed.process.runtime.Network;
|
||||
|
||||
/**
|
||||
* Utility class for Mongo integration tests.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
final class MongoITestUtils {
|
||||
|
||||
private MongoITestUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link MongodExecutable} for use in integration tests.
|
||||
* @param port the port for embedded Mongo to bind to
|
||||
* @return the {@link MongodExecutable} instance
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
static MongodExecutable embeddedMongoServer(int port) throws IOException {
|
||||
IMongodConfig mongodConfig = new MongodConfigBuilder()
|
||||
.version(Version.Main.PRODUCTION)
|
||||
.net(new Net(port, Network.localhostIsIPv6()))
|
||||
.build();
|
||||
MongodStarter mongodStarter = MongodStarter.getDefaultInstance();
|
||||
return mongodStarter.prepare(mongodConfig);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,27 +15,27 @@
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.mongodb.MongoClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.geo.GeoModule;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MongoOperationsSessionRepository} that use
|
||||
* {@link JacksonMongoSessionConverter} based session serialization.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class MongoRepositoryJacksonITests extends AbstractMongoRepositoryITests {
|
||||
@@ -57,12 +57,7 @@ public class MongoRepositoryJacksonITests extends AbstractMongoRepositoryITests
|
||||
|
||||
@Configuration
|
||||
@EnableMongoHttpSession
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public MongoOperations mongoOperations() throws UnknownHostException {
|
||||
return new MongoTemplate(new MongoClient(), "test");
|
||||
}
|
||||
static class Config extends BaseConfig {
|
||||
|
||||
@Bean
|
||||
public AbstractMongoSessionConverter mongoSessionConverter() {
|
||||
|
||||
@@ -15,23 +15,23 @@
|
||||
*/
|
||||
package org.springframework.session.data.mongo;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MongoOperationsSessionRepository} that use
|
||||
* {@link JacksonMongoSessionConverter} based session serialization.
|
||||
*
|
||||
* @author Jakub Kubrynski
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class MongoRepositoryJdkSerializationITests extends AbstractMongoRepositoryITests {
|
||||
@@ -75,12 +75,7 @@ public class MongoRepositoryJdkSerializationITests extends AbstractMongoReposito
|
||||
|
||||
@Configuration
|
||||
@EnableMongoHttpSession
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public MongoOperations mongoOperations() throws UnknownHostException {
|
||||
return new MongoTemplate(new MongoClient(), "test");
|
||||
}
|
||||
static class Config extends BaseConfig {
|
||||
|
||||
@Bean
|
||||
public AbstractMongoSessionConverter mongoSessionConverter() {
|
||||
|
||||
Reference in New Issue
Block a user