package mireka.transmission.dsn;

import static mireka.ExampleAddress.*;
import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.ParseException;

import mireka.ExampleMail;
import mireka.MailData;
import mireka.address.MailAddressFactory;
import mireka.transmission.EnhancedStatus;
import mireka.transmission.Mail;
import mireka.transmission.immediate.RemoteMta;
import mireka.transmission.immediate.Rfc821Status;

import org.apache.james.mime4j.message.Message;
import org.junit.Test;
import org.subethamail.smtp.client.SMTPClient.Response;

public class DsnMailCreatorTest {

    @Test
    public void testCreate() throws MessagingException, IOException {
        DsnMailCreator dsnMailCreator = createDsnMailCreator();
        Mail mail = ExampleMail.simple();
        List<PermanentFailureReport> recipientStatuses =
                createRecipientFailure();
        Mail dsnMail = dsnMailCreator.create(mail, recipientStatuses);

        writeToFileForDebugging(dsnMail.mailData);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        dsnMail.mailData.writeTo(out);
        byte[] bytes = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        Message message = new Message(in);
        assertEquals(message.getMimeType(), "multipart/report");
    }

    static DsnMailCreator createDsnMailCreator() {
        NameAddr from =
                new NameAddr("Mail Delivery Subsystem",
                        "mailer-daemon@example.com");
        return new DsnMailCreator(IP2.getHostName(), from);
    }

    static List<PermanentFailureReport> createRecipientFailure()
            throws ParseException {
        PermanentFailureReport f = new PermanentFailureReport();
        f.recipient =
                new MailAddressFactory().createRecipient("jane@example.com");
        // f.status =
        // new EnhancedStatus(550, "5.2.1",
        // "Mailbox disabled, not accepting messages");

        f.status = new EnhancedStatus(550, "5.2.1", longErrorMessage());
        f.remoteMtaDiagnosticStatus =
                new Rfc821Status(new Response(550,
                        "Requested action not taken: mailbox unavailable"));
        f.remoteMta = new RemoteMta(IP3.getHostName());
        f.failureDate = new Date();
        return Collections.singletonList(f);
    }

    private static String longErrorMessage() {
        String s = "";
        for (int i = 0; i < 100; i++) {
            s += "0123456789 ";
        }
        return s;
    }

    private void writeToFileForDebugging(MailData content)
            throws FileNotFoundException, IOException {
        File file =
                new File(System.getProperty("java.io.tmpdir"), getClass()
                        .getSimpleName()
                        + ".eml");
        FileOutputStream fout = new FileOutputStream(file);
        content.writeTo(fout);
        fout.close();
    }

}

@@ -2591,24 +2591,56 @@
 new Date();%0A
+        f.logId = %22NO_1_ENTRY%22;%0A
         retu

fix test, logId is required