From 6fb5259e37e52f8b7f047f9163db370ae109148b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 1 Sep 2014 12:35:42 +0200 Subject: [PATCH] #8 - Add sample for @Meta usage. Renamed the geo-spatial example to example only as it not only covers geo-spatial samples. Added advanced example to show the usage of the @Meta annotation. Original pull request: #11. --- README.md | 2 +- mongodb/{geo-spatial => example}/pom.xml | 4 +- .../mongodb/advanced/AdvancedRepository.java | 49 +++++++++++ .../advanced/ApplicationConfiguration.java | 69 +++++++++++++++ .../springdata/mongodb/customer/Address.java | 0 .../customer/ApplicationConfiguration.java | 2 +- .../springdata/mongodb/customer/Customer.java | 0 .../mongodb/customer/CustomerRepository.java | 0 .../advanced/AdvancedIntegrationTests.java | 85 +++++++++++++++++++ .../mongodb/advanced/package-info.java | 5 ++ .../CustomerRepositoryIntegrationTest.java | 0 .../mongodb/customer/package-info.java | 5 ++ mongodb/pom.xml | 2 +- 13 files changed, 218 insertions(+), 5 deletions(-) rename mongodb/{geo-spatial => example}/pom.xml (91%) create mode 100644 mongodb/example/src/main/java/example/springdata/mongodb/advanced/AdvancedRepository.java create mode 100644 mongodb/example/src/main/java/example/springdata/mongodb/advanced/ApplicationConfiguration.java rename mongodb/{geo-spatial => example}/src/main/java/example/springdata/mongodb/customer/Address.java (100%) rename mongodb/{geo-spatial => example}/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java (96%) rename mongodb/{geo-spatial => example}/src/main/java/example/springdata/mongodb/customer/Customer.java (100%) rename mongodb/{geo-spatial => example}/src/main/java/example/springdata/mongodb/customer/CustomerRepository.java (100%) create mode 100644 mongodb/example/src/test/java/example/springdata/mongodb/advanced/AdvancedIntegrationTests.java create mode 100644 mongodb/example/src/test/java/example/springdata/mongodb/advanced/package-info.java rename mongodb/{geo-spatial => example}/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java (100%) create mode 100644 mongodb/example/src/test/java/example/springdata/mongodb/customer/package-info.java diff --git a/README.md b/README.md index ea4886e8..a4c417bf 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ We have separate folders for the samples of individual modules: ## Spring Data MongoDB -* `geo-spatial` - Example project for general repository functionality (including geo-spatial functionality) and Querydsl integration +* `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics. * `aggregation` - Example project to showcase the MongoDB aggregation framework support. * `text-search` - Example project showing usage of MongoDB text search feature. diff --git a/mongodb/geo-spatial/pom.xml b/mongodb/example/pom.xml similarity index 91% rename from mongodb/geo-spatial/pom.xml rename to mongodb/example/pom.xml index 80c2af7f..5c3bebda 100644 --- a/mongodb/geo-spatial/pom.xml +++ b/mongodb/example/pom.xml @@ -2,9 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - spring-data-mongodb-geospatial + spring-data-mongodb-example - Spring Data MongoDB - Geo-spatial & Querydsl + Spring Data MongoDB - Example org.springframework.data.examples diff --git a/mongodb/example/src/main/java/example/springdata/mongodb/advanced/AdvancedRepository.java b/mongodb/example/src/main/java/example/springdata/mongodb/advanced/AdvancedRepository.java new file mode 100644 index 00000000..86396dd7 --- /dev/null +++ b/mongodb/example/src/main/java/example/springdata/mongodb/advanced/AdvancedRepository.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 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 example.springdata.mongodb.advanced; + +import java.util.List; + +import org.springframework.data.mongodb.repository.Meta; + +import example.springdata.mongodb.customer.Customer; +import example.springdata.mongodb.customer.CustomerRepository; + +/** + * Repository interface to manage {@link Customer} instances. + * + * @author Christoph Strobl + */ +public interface AdvancedRepository extends CustomerRepository { + + String META_COMMENT = "s2gx-2014-rocks!"; + + /** + * Derived query using {@code $comment} meta attribute for quick lookup.
+ * Have a look at the {@literal mongodb shell} and execute: + * + *
+	 * 
+	 *  db['system.profile'].find({'query.$comment':'s2gx-2014-rocks!'})
+	 * 
+	 * 
+ * + * @param firstname + * @return + */ + @Meta(comment = META_COMMENT) + List findByFirstname(String firstname); +} diff --git a/mongodb/example/src/main/java/example/springdata/mongodb/advanced/ApplicationConfiguration.java b/mongodb/example/src/main/java/example/springdata/mongodb/advanced/ApplicationConfiguration.java new file mode 100644 index 00000000..4bc0a9de --- /dev/null +++ b/mongodb/example/src/main/java/example/springdata/mongodb/advanced/ApplicationConfiguration.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 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 example.springdata.mongodb.advanced; + +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.Configuration; +import org.springframework.data.mongodb.core.MongoOperations; + +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClient; + +/** + * Test configuration to connect to a MongoDB named "test" and using a {@link MongoClient} with profiling enabled. + * + * @author Christoph Strobl + */ +@Configuration +@EnableAutoConfiguration +class ApplicationConfiguration { + + static final String SYSTEM_PROFILE_DB = "system.profile"; + + @Autowired MongoOperations operations; + + /** + * Initialize db instance with defaults. + */ + @PostConstruct + public void initializeWithDefaults() { + + // Enable profiling + setProfilingLevel(2); + } + + /** + * Clean up resources on shutdown + */ + @PreDestroy + public void cleanUpWhenShuttingDown() { + + // Disable profiling + setProfilingLevel(0); + + if (operations.collectionExists(SYSTEM_PROFILE_DB)) { + operations.dropCollection(SYSTEM_PROFILE_DB); + } + } + + private void setProfilingLevel(int level) { + operations.executeCommand(new BasicDBObject("profile", level)); + } +} diff --git a/mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/Address.java b/mongodb/example/src/main/java/example/springdata/mongodb/customer/Address.java similarity index 100% rename from mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/Address.java rename to mongodb/example/src/main/java/example/springdata/mongodb/customer/Address.java diff --git a/mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java b/mongodb/example/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java similarity index 96% rename from mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java rename to mongodb/example/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java index 97fa329a..e5af31e3 100644 --- a/mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java +++ b/mongodb/example/src/main/java/example/springdata/mongodb/customer/ApplicationConfiguration.java @@ -28,4 +28,4 @@ import com.mongodb.MongoClient; */ @Configuration @EnableAutoConfiguration -public class ApplicationConfiguration {} +class ApplicationConfiguration {} diff --git a/mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/Customer.java b/mongodb/example/src/main/java/example/springdata/mongodb/customer/Customer.java similarity index 100% rename from mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/Customer.java rename to mongodb/example/src/main/java/example/springdata/mongodb/customer/Customer.java diff --git a/mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/CustomerRepository.java b/mongodb/example/src/main/java/example/springdata/mongodb/customer/CustomerRepository.java similarity index 100% rename from mongodb/geo-spatial/src/main/java/example/springdata/mongodb/customer/CustomerRepository.java rename to mongodb/example/src/main/java/example/springdata/mongodb/customer/CustomerRepository.java diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/advanced/AdvancedIntegrationTests.java b/mongodb/example/src/test/java/example/springdata/mongodb/advanced/AdvancedIntegrationTests.java new file mode 100644 index 00000000..1ceb3f44 --- /dev/null +++ b/mongodb/example/src/test/java/example/springdata/mongodb/advanced/AdvancedIntegrationTests.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 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 example.springdata.mongodb.advanced; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import org.junit.Before; +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.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Meta; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBCursor; +import com.mongodb.DBObject; + +import example.springdata.mongodb.customer.Customer; + +/** + * @author Christoph Strobl + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = ApplicationConfiguration.class) +public class AdvancedIntegrationTests { + + @Autowired AdvancedRepository repository; + @Autowired MongoOperations operations; + + Customer dave, oliver, carter; + + @Before + public void setUp() { + + repository.deleteAll(); + + dave = repository.save(new Customer("Dave", "Matthews")); + oliver = repository.save(new Customer("Oliver August", "Matthews")); + carter = repository.save(new Customer("Carter", "Beauford")); + } + + /** + * This test demonstrates usage of {@code $comment} {@link Meta} usage. One can also enable profiling using + * {@code --profile=2} when starting {@literal mongod}. + *

+ * NOTE: Requires MongoDB v. 2.6.4+ + */ + @Test + public void findByFirstnameUsingMetaAttributes() { + + // execute derived finder method just to get the comment in the profile log + repository.findByFirstname(dave.getFirstname()); + + // execute another finder without meta attributes that should not be picked up + repository.findByLastname(dave.getLastname(), new Sort("firstname")); + + DBCursor cursor = operations.getCollection(ApplicationConfiguration.SYSTEM_PROFILE_DB).find( + new BasicDBObject("query.$comment", AdvancedRepository.META_COMMENT)); + + while (cursor.hasNext()) { + + DBObject dbo = cursor.next(); + DBObject query = (DBObject) dbo.get("query"); + + assertThat(query.containsField("$comment"), is(true)); + } + } +} diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/advanced/package-info.java b/mongodb/example/src/test/java/example/springdata/mongodb/advanced/package-info.java new file mode 100644 index 00000000..81b41fed --- /dev/null +++ b/mongodb/example/src/test/java/example/springdata/mongodb/advanced/package-info.java @@ -0,0 +1,5 @@ +/** + * Package showing usage of Spring Data abstractions for special (advanced) MongoDB operations. + */ +package example.springdata.mongodb.advanced; + diff --git a/mongodb/geo-spatial/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java b/mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java similarity index 100% rename from mongodb/geo-spatial/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java rename to mongodb/example/src/test/java/example/springdata/mongodb/customer/CustomerRepositoryIntegrationTest.java diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/customer/package-info.java b/mongodb/example/src/test/java/example/springdata/mongodb/customer/package-info.java new file mode 100644 index 00000000..e5f88299 --- /dev/null +++ b/mongodb/example/src/test/java/example/springdata/mongodb/customer/package-info.java @@ -0,0 +1,5 @@ +/** + * Package showing basic usage of Spring Data MongoDB Repositories. + */ +package example.springdata.mongodb.customer; + diff --git a/mongodb/pom.xml b/mongodb/pom.xml index 7a0a644c..d30642ad 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -17,7 +17,7 @@ 2011-2014 - geo-spatial + example aggregation text-search