@Test
 public void testMessageToContentValuesWithCalllogFromUnknownPerson() throws Exception {
   PersonRecord record = new PersonRecord(-1, null, null, null);
   when(personLookup.lookupPerson("+12121")).thenReturn(record);
   final ContentValues values = messageConverter.messageToContentValues(createCallLogMessage());
   assertThat(values.containsKey(CallLog.Calls.CACHED_NAME)).isFalse();
   assertThat(values.containsKey(CallLog.Calls.CACHED_NUMBER_TYPE)).isFalse();
 }
 @Test
 public void testMessageToContentValuesWithCalllog() throws Exception {
   PersonRecord record = new PersonRecord(1, "The name", "*****@*****.**", "+1234");
   when(personLookup.lookupPerson("+12121")).thenReturn(record);
   final ContentValues values = messageConverter.messageToContentValues(createCallLogMessage());
   assertThat(values.getAsString(CallLog.Calls.NUMBER)).isEqualTo("+12121");
   assertThat(values.getAsString(CallLog.Calls.TYPE)).isEqualTo("3");
   assertThat(values.getAsString(CallLog.Calls.DATE)).isEqualTo("1419163218194");
   assertThat(values.getAsLong(CallLog.Calls.DURATION)).isEqualTo(44L);
   assertThat(values.getAsInteger(CallLog.Calls.NEW)).isEqualTo(0);
   assertThat(values.getAsString(CallLog.Calls.CACHED_NAME)).isEqualTo("The name");
   assertThat(values.getAsInteger(CallLog.Calls.CACHED_NUMBER_TYPE)).isEqualTo(-2);
 }
  public ContentValues messageToContentValues(final Message message)
      throws IOException, MessagingException {
    if (message == null) throw new MessagingException("message is null");

    final ContentValues values = new ContentValues();
    switch (getDataType(message)) {
      case SMS:
        if (message.getBody() == null) throw new MessagingException("body is null");

        InputStream is = message.getBody().getInputStream();
        if (is == null) {
          throw new MessagingException("body.getInputStream() is null for " + message.getBody());
        }
        final String body = IOUtils.toString(is);
        final String address = Headers.get(message, Headers.ADDRESS);
        values.put(SmsConsts.BODY, body);
        values.put(SmsConsts.ADDRESS, address);
        values.put(SmsConsts.TYPE, Headers.get(message, Headers.TYPE));
        values.put(SmsConsts.PROTOCOL, Headers.get(message, Headers.PROTOCOL));
        values.put(SmsConsts.SERVICE_CENTER, Headers.get(message, Headers.SERVICE_CENTER));
        values.put(SmsConsts.DATE, Headers.get(message, Headers.DATE));
        values.put(SmsConsts.STATUS, Headers.get(message, Headers.STATUS));
        values.put(SmsConsts.THREAD_ID, threadHelper.getThreadId(mContext, address));
        values.put(SmsConsts.READ, mMarkAsReadOnRestore ? "1" : Headers.get(message, Headers.READ));
        break;
      case CALLLOG:
        values.put(CallLog.Calls.NUMBER, Headers.get(message, Headers.ADDRESS));
        values.put(CallLog.Calls.TYPE, Integer.valueOf(Headers.get(message, Headers.TYPE)));
        values.put(CallLog.Calls.DATE, Headers.get(message, Headers.DATE));
        values.put(CallLog.Calls.DURATION, Long.valueOf(Headers.get(message, Headers.DURATION)));
        values.put(CallLog.Calls.NEW, 0);

        PersonRecord record = mPersonLookup.lookupPerson(Headers.get(message, Headers.ADDRESS));
        if (!record.isUnknown()) {
          values.put(CallLog.Calls.CACHED_NAME, record.getName());
          values.put(CallLog.Calls.CACHED_NUMBER_TYPE, -2);
        }

        break;
      default:
        throw new MessagingException("don't know how to restore " + getDataType(message));
    }

    return values;
  }