From 96ded2d22b19f0b8a16b25690262d2ba398c724d Mon Sep 17 00:00:00 2001
From: "Stefan E. Funk" <funk@sub.uni-goettingen.de>
Date: Fri, 18 Nov 2016 23:39:04 +0100
Subject: [PATCH] New online tests added.

---
 .../middleware/OaiPmhOnlineTests.java         | 236 +++++++++++++-----
 1 file changed, 169 insertions(+), 67 deletions(-)

diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhOnlineTests.java b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhOnlineTests.java
index f96a02bc..71b40034 100644
--- a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhOnlineTests.java
+++ b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhOnlineTests.java
@@ -5,7 +5,6 @@ import info.textgrid.utils.httpclient.TGHttpClient;
 import info.textgrid.utils.httpclient.TGHttpResponse;
 
 import java.io.IOException;
-import java.net.MalformedURLException;
 import java.net.URL;
 
 import org.apache.cxf.helpers.IOUtils;
@@ -27,22 +26,17 @@ import org.junit.Test;
 
 public class OaiPmhOnlineTests {
 
-	// Time to wait between the single queries using resumption tokens in
-	// milliseconds.
-	private static final Long	TIME		= 100l;
-
 	// The OAIPMH host to be tested.
-	private static String		host		= "http://textgrid-esx1.gwdg.de/1.0/tgoaipmh/";
+	private static String		host	= "http://textgrid-esx1.gwdg.de/1.0/tgoaipmh/";
 
-	private static String		verb		= "ListRecords";
-	private static String		mdprefix	= "oai_dc";
+	// Some output finals.
+	private static final String	ERROR	= ">>> ERROR";
+	private static final String	OK		= ">>> OKIDO";
+	private static final String	TESTING	= "\n>>> TESTING ";
 
-	// The project ID of the project to be tested.
-	// Digitale Bibliothek (esx1 and esx2)
-	// private static String set =
-	// "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c";
-	// fu's shakespeare (esx1)
-	private static String		set			= "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d";
+	// Time to wait between the single queries using resumption tokens in
+	// milliseconds.
+	private static final Long	TIME	= 100l;
 
 	// **
 	// PREPARATIONS
@@ -82,10 +76,6 @@ public class OaiPmhOnlineTests {
 	// **
 
 	/**
-	 * <p>
-	 * Runs a getVersion test.
-	 * </p>
-	 * 
 	 * @throws IOException
 	 */
 	@Test
@@ -93,7 +83,7 @@ public class OaiPmhOnlineTests {
 
 		String shouldStartWith = "oaipmh-core";
 
-		System.out.println("...testing #GETVERSION for '" + shouldStartWith
+		System.out.println(TESTING + "#GETVERSION for '" + shouldStartWith
 				+ "'");
 
 		TGHttpResponse httpResponse = getHttpResponse(host + "oai/version");
@@ -103,50 +93,173 @@ public class OaiPmhOnlineTests {
 				.getInputstream());
 
 		if (status != HttpStatus.SC_OK || !response.startsWith(shouldStartWith)) {
-			System.out.println("\tstatus: " + status);
-			System.out.println("\tresponse should start with '"
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR + ": response should start with '"
 					+ shouldStartWith + "'");
 			assertTrue(false);
 		}
 
 		System.out.println("\tresponse: " + response);
+		System.out.println(OK);
 	}
 
 	/**
-	 * @param args
-	 * @throws InterruptedException
+	 * @throws IOException
 	 */
 	@Test
-	@Ignore
-	public void testItest() throws InterruptedException {
+	public void testRootUrl() throws IOException {
 
-		try {
-			String url = host + "/oai?verb=" + verb + "&metadataPrefix="
-					+ mdprefix + "&set=" + set;
+		String shouldContain = "www.textgridrep.de";
 
-			TGHttpResponse httpResponse = getHttpResponse(url);
-			int status = httpResponse.getStatusCode();
+		System.out.println(TESTING + "#ROOTURL");
 
-			System.out.println("  ##  status:   " + status + " "
-					+ httpResponse.getReasonPhrase());
+		TGHttpResponse httpResponse = getHttpResponse(host + "oai");
+		int status = httpResponse.getStatusCode();
 
-			String restok = showOAI(httpResponse, "oldtok");
+		String response = IOUtils.readStringFromStream(httpResponse
+				.getInputstream());
 
-			while (status == HttpStatus.SC_OK && !restok.equals("-1")) {
-				url = host + "/oai?verb=" + verb + "&resumptionToken=" + restok;
-				httpResponse = getHttpResponse(url);
-				restok = showOAI(httpResponse, restok);
-			}
+		if (status != HttpStatus.SC_OK || !response.contains(shouldContain)) {
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR + ": response should contain '"
+					+ shouldContain + "'");
+			assertTrue(false);
+		}
 
-		} catch (MalformedURLException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+		System.out.println("\tresponse: " + response);
+		System.out.println(OK);
+	}
+
+	/**
+	 * @throws IOException
+	 */
+	@Test
+	public void testIdentify() throws IOException {
+
+		String verb = "Identify";
+		String shouldContain = "www.textgridrep.de";
+
+		System.out.println(TESTING + "#IDENTIFY");
+
+		TGHttpResponse httpResponse = getHttpResponse(host + "oai?verb=" + verb);
+		int status = httpResponse.getStatusCode();
+
+		String response = IOUtils.readStringFromStream(httpResponse
+				.getInputstream());
+
+		if (status != HttpStatus.SC_OK || !response.contains(shouldContain)) {
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR + ": response should contain '"
+					+ shouldContain + "'");
+			assertTrue(false);
+		}
+
+		System.out.println("\tresponse: " + response);
+		System.out.println(OK);
+	}
+
+	/**
+	 * @throws IOException
+	 */
+	@Test
+	public void testListSets() throws IOException {
+
+		String verb = "ListSets";
+		String shouldContain = "<setSpec>project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c</setSpec>";
+
+		System.out.println(TESTING + "#LISTSETS");
+
+		TGHttpResponse httpResponse = getHttpResponse(host + "oai?verb=" + verb);
+		int status = httpResponse.getStatusCode();
+
+		String response = IOUtils.readStringFromStream(httpResponse
+				.getInputstream());
+
+		if (status != HttpStatus.SC_OK || !response.contains(shouldContain)) {
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR + ": response should contain '"
+					+ shouldContain + "'");
+			assertTrue(false);
+		}
+
+		System.out.println("\tresponse: " + response);
+		System.out.println(OK);
+	}
+
+	/**
+	 * @throws IOException
+	 */
+	@Test
+	public void testListRecords() throws IOException {
+
+		String verb = "ListRecords";
+		String prefix = "oai_dc";
+		String set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d";
+
+		System.out.println(TESTING + "#LISTRECORDS");
+
+		String url = host + "oai?verb=" + verb + "&metadataPrefix=" + prefix
+				+ "&set=" + set;
+
+		TGHttpResponse httpResponse = getHttpResponse(url);
+		int status = httpResponse.getStatusCode();
+
+		String restok = showOAI(httpResponse, "oldtok");
+
+		while (status == HttpStatus.SC_OK && !restok.equals("-1")) {
+			url = host + "/oai?verb=" + verb + "&resumptionToken=" + restok;
+			httpResponse = getHttpResponse(url);
+			restok = showOAI(httpResponse, restok);
+		}
+
+		if (status != HttpStatus.SC_OK) {
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR);
+			assertTrue(false);
+		}
+
+		System.out.println(OK);
+	}
+
+	/**
+	 * @throws IOException
+	 */
+	@Test
+	public void testListIdentifier() throws IOException {
+
+		String verb = "ListIdentifiers";
+		String prefix = "oai_dc";
+		String set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d";
+
+		System.out.println(TESTING + "#LISTIDENTIFIER");
+
+		String url = host + "oai?verb=" + verb + "&metadataPrefix=" + prefix
+				+ "&set=" + set;
+
+		TGHttpResponse httpResponse = getHttpResponse(url);
+		int status = httpResponse.getStatusCode();
+
+		String restok = showOAI(httpResponse, "oldtok");
+
+		while (status == HttpStatus.SC_OK && !restok.equals("-1")) {
+			url = host + "/oai?verb=" + verb + "&resumptionToken=" + restok;
+			httpResponse = getHttpResponse(url);
+			restok = showOAI(httpResponse, restok);
+		}
+
+		if (status != HttpStatus.SC_OK) {
+			System.err.println("\tstatus: " + status);
+			System.err.println(ERROR + ": " + restok);
+			assertTrue(false);
 		}
+
+		System.out.println(OK);
 	}
 
+	// **
+	// PRIVATE METHODS
+	// **
+
 	/**
 	 * @param theUrl
 	 * @return
@@ -162,7 +275,7 @@ public class OaiPmhOnlineTests {
 			e.printStackTrace();
 		}
 
-		System.out.println("\trequest: " + theUrl);
+		System.out.println("\trequest:  " + theUrl);
 
 		URL url = new URL(theUrl);
 		String host = url.getProtocol() + "://" + url.getHost();
@@ -181,20 +294,20 @@ public class OaiPmhOnlineTests {
 
 		String res = IOUtils.readStringFromStream(theResponse.getInputstream());
 
-		// System.out.println("  ##  response: " + res);
+		// System.out.println("\tresponse: " + res);
 
 		// String tguri = res.substring(res.indexOf("<ns2:relation>textgrid:",
 		// res.indexOf("</ns2:relation>")));
-		// System.out.println("  ##  tguri:    " + tguri);
+		// System.out.println("\ttguri:    " + tguri);
 
 		try {
 			String restokTmp = res.substring(res.indexOf("<resumptionToken"),
 					res.indexOf("</resumptionToken>"));
 			String restok = restokTmp.substring(restokTmp.indexOf(">") + 1);
-			System.out.println("  ##  token:    " + restok);
+			System.out.println("\ttoken:    " + restok);
 
 			String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1);
-			System.out.println("  ##  tokentag: " + toktag);
+			System.out.println("\ttokentag: " + toktag);
 
 			String sizeStr = toktag.substring(toktag
 					.indexOf("completeListSize=\"") + 18);
@@ -204,7 +317,7 @@ public class OaiPmhOnlineTests {
 					.substring(toktag.indexOf("cursor=\"") + 8);
 			int cursor = Integer.parseInt(cursorStr.substring(0,
 					cursorStr.indexOf("\"")));
-			System.out.println("  ##  size:     " + size + " / " + cursor);
+			System.out.println("\tsize:     " + size + " / " + cursor);
 
 			// if (restok.equals(theOldtok)) {
 			// System.out.println("  ## ERROR ##  oldtok == restok: ");
@@ -212,31 +325,20 @@ public class OaiPmhOnlineTests {
 			// System.out.println("  ##  oldtok: " + theOldtok);
 			// }
 
-			System.out
-					.println("--------------------------------------------------------------------------------");
-
-			if (cursor >= size) {
+			System.out.println("\t------------");
 
-				System.out.println("cursor >= size");
-
-				if (restok.isEmpty()) {
-					System.out.println("complete");
-				} else {
-					System.out
-							.println("error: token existing and size exceeded");
-				}
-
-				return "-1";
+			if (cursor >= size && !restok.isEmpty()) {
+				System.err.println(ERROR
+						+ ": size exeeded and token still contained");
+				assertTrue(false);
 			}
 
 			return restok;
 
 		} catch (IndexOutOfBoundsException e) {
-
-			System.out.println("error: " + e.getMessage());
 			e.printStackTrace();
 
-			return "-1";
+			return e.getMessage();
 		}
 	}
 
-- 
GitLab