package fr.sii.ogham.helper.email;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.internal.ArrayComparisonFailure;

/**
 * Utility class for checking the attachment of the received email is as
 * expected.
 * 
 * @author Aurélien Baudet
 *
 */
public class AssertAttachment {
	/**
	 * Shortcut for use with GreenMail. See
	 * {@link #assertEquals(ExpectedAttachment, Message)}.
	 * 
	 * @param expected
	 *            the expected attachment values
	 * @param actual
	 *            the received email that must contain the attachment (the array
	 *            must have only one email)
	 * @throws IOException
	 *             when email can't be read
	 * @throws MessagingException
	 *             when email values can't be accessed
	 */
	public static void assertEquals(ExpectedAttachment expected, Message[] actual) throws IOException, MessagingException {
		if (actual.length != 1) {
			throw new IllegalArgumentException("should have only one message but was " + actual.length);
		}
		assertEquals(expected, actual[0]);
	}

	/**
	 * Checks if the received email contains the expected attachment. It also
	 * ensures that the values of the attachment are respected by checking:
	 * <ul>
	 * <li>The mimetype of the attachment</li>
	 * <li>The description of the attachment</li>
	 * <li>The disposition of the attachment</li>
	 * <li>The content of the attachment</li>
	 * </ul>
	 * 
	 * @param expected
	 *            the expected attachment values
	 * @param actual
	 *            the received email that must contain the attachment
	 * @throws IOException
	 *             when email can't be read
	 * @throws MessagingException
	 *             when email values can't be accessed
	 */
	public static void assertEquals(ExpectedAttachment expected, Message actual) throws IOException, MessagingException {
		Object content = actual.getContent();
		Assert.assertTrue("should be multipart message", content instanceof Multipart);
		BodyPart part = getAttachment((Multipart) content, expected.getName());
		assertEquals(expected, part);
	}

	/**
	 * It ensures that the values of the received attachment are respected by
	 * checking:
	 * <ul>
	 * <li>The mimetype of the attachment</li>
	 * <li>The description of the attachment</li>
	 * <li>The disposition of the attachment</li>
	 * <li>The content of the attachment</li>
	 * </ul>
	 * 
	 * @param expected
	 *            the expected attachment values
	 * @param attachment
	 *            the received attachment
	 * @throws IOException
	 *             when email can't be read
	 * @throws MessagingException
	 *             when email values can't be accessed
	 */
	public static void assertEquals(ExpectedAttachment expected, BodyPart attachment) throws MessagingException, ArrayComparisonFailure, IOException {
		Assert.assertTrue("attachment " + expected.getName() + " should have mimetype " + expected.getMimetype() + " but was " + attachment.getContentType(),
				expected.getMimetype().matcher(attachment.getContentType()).matches());
		Assert.assertEquals("attachment " + expected.getName() + " should have description " + expected.getDescription(), expected.getDescription(), attachment.getDescription());
		Assert.assertEquals("attachment " + expected.getName() + " should have disposition " + expected.getDisposition(), expected.getDisposition(), attachment.getDisposition());
		Assert.assertArrayEquals("attachment " + expected.getName() + " has invalid content", expected.getContent(), getContent(attachment));
	}

	private static BodyPart getAttachment(Multipart multipart, String name) throws MessagingException {
		for (int i = 0; i < multipart.getCount(); i++) {
			BodyPart bodyPart = multipart.getBodyPart(i);
			if (name.equals(bodyPart.getFileName())) {
				return bodyPart;
			}
		}
		Assert.fail("No attachment with the name " + name);
		return null;
	}

	private static byte[] getContent(Part part) throws IOException, MessagingException {
		InputStream stream = part.getInputStream();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		IOUtils.copy(stream, baos);
		return baos.toByteArray();
	}
}

@@ -2877,32 +2877,121 @@
 n't be accessed%0A
+%09 * @throws ArrayComparisonFailure%0A%09 *             when there are unexpected differences%0A
 %09 */%0A%09public sta

Add missing javadoc