/**
   * @param statusResult the mail status results retrieved from a Mail Listener
   * @return the errors serialized as a String
   */
  public static String serializeErrors(MailStatusResult statusResult) {
    String result;

    Iterator<MailStatus> statuses = statusResult.getByState(MailState.FAILED);
    if (statuses.hasNext()) {
      StringBuilder builder =
          new StringBuilder("Some messages have failed to be sent for the following reasons: [");
      while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        builder.append('[');
        builder.append('[').append(status.getErrorSummary()).append(']').append(',');
        builder.append('[').append(status.getErrorDescription()).append(']');
        builder.append(']');
      }
      builder.append(']');
      result = builder.toString();
    } else {
      result = null;
    }
    return result;
  }
  @Test
  public void serializeErrors() throws Exception {
    MailStatusResult statusResult = mock(MailStatusResult.class);
    Date date = new Date();

    // Return failures for the test
    MailStatus status1 = new MailStatus();
    status1.setBatchId("batch1");
    status1.setState("prepare_error");
    status1.setDate(date);
    status1.setMessageId("<local@domain>");
    status1.setRecipients("*****@*****.**");
    status1.setErrorSummary("errorsummary1");
    status1.setErrorDescription("errordescription1");
    MailStatus status2 = new MailStatus();
    status2.setBatchId("batch2");
    status2.setState("send_error");
    status2.setDate(date);
    status2.setMessageId("<local@domain>");
    status2.setRecipients("*****@*****.**");
    status2.setErrorSummary("errorsummary2");
    status2.setErrorDescription("errordescription2");
    when(statusResult.getAllErrors()).thenReturn(Arrays.asList(status1, status2).iterator());

    assertEquals(
        "Some messages have failed to be sent: "
            + "[[messageId = [<local@domain>], batchId = [batch1], state = [prepare_error], "
            + "date = ["
            + date.toString()
            + "], recipients = [[email protected]], errorSummary = [errorsummary1], "
            + "errorDescription = [errordescription1]][messageId = [<local@domain>], batchId = [batch2], "
            + "state = [send_error], date = ["
            + date.toString()
            + "], recipients = [[email protected]], "
            + "errorSummary = [errorsummary2], errorDescription = [errordescription2]]]",
        MailStatusResultSerializer.serializeErrors(statusResult));
  }