#8 - Switch to local source data for text search.
Remove online resources and just rely on local data. Though we had to keep the BlogPostInitializer for setting up the tests only using the template without repository support. Original pull request: #18.
This commit is contained in:
committed by
Oliver Gierke
parent
de7c2c5522
commit
f625317445
@@ -11,5 +11,14 @@
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -22,12 +22,15 @@ import static org.junit.Assert.*;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link OrderRepository}.
|
||||
*
|
||||
@@ -38,6 +41,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class OrderRepositoryIntegrationTests {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired OrderRepository repository;
|
||||
|
||||
private final static LineItem product1 = new LineItem("p1", 1.23);
|
||||
|
||||
@@ -12,6 +12,15 @@
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -26,6 +27,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Meta;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -33,6 +35,7 @@ import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
import example.springdata.mongodb.customer.Customer;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -41,6 +44,8 @@ import example.springdata.mongodb.customer.Customer;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class AdvancedIntegrationTests {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
@Autowired AdvancedRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
|
||||
@@ -15,15 +15,13 @@
|
||||
*/
|
||||
package example.springdata.mongodb.customer;
|
||||
|
||||
import static org.hamcrest.Matchers.closeTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -37,6 +35,8 @@ import org.springframework.data.mongodb.core.index.GeospatialIndex;
|
||||
import org.springframework.data.querydsl.QSort;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration test for {@link CustomerRepository}.
|
||||
*
|
||||
@@ -46,6 +46,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired CustomerRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
<inceptionYear>2011-2014</inceptionYear>
|
||||
|
||||
<modules>
|
||||
<module>aggregation</module>
|
||||
<module>example</module>
|
||||
<module>aggregation</module>
|
||||
<module>text-search</module>
|
||||
<module>util</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -14,13 +14,14 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rometools</groupId>
|
||||
<artifactId>rome</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -39,4 +39,9 @@ public class BlogPost {
|
||||
private @TextIndexed(weight = 2) String content;
|
||||
private @TextIndexed List<String> categories;
|
||||
private @TextScore Float score;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlogPost [id=" + id + ", score=" + score + ", title=" + title + ", categories=" + categories + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,16 @@
|
||||
*/
|
||||
package example.springdata.mongodb.textsearch;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
|
||||
import example.springdata.mongodb.util.BlogPostInitializer;
|
||||
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -35,15 +36,12 @@ public class MongoTestConfiguration {
|
||||
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
/**
|
||||
* Initializes the repository with a predefined set of entities.
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@PostConstruct
|
||||
void initialize() throws Exception {
|
||||
BlogPostInitializer.INSTANCE.initialize(operations);
|
||||
@Bean
|
||||
public Jackson2RepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||
|
||||
Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
factoryBean.setResources(new Resource[] { new ClassPathResource("spring-blog.atom.json") });
|
||||
return factoryBean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package example.springdata.mongodb.textsearch;
|
||||
|
||||
import static example.springdata.mongodb.util.ConsoleResultPrinter.printResult;
|
||||
import static example.springdata.mongodb.util.ConsoleResultPrinter.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.MongoVersionRule;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests showing the text search functionality using repositories.
|
||||
@@ -44,7 +44,7 @@ public class TextSearchRepositoryTests {
|
||||
|
||||
@Autowired BlogPostRepository repo;
|
||||
|
||||
@ClassRule public static MongoVersionRule versionRule = MongoVersionRule.atLeast(new Version(2, 6));
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
/**
|
||||
* Show how to do simple matching. <br />
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
*/
|
||||
package example.springdata.mongodb.textsearch;
|
||||
|
||||
import static example.springdata.mongodb.util.ConsoleResultPrinter.printResult;
|
||||
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||
import static example.springdata.mongodb.util.ConsoleResultPrinter.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.springframework.data.mongodb.core.query.TextQuery;
|
||||
import org.springframework.data.util.Version;
|
||||
|
||||
import example.springdata.mongodb.util.BlogPostInitializer;
|
||||
import example.springdata.mongodb.util.MongoVersionRule;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -43,7 +43,7 @@ public class TextSearchTemplateTests {
|
||||
|
||||
MongoOperations operations;
|
||||
|
||||
@ClassRule public static MongoVersionRule versionRule = MongoVersionRule.atLeast(new Version(2, 6));
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
@@ -15,21 +15,12 @@
|
||||
*/
|
||||
package example.springdata.mongodb.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.data.repository.init.Jackson2ResourceReader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.rometools.rome.feed.atom.Category;
|
||||
import com.rometools.rome.feed.atom.Content;
|
||||
import com.rometools.rome.feed.atom.Entry;
|
||||
import com.rometools.rome.feed.atom.Feed;
|
||||
|
||||
import example.springdata.mongodb.textsearch.BlogPost;
|
||||
|
||||
@@ -44,73 +35,31 @@ public enum BlogPostInitializer {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private final Converter<Entry, BlogPost> converter = EntryConverter.INSTANCE;
|
||||
private final String url = "https://spring.io/blog.atom";
|
||||
|
||||
/**
|
||||
* Initializes the given {@link MongoOperations} with {@link BlogPost}s from the Spring Blog.
|
||||
*
|
||||
* @param operations must not be {@literal null}.
|
||||
* @throws Exception
|
||||
*/
|
||||
public void initialize(MongoOperations operations) {
|
||||
public void initialize(MongoOperations operations) throws Exception {
|
||||
|
||||
Assert.notNull(operations, "MongoOperations must not be null!");
|
||||
|
||||
ResponseEntity<Feed> feed = restTemplate.getForEntity(url, Feed.class);
|
||||
int count = 0;
|
||||
|
||||
if (feed.hasBody()) {
|
||||
for (Object entry : feed.getBody().getEntries()) {
|
||||
if (entry instanceof Entry) {
|
||||
operations.save(converter.convert((Entry) entry));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Imported {} blog posts from spring.io!", count);
|
||||
loadFromClasspathSource(operations);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Converter} implementation capable of converting atom feed {@link Entry} into {@link BlogPost}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
private enum EntryConverter implements Converter<Entry, BlogPost> {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void loadFromClasspathSource(MongoOperations operations) throws Exception {
|
||||
|
||||
INSTANCE;
|
||||
Jackson2ResourceReader reader = new Jackson2ResourceReader();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public BlogPost convert(Entry source) {
|
||||
Object source = reader.readFrom(new ClassPathResource("spring-blog.atom.json"), this.getClass().getClassLoader());
|
||||
|
||||
BlogPost post = new BlogPost();
|
||||
|
||||
post.setId(source.getId());
|
||||
post.setTitle(source.getTitle());
|
||||
|
||||
for (Object content : source.getContents()) {
|
||||
if (content instanceof Content) {
|
||||
post.setContent(((Content) content).getValue());
|
||||
}
|
||||
}
|
||||
|
||||
List<String> categories = new ArrayList<String>();
|
||||
|
||||
for (Object category : source.getCategories()) {
|
||||
if (category instanceof Category) {
|
||||
categories.add(((Category) category).getLabel());
|
||||
}
|
||||
}
|
||||
|
||||
post.setCategories(categories);
|
||||
|
||||
return post;
|
||||
if (source instanceof Iterable) {
|
||||
((Iterable) source).forEach(element -> operations.save(element));
|
||||
} else {
|
||||
operations.save(source);
|
||||
}
|
||||
|
||||
log.info("Imported blog posts from classpath!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ package example.springdata.mongodb.util;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.data.mongodb.core.query.TextCriteria;
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
||||
|
||||
import example.springdata.mongodb.textsearch.BlogPost;
|
||||
|
||||
@@ -28,7 +28,7 @@ import example.springdata.mongodb.textsearch.BlogPost;
|
||||
*/
|
||||
public class ConsoleResultPrinter {
|
||||
|
||||
public static void printResult(Collection<BlogPost> blogPosts, TextCriteria criteria) {
|
||||
public static void printResult(Collection<BlogPost> blogPosts, CriteriaDefinition criteria) {
|
||||
|
||||
System.out.println(String.format("XXXXXXXXXXXX -- Found %s blogPosts matching '%s' --XXXXXXXXXXXX",
|
||||
blogPosts.size(), criteria != null ? criteria.getCriteriaObject() : ""));
|
||||
|
||||
201
mongodb/text-search/src/test/resources/spring-blog.atom.json
Normal file
201
mongodb/text-search/src/test/resources/spring-blog.atom.json
Normal file
File diff suppressed because one or more lines are too long
21
mongodb/util/pom.xml
Normal file
21
mongodb/util/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
|
||||
<name>Spring Data MongoDB - Example utilities</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package example.springdata.mongodb.util;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.internal.AssumptionViolatedException;
|
||||
@@ -37,7 +39,7 @@ import com.mongodb.MongoClient;
|
||||
* @author Christoph Strobl
|
||||
* @since 1.6
|
||||
*/
|
||||
public class MongoVersionRule implements TestRule {
|
||||
public class RequiresMongoDB implements TestRule {
|
||||
|
||||
private String host = "localhost";
|
||||
private int port = 27017;
|
||||
@@ -47,24 +49,24 @@ public class MongoVersionRule implements TestRule {
|
||||
|
||||
private Version currentVersion;
|
||||
|
||||
public MongoVersionRule(Version min, Version max) {
|
||||
public RequiresMongoDB(Version min, Version max) {
|
||||
this.minVersion = min;
|
||||
this.maxVersion = max;
|
||||
}
|
||||
|
||||
public static MongoVersionRule any() {
|
||||
return new MongoVersionRule(new Version(0, 0, 0), new Version(9999, 9999, 9999));
|
||||
public static RequiresMongoDB anyVersion() {
|
||||
return new RequiresMongoDB(new Version(0, 0, 0), new Version(9999, 9999, 9999));
|
||||
}
|
||||
|
||||
public static MongoVersionRule atLeast(Version minVersion) {
|
||||
return new MongoVersionRule(minVersion, new Version(9999, 9999, 9999));
|
||||
public static RequiresMongoDB atLeast(Version minVersion) {
|
||||
return new RequiresMongoDB(minVersion, new Version(9999, 9999, 9999));
|
||||
}
|
||||
|
||||
public static MongoVersionRule atMost(Version maxVersion) {
|
||||
return new MongoVersionRule(new Version(0, 0, 0), maxVersion);
|
||||
public static RequiresMongoDB atMost(Version maxVersion) {
|
||||
return new RequiresMongoDB(new Version(0, 0, 0), maxVersion);
|
||||
}
|
||||
|
||||
public MongoVersionRule withServerRunningAt(String host, int port) {
|
||||
public RequiresMongoDB serverRunningAt(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
|
||||
@@ -99,8 +101,8 @@ public class MongoVersionRule implements TestRule {
|
||||
DB db = client.getDB("test");
|
||||
CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get());
|
||||
this.currentVersion = Version.parse(result.get("version").toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} catch (com.mongodb.MongoTimeoutException | UnknownHostException e) {
|
||||
throw new AssumptionViolatedException("Seems as mongodb server is not running.", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user