@Test public void testRemovingDocument() throws Exception { PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); p1.setFirstName("Sven_to_be_removed"); p1.setAge(51); template.insert(p1); Query q1 = new Query(Criteria.where("id").is(p1.getId())); PersonWithIdPropertyOfTypeObjectId found1 = template.findOne(q1, PersonWithIdPropertyOfTypeObjectId.class); assertThat(found1, notNullValue()); Query _q = new Query(Criteria.where("_id").is(p1.getId())); template.remove(_q, PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId notFound1 = template.findOne(q1, PersonWithIdPropertyOfTypeObjectId.class); assertThat(notFound1, nullValue()); PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); p2.setFirstName("Bubba_to_be_removed"); p2.setAge(51); template.insert(p2); Query q2 = new Query(Criteria.where("id").is(p2.getId())); PersonWithIdPropertyOfTypeObjectId found2 = template.findOne(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(found2, notNullValue()); template.remove(q2, PersonWithIdPropertyOfTypeObjectId.class); PersonWithIdPropertyOfTypeObjectId notFound2 = template.findOne(q2, PersonWithIdPropertyOfTypeObjectId.class); assertThat(notFound2, nullValue()); }
/** @see DATADOC-230 */ @Test public void removesEntityFromCollection() { template.remove(new Query(), "mycollection"); Person person = new Person("Dave"); template.save(person, "mycollection"); assertThat(template.findAll(TestClass.class, "mycollection").size(), is(1)); template.remove(person, "mycollection"); assertThat(template.findAll(Person.class, "mycollection").isEmpty(), is(true)); }
@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 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 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 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)); }
/** @see DATADOC-349 */ @Test public void removesEntityWithAnnotatedIdIfIdNeedsMassaging() { String id = new ObjectId().toString(); Sample sample = new Sample(); sample.id = id; template.save(sample); assertThat(template.findOne(query(where("id").is(id)), Sample.class).id, is(id)); template.remove(sample); assertThat(template.findOne(query(where("id").is(id)), Sample.class), is(nullValue())); }
@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) { } }
/** @see DATADOC-166 */ @Test public void removingNullIsANoOp() { template.remove(null); }