@Test
 public void testFindAndUpdateUpsert() {
   template.insert(new Person("Tom", 21));
   template.insert(new Person("Dick", 22));
   Query query = new Query(Criteria.where("firstName").is("Harry"));
   Update update = new Update().set("age", 23);
   Person p =
       template.findAndModify(
           query, update, new FindAndModifyOptions().upsert(true).returnNew(true), Person.class);
   assertThat(p.getFirstName(), is("Harry"));
   assertThat(p.getAge(), is(23));
 }
  @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 bogusUpdateDoesNotTriggerException() throws Exception {

    MongoTemplate mongoTemplate = new MongoTemplate(factory);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);

    Person person = new Person("Oliver2");
    person.setAge(25);
    mongoTemplate.insert(person);

    Query q = new Query(Criteria.where("BOGUS").gt(22));
    Update u = new Update().set("firstName", "Sven");
    mongoTemplate.updateFirst(q, u, Person.class);
  }
  @Test
  public void testEnsureIndex() throws Exception {

    Person p1 = new Person("Oliver");
    p1.setAge(25);
    template.insert(p1);
    Person p2 = new Person("Sven");
    p2.setAge(40);
    template.insert(p2);

    template
        .indexOps(Person.class)
        .ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP));

    DBCollection coll = template.getCollection(template.getCollectionName(Person.class));
    List<DBObject> indexInfo = coll.getIndexInfo();
    assertThat(indexInfo.size(), is(2));
    String indexKey = null;
    boolean unique = false;
    boolean dropDupes = false;
    for (DBObject ix : indexInfo) {
      if ("age_-1".equals(ix.get("name"))) {
        indexKey = ix.get("key").toString();
        unique = (Boolean) ix.get("unique");
        dropDupes = (Boolean) ix.get("dropDups");
      }
    }
    assertThat(indexKey, is("{ \"age\" : -1}"));
    assertThat(unique, is(true));
    assertThat(dropDupes, is(true));

    List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
    System.out.println(indexInfoList);
    assertThat(indexInfoList.size(), is(2));
    IndexInfo ii = indexInfoList.get(1);
    assertThat(ii.isUnique(), is(true));
    assertThat(ii.isDropDuplicates(), is(true));
    assertThat(ii.isSparse(), is(false));
    assertThat(ii.getFieldSpec().containsKey("age"), is(true));
    assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true));
  }
  /** @see DATAMONGO-234 */
  @Test
  public void testFindAndUpdate() {

    template.insert(new Person("Tom", 21));
    template.insert(new Person("Dick", 22));
    template.insert(new Person("Harry", 23));

    Query query = new Query(Criteria.where("firstName").is("Harry"));
    Update update = new Update().inc("age", 1);
    Person p = template.findAndModify(query, update, Person.class); // return old
    assertThat(p.getFirstName(), is("Harry"));
    assertThat(p.getAge(), is(23));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(24));

    p = template.findAndModify(query, update, Person.class, "person");
    assertThat(p.getAge(), is(24));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(25));

    p =
        template.findAndModify(
            query, update, new FindAndModifyOptions().returnNew(true), Person.class);
    assertThat(p.getAge(), is(26));

    p = template.findAndModify(query, update, null, Person.class, "person");
    assertThat(p.getAge(), is(26));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(27));

    Query query2 = new Query(Criteria.where("firstName").is("Mary"));
    p =
        template.findAndModify(
            query2, update, new FindAndModifyOptions().returnNew(true).upsert(true), Person.class);
    assertThat(p.getFirstName(), is("Mary"));
    assertThat(p.getAge(), is(1));
  }