From fbc3539b6eebd21aeb0d50545c8463296904992c Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sun, 26 Jul 2015 16:01:12 +0200 Subject: [PATCH] Initial commit --- spring-data-mongodb/.classpath | 26 ++++ spring-data-mongodb/.project | 23 +++ spring-data-mongodb/pom.xml | 102 ++++++++++++++ .../main/java/org/baeldung/model/User.java | 37 +++++ .../baeldung/repository/UserRepository.java | 68 +++++++++ .../webapp/WEB-INF/dispatcher-servlet.xml | 24 ++++ .../src/main/webapp/WEB-INF/web.xml | 26 ++++ .../repository/UserRepositoryTest.java | 131 ++++++++++++++++++ 8 files changed, 437 insertions(+) create mode 100644 spring-data-mongodb/.classpath create mode 100644 spring-data-mongodb/.project create mode 100644 spring-data-mongodb/pom.xml create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/model/User.java create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java create mode 100644 spring-data-mongodb/src/main/webapp/WEB-INF/dispatcher-servlet.xml create mode 100644 spring-data-mongodb/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryTest.java diff --git a/spring-data-mongodb/.classpath b/spring-data-mongodb/.classpath new file mode 100644 index 0000000000..83dd2f44a7 --- /dev/null +++ b/spring-data-mongodb/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-mongodb/.project b/spring-data-mongodb/.project new file mode 100644 index 0000000000..ad5b57cb82 --- /dev/null +++ b/spring-data-mongodb/.project @@ -0,0 +1,23 @@ + + + spring-data-mongodb + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml new file mode 100644 index 0000000000..9087dbfbc7 --- /dev/null +++ b/spring-data-mongodb/pom.xml @@ -0,0 +1,102 @@ + + 4.0.0 + + org.baeldung + spring-data-mongodb + 0.0.1-SNAPSHOT + jar + + spring-data-mongodb + http://maven.apache.org + + + UTF-8 + + 4.1.6.RELEASE + 1.3 + 4.11 + 2.4.1 + + + + + + + + org.springframework + spring-orm + ${org.springframework.version} + + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + org.springframework.data + spring-data-mongodb + 1.7.1.RELEASE + + + + jstl + jstl + 1.2 + + + + junit + junit + 3.8.1 + test + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.springframework + spring-test + 3.2.0.RELEASE + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + + + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + + diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java new file mode 100644 index 0000000000..5fef9ff81e --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -0,0 +1,37 @@ +package org.baeldung.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class User { + + @Id + private String id; + private String name; + private Integer age; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java b/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java new file mode 100644 index 0000000000..49be049356 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/repository/UserRepository.java @@ -0,0 +1,68 @@ +package org.baeldung.repository; + +import java.util.List; + +import org.baeldung.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Repository; + +import com.mongodb.WriteResult; + +@Repository +public class UserRepository { + + @Autowired + private MongoTemplate mongoTemplate; + + public void insertUser(User user) { + mongoTemplate.insert(user, "user"); + } + + public List listUser() { + return mongoTemplate.findAll(User.class, "user"); + } + + public void removeUser(User user) { + mongoTemplate.remove(user, "user"); + } + + public void saveUser(User user) { + mongoTemplate.save(user, "user"); + } + + public User findAndModifyUser(String name, String newName) { + Query query = new Query(); + query.addCriteria(Criteria.where("name").is(name)); + Update update = new Update(); + update.set("name", newName); + return mongoTemplate.findAndModify(query, update, User.class); + } + + public void updateFirstUser(String name, String newName) { + Query query = new Query(); + query.addCriteria(Criteria.where("name").is(name)); + Update update = new Update(); + update.set("name", newName); + mongoTemplate.updateFirst(query, update, User.class); + } + + public WriteResult upsertUser(String name, String newName) { + Query query = new Query(); + query.addCriteria(Criteria.where("name").is(name)); + Update update = new Update(); + update.set("name", newName); + return mongoTemplate.upsert(query, update, User.class); + } + + public void updateMultiUser(String name, String newName) { + Query query = new Query(); + query.addCriteria(Criteria.where("name").is(name)); + Update update = new Update(); + update.set("name", newName); + mongoTemplate.updateMulti(query, update, User.class); + } +} diff --git a/spring-data-mongodb/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/spring-data-mongodb/src/main/webapp/WEB-INF/dispatcher-servlet.xml new file mode 100644 index 0000000000..1b118d18d4 --- /dev/null +++ b/spring-data-mongodb/src/main/webapp/WEB-INF/dispatcher-servlet.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-mongodb/src/main/webapp/WEB-INF/web.xml b/spring-data-mongodb/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..79550fcbab --- /dev/null +++ b/spring-data-mongodb/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,26 @@ + + + + dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + + dispatcher + / + + + + contextConfigLocation + /WEB-INF/dispatcher-servlet.xml + + + + org.springframework.web.context.ContextLoaderListener + + + \ No newline at end of file diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryTest.java new file mode 100644 index 0000000000..cabfa09fc1 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryTest.java @@ -0,0 +1,131 @@ +package org.baeldung.repository; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.baeldung.model.User; +import org.baeldung.repository.UserRepository; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("file:src/main/webapp/WEB-INF/dispatcher-servlet.xml") +public class UserRepositoryTest { + @Autowired + private UserRepository userRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + mongoOps.createCollection(User.class); + } + + @After + public void tearDown() { + mongoOps.dropCollection(User.class); + } + + @Test + public void testInsert() { + User user = new User(); + user.setName("Jon"); + userRepository.insertUser(user); + + assertThat(mongoOps.findOne(Query.query(Criteria.where("name").is("Jon")), User.class).getName(), is("Jon")); + } + + @Test + public void testSave() { + User user = new User(); + user.setName("Jack"); + userRepository.insertUser(user); + + user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class); + String id = user.getId(); + + user.setName("Jim"); + userRepository.saveUser(user); + + assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim")); + } + + @Test + public void testUpdateFirst() { + User user = new User(); + user.setName("Alex"); + mongoOps.insert(user); + + user = new User(); + user.setName("Alex"); + mongoOps.insert(user); + + userRepository.updateFirstUser("Alex", "James"); + + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("James")), User.class).size(), is(1)); + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Alex")), User.class).size(), is(1)); + } + + @Test + public void testUpdateMulti() { + User user = new User(); + user.setName("Eugen"); + mongoOps.insert(user); + + user = new User(); + user.setName("Eugen"); + mongoOps.insert(user); + + userRepository.updateMultiUser("Eugen", "Victor"); + + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Victor")), User.class).size(), is(2)); + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Eugen")), User.class).size(), is(0)); + } + + @Test + public void testFindAndModify() { + User user = new User(); + user.setName("Markus"); + mongoOps.insert(user); + + user = userRepository.findAndModifyUser("Markus", "Nick"); + + assertThat(user.getName(), is("Markus")); + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Nick")), User.class).size(), is(1)); + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Markus")), User.class).size(), is(0)); + } + + @Test + public void testUpsert() { + User user = new User(); + user.setName("Markus"); + mongoOps.insert(user); + + userRepository.upsertUser("Markus", "Nick"); + + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Nick")), User.class).size(), is(1)); + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Markus")), User.class).size(), is(0)); + } + + @Test + public void testRemove() { + User user = new User(); + user.setName("Benn"); + mongoOps.insert(user); + + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(1)); + + userRepository.removeUser(user); + + assertThat(mongoOps.find(Query.query(Criteria.where("name").is("Benn")), User.class).size(), is(0)); + } +}