From f57fcc91e2d9d38759790ac6c920a1ed6ce989d2 Mon Sep 17 00:00:00 2001 From: "Stefan E. Funk" <funk@sub.uni-goettingen.de> Date: Wed, 16 May 2018 16:47:41 +0200 Subject: [PATCH] POST removed again... testing... --- .../info/textgrid/middleware/OAIPMHImpl.java | 214 +++-- .../textgrid/middleware/OAIPMHProducer.java | 66 +- .../middleware/OaiPmhDariahdeOnlineTests.java | 836 ++++++++--------- .../middleware/OaiPmhTextgridOnlineTests.java | 866 +++++++++--------- .../src/main/webapp/WEB-INF/beans.xml | 87 +- 5 files changed, 1027 insertions(+), 1042 deletions(-) diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java index 53baf3ba..d4feeeca 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java @@ -24,12 +24,14 @@ import info.textgrid.middleware.oaipmh.RequestType; import info.textgrid.middleware.oaipmh.VerbType; /** + * <p> * This class takes all possible arguments of an OAI-PMH request and proceeds depended on the given - * arguments. It also deals as response handler + * arguments. It also deals as response handler. + * </p> * - * @author Maximilian Brodhun: SUB - * @version 1.0 - * @since 29.01.2014 + * @author Maximilian Brodhun, SUB Göttingen + * @version 2018-05-16 + * @since 2014-01-29 */ public class OAIPMHImpl implements OAIPMHProducer { @@ -47,6 +49,15 @@ public class OAIPMHImpl implements OAIPMHProducer { ListIdentifiersType lit = new ListIdentifiersType(); + /** + * @param oaiEsClient + * @param rep + * @param recDeliv + * @param recordList + * @param metadataFormatList + * @param setList + * @param identifierList + */ public OAIPMHImpl(OAI_ESClient oaiEsClient, RepIdentification rep, RecordDeliverer recDeliv, RecordListDeliverer recordList, MetadataFormatListDeliverer metadataFormatList, SetDeliverer setList, IdentifierListDeliverer identifierList) { @@ -59,19 +70,19 @@ public class OAIPMHImpl implements OAIPMHProducer { this.identifierList = identifierList; } - /* - * (non-Javadoc) - * - * @see info.textgrid.middleware.OAIPMHProducer#getRequest(java.lang.String, java.lang.String, - * java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - @POST - @Path("/") - @Produces("text/xml") - public JAXBElement<OAIPMHType> postRequest(String verb, String identifier, String metadataPrefix, - String set, String from, String until, String resumptionToken) { - return getRequest(verb, identifier, metadataPrefix, set, from, until, resumptionToken); - } +// /* +// * (non-Javadoc) +// * +// * @see info.textgrid.middleware.OAIPMHProducer#getRequest(java.lang.String, java.lang.String, +// * java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) +// */ +// @POST +// @Path("/") +// @Produces("text/xml") +// public JAXBElement<OAIPMHType> postRequest(String verb, String identifier, String metadataPrefix, +// String set, String from, String until, String resumptionToken) { +// return handleRequest(verb, identifier, metadataPrefix, set, from, until, resumptionToken); +// } /* * (non-Javadoc) @@ -84,71 +95,18 @@ public class OAIPMHImpl implements OAIPMHProducer { @Produces("text/xml") public JAXBElement<OAIPMHType> getRequest(String verb, String identifier, String metadataPrefix, String set, String from, String until, String resumptionToken) { + return handleRequest(verb, identifier, metadataPrefix, set, from, until, resumptionToken); + } - ObjectFactory obf = new ObjectFactory(); - OAIPMHType response = new OAIPMHType(); - JAXBElement<OAIPMHType> oaipmhRoot = obf.createOAIPMH(response); - VerbType verbParam = setVerb(verb); - RequestType request = new RequestType(); - - /*----Setting the responseDate of today-----*/ - try { - XMLGregorianCalendar convertedDateFormat; - convertedDateFormat = OAIPMHUtillities - .convertDateFormat(OAIPMHUtillities.getXMLGregorianCalendarNow().toString()); - response.setResponseDate(convertedDateFormat); - } catch (ParseException e) { - log.debug("can not parse date format"); - } catch (DatatypeConfigurationException e) { - log.debug("Datatype configuration failed"); - } - - /* converting the request values */ - - setFromRequestValue(from, request); - setUntilRequestValue(until, request); - setIdentifierRequestValue(identifier, request); - setMetadataPrefixRequestValue(metadataPrefix, request); - setResumptionTokenRequestValue(resumptionToken, request); - setSetRequestValue(set, request); - // request.setSet(set); - request.setVerb(verbParam); - - if (textgrid == true) { - request.setValue(TGConstants.TG_REP_BASEURL); - } - if (dariah == true) { - request.setValue(DARIAHConstants.DARIAH_REP_BASEURL); - } - - if (verbParam != null) { - if (verbParam.value().equals("Identify")) { - oaipmhRoot = identifyRequest(obf, response, request); - } else if (verbParam.value().equals("GetRecord")) { - oaipmhRoot = getRecordRequest(obf, response, request); - } else if (verbParam.value().equals("ListIdentifiers")) { - try { - oaipmhRoot = listIdentifiersRequest(obf, response, request); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else if (verbParam.value().equals("ListSets")) { - oaipmhRoot = listSetsRequest(obf, response, request); - } else if (verbParam.value().equals("ListMetadataFormats")) { - oaipmhRoot = listMetadataFormatsRequest(identifier, obf, response, request); - } else if (verbParam.value().equals("ListRecords")) { - oaipmhRoot = listRecordsRequest(obf, response, request); - } - } else { - response.getError().add(error.setError("VerbError")); - } - - response.setRequest(request); - - oaipmhRoot.setValue(response); - - return oaipmhRoot; + /** + * @return + */ + @GET + @Path("/version") + @Produces(MediaType.TEXT_PLAIN) + public String getVersion() { + return OaipmhServiceVersion.BUILDNAME + "-" + OaipmhServiceVersion.VERSION + "." + + OaipmhServiceVersion.BUILDDATE; } /** @@ -162,6 +120,7 @@ public class OAIPMHImpl implements OAIPMHProducer { */ public JAXBElement<OAIPMHType> identifyRequest(ObjectFactory obf, OAIPMHType oai, RequestType request) { + List<String> errorValues = new ArrayList<String>(); JAXBElement<OAIPMHType> oaipmhRoot = obf.createOAIPMH(oai); IdentifyType tgRepIdentificationRequest = new IdentifyType(); @@ -216,6 +175,7 @@ public class OAIPMHImpl implements OAIPMHProducer { */ public JAXBElement<OAIPMHType> getRecordRequest(ObjectFactory obf, OAIPMHType oai, RequestType request) { + List<String> errorValues = new ArrayList<String>(); // RecordDeliverer recDeliv = new RecordDeliverer(oaiEsClient, // TGConstants.TGFields); @@ -350,11 +310,8 @@ public class OAIPMHImpl implements OAIPMHProducer { RequestType request) { ListSetsType sl = new ListSetsType(); - JAXBElement<OAIPMHType> oaipmhRoot = obf.createOAIPMH(oai); - sl = this.setList.setListBuilder(); - oai.setListSets(sl); return oaipmhRoot; @@ -369,6 +326,7 @@ public class OAIPMHImpl implements OAIPMHProducer { */ public JAXBElement<OAIPMHType> listMetadataFormatsRequest(String id, ObjectFactory obf, OAIPMHType oai, RequestType request) { + List<String> errorValues = new ArrayList<String>(); ListMetadataFormatsType listMF = new ListMetadataFormatsType(); JAXBElement<OAIPMHType> oaipmhRoot = obf.createOAIPMH(oai); @@ -422,6 +380,7 @@ public class OAIPMHImpl implements OAIPMHProducer { */ public JAXBElement<OAIPMHType> listRecordsRequest(ObjectFactory obf, OAIPMHType oai, RequestType request) { + // System.out.println("blablablabla"); List<String> errorValues = new ArrayList<String>(); ListRecordsType listRecords = new ListRecordsType(); @@ -473,6 +432,7 @@ public class OAIPMHImpl implements OAIPMHProducer { * @return verbParam: the verbParam as an OAIPMH verb type */ public VerbType setVerb(String verb) { + VerbType verbParam = null; if (verb == null) { @@ -505,11 +465,9 @@ public class OAIPMHImpl implements OAIPMHProducer { */ public void setFromRequestValue(String from, RequestType request) { - if (!from.isEmpty()) { request.setFrom(from); } - } /** @@ -584,15 +542,87 @@ public class OAIPMHImpl implements OAIPMHProducer { this.dariah = dariah; } + // ** + // PRIVATE METHODS + // ** + /** + * @param verb + * @param identifier + * @param metadataPrefix + * @param set + * @param from + * @param until + * @param resumptionToken * @return */ - @GET - @Path("/version") - @Produces(MediaType.TEXT_PLAIN) - public String getVersion() { - return OaipmhServiceVersion.BUILDNAME + "-" + OaipmhServiceVersion.VERSION + "." - + OaipmhServiceVersion.BUILDDATE; + private JAXBElement<OAIPMHType> handleRequest(String verb, String identifier, + String metadataPrefix, String set, String from, String until, String resumptionToken) { + + ObjectFactory obf = new ObjectFactory(); + OAIPMHType response = new OAIPMHType(); + JAXBElement<OAIPMHType> oaipmhRoot = obf.createOAIPMH(response); + VerbType verbParam = setVerb(verb); + RequestType request = new RequestType(); + + /*----Setting the responseDate of today-----*/ + try { + XMLGregorianCalendar convertedDateFormat; + convertedDateFormat = OAIPMHUtillities + .convertDateFormat(OAIPMHUtillities.getXMLGregorianCalendarNow().toString()); + response.setResponseDate(convertedDateFormat); + } catch (ParseException e) { + log.debug("can not parse date format"); + } catch (DatatypeConfigurationException e) { + log.debug("Datatype configuration failed"); + } + + /* converting the request values */ + + setFromRequestValue(from, request); + setUntilRequestValue(until, request); + setIdentifierRequestValue(identifier, request); + setMetadataPrefixRequestValue(metadataPrefix, request); + setResumptionTokenRequestValue(resumptionToken, request); + setSetRequestValue(set, request); + // request.setSet(set); + request.setVerb(verbParam); + + if (textgrid == true) { + request.setValue(TGConstants.TG_REP_BASEURL); + } + if (dariah == true) { + request.setValue(DARIAHConstants.DARIAH_REP_BASEURL); + } + + if (verbParam != null) { + if (verbParam.value().equals("Identify")) { + oaipmhRoot = identifyRequest(obf, response, request); + } else if (verbParam.value().equals("GetRecord")) { + oaipmhRoot = getRecordRequest(obf, response, request); + } else if (verbParam.value().equals("ListIdentifiers")) { + try { + oaipmhRoot = listIdentifiersRequest(obf, response, request); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else if (verbParam.value().equals("ListSets")) { + oaipmhRoot = listSetsRequest(obf, response, request); + } else if (verbParam.value().equals("ListMetadataFormats")) { + oaipmhRoot = listMetadataFormatsRequest(identifier, obf, response, request); + } else if (verbParam.value().equals("ListRecords")) { + oaipmhRoot = listRecordsRequest(obf, response, request); + } + } else { + response.getError().add(error.setError("VerbError")); + } + + response.setRequest(request); + + oaipmhRoot.setValue(response); + + return oaipmhRoot; } } diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHProducer.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHProducer.java index 3a3a791f..450bdb68 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHProducer.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHProducer.java @@ -17,8 +17,8 @@ import javax.xml.bind.JAXBElement; * </p> * * @author Maximilian Brodhun, SUB - * @version 1.0 - * @since 15.01.2014 + * @version 2018-05-16 + * @since 2014-01-15 */ public interface OAIPMHProducer { @@ -55,37 +55,37 @@ public interface OAIPMHProducer { @QueryParam("until") @DefaultValue("") String until, @QueryParam("resumptionToken") @DefaultValue("") String resumptionToken); - /** - * <p> - * Getting the request values as FormParams from a REST POST request and proceeds appropriate to - * them. - * </p> - * - * @param verb - The parameter verb is a required parameter to all requests. Depending on it the - * request will be proceed. Possible values are: GetRecord, Identify, ListIdentifiers, - * ListMetadataFormats, ListRecords and ListSets In case of other verbs an error response - * will be send back - * @param identifier - This value indicates if the request will be proceed just for a single - * identifier of the repository - * @param metadataPrefix - To limit the response just for a special metadata format this value can - * be used. The prefix "oai_dc" has to be supported by all repositories - * @param set - To represent the architecture of the repository it is possible to make sets which - * work like categories. At the moment textgrid does not support sets - * @param from - Start value to filter the response for a specific interval - * @param until - End value to filter the response for a specific interval - * @param resumptionToken - Indicates how many value will be send back in the response - * @return OAIPMHType object containing the whole response - */ - @POST - @Path("/") - @Produces(MediaType.TEXT_XML) - JAXBElement<OAIPMHType> postRequest(@FormParam("verb") String verb, - @FormParam("identifier") @DefaultValue("") String identifier, - @FormParam("metadataPrefix") @DefaultValue("") String metadataPrefix, - @FormParam("set") @DefaultValue("") String set, - @FormParam("from") @DefaultValue("") String from, - @FormParam("until") @DefaultValue("") String until, - @FormParam("resumptionToken") @DefaultValue("") String resumptionToken); +// /** +// * <p> +// * Getting the request values as FormParams from a REST POST request and proceeds appropriate to +// * them. +// * </p> +// * +// * @param verb - The parameter verb is a required parameter to all requests. Depending on it the +// * request will be proceed. Possible values are: GetRecord, Identify, ListIdentifiers, +// * ListMetadataFormats, ListRecords and ListSets In case of other verbs an error response +// * will be send back +// * @param identifier - This value indicates if the request will be proceed just for a single +// * identifier of the repository +// * @param metadataPrefix - To limit the response just for a special metadata format this value can +// * be used. The prefix "oai_dc" has to be supported by all repositories +// * @param set - To represent the architecture of the repository it is possible to make sets which +// * work like categories. At the moment textgrid does not support sets +// * @param from - Start value to filter the response for a specific interval +// * @param until - End value to filter the response for a specific interval +// * @param resumptionToken - Indicates how many value will be send back in the response +// * @return OAIPMHType object containing the whole response +// */ +// @POST +// @Path("/") +// @Produces(MediaType.TEXT_XML) +// JAXBElement<OAIPMHType> postRequest(@FormParam("verb") String verb, +// @FormParam("identifier") @DefaultValue("") String identifier, +// @FormParam("metadataPrefix") @DefaultValue("") String metadataPrefix, +// @FormParam("set") @DefaultValue("") String set, +// @FormParam("from") @DefaultValue("") String from, +// @FormParam("until") @DefaultValue("") String until, +// @FormParam("resumptionToken") @DefaultValue("") String resumptionToken); /** * @return diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhDariahdeOnlineTests.java b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhDariahdeOnlineTests.java index f5b45b8a..d629d090 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhDariahdeOnlineTests.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhDariahdeOnlineTests.java @@ -1,10 +1,8 @@ package info.textgrid.middleware; import static org.junit.Assert.assertTrue; - import java.io.IOException; import java.net.URL; - import org.apache.cxf.helpers.IOUtils; import org.apache.http.HttpStatus; import org.junit.After; @@ -12,7 +10,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; - import info.textgrid.utils.httpclient.TGHttpClient; import info.textgrid.utils.httpclient.TGHttpResponse; @@ -21,441 +18,410 @@ import info.textgrid.utils.httpclient.TGHttpResponse; * Some online tests for the DARIAH-DE Repository OAIMPH service. * </p> * - * @author fugu + * @author Stefan E. Funk (SUB Göttingen) */ public class OaiPmhDariahdeOnlineTests { - // The OAIPMH host to be tested. - // private static String host = - // "http://trep.de.dariah.eu/1.0/oaipmh/"; - private static String host = "http://repository.de.dariah.eu/1.0/oaipmh/"; - - // Some output finals. - private static final String ERROR = ">>> ERROR"; - private static final String OK = ">>> OKIDO"; - private static final String TESTING = "\n>>> TESTING "; - - // Time to wait between the single queries using resumption tokens in - // milliseconds. - private static final Long TIME = 30000l; - - // ** - // PREPARATIONS - // ** - - /** - * @throws Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - - } - - /** - * @throws Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws Exception - */ - @After - public void tearDown() throws Exception { - } - - // ** - // TESTS - // ** - - /** - * @throws IOException - */ - @Test - public void testGetVersion() throws IOException { - - String shouldStartWith = "oaipmh-core"; - - System.out - .println(TESTING + "#GETVERSION for '" + shouldStartWith + "'"); - - TGHttpResponse httpResponse = getHttpResponse(host + "oai/version"); - int status = httpResponse.getStatusCode(); - - String response = IOUtils - .readStringFromStream(httpResponse.getInputstream()); - - if (status != HttpStatus.SC_OK - || !response.startsWith(shouldStartWith)) { - 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); - } - - /** - * @throws IOException - */ - @Test - public void testRootUrl() throws IOException { - - String shouldContain = "repository.de.dariah.eu"; - - System.out.println(TESTING + "#ROOTURL"); - - TGHttpResponse httpResponse = getHttpResponse(host + "oai"); - 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 testIdentify() throws IOException { - - String verb = "Identify"; - String shouldContain = "repository.de.dariah.eu"; - - 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"; - // Testing for set: Bahnsen 1670_Erschließung_20161017.xlsx - String shouldContain = "<setSpec>hdl:21.11113/0000-000B-CAD1-5</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 testListRecordsSinglePage() throws IOException { - - System.out.println(TESTING - + "#LISTRECORDS with expected single page (without token)"); - - // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx - // String set = "21.11113/0000-000B-CAD1-5"; - String set = ""; - - testList("ListRecords", set); - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsMorePages() throws IOException { - - System.out.println( - TESTING + "#LISTRECORDS with expected resumption token usage"); - - // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx - // String set = "21.11113/0000-000B-CAD1-5"; - String set = ""; - - testList("ListRecords", set); - } - - /** - * @throws IOException - */ - @Test - public void testListIdentifierSinglePage() throws IOException { - - System.out.println(TESTING - + "#LISTIDENTIFIERS with expected single page (without token)"); - - // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx - // String set = "21.11113/0000-000B-CAD1-5"; - String set = ""; - - testList("ListIdentifiers", set); - } - - /** - * @throws IOException - */ - @Test - public void testListIdentifiersMorePages() throws IOException { - - System.out.println(TESTING - + "#LISTIDENTIFIERS with expected resumption token usage"); - - // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx - // String set = "21.11113/0000-000B-CAD1-5"; - String set = ""; - - testList("ListIdentifiers", set); - } - - // ** - // PRIVATE METHODS - // ** - - /** - * @param theUrl - * @return - * @throws IOException - */ - private static TGHttpResponse getHttpResponse(String theUrl) - throws IOException { - - try { - Thread.sleep(TIME); - } catch (InterruptedException e) { - // Nothing interrupts here! - } - - System.out.println("\trequest: " + theUrl); - - URL url = new URL(theUrl); - String host = url.getProtocol() + "://" + url.getHost(); - String query = url.getPath() + "?" + url.getQuery(); - TGHttpClient client = new TGHttpClient(host); - - // Get response. - TGHttpResponse response = client.get(query); - - // Check HTTP status. - int status = response.getStatusCode(); - String reason = response.getReasonPhrase(); - - if (status != HttpStatus.SC_OK) { - System.err.println("\tstatus: " + status + " " + reason); - System.err.println(ERROR); - assertTrue(false); - } else { - System.out.println("\tstatus: " + status + " " + reason); - } - - return response; - } - - /** - * @param theResponse - * @return Resumption token string, if existing, -1 if either no - * <resumptionToken> tag is existing, tag is existing and has no - * token value. - * @throws IOException - */ - private static String examineResumptionTokenTag(TGHttpResponse theResponse, - String recordOrHeader, String oldtok) throws IOException { - - String res = IOUtils.readStringFromStream(theResponse.getInputstream()); - - // System.out.println(res); - - // Test for OAIPMH errors. - if (res.contains("<error code=\"badArgument\">")) { - System.err.println(ERROR + " IN OAIPMH RESPONSE: " + res); - assertTrue(false); - } - - // Count response objects at first. - int recordCount = 0; - int i = res.indexOf("<" + recordOrHeader + ">", 0); - while (i != -1) { - recordCount++; - i++; - i = res.indexOf("<" + recordOrHeader + ">", i); - } - - System.out.println("\t" + recordOrHeader + "s: " + recordCount); - - // Check if token tag is existing. - int tokStart = res.indexOf("<resumptionToken"); - int tokEnd = res.indexOf("</resumptionToken"); - - if (tokStart == -1 && tokEnd == -1) { - System.out.println("\ttoken: no token"); - - return "-1"; - } - - String restokTmp = res.substring(tokStart, tokEnd); - // Get token tag. - String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1) - .trim(); - System.out.println("\ttokentag: " + toktag); - - // Get token. - String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); - System.out.println("\ttoken: " + restok); - - // Check if old and new token are equal or not. - boolean tokchanged = !oldtok.equals(restok); - System.out.println("\ttokchngd: " + tokchanged); - - // Get completeListSize and cursor. - String sizeStr = toktag - .substring(toktag.indexOf("completeListSize=\"") + 18); - int size = Integer - .parseInt(sizeStr.substring(0, sizeStr.indexOf("\""))); - String cursorStr = toktag.substring(toktag.indexOf("cursor=\"") + 8); - int cursor = Integer - .parseInt(cursorStr.substring(0, cursorStr.indexOf("\""))); - System.out.println("\tsize: " + size + " / " + cursor); - - // If token is provided, and we have less than 100 elements: mekkern! - if (!restok.isEmpty()) { - synchronized (OaiPmhDariahdeOnlineTests.class) { - // Check <record> or <header> count, must be 100! - if (recordCount != 100) { - System.err.println(ERROR + ": " + recordOrHeader - + " count mismatch, must be 100 if token is provided, but is " - + recordCount); - } - if (size <= 100) { - System.err.println(ERROR - + ": completeListSize count mismatch, must be > 100 if token is provided, but is " - + size); - } - if (recordCount != 100 || size <= 100) { - assertTrue(false); - } - } - } - - // If no token is provided, stop querying. - else { - // Check <record> count, must be completeListSize % 100. - if (recordCount != size % 100) { - System.err.println(ERROR + ": " + recordOrHeader - + " count mismatch, should be " + size % 100 - + ", but is " + recordCount); - assertTrue(false); - } - - // No resumption token available in response. - return "-1"; - } - - System.out.println("\t------------"); - - return restok; - } - - /** - * @param theMillis - * @return - */ - public static String getDurationInSecs(long theMillis) { - - int SECS_IN_MILLIS = 1000; - int secs = SECS_IN_MILLIS; - - return theMillis / secs + " second" - + ((theMillis / secs) != 1 ? "s" : ""); - } - - /** - * @param theVerb - * @throws IOException - */ - private static void testList(String theVerb, String theSet) - throws IOException { - - long startTime = System.currentTimeMillis(); - - String testOccurance = "header"; - if (theVerb.equals("ListRecords")) { - testOccurance = "record"; - } - - String prefix = "oai_dc"; - - String url = host + "oai?verb=" + theVerb + "&metadataPrefix=" + prefix; - if (theSet != null && !theSet.equals("")) { - url += "&set=" + theSet; - } - - TGHttpResponse httpResponse = getHttpResponse(url); - int status = httpResponse.getStatusCode(); - - long timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + getDurationInSecs(timeRunning)); - - String restok = examineResumptionTokenTag(httpResponse, testOccurance, - ""); - - while (status == HttpStatus.SC_OK && !restok.equals("-1")) { - url = host + "/oai?verb=" + theVerb + "&resumptionToken=" + restok; - httpResponse = getHttpResponse(url); - timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + getDurationInSecs(timeRunning)); - restok = examineResumptionTokenTag(httpResponse, testOccurance, - restok); - } - - System.out.println(OK); - } + // The OAIPMH host to be tested. + // private static String host = + // "http://trep.de.dariah.eu/1.0/oaipmh/"; + private static String host = "https://repository.de.dariah.eu/1.0/oaipmh/"; + + // Some output finals. + private static final String ERROR = ">>> ERROR"; + private static final String OK = ">>> OKIDO"; + private static final String TESTING = "\n>>> TESTING "; + + // Time to wait between the single queries using resumption tokens in + // milliseconds. + private static final Long TIME = 30000l; + + // ** + // PREPARATIONS + // ** + + /** + * @throws Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + } + + /** + * @throws Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception {} + + /** + * @throws Exception + */ + @Before + public void setUp() throws Exception {} + + /** + * @throws Exception + */ + @After + public void tearDown() throws Exception {} + + // ** + // TESTS + // ** + + /** + * @throws IOException + */ + @Test + public void testGetVersion() throws IOException { + + String shouldStartWith = "oaipmh-core"; + + System.out.println(TESTING + "#GETVERSION for '" + shouldStartWith + "'"); + + TGHttpResponse httpResponse = getHttpResponse(host + "oai/version"); + int status = httpResponse.getStatusCode(); + + String response = IOUtils.readStringFromStream(httpResponse.getInputstream()); + + if (status != HttpStatus.SC_OK || !response.startsWith(shouldStartWith)) { + 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); + } + + /** + * @throws IOException + */ + @Test + public void testRootUrl() throws IOException { + + String shouldContain = "repository.de.dariah.eu"; + + System.out.println(TESTING + "#ROOTURL"); + + TGHttpResponse httpResponse = getHttpResponse(host + "oai"); + 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 testIdentify() throws IOException { + + String verb = "Identify"; + String shouldContain = "repository.de.dariah.eu"; + + 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"; + // Testing for set: Bahnsen 1670_Erschließung_20161017.xlsx + String shouldContain = "<setSpec>hdl:21.11113/0000-000B-CAD1-5</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 testListRecordsSinglePage() throws IOException { + + System.out.println(TESTING + "#LISTRECORDS with expected single page (without token)"); + + // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx + // String set = "21.11113/0000-000B-CAD1-5"; + String set = ""; + + testList("ListRecords", set); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsMorePages() throws IOException { + + System.out.println(TESTING + "#LISTRECORDS with expected resumption token usage"); + + // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx + // String set = "21.11113/0000-000B-CAD1-5"; + String set = ""; + + testList("ListRecords", set); + } + + /** + * @throws IOException + */ + @Test + public void testListIdentifierSinglePage() throws IOException { + + System.out.println(TESTING + "#LISTIDENTIFIERS with expected single page (without token)"); + + // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx + // String set = "21.11113/0000-000B-CAD1-5"; + String set = ""; + + testList("ListIdentifiers", set); + } + + /** + * @throws IOException + */ + @Test + public void testListIdentifiersMorePages() throws IOException { + + System.out.println(TESTING + "#LISTIDENTIFIERS with expected resumption token usage"); + + // Testing set: Bahnsen 1670_Erschließung_20161017.xlsx + // String set = "21.11113/0000-000B-CAD1-5"; + String set = ""; + + testList("ListIdentifiers", set); + } + + // ** + // PRIVATE METHODS + // ** + + /** + * @param theUrl + * @return + * @throws IOException + */ + private static TGHttpResponse getHttpResponse(String theUrl) throws IOException { + + try { + Thread.sleep(TIME); + } catch (InterruptedException e) { + // Nothing interrupts here! + } + + System.out.println("\trequest: " + theUrl); + + URL url = new URL(theUrl); + String host = url.getProtocol() + "://" + url.getHost(); + String query = url.getPath() + "?" + url.getQuery(); + TGHttpClient client = new TGHttpClient(host); + + // Get response. + TGHttpResponse response = client.get(query); + + // Check HTTP status. + int status = response.getStatusCode(); + String reason = response.getReasonPhrase(); + + if (status != HttpStatus.SC_OK) { + System.err.println("\tstatus: " + status + " " + reason); + System.err.println(ERROR); + assertTrue(false); + } else { + System.out.println("\tstatus: " + status + " " + reason); + } + + return response; + } + + /** + * @param theResponse + * @return Resumption token string, if existing, -1 if either no <resumptionToken> tag is + * existing, tag is existing and has no token value. + * @throws IOException + */ + private static String examineResumptionTokenTag(TGHttpResponse theResponse, String recordOrHeader, + String oldtok) throws IOException { + + String res = IOUtils.readStringFromStream(theResponse.getInputstream()); + + // System.out.println(res); + + // Test for OAIPMH errors. + if (res.contains("<error code=\"badArgument\">")) { + System.err.println(ERROR + " IN OAIPMH RESPONSE: " + res); + assertTrue(false); + } + + // Count response objects at first. + int recordCount = 0; + int i = res.indexOf("<" + recordOrHeader + ">", 0); + while (i != -1) { + recordCount++; + i++; + i = res.indexOf("<" + recordOrHeader + ">", i); + } + + System.out.println("\t" + recordOrHeader + "s: " + recordCount); + + // Check if token tag is existing. + int tokStart = res.indexOf("<resumptionToken"); + int tokEnd = res.indexOf("</resumptionToken"); + + if (tokStart == -1 && tokEnd == -1) { + System.out.println("\ttoken: no token"); + + return "-1"; + } + + String restokTmp = res.substring(tokStart, tokEnd); + // Get token tag. + String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1).trim(); + System.out.println("\ttokentag: " + toktag); + + // Get token. + String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); + System.out.println("\ttoken: " + restok); + + // Check if old and new token are equal or not. + boolean tokchanged = !oldtok.equals(restok); + System.out.println("\ttokchngd: " + tokchanged); + + // Get completeListSize and cursor. + String sizeStr = toktag.substring(toktag.indexOf("completeListSize=\"") + 18); + int size = Integer.parseInt(sizeStr.substring(0, sizeStr.indexOf("\""))); + String cursorStr = toktag.substring(toktag.indexOf("cursor=\"") + 8); + int cursor = Integer.parseInt(cursorStr.substring(0, cursorStr.indexOf("\""))); + System.out.println("\tsize: " + size + " / " + cursor); + + // If token is provided, and we have less than 100 elements: mekkern! + if (!restok.isEmpty()) { + synchronized (OaiPmhDariahdeOnlineTests.class) { + // Check <record> or <header> count, must be 100! + if (recordCount != 100) { + System.err.println(ERROR + ": " + recordOrHeader + + " count mismatch, must be 100 if token is provided, but is " + recordCount); + } + if (size <= 100) { + System.err.println(ERROR + + ": completeListSize count mismatch, must be > 100 if token is provided, but is " + + size); + } + if (recordCount != 100 || size <= 100) { + assertTrue(false); + } + } + } + + // If no token is provided, stop querying. + else { + // Check <record> count, must be completeListSize % 100. + if (recordCount != size % 100) { + System.err.println(ERROR + ": " + recordOrHeader + " count mismatch, should be " + + size % 100 + ", but is " + recordCount); + assertTrue(false); + } + + // No resumption token available in response. + return "-1"; + } + + System.out.println("\t------------"); + + return restok; + } + + /** + * @param theMillis + * @return + */ + public static String getDurationInSecs(long theMillis) { + + int SECS_IN_MILLIS = 1000; + int secs = SECS_IN_MILLIS; + + return theMillis / secs + " second" + ((theMillis / secs) != 1 ? "s" : ""); + } + + /** + * @param theVerb + * @throws IOException + */ + private static void testList(String theVerb, String theSet) throws IOException { + + long startTime = System.currentTimeMillis(); + + String testOccurance = "header"; + if (theVerb.equals("ListRecords")) { + testOccurance = "record"; + } + + String prefix = "oai_dc"; + + String url = host + "oai?verb=" + theVerb + "&metadataPrefix=" + prefix; + if (theSet != null && !theSet.equals("")) { + url += "&set=" + theSet; + } + + TGHttpResponse httpResponse = getHttpResponse(url); + int status = httpResponse.getStatusCode(); + + long timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + getDurationInSecs(timeRunning)); + + String restok = examineResumptionTokenTag(httpResponse, testOccurance, ""); + + while (status == HttpStatus.SC_OK && !restok.equals("-1")) { + url = host + "/oai?verb=" + theVerb + "&resumptionToken=" + restok; + httpResponse = getHttpResponse(url); + timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + getDurationInSecs(timeRunning)); + restok = examineResumptionTokenTag(httpResponse, testOccurance, restok); + } + + System.out.println(OK); + } } diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTextgridOnlineTests.java b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTextgridOnlineTests.java index b1690766..d82601fd 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTextgridOnlineTests.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTextgridOnlineTests.java @@ -1,10 +1,8 @@ package info.textgrid.middleware; import static org.junit.Assert.assertTrue; - import java.io.IOException; import java.net.URL; - import org.apache.cxf.helpers.IOUtils; import org.apache.http.HttpStatus; import org.junit.After; @@ -12,7 +10,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; - import info.textgrid.utils.httpclient.TGHttpClient; import info.textgrid.utils.httpclient.TGHttpResponse; @@ -21,456 +18,423 @@ import info.textgrid.utils.httpclient.TGHttpResponse; * Some online tests for the TextGrid OAIMPH service. * </p> * - * @author fugu + * @author Stefan E. Funk, SUB Göttingen */ - public class OaiPmhTextgridOnlineTests { - // The OAIPMH host to be tested. - // private static String host = - // "http://textgrid-esx1.gwdg.de/1.0/tgoaipmh/"; - private static String host = "http://textgridlab.org/1.0/tgoaipmh/"; - - // Some output finals. - private static final String ERROR = ">>> ERROR"; - private static final String OK = ">>> OKIDO"; - private static final String TESTING = "\n>>> TESTING "; - - // Time to wait between the single queries using resumption tokens in - // milliseconds. - private static final Long TIME = 250l; - - // ** - // PREPARATIONS - // ** - - /** - * @throws Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - - } - - /** - * @throws Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws Exception - */ - @After - public void tearDown() throws Exception { - } - - // ** - // TESTS - // ** - - /** - * @throws IOException - */ - @Test - public void testGetVersion() throws IOException { - - String shouldStartWith = "oaipmh-core"; - - System.out - .println(TESTING + "#GETVERSION for '" + shouldStartWith + "'"); - - TGHttpResponse httpResponse = getHttpResponse(host + "oai/version"); - int status = httpResponse.getStatusCode(); - - String response = IOUtils - .readStringFromStream(httpResponse.getInputstream()); - - if (status != HttpStatus.SC_OK - || !response.startsWith(shouldStartWith)) { - 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); - } - - /** - * @throws IOException - */ - @Test - public void testRootUrl() throws IOException { - - String shouldContain = "www.textgridrep.de"; - - System.out.println(TESTING + "#ROOTURL"); - - TGHttpResponse httpResponse = getHttpResponse(host + "oai"); - 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 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"; - // Testing for set: Digital Bibliothek. - 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 testListRecordsSinglePage() throws IOException { - - System.out.println(TESTING - + "#LISTRECORDS with expected single page (without token)"); - - // IIIF - String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; - - testList("ListRecords", set); - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsMorePages() throws IOException { - - System.out.println( - TESTING + "#LISTRECORDS with expected resumption token usage"); - - // Testing set: Digitale Bibliothek (TODO Find another project with less - // records for testing!). - String set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; - // Testing set: IIIF - // String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; - // Use fu's test project, if test instance used in host name. - if (host.contains("textgrid-esx1.gwdg.de")) { - // Testing set: fu's shakespeare. - set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d"; - // Testing set: Digitale Bibliothek. - // set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; - } - - testList("ListRecords", set); - } - - /** - * @throws IOException - */ - @Test - // @Ignore - public void testListIdentifierSinglePage() throws IOException { - - System.out.println(TESTING - + "#LISTIDENTIFIERS with expected single page (without token)"); - - // IIIF - String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; - - testList("ListIdentifiers", set); - } - - /** - * @throws IOException - */ - @Test - public void testListIdentifiersMorePages() throws IOException { - - System.out.println(TESTING - + "#LISTIDENTIFIERS with expected resumption token usage"); - - // Testing set: Digitale Bibliothek (TODO Find another project with less - // records for testing!). - String set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; - // Testing set: IIIF - // String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; - // Use fu's test project, if test instance used in host name. - if (host.contains("textgrid-esx1.gwdg.de")) { - // Testing set: fu's shakespeare. - set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d"; - // Testing set: Digitale Bibliothek. - // set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; - } - - testList("ListIdentifiers", set); - } - - // ** - // PRIVATE METHODS - // ** - - /** - * @param theUrl - * @return - * @throws IOException - */ - private static TGHttpResponse getHttpResponse(String theUrl) - throws IOException { - - try { - Thread.sleep(TIME); - } catch (InterruptedException e) { - // Nothing interrupts here! - } - - System.out.println("\trequest: " + theUrl); - - URL url = new URL(theUrl); - String host = url.getProtocol() + "://" + url.getHost(); - String query = url.getPath() + "?" + url.getQuery(); - TGHttpClient client = new TGHttpClient(host); - - // Get response. - TGHttpResponse response = client.get(query); - - // Check HTTP status. - int status = response.getStatusCode(); - String reason = response.getReasonPhrase(); - - if (status != HttpStatus.SC_OK) { - System.err.println("\tstatus: " + status + " " + reason); - System.err.println(ERROR); - assertTrue(false); - } else { - System.out.println("\tstatus: " + status + " " + reason); - } - - return response; - } - - /** - * @param theResponse - * @return Resumption token string, if existing, -1 if either no - * <resumptionToken> tag is existing, tag is existing and has no - * token value. - * @throws IOException - */ - private static String examineResumptionTokenTag(TGHttpResponse theResponse, - String recordOrHeader, String oldtok) throws IOException { - - String res = IOUtils.readStringFromStream(theResponse.getInputstream()); - - // System.out.println(res); - - // Test for OAIPMH errors. - if (res.contains("<error code=\"badArgument\">")) { - System.err.println(ERROR + " IN OAIPMH RESPONSE: " + res); - assertTrue(false); - } - - // Count response objects at first. - int recordCount = 0; - int i = res.indexOf("<" + recordOrHeader + ">", 0); - while (i != -1) { - recordCount++; - i++; - i = res.indexOf("<" + recordOrHeader + ">", i); - } - - System.out.println("\t" + recordOrHeader + "s: " + recordCount); - - // Check if token tag is existing. - int tokStart = res.indexOf("<resumptionToken"); - int tokEnd = res.indexOf("</resumptionToken"); - - if (tokStart == -1 && tokEnd == -1) { - System.out.println("\ttoken: no token"); - - return "-1"; - } - - String restokTmp = res.substring(tokStart, tokEnd); - // Get token tag. - String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1) - .trim(); - System.out.println("\ttokentag: " + toktag); - - // Get token. - String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); - System.out.println("\ttoken: " + restok); - - // Check if old and new token are equal or not. - boolean tokchanged = !oldtok.equals(restok); - System.out.println("\ttokchngd: " + tokchanged); - - // Get completeListSize and cursor. - String sizeStr = toktag - .substring(toktag.indexOf("completeListSize=\"") + 18); - int size = Integer - .parseInt(sizeStr.substring(0, sizeStr.indexOf("\""))); - String cursorStr = toktag.substring(toktag.indexOf("cursor=\"") + 8); - int cursor = Integer - .parseInt(cursorStr.substring(0, cursorStr.indexOf("\""))); - System.out.println("\tsize: " + size + " / " + cursor); - - // If token is provided, and we have less than 100 elements: mekkern! - if (!restok.isEmpty()) { - synchronized (OaiPmhTextgridOnlineTests.class) { - // Check <record> or <header> count, must be 100! - if (recordCount != 100) { - System.err.println(ERROR + ": " + recordOrHeader - + " count mismatch, must be 100 if token is provided, but is " - + recordCount); - } - if (size <= 100) { - System.err.println(ERROR - + ": completeListSize count mismatch, must be > 100 if token is provided, but is " - + size); - } - if (recordCount != 100 || size <= 100) { - assertTrue(false); - } - } - } - - // If no token is provided, stop querying. - else { - // Check <record> count, must be completeListSize % 100. - if (recordCount != size % 100) { - System.err.println(ERROR + ": " + recordOrHeader - + " count mismatch, should be " + size % 100 - + ", but is " + recordCount); - assertTrue(false); - } - - // No resumption token available in response. - return "-1"; - } - - System.out.println("\t------------"); - - return restok; - } - - /** - * @param theMillis - * @return - */ - public static String getDurationInSecs(long theMillis) { - - int SECS_IN_MILLIS = 1000; - int secs = SECS_IN_MILLIS; - - return theMillis / secs + " second" - + ((theMillis / secs) != 1 ? "s" : ""); - } - - /** - * @param theVerb - * @throws IOException - */ - private static void testList(String theVerb, String theSet) - throws IOException { - - long startTime = System.currentTimeMillis(); - - String testOccurance = "header"; - if (theVerb.equals("ListRecords")) { - testOccurance = "record"; - } - - String prefix = "oai_dc"; - - String url = host + "oai?verb=" + theVerb + "&metadataPrefix=" + prefix - + "&set=" + theSet; - - TGHttpResponse httpResponse = getHttpResponse(url); - int status = httpResponse.getStatusCode(); - - long timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + getDurationInSecs(timeRunning)); - - String restok = examineResumptionTokenTag(httpResponse, testOccurance, - ""); - - while (status == HttpStatus.SC_OK && !restok.equals("-1")) { - url = host + "/oai?verb=" + theVerb + "&resumptionToken=" + restok; - httpResponse = getHttpResponse(url); - timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + getDurationInSecs(timeRunning)); - restok = examineResumptionTokenTag(httpResponse, testOccurance, - restok); - } - - System.out.println(OK); - } + // The OAIPMH host to be tested. + // private static String host = + // "http://textgrid-esx1.gwdg.de/1.0/tgoaipmh/"; + private static String host = "https://textgridlab.org/1.0/tgoaipmh/"; + + // Some output finals. + private static final String ERROR = ">>> ERROR"; + private static final String OK = ">>> OKIDO"; + private static final String TESTING = "\n>>> TESTING "; + + // Time to wait between the single queries using resumption tokens in + // milliseconds. + private static final Long TIME = 250l; + + // ** + // PREPARATIONS + // ** + + /** + * @throws Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + } + + /** + * @throws Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception {} + + /** + * @throws Exception + */ + @Before + public void setUp() throws Exception {} + + /** + * @throws Exception + */ + @After + public void tearDown() throws Exception {} + + // ** + // TESTS + // ** + + /** + * @throws IOException + */ + @Test + public void testGetVersion() throws IOException { + + String shouldStartWith = "oaipmh-core"; + + System.out.println(TESTING + "#GETVERSION for '" + shouldStartWith + "'"); + + TGHttpResponse httpResponse = getHttpResponse(host + "oai/version"); + int status = httpResponse.getStatusCode(); + + String response = IOUtils.readStringFromStream(httpResponse.getInputstream()); + + if (status != HttpStatus.SC_OK || !response.startsWith(shouldStartWith)) { + 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); + } + + /** + * @throws IOException + */ + @Test + public void testRootUrl() throws IOException { + + String shouldContain = "www.textgridrep.de"; + + System.out.println(TESTING + "#ROOTURL"); + + TGHttpResponse httpResponse = getHttpResponse(host + "oai"); + 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 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"; + // Testing for set: Digital Bibliothek. + 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 testListRecordsSinglePage() throws IOException { + + System.out.println(TESTING + "#LISTRECORDS with expected single page (without token)"); + + // IIIF + String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; + + testList("ListRecords", set); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsMorePages() throws IOException { + + System.out.println(TESTING + "#LISTRECORDS with expected resumption token usage"); + + // Testing set: Digitale Bibliothek (TODO Find another project with less + // records for testing!). + String set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; + // Testing set: IIIF + // String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; + // Use fu's test project, if test instance used in host name. + if (host.contains("textgrid-esx1.gwdg.de")) { + // Testing set: fu's shakespeare. + set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d"; + // Testing set: Digitale Bibliothek. + // set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; + } + + testList("ListRecords", set); + } + + /** + * @throws IOException + */ + @Test + // @Ignore + public void testListIdentifierSinglePage() throws IOException { + + System.out.println(TESTING + "#LISTIDENTIFIERS with expected single page (without token)"); + + // IIIF + String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; + + testList("ListIdentifiers", set); + } + + /** + * @throws IOException + */ + @Test + public void testListIdentifiersMorePages() throws IOException { + + System.out.println(TESTING + "#LISTIDENTIFIERS with expected resumption token usage"); + + // Testing set: Digitale Bibliothek (TODO Find another project with less + // records for testing!). + String set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; + // Testing set: IIIF + // String set = "project:TGPR-2e532567-c3e3-36f5-3ecc-520cea7904cc"; + // Use fu's test project, if test instance used in host name. + if (host.contains("textgrid-esx1.gwdg.de")) { + // Testing set: fu's shakespeare. + set = "project:TGPR-962c949c-56a3-7134-9265-582f29a0992d"; + // Testing set: Digitale Bibliothek. + // set = "project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c"; + } + + testList("ListIdentifiers", set); + } + + // ** + // PRIVATE METHODS + // ** + + /** + * @param theUrl + * @return + * @throws IOException + */ + private static TGHttpResponse getHttpResponse(String theUrl) throws IOException { + + try { + Thread.sleep(TIME); + } catch (InterruptedException e) { + // Nothing interrupts here! + } + + System.out.println("\trequest: " + theUrl); + + URL url = new URL(theUrl); + String host = url.getProtocol() + "://" + url.getHost(); + String query = url.getPath() + "?" + url.getQuery(); + TGHttpClient client = new TGHttpClient(host); + + // Get response. + TGHttpResponse response = client.get(query); + + // Check HTTP status. + int status = response.getStatusCode(); + String reason = response.getReasonPhrase(); + + if (status != HttpStatus.SC_OK) { + System.err.println("\tstatus: " + status + " " + reason); + System.err.println(ERROR); + assertTrue(false); + } else { + System.out.println("\tstatus: " + status + " " + reason); + } + + return response; + } + + /** + * @param theResponse + * @return Resumption token string, if existing, -1 if either no <resumptionToken> tag is + * existing, tag is existing and has no token value. + * @throws IOException + */ + private static String examineResumptionTokenTag(TGHttpResponse theResponse, String recordOrHeader, + String oldtok) throws IOException { + + String res = IOUtils.readStringFromStream(theResponse.getInputstream()); + + // System.out.println(res); + + // Test for OAIPMH errors. + if (res.contains("<error code=\"badArgument\">")) { + System.err.println(ERROR + " IN OAIPMH RESPONSE: " + res); + assertTrue(false); + } + + // Count response objects at first. + int recordCount = 0; + int i = res.indexOf("<" + recordOrHeader + ">", 0); + while (i != -1) { + recordCount++; + i++; + i = res.indexOf("<" + recordOrHeader + ">", i); + } + + System.out.println("\t" + recordOrHeader + "s: " + recordCount); + + // Check if token tag is existing. + int tokStart = res.indexOf("<resumptionToken"); + int tokEnd = res.indexOf("</resumptionToken"); + + if (tokStart == -1 && tokEnd == -1) { + System.out.println("\ttoken: no token"); + + return "-1"; + } + + String restokTmp = res.substring(tokStart, tokEnd); + // Get token tag. + String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1).trim(); + System.out.println("\ttokentag: " + toktag); + + // Get token. + String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); + System.out.println("\ttoken: " + restok); + + // Check if old and new token are equal or not. + boolean tokchanged = !oldtok.equals(restok); + System.out.println("\ttokchngd: " + tokchanged); + + // Get completeListSize and cursor. + String sizeStr = toktag.substring(toktag.indexOf("completeListSize=\"") + 18); + int size = Integer.parseInt(sizeStr.substring(0, sizeStr.indexOf("\""))); + String cursorStr = toktag.substring(toktag.indexOf("cursor=\"") + 8); + int cursor = Integer.parseInt(cursorStr.substring(0, cursorStr.indexOf("\""))); + System.out.println("\tsize: " + size + " / " + cursor); + + // If token is provided, and we have less than 100 elements: mekkern! + if (!restok.isEmpty()) { + synchronized (OaiPmhTextgridOnlineTests.class) { + // Check <record> or <header> count, must be 100! + if (recordCount != 100) { + System.err.println(ERROR + ": " + recordOrHeader + + " count mismatch, must be 100 if token is provided, but is " + recordCount); + } + if (size <= 100) { + System.err.println(ERROR + + ": completeListSize count mismatch, must be > 100 if token is provided, but is " + + size); + } + if (recordCount != 100 || size <= 100) { + assertTrue(false); + } + } + } + + // If no token is provided, stop querying. + else { + // Check <record> count, must be completeListSize % 100. + if (recordCount != size % 100) { + System.err.println(ERROR + ": " + recordOrHeader + " count mismatch, should be " + + size % 100 + ", but is " + recordCount); + assertTrue(false); + } + + // No resumption token available in response. + return "-1"; + } + + System.out.println("\t------------"); + + return restok; + } + + /** + * @param theMillis + * @return + */ + public static String getDurationInSecs(long theMillis) { + + int SECS_IN_MILLIS = 1000; + int secs = SECS_IN_MILLIS; + + return theMillis / secs + " second" + ((theMillis / secs) != 1 ? "s" : ""); + } + + /** + * @param theVerb + * @throws IOException + */ + private static void testList(String theVerb, String theSet) throws IOException { + + long startTime = System.currentTimeMillis(); + + String testOccurance = "header"; + if (theVerb.equals("ListRecords")) { + testOccurance = "record"; + } + + String prefix = "oai_dc"; + + String url = host + "oai?verb=" + theVerb + "&metadataPrefix=" + prefix + "&set=" + theSet; + + TGHttpResponse httpResponse = getHttpResponse(url); + int status = httpResponse.getStatusCode(); + + long timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + getDurationInSecs(timeRunning)); + + String restok = examineResumptionTokenTag(httpResponse, testOccurance, ""); + + while (status == HttpStatus.SC_OK && !restok.equals("-1")) { + url = host + "/oai?verb=" + theVerb + "&resumptionToken=" + restok; + httpResponse = getHttpResponse(url); + timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + getDurationInSecs(timeRunning)); + restok = examineResumptionTokenTag(httpResponse, testOccurance, restok); + } + + System.out.println(OK); + } } diff --git a/oaipmh-webapp/src/main/webapp/WEB-INF/beans.xml b/oaipmh-webapp/src/main/webapp/WEB-INF/beans.xml index 0bdfb98e..c5f849dd 100644 --- a/oaipmh-webapp/src/main/webapp/WEB-INF/beans.xml +++ b/oaipmh-webapp/src/main/webapp/WEB-INF/beans.xml @@ -1,8 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" - xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:jaxws="http://cxf.apache.org/jaxws" + xmlns:jaxrs="http://cxf.apache.org/jaxrs" + xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws @@ -22,46 +24,55 @@ class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> - <value>file:/etc/dariah/oaipmh/oaipmh.properties</value> <value>file:/etc/dhrep/oaipmh/oaipmh.properties</value> - <value>file:/etc/textgrid/oaipmh/oaipmh.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean> - <bean id="ElasticSearchClient" class="info.textgrid.middleware.OAI_ESClient"> - <constructor-arg index="0" value="${elasticSearch.url}" /> - <constructor-arg index="1" value="${elasticSearch.port}" /> - <constructor-arg index="2" value="${elasticSearch.clusterName}" /> + <bean id="ElasticSearchClient" + class="info.textgrid.middleware.OAI_ESClient"> + <constructor-arg index="0" + value="${elasticSearch.url}" /> + <constructor-arg index="1" + value="${elasticSearch.port}" /> + <constructor-arg index="2" + value="${elasticSearch.clusterName}" /> <property name="esIndex" value="${elasticSearch.index}" /> <property name="esType" value="${elasticSearch.type}" /> </bean> <!--bean id="drc" class="info.textgrid.middleware.oaipmh.${DeletedRecordInfo}" --> - <bean id="RepIdentification" class="info.textgrid.middleware.RepIdentification"> + <bean id="RepIdentification" + class="info.textgrid.middleware.RepIdentification"> <constructor-arg index="0" value="${RepositoryName}" /> <constructor-arg index="1" value="${BaseUrl}" /> - <constructor-arg index="2" value="${DeletedRecordInfo}" /> - <constructor-arg index="3" value="${EarliestDateStamp}" /> + <constructor-arg index="2" + value="${DeletedRecordInfo}" /> + <constructor-arg index="3" + value="${EarliestDateStamp}" /> <constructor-arg index="4" value="${Granularity}" /> <constructor-arg index="5" value="${ProtocolVersion}" /> <constructor-arg index="6" value="${AdminMail}" /> </bean> - <bean id="RecordList" class="info.textgrid.middleware.RecordListDeliverer"> - <!--constructor-arg index="0" ref="ElasticSearchClient" /--> + <bean id="RecordList" + class="info.textgrid.middleware.RecordListDeliverer"> + <!--constructor-arg index="0" ref="ElasticSearchClient" / --> <constructor-arg index="0" value="${fields}" /> <constructor-arg index="1" value="${workFields}" /> <constructor-arg index="2" value="${textgrid}" /> <constructor-arg index="3" value="${dariah}" /> <property name="formatField" value="${formatField}" /> <property name="formatToFilter" value="${formatToFilter}" /> - <property name="dateOfObjectCreation" value="${dateOfObjectCreation}" /> - <property name="relationToFurtherMetadataObject" value="${relationToFurtherMetadataObject}" /> - <property name="repositoryObjectURIPrefix" value="${repositoryObjectURIPrefix}" /> + <property name="dateOfObjectCreation" + value="${dateOfObjectCreation}" /> + <property name="relationToFurtherMetadataObject" + value="${relationToFurtherMetadataObject}" /> + <property name="repositoryObjectURIPrefix" + value="${repositoryObjectURIPrefix}" /> <property name="FieldForRange" value="${rangeField}" /> <property name="contributor" value="${contributorList}" /> <property name="coverage" value="${coverageList}" /> @@ -73,7 +84,8 @@ <property name="languages" value="${languageList}" /> <property name="publishers" value="${publisherList}" /> <property name="relations" value="${relationList}" /> - <property name="relationsForWork" value="${relationsForWorkList}" /> + <property name="relationsForWork" + value="${relationsForWorkList}" /> <property name="rights" value="${rightsList}" /> <property name="sources" value="${sourceList}" /> <property name="subjects" value="${subjectList}" /> @@ -81,7 +93,8 @@ <property name="types" value="${typeList}" /> <property name="modifiedField" value="${modifiedField}" /> <property name="identifierField" value="${identifierField}" /> - <property name="searchResponseSize" value="${searchResponseSize}" /> + <property name="searchResponseSize" + value="${searchResponseSize}" /> </bean> <bean id="Record" class="info.textgrid.middleware.RecordDeliverer"> @@ -91,9 +104,12 @@ <constructor-arg index="3" value="${dariah}" /> <property name="formatField" value="${formatField}" /> <property name="formatToFilter" value="${formatToFilter}" /> - <property name="dateOfObjectCreation" value="${dateOfObjectCreation}" /> - <property name="relationToFurtherMetadataObject" value="${relationToFurtherMetadataObject}" /> - <property name="repositoryObjectURIPrefix" value="${repositoryObjectURIPrefix}" /> + <property name="dateOfObjectCreation" + value="${dateOfObjectCreation}" /> + <property name="relationToFurtherMetadataObject" + value="${relationToFurtherMetadataObject}" /> + <property name="repositoryObjectURIPrefix" + value="${repositoryObjectURIPrefix}" /> <property name="identifierField" value="${identifierField}" /> <property name="fields" value="${fields}" /> <property name="contributor" value="${contributorList}" /> @@ -106,7 +122,8 @@ <property name="languages" value="${languageList}" /> <property name="publishers" value="${publisherList}" /> <property name="relations" value="${relationList}" /> - <property name="relationsForWork" value="${relationsForWorkList}" /> + <property name="relationsForWork" + value="${relationsForWorkList}" /> <property name="rights" value="${rightsList}" /> <property name="sources" value="${sourceList}" /> <property name="subjects" value="${subjectList}" /> @@ -114,33 +131,41 @@ <property name="types" value="${typeList}" /> </bean> - <bean id="ListIdentifier" class="info.textgrid.middleware.IdentifierListDeliverer"> + <bean id="ListIdentifier" + class="info.textgrid.middleware.IdentifierListDeliverer"> <constructor-arg index="0" ref="ElasticSearchClient" /> <constructor-arg index="1" value="${textgrid}" /> <constructor-arg index="2" value="${dariah}" /> <property name="formatField" value="${formatField}" /> <property name="formatToFilter" value="${formatToFilter}" /> - <property name="dateOfObjectCreation" value="${dateOfObjectCreation}" /> - <property name="repositoryObjectURIPrefix" value="${repositoryObjectURIPrefix}" /> - <property name="identifierListFields" value="${identifierListFields}"></property> + <property name="dateOfObjectCreation" + value="${dateOfObjectCreation}" /> + <property name="repositoryObjectURIPrefix" + value="${repositoryObjectURIPrefix}" /> + <property name="identifierListFields" + value="${identifierListFields}"></property> <property name="FieldForRange" value="${rangeField}" /> - <property name="identifierField" value="${identifierField}" /> - <property name="searchResponseSize" value="${searchResponseSize}" /> + <property name="identifierField" value="${identifierField}" /> + <property name="searchResponseSize" + value="${searchResponseSize}" /> </bean> - <bean id="SetDeliverer" class="info.textgrid.middleware.SetDeliverer"> + <bean id="SetDeliverer" + class="info.textgrid.middleware.SetDeliverer"> <constructor-arg ref="ElasticSearchClient" /> <constructor-arg index="1" value="${textgrid}" /> <constructor-arg index="2" value="${dariah}" /> <property name="formatField" value="${formatField}" /> <property name="formatToFilter" value="${formatToFilter}" /> <property name="identifierField" value="${identifierField}" /> - <property name="repositoryObjectURIPrefix" value="${repositoryObjectURIPrefix}" /> + <property name="repositoryObjectURIPrefix" + value="${repositoryObjectURIPrefix}" /> <property name="specField" value="${specField}" /> <property name="specFieldPrefix" value="${specFieldPrefix}" /> </bean> - <bean id="MetadataFormatList" class="info.textgrid.middleware.MetadataFormatListDeliverer"> + <bean id="MetadataFormatList" + class="info.textgrid.middleware.MetadataFormatListDeliverer"> <constructor-arg ref="ElasticSearchClient" /> </bean> -- GitLab