@Test
  public void integrationTestMarkCallAsBogus() throws Exception {
    missedCallsDAO = new MissedCallsDAO(Resources.getDatabaseActivity());

    DBObject bogusCall = new BasicDBObject("who", 1234567890L);
    try {
      assertTrue(missedCallsDAO.insertCall(bogusCall));
      assertEquals("Pending", bogusCall.get("state"));
      missedCallsDAO.setState(bogusCall, "Processing");
      bogusCall = refetchCall(bogusCall);
      missedCallsDAO.setState(bogusCall, "Bogus");
      assertEquals("Bogus", refetchCall(bogusCall).get("state"));
    } finally {
      deleteCall(bogusCall);
    }
  }
  @Test
  public void testSetState() throws Exception {
    WriteResult response = mock(WriteResult.class);
    when(response.getN()).thenReturn(1);
    when(missedCalls.update(any(DBObject.class), any(DBObject.class))).thenReturn(response);

    assertTrue(missedCallsDAO.setState(new BasicDBObject("state", "Processing"), "Bogus"));
    verify(missedCalls, times(1)).update(any(DBObject.class), any(DBObject.class));
  }
  @Test
  public void testInsertCall() throws Exception {
    DBObject callObj = new BasicDBObject();

    assertTrue(missedCallsDAO.insertCall(callObj));
    assertTrue(callObj.containsField("arrival"));
    assertEquals("Pending", callObj.get("state"));
    verify(missedCalls, times(1)).insert(any(DBObject.class));
  }
 @Test
 public void testFetchNextPendingCall() throws Exception {
   missedCallsDAO.fetchNextPendingCall();
   verify(missedCalls, times(1))
       .findAndModify(
           any(DBObject.class),
           isNull(DBObject.class),
           any(DBObject.class),
           eq(false),
           any(DBObject.class),
           eq(true),
           eq(false));
 }
  @Test
  public void integrationTestFetchNextPendingCall() throws Exception {

    missedCallsDAO = new MissedCallsDAO(Resources.getDatabaseActivity());

    DBObject testCallFirst = new BasicDBObject("who", 9581300084L);
    DBObject testCallSecond = new BasicDBObject("who", 9581300084L);

    try {
      assertTrue(missedCallsDAO.insertCall(testCallFirst));
      Thread.sleep(1000);
      assertTrue(missedCallsDAO.insertCall(testCallSecond));

      Date tsFirst = (Date) testCallFirst.get("arrival");
      Date tsSecond = (Date) testCallSecond.get("arrival");
      assertTrue(tsFirst.before(tsSecond));

      DBObject fetchedCallFirst = missedCallsDAO.fetchNextPendingCall();
      assertNotNull(fetchedCallFirst);
      assertTrue(fetchedCallFirst.containsField("who"));
      assertEquals("Processing", fetchedCallFirst.get("state"));
      assertEquals(testCallSecond.get("_id"), fetchedCallFirst.get("_id"));

      DBObject fetchedCallSecond = missedCallsDAO.fetchNextPendingCall();
      assertNotNull(fetchedCallSecond);
      assertTrue(fetchedCallSecond.containsField("who"));
      assertEquals("Processing", fetchedCallSecond.get("state"));
      assertEquals(testCallFirst.get("_id"), fetchedCallSecond.get("_id"));

      tsFirst = (Date) fetchedCallFirst.get("arrival");
      tsSecond = (Date) fetchedCallSecond.get("arrival");
      assertTrue(tsFirst.after(tsSecond));
    } finally {
      deleteCall(testCallFirst);
      deleteCall(testCallSecond);
    }
  }
  @Test
  public void integrationTestInsertCall() throws Exception {

    missedCallsDAO = new MissedCallsDAO(Resources.getDatabaseActivity());

    DBObject testCall = new BasicDBObject("who", 9581300084L);
    try {
      assertTrue(missedCallsDAO.insertCall(testCall));
      assertTrue(testCall.containsField("arrival"));
      assertEquals("Pending", testCall.get("state"));
      assertTrue(testCall.containsField("_id"));
    } finally {
      deleteCall(testCall);
    }
  }