package pt.fccn.mobile.arquivo.tests.screenshot;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import pt.fccn.arquivo.selenium.Retry;
import pt.fccn.arquivo.util.AppendableErrorsBaseTest;

/**
 *
 * @author Ivo Branco <ivo.branco@fccn.pt>
 *
 */
public class ScreenshotTest extends AppendableErrorsBaseTest {

	private String testURL;

	public ScreenshotTest() {
		this.testURL = System.getProperty("test.url");
	}

	@Test
	@Retry
	public void screenshotTest() {
		String screenshotUrlStr = this.testURL + "/screenshot/?url=" + this.testURL
				+ "/noFrame/replay/19961013145650/http://www.fccn.pt/&width=2560&height=1440";

		byte[] imageBytes = takeScreenshot(screenshotUrlStr);

		String imagemd5 = getMd5(imageBytes);

		assertEquals("Verify screenshot md5sum", "8cd46ca5b1e2d1f899033e8335a41862", imagemd5);

	}

	private byte[] takeScreenshot(String screenshotUrlStr) {
		URL url;
		try {
			url = new URL(screenshotUrlStr);
		} catch (MalformedURLException e) {
			throw new RuntimeException(
					"Error generating URL to a screenshot of a page verify if you have passed correct test url parameter",
					e);
		}

		byte[] imageBytes;
		try {
			InputStream is = url.openStream();
			imageBytes = IOUtils.toByteArray(is);
		} catch (IOException e) {
			throw new RuntimeException("Error downloading a screenshot", e);
		}
		return imageBytes;
	}

	public static String getMd5(byte[] input) {
		try {

			// Static getInstance method is called with hashing MD5
			MessageDigest md = MessageDigest.getInstance("MD5");

			// digest() method is called to calculate message digest
			// of an input digest() return array of byte
			byte[] messageDigest = md.digest(input);

			// Convert byte array into signum representation
			BigInteger no = new BigInteger(1, messageDigest);

			// Convert message digest into hex value
			String hashtext = no.toString(16);
			while (hashtext.length() < 32) {
				hashtext = "0" + hashtext;
			}
			return hashtext;
		}

		// For specifying wrong message digest algorithms
		catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

}

@@ -700,16 +700,17 @@
 l%22);%0A%09%7D%0A
+%09
 %0A%09@Test%0A
@@ -749,16 +749,19 @@
 est() %7B%0A
+%09%09%0A
 %09%09String
@@ -1060,40 +1060,40 @@
 %22, %22
-8cd46ca5b1e2d1f899033e8335a41862
+0dfbcc33819da0b220cd649a852a53c3
 %22, i
@@ -1102,16 +1102,18 @@
 gemd5);%0A
+%09%09
 %0A%09%7D%0A%0A%09pr

Fix md5