@Test public void testUsingRegexQueryWithOptions() throws Exception { template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven"); p1.setAge(11); template.insert(p1); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Mary"); p2.setAge(21); template.insert(p2); PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId(); p3.setFirstName("Ann"); p3.setAge(31); template.insert(p3); PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId(); p4.setFirstName("samantha"); p4.setAge(41); template.insert(p4); Query q1 = new Query(Criteria.where("firstName").regex("S.*")); List<PersonWithIdPropertyOfTypeObjectId> results1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class); Query q2 = new Query(Criteria.where("firstName").regex("S.*", "i")); List<PersonWithIdPropertyOfTypeObjectId> results2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(results1.size(), is(1)); assertThat(results2.size(), is(2)); }
@Test public void testUsingUpdateWithMultipleSet() throws Exception { template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven"); p1.setAge(11); template.insert(p1); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Mary"); p2.setAge(21); template.insert(p2); Update u = new Update().set("firstName", "Bob").set("age", 10); WriteResult wr = template.updateMulti(new Query(), u, PersonWithIdPropertyOfTypeObjectId.class); assertThat(wr.getN(), is(2)); Query q1 = new Query(Criteria.where("age").in(11, 21)); List<PersonWithIdPropertyOfTypeObjectId> r1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class); assertThat(r1.size(), is(0)); Query q2 = new Query(Criteria.where("age").is(10)); List<PersonWithIdPropertyOfTypeObjectId> r2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(r2.size(), is(2)); for (PersonWithIdPropertyOfTypeObjectId p : r2) { assertThat(p.getAge(), is(10)); assertThat(p.getFirstName(), is("Bob")); } }
@Test public void testUsingAnInQueryWithStringId() throws Exception { template.remove(new Query(), PersonWithIdPropertyOfTypeString.class); PersonWithIdPropertyOfTypeString p1 = new PersonWithIdPropertyOfTypeString(); p1.setFirstName("Sven"); p1.setAge(11); template.insert(p1); PersonWithIdPropertyOfTypeString p2 = new PersonWithIdPropertyOfTypeString(); p2.setFirstName("Mary"); p2.setAge(21); template.insert(p2); PersonWithIdPropertyOfTypeString p3 = new PersonWithIdPropertyOfTypeString(); p3.setFirstName("Ann"); p3.setAge(31); template.insert(p3); PersonWithIdPropertyOfTypeString p4 = new PersonWithIdPropertyOfTypeString(); p4.setFirstName("John"); p4.setAge(41); template.insert(p4); Query q1 = new Query(Criteria.where("age").in(11, 21, 41)); List<PersonWithIdPropertyOfTypeString> results1 = template.find(q1, PersonWithIdPropertyOfTypeString.class); Query q2 = new Query(Criteria.where("firstName").in("Ann", "Mary")); List<PersonWithIdPropertyOfTypeString> results2 = template.find(q2, PersonWithIdPropertyOfTypeString.class); Query q3 = new Query(Criteria.where("id").in(p3.getId(), p4.getId())); List<PersonWithIdPropertyOfTypeString> results3 = template.find(q3, PersonWithIdPropertyOfTypeString.class); assertThat(results1.size(), is(3)); assertThat(results2.size(), is(2)); assertThat(results3.size(), is(2)); }
@Test public void testUsingAnOrQuery() throws Exception { template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven"); p1.setAge(11); template.insert(p1); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Mary"); p2.setAge(21); template.insert(p2); PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId(); p3.setFirstName("Ann"); p3.setAge(31); template.insert(p3); PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId(); p4.setFirstName("John"); p4.setAge(41); template.insert(p4); Query orQuery = new Query(new Criteria().orOperator(where("age").in(11, 21), where("age").is(31))); List<PersonWithIdPropertyOfTypeObjectId> results = template.find(orQuery, PersonWithIdPropertyOfTypeObjectId.class); assertThat(results.size(), is(3)); for (PersonWithIdPropertyOfTypeObjectId p : results) { assertThat(p.getAge(), isOneOf(11, 21, 31)); } }
@Test public void testUsingInQueryWithList() throws Exception { template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven"); p1.setAge(11); template.insert(p1); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Mary"); p2.setAge(21); template.insert(p2); PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId(); p3.setFirstName("Ann"); p3.setAge(31); template.insert(p3); PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId(); p4.setFirstName("John"); p4.setAge(41); template.insert(p4); List<Integer> l1 = new ArrayList<Integer>(); l1.add(11); l1.add(21); l1.add(41); Query q1 = new Query(Criteria.where("age").in(l1)); List<PersonWithIdPropertyOfTypeObjectId> results1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class); Query q2 = new Query(Criteria.where("age").in(l1.toArray())); List<PersonWithIdPropertyOfTypeObjectId> results2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(results1.size(), is(3)); assertThat(results2.size(), is(3)); try { List<Integer> l2 = new ArrayList<Integer>(); l2.add(31); Query q3 = new Query(Criteria.where("age").in(l1, l2)); template.find(q3, PersonWithIdPropertyOfTypeObjectId.class); Assert.fail("Should have trown an InvalidDocumentStoreApiUsageException"); } catch (InvalidMongoDbApiUsageException e) { } }
@Test public void insertsSimpleEntityCorrectly() throws Exception { Person person = new Person("Oliver"); person.setAge(25); template.insert(person); List<Person> result = template.find(new Query(Criteria.where("_id").is(person.getId())), Person.class); assertThat(result.size(), is(1)); assertThat(result, hasItem(person)); }
@Test public void returnsEntityWhenQueryingForDateTime() { DateTime dateTime = new DateTime(2011, 3, 3, 12, 0, 0, 0); TestClass testClass = new TestClass(dateTime); mappingTemplate.save(testClass); List<TestClass> testClassList = mappingTemplate.find( new Query(Criteria.where("myDate").is(dateTime.toDate())), TestClass.class); assertThat(testClassList.size(), is(1)); assertThat(testClassList.get(0).myDate, is(testClass.myDate)); }