package com.github.thanospapapetrou.nefeli.oai.pmh.domain;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Iterator;

import org.testng.Assert;
import org.testng.TestException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
 * Test for {@link OaiPmh}.
 * 
 * @author thanos
 */
@Test
public class OaiPmhTest {
	private static final String EXAMPLES = "/com/github/thanospapapetrou/nefeli/oai/pmh/examples";

	@DataProvider(name = "marshalUnmarshal")
	private static Iterator<Object[]> testMarshalUnmarshalData() throws URISyntaxException {
		final Iterator<File> examples = Arrays.asList(new File(OaiPmhTest.class.getResource(EXAMPLES).toURI()).listFiles()).iterator();
		return new Iterator<Object[]>() {
			@Override
			public boolean hasNext() {
				return examples.hasNext();
			}

			@Override
			public Object[] next() {
				final File example = examples.next();
				try (final InputStream inputStream = new FileInputStream(example)) {
					return new Object[] {OaiPmh.unmarshal(inputStream, Granularity.DAY)};
				} catch (final IOException e) {
					throw new TestException(String.format("Error loading OAI-PMH response from file %1$s", example), e);
				}
			}

			@Override
			public void remove() {
				examples.remove();
			}
		};
	}

	/**
	 * Test for {@link OaiPmh#marshal(java.io.OutputStream, Granularity)} and {@link OaiPmh#unmarshal(InputStream, Granularity)}.
	 * 
	 * @param oaiPmh
	 *            the OAI-PMH response to test with
	 * @throws IOException
	 *             if any errors occur
	 */
	@Test(dataProvider = "marshalUnmarshal")
	public void testMarshalUnmarshal(final OaiPmh oaiPmh) throws IOException {
		final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		oaiPmh.marshal(outputStream, Granularity.DAY);
		Assert.assertEquals(OaiPmh.unmarshal(new ByteArrayInputStream(outputStream.toByteArray()), Granularity.DAY), oaiPmh);
	}
}

@@ -356,41 +356,8 @@
 rt;%0A
-import org.testng.TestException;%0A
 impo
@@ -396,16 +396,16 @@
 ovider;%0A
+
 import o
@@ -775,21 +775,18 @@
 r%3CFile%3E 
-examp
+fi
 les = Ar
@@ -969,21 +969,18 @@
 %09return 
-examp
+fi
 les.hasN
@@ -1041,341 +1041,43 @@
 %09%09%09%09
-final File example = examples.next();%0A%09%09%09%09try (final InputStream inputStream = new FileInputStream(example)) %7B%0A%09%09%09%09%09return new Object%5B%5D %7BOaiPmh.unmarshal(inputStream, Granularity.DAY)%7D;%0A%09%09%09%09%7D catch (final IOException e) %7B%0A%09%09%09%09%09throw new TestException(String.format(%22Error loading OAI-PMH response from file %251$s%22, example), e);%0A%09%09%09%09%7D
+return new Object%5B%5D %7Bfiles.next()%7D;
 %0A%09%09%09
@@ -1122,21 +1122,18 @@
 ) %7B%0A%09%09%09%09
-examp
+fi
 les.remo
@@ -1300,22 +1300,23 @@
  @param 
-oaiPmh
+example
 %0A%09 *    
@@ -1331,23 +1331,14 @@
 the 
-OAI-PMH respons
+exampl
 e to
@@ -1499,21 +1499,20 @@
 nal 
-OaiPmh oaiPmh
+File example
 ) th
@@ -1522,32 +1522,177 @@
 s IOException %7B%0A
+%09%09try (final InputStream inputStream = new FileInputStream(example)) %7B%0A%09%09%09final OaiPmh oaiPmh = OaiPmh.unmarshal(inputStream, Granularity.DAY);%0A%09
 %09%09final ByteArra
@@ -1749,16 +1749,17 @@
 ream();%0A
+%09
 %09%09oaiPmh
@@ -1801,16 +1801,17 @@
 DAY);%0A%09%09
+%09
 Assert.a
@@ -1920,13 +1920,17 @@
 aiPmh);%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

Revert "Refactored test"