diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/OAIPMHUtilitiesOnline.java b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/OAIPMHUtilitiesOnline.java index e06e5a1d4e0bfc7e44cd4fbd1ced904a59005fb9..bf1aaa4e732ee5bf053b872e1565ecbd0225ac4e 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/OAIPMHUtilitiesOnline.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/OAIPMHUtilitiesOnline.java @@ -2,15 +2,24 @@ package info.textgrid.middleware.test.online; import static org.junit.Assert.assertTrue; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Properties; import javax.ws.rs.core.Response; +import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.jaxrs.client.Client; +import org.apache.cxf.jaxrs.client.JAXRSClientFactory; import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.transport.http.HTTPConduit; +import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.apache.http.HttpStatus; +import info.textgrid.middleware.OAIPMHProducer; /** * <p> @@ -54,6 +63,9 @@ public class OAIPMHUtilitiesOnline { // Time to wait between the single queries using resumption tokens in milliseconds. protected static final Long TIME = 275l; + protected static final String HDL_PREFIX = "hdl:"; + protected static final String STAR_PREFIX = "***"; + protected static final long OAIPMH_CLIENT_TIMEOUT = 120000; /** @@ -166,4 +178,526 @@ public class OAIPMHUtilitiesOnline { return result; } + /** + * @return + * @throws IOException + * @throws FileNotFoundException + */ + protected static Properties getPropertiesFromFile(String theConfigFile) + throws FileNotFoundException, IOException { + + Properties result = null; + + // Load properties file. + result = new Properties(); + result.load(new FileInputStream(getResource(theConfigFile))); + + System.out.println("Properties file: " + theConfigFile); + result.list(System.out); + + return result; + } + + /** + * @param theEndpoint + * @return + */ + protected static Client getOAIPMHWEebClient(String theEndpoint) { + + Client result = null; + + // Get OAI-PMH REST endpoint and HTTP client. + System.out.println("Getting OAI-PMH HTTP client --> " + theEndpoint + " <--"); + + // Get proxy first, set policy. + OAIPMHProducer JAXRSClient = JAXRSClientFactory.create(theEndpoint, OAIPMHProducer.class); + HTTPConduit conduit = WebClient.getConfig(JAXRSClient).getHttpConduit(); + HTTPClientPolicy policy = new HTTPClientPolicy(); + policy.setReceiveTimeout(OAIPMHUtilitiesOnline.OAIPMH_CLIENT_TIMEOUT); + conduit.setClient(policy); + + // Create Web Client from Web Proxy. + result = WebClient.client(JAXRSClient); + + return result; + } + + // ** + // PRIVATE METHODS + // ** + + /** + * TODO Generalise in OaiPmhTestUtilities! + * + * @param theClient + * @param theVerb + * @param theSet + * @param theMetadataPrefix + * @param maxNumberOfPagesToTest + * @param recordsExpectedPerRequest + * @param theThreadName + * @param from + * @param until + * @param resumptionTokenANDMetadataPrefix + * @throws IOException + */ + protected static void testList(Client theClient, final String theVerb, final String theSet, + final String theMetadataPrefix, final int maxNumberOfPagesToTest, + final int recordsExpectedPerRequest, final String theThreadName, final String from, + final String until, final boolean resumptionTokenANDMetadataPrefix) throws IOException { + + String threadName = tn(theThreadName); + + long startTime = System.currentTimeMillis(); + + String testOccurance = "header"; + if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { + testOccurance = "record"; + } + + String path = "verb=" + theVerb + "&metadataPrefix=" + theMetadataPrefix; + + if (theSet != null) { + path += "&set=" + theSet; + } + + if (from != null && until != null) { + path += "&from=" + from + "&until=" + until; + } + + Response httpResponse = OAIPMHUtilitiesOnline.getHttpResponse(theClient, path, threadName); + int status = httpResponse.getStatus(); + + int loopCount = 1; + long timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\t" + threadName + "time: " + + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning) + " (loop " + loopCount + + (maxNumberOfPagesToTest != 0 ? "/" + maxNumberOfPagesToTest : "") + ")"); + + String responseString = IOUtils.readStringFromStream((InputStream) httpResponse.getEntity()); + + // Test resumption token tags. + String restok = + examineResumptionTokenTag(responseString, testOccurance, OAIPMHUtilitiesOnline.NO_TOKEN, + recordsExpectedPerRequest, loopCount, threadName, theMetadataPrefix); + + // Test general metadata content (if verb is listRecords!), must go conform with the metadata + // prefix setting. + if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { + examineContent(responseString, theMetadataPrefix); + } + + while (status == HttpStatus.SC_OK && !restok.equals(OAIPMHUtilitiesOnline.NO_TOKEN)) { + loopCount += 1; + synchronized (threadName) { + if (maxNumberOfPagesToTest > 0 && loopCount > maxNumberOfPagesToTest) { + System.out + .println("\t" + threadName + "TESTING ONLY " + maxNumberOfPagesToTest + " pages!"); + break; + } + path = "verb=" + theVerb + "&resumptionToken=" + restok; + if (resumptionTokenANDMetadataPrefix) { + path += "&metadataPrefix=" + theMetadataPrefix; + } + httpResponse = OAIPMHUtilitiesOnline.getHttpResponse(theClient, path, threadName); + timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\t" + threadName + "time: " + + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning) + " (loop " + loopCount + + (maxNumberOfPagesToTest != 0 ? "/" + maxNumberOfPagesToTest : "") + ")"); + + // Test resumption token tags. + responseString = IOUtils.readStringFromStream((InputStream) httpResponse.getEntity()); + restok = examineResumptionTokenTag(responseString, testOccurance, restok, + recordsExpectedPerRequest, loopCount, threadName, theMetadataPrefix); + + // Test general metadata content (if verb is listRecords!), must go conform with the + // metadata // prefix setting. + if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { + examineContent(responseString, theMetadataPrefix); + } + } + } + + // Only check for max loops, if we do have less loops and no resumption token, it will be fine, + // too! + if (!restok.equals(OAIPMHUtilitiesOnline.NO_TOKEN) && loopCount < maxNumberOfPagesToTest) { + System.out.println(threadName + OAIPMHUtilitiesOnline.ERROR + ": Must have done " + + maxNumberOfPagesToTest + " loops, but did only " + loopCount); + assertTrue(false); + } + + // Check for single loop, we need MORE! + if (loopCount == 1) { + System.out + .println(threadName + OAIPMHUtilitiesOnline.ERROR + ": Must have more than one loop!"); + assertTrue(false); + } + + System.out.println("\t" + threadName + OAIPMHUtilitiesOnline.OK); + } + + /** + * @param theResponseString + * @param recordOrHeader + * @param oldtok + * @param recordsExpectedPerRequest + * @param loopCount + * @param theThreadName + * @param theMetadataPrefix + * @return Resumption token string, if existing, -1 if either no <resumptionToken> tag is + * existing, tag is existing and has no token value. + * @throws IOException + */ + protected static String examineResumptionTokenTag(final String theResponseString, + final String recordOrHeader, final String oldtok, final int recordsExpectedPerRequest, + final int loopCount, final String theThreadName, final String theMetadataPrefix) + throws IOException { + + // Test for OAIPMH errors. + if (theResponseString.contains("<error code=\"badArgument\">")) { + String message = + theThreadName + OAIPMHUtilitiesOnline.ERROR + " IN OAIPMH RESPONSE: " + + theResponseString; + System.out.println(message); + throw new IOException(message); + } + + // Count response objects at first. + int recordCount = 0; + int i = theResponseString.indexOf("<" + recordOrHeader + ">", 0); + while (i != -1) { + recordCount++; + i++; + i = theResponseString.indexOf("<" + recordOrHeader + ">", i); + } + + System.out.println("\t" + theThreadName + recordOrHeader + "s: " + recordCount); + + // Check if token tag is existing. + int tokStart = theResponseString.indexOf("<resumptionToken"); + int tokEnd = theResponseString.indexOf("</resumptionToken"); + + if (tokStart == -1 && tokEnd == -1) { + System.out.println("\t" + theThreadName + "token: no token"); + + return OAIPMHUtilitiesOnline.NO_TOKEN; + } + + String restokTmp = theResponseString.substring(tokStart, tokEnd); + // Get token tag. + String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1).trim(); + System.out.println("\t" + theThreadName + "tokentag: " + toktag); + + // Get token. + String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); + System.out.println("\t" + theThreadName + "token: " + restok); + + // Check if old and new token are equal or not. + boolean tokchanged = !oldtok.equals(restok); + System.out.println("\t" + theThreadName + "tokchngd: " + 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("\t" + theThreadName + "size: " + size + " / " + cursor); + + // If token is provided, test cursor and element count! + if (!restok.isEmpty()) { + synchronized (TestGetRecordOnline.class) { + // Check <record> or <header> count, must be recordsExpectedPerRequest! + if (recordCount != recordsExpectedPerRequest) { + String message = + OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader + " count mismatch, must be " + + recordsExpectedPerRequest + " if token is provided, but is " + recordCount; + assertTrue(message, false); + } + if (size <= recordsExpectedPerRequest) { + String message = + OAIPMHUtilitiesOnline.ERROR + ": completeListSize count mismatch, must be > " + + recordsExpectedPerRequest + " if token is provided, but is " + size; + assertTrue(message, false); + } + if (cursor != recordsExpectedPerRequest * loopCount) { + String message = OAIPMHUtilitiesOnline.ERROR + ": cursor must be " + + (loopCount * recordsExpectedPerRequest) + " in loop " + loopCount + ", but is " + + cursor; + assertTrue(message, false); + } + } + } + + // If no token is provided, stop querying. + else { + // Check <record> count, must be completeListSize % recordsExpectedPerRequest. + if (recordCount != size % recordsExpectedPerRequest) { + System.err.println(theThreadName + OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader + + " count mismatch, should be " + size % recordsExpectedPerRequest + ", but is " + + recordCount); + assertTrue(false); + } + + // No resumption token available in response. + return OAIPMHUtilitiesOnline.NO_TOKEN; + } + + return restok; + } + + /** + * @param theThreadName + * @return + */ + protected static String tn(String theThreadName) { + + String result = theThreadName; + + if (theThreadName != null && !theThreadName.equals("") && !theThreadName.endsWith(" ")) { + result = "[" + theThreadName + "] "; + } + + return result; + } + + /** + * @param theResponseString + * @param theMetadataFormat + */ + protected static void examineContent(String theResponseString, String theMetadataFormat) { + + // Check for correct metadata content according to metadata prefix. + if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_DC_PREFIX) + && !theResponseString + .contains("metadataPrefix=\"" + OAIPMHUtilitiesOnline.OAI_DC_PREFIX + "\"") + && !theResponseString.contains(OAIPMHUtilitiesOnline.EXPECTED_OAIDC_FORMAT_CONTENT)) { + System.out + .println(OAIPMHUtilitiesOnline.OAI_DC_PREFIX + " needs to deliver content with schema: " + + OAIPMHUtilitiesOnline.EXPECTED_OAIDC_FORMAT_CONTENT + "!"); + + System.out.println("ERROR:\n" + theResponseString); + + assertTrue(false); + } else if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX) + && !theResponseString.contains( + "metadataPrefix=\"" + OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT + + "\"") + && !theResponseString + .contains(OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT)) { + System.out.println( + OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX + " needs to deliver content with schema: " + + OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT + "!"); + + System.out.println("ERROR:\n" + theResponseString); + + assertTrue(false); + } else if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX) + && !theResponseString + .contains("metadataPrefix=\"" + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX + "\"") + && !theResponseString.contains(OAIPMHUtilitiesOnline.EXPECTED_DATACITE_FORMAT_CONTENT)) { + System.out.println( + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX + " needs to deliver content with schema: " + + OAIPMHUtilitiesOnline.EXPECTED_DATACITE_FORMAT_CONTENT + "!"); + + System.out.println("ERROR:\n" + theResponseString); + + assertTrue(false); + } else { + System.out.println(theResponseString); + } + } + + /** + * @param theResponseString + * @param recordOrHeader + * @param oldtok + * @param theMetadataPrefix + * @return Resumption token string, if existing, -1 if either no <resumptionToken> tag is + * existing, tag is existing and has no token value. + * @throws IOException + */ + protected static String examineResumptionTokenTag(String theResponseString, String recordOrHeader, + String oldtok, String theMetadataPrefix) throws IOException { + + // Default entries per page are 100 (IDIOM: 30). + // momentarily. + int expectedCount = 100; + if (theMetadataPrefix.equals(OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX)) { + expectedCount = 30; + } + + // Test for OAIPMH errors. + if (theResponseString.contains("<error code=\"badArgument\">")) { + System.err.println(OAIPMHUtilitiesOnline.ERROR + " IN OAIPMH RESPONSE: " + theResponseString); + assertTrue(false); + } + + // Count response objects at first. + int recordCount = 0; + int i = theResponseString.indexOf("<" + recordOrHeader + ">", 0); + while (i != -1) { + recordCount++; + i++; + i = theResponseString.indexOf("<" + recordOrHeader + ">", i); + } + + System.out.println("\t" + recordOrHeader + "s: " + recordCount); + + // Check if token tag is existing. + int tokStart = theResponseString.indexOf("<resumptionToken"); + int tokEnd = theResponseString.indexOf("</resumptionToken"); + + if (tokStart == -1 && tokEnd == -1) { + System.out.println("\ttoken: no token"); + + return "-1"; + } + + String restokTmp = theResponseString.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 (IDIOM: 30) elements: mekkern! + if (!restok.isEmpty()) { + synchronized (TestDHOAIPMHOnline.class) { + // Check <record> or <header> count, must be 100 (IDIOM: 30)! + if (recordCount != expectedCount) { + System.err.println( + OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader + " count mismatch, must be " + + expectedCount + " if token is provided, but is " + recordCount); + } + if (size <= expectedCount) { + System.err + .println(OAIPMHUtilitiesOnline.ERROR + ": completeListSize count mismatch, must be > " + + expectedCount + " if token is provided, but is " + size); + } + if (recordCount != expectedCount || size <= expectedCount) { + assertTrue(false); + } + } + } + + // If no token is provided, stop querying. + else { + // Check <record> count, must be completeListSize % 100 (IDIOM: 30). + if (recordCount != size % expectedCount) { + System.err.println(OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader + + " count mismatch, should be " + size % expectedCount + ", but is " + recordCount); + assertTrue(false); + } + + // No resumption token available in response. + return "-1"; + } + + return restok; + } + + /** + * @param theWebClient + * @param theVerb + * @param theMetadataPrefix + * @param theSet + * @return + * @throws IOException + */ + protected static int testList(Client theWebClient, String theVerb, String theMetadataPrefix, + String theSet) throws IOException { + return testList(theWebClient, theVerb, theMetadataPrefix, theSet, OAIPMHUtilitiesOnline.NO_FROM, + OAIPMHUtilitiesOnline.NO_UNTIL); + } + + /** + * @param theWebClient + * @param theVerb + * @param theMetadataPrefix + * @param theSet + * @param from + * @param until + * @return How many pages were delivered + * @throws IOException + */ + protected static int testList(Client theWebClient, String theVerb, String theMetadataPrefix, + String theSet, String from, String until) throws IOException { + + int result; + + long startTime = System.currentTimeMillis(); + + String testOccurance = "header"; + if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { + testOccurance = "record"; + } + + String path = "verb=" + theVerb + "&metadataPrefix=" + theMetadataPrefix; + + if (theSet != null && !theSet.isEmpty()) { + path += "&set=" + theSet; + } + + if (from != null && until != null) { + path += "&from=" + from + "&until=" + until; + } + + Response response = OAIPMHUtilitiesOnline.getHttpResponse(theWebClient, path); + String responseString = IOUtils.readStringFromStream((InputStream) response.getEntity()); + int status = response.getStatus(); + + long timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning)); + + String restok = examineResumptionTokenTag(responseString, testOccurance, "", theMetadataPrefix); + examineIdentifiers(responseString); + + result = 1; + while (status == HttpStatus.SC_OK && !restok.equals("-1")) { + path = "verb=" + theVerb + "&resumptionToken=" + restok; + response = OAIPMHUtilitiesOnline.getHttpResponse(theWebClient, path); + responseString = IOUtils.readStringFromStream((InputStream) response.getEntity()); + timeRunning = System.currentTimeMillis() - startTime; + System.out.println("\ttime: " + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning)); + restok = examineResumptionTokenTag(responseString, testOccurance, restok, theMetadataPrefix); + examineIdentifiers(responseString); + result++; + } + + System.out.println("\tpage amount: " + result); + System.out.println(OAIPMHUtilitiesOnline.OK); + + return result; + } + + /** + * @param theResponseString + * @return + * @throws IOException + */ + protected static void examineIdentifiers(String theResponseString) throws IOException { + if (theResponseString.contains("<identifier>hdl:21</identifier>")) { + System.err.println(OAIPMHUtilitiesOnline.ERROR + + " IN OAIPMH RESPONSE: identifier tag is corrupt!" + theResponseString); + assertTrue(false); + } + } + } diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestDHOAIPMHOnline.java b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestDHOAIPMHOnline.java index dd983a7ce8b31a694e0bfd9660411df2d920800a..96468b5edcc367ae4a1ddf0e040806e08d897591 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestDHOAIPMHOnline.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestDHOAIPMHOnline.java @@ -4,8 +4,6 @@ import static org.junit.Assert.assertTrue; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; import java.util.Properties; import javax.ws.rs.core.Response; import org.apache.cxf.helpers.IOUtils; @@ -40,9 +38,6 @@ public class TestDHOAIPMHOnline { private static final String PROPERTIES_FILE = "oaipmh.test.repository-de-dariah-eu.properties"; // private static final String PROPERTIES_FILE = "oaipmh.test.trep-de-dariah-eu.properties"; - private static final String HDL_PREFIX = "hdl:"; - private static final String STAR_PREFIX = "***"; - // ** // STATICS // ** @@ -54,20 +49,8 @@ public class TestDHOAIPMHOnline { // Properties private static String expectedListSets; - private static String checkGetRecordDC; - private static String expectedGetRecordDC; - private static String checkGetRecordDATACITE; - private static String expectedGetRecordDATACITE; - private static String checkListRecordsDC; - private static String checkListRecordsDCFrom; - private static String checkListRecordsDCUntil; - private static String checkListRecordsDATACITEFrom; - private static String checkListRecordsDATACITEUntil; - private static String checkListRecordsSet; - private static Integer checkListRecordsSetExpectedPages; private static String checkListIdentifiersSet; private static Integer checkListIdentifiersSetExpectedPages; - private static List<String> checkGetRecordIDList = new ArrayList<String>(); // ** // PREPARATIONS @@ -90,25 +73,9 @@ public class TestDHOAIPMHOnline { // Get other needed properties. oaipmhEndpoint = p.getProperty("oaipmhEndpoint"); expectedListSets = p.getProperty("expectedListSets"); - checkGetRecordDC = p.getProperty("checkGetRecordDC"); - expectedGetRecordDC = p.getProperty("expectedGetRecordDC"); - checkGetRecordDATACITE = p.getProperty("checkGetRecordDATACITE"); - expectedGetRecordDATACITE = p.getProperty("expectedGetRecordDATACITE"); - checkListRecordsDC = p.getProperty("checkListRecordsDC"); - checkListRecordsDCFrom = p.getProperty("checkListRecordsDCFrom"); - checkListRecordsDCUntil = p.getProperty("checkListRecordsDCUntil"); - checkListRecordsDATACITEFrom = p - .getProperty("checkListRecordsDATACITEFrom"); - checkListRecordsDATACITEUntil = p - .getProperty("checkListRecordsDATACITEUntil"); - checkListRecordsSet = p.getProperty("checkListRecordsSet"); - checkListRecordsSetExpectedPages = - Integer.parseInt(p.getProperty("checkListRecordsSetExpectedPages").trim()); checkListIdentifiersSet = p.getProperty("checkListIdentifiersSet"); checkListIdentifiersSetExpectedPages = Integer.parseInt(p.getProperty("checkListIdentifiersSetExpectedPages").trim()); - checkGetRecordIDList = OAIPMHUtilitiesOnline - .getListFromProperties((String) p.get("checkGetRecordIDList")); // Get OAI-PMH REST endpoint and HTTP client. System.out.println( @@ -259,113 +226,10 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); - testList("ListRecords", "oai_dc", OAIPMHUtilitiesOnline.NO_SET); - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsSetDC() throws IOException { - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); - - int pages = testList("ListRecords", "oai_dc", checkListRecordsSet); - - if (pages != checkListRecordsSetExpectedPages) { - assertTrue(pages + " != " + checkListRecordsSetExpectedPages, false); - } - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsSetDCDifferentPrefixes() throws IOException { - - String uri = checkListRecordsSet; - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - HDL PREFIX"); - - if (!uri.startsWith(HDL_PREFIX)) { - assertTrue("missing '" + HDL_PREFIX + "' prefix!", false); - } - int pages1 = testList("ListRecords", "oai_dc", uri); - if (pages1 != checkListRecordsSetExpectedPages) { - System.out.println(pages1 + " != " + checkListRecordsSetExpectedPages); - } - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - NO PREFIX"); - - if (checkListRecordsSet.startsWith(HDL_PREFIX)) { - uri = checkListRecordsSet.substring(4); - } - int pages2 = testList("ListRecords", "oai_dc", uri); - if (pages2 != checkListRecordsSetExpectedPages) { - System.out.println(pages2 + " != " + checkListRecordsSetExpectedPages); - } - - // Check pages count. - if (pages1 != checkListRecordsSetExpectedPages || pages2 != checkListRecordsSetExpectedPages) { - assertTrue("pages count does not match", false); - } - } - - /** - * @throws IOException - */ - @Test - @Ignore - public void testListRecordsSetDCStarPrefix() throws IOException { - - // TODO Un-ignore if identifier check is implemented! - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - STAR PREFIX"); - - if (checkListRecordsSet.startsWith(HDL_PREFIX)) { - checkListRecordsSet = STAR_PREFIX + checkListRecordsSet.substring(4); - } - - int pages3 = testList("ListRecords", "oai_dc", checkListRecordsSet); - if (pages3 != 1) { - System.out.println(pages3 + " != " + 1); - } - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsDCFromUntil() throws IOException { - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); - - testList("ListRecords", "oai_dc", OAIPMHUtilitiesOnline.NO_SET, - checkListRecordsDCFrom, checkListRecordsDCUntil); - } - - /** - * @throws IOException - */ - @Test - public void testListRecordsDATASITE() throws IOException { - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); - - testList("ListRecords", "oai_datacite", OAIPMHUtilitiesOnline.NO_SET); + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListRecords", "oai_dc", + OAIPMHUtilitiesOnline.NO_SET); } - /** - * @throws IOException - */ - @Test - public void testListRecordsDATASITEFromUntil() throws IOException { - - System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); - - testList("ListRecords", "oai_datacite", OAIPMHUtilitiesOnline.NO_SET, - checkListRecordsDATACITEFrom, checkListRecordsDATACITEUntil); - } /** * @throws IOException @@ -411,7 +275,8 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS"); - testList("ListIdentifiers", "oai_dc", OAIPMHUtilitiesOnline.NO_SET); + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_dc", + OAIPMHUtilitiesOnline.NO_SET); } /** @@ -422,7 +287,9 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS"); - int pages = testList("ListIdentifiers", "oai_dc", checkListIdentifiersSet); + int pages = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_dc", + checkListIdentifiersSet); if (pages != checkListIdentifiersSetExpectedPages) { assertTrue(pages + " != " + checkListIdentifiersSetExpectedPages, false); @@ -439,28 +306,29 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS - HDL PREFIX"); - if (!uri.startsWith(HDL_PREFIX)) { - assertTrue("missing '" + HDL_PREFIX + "' prefix!", false); + if (!uri.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { + assertTrue("missing '" + OAIPMHUtilitiesOnline.HDL_PREFIX + "' prefix!", false); } System.out.println("uri: " + uri); - int pages1 = testList("ListIdentifiers", "oai_dc", uri); + int pages1 = OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_dc", uri); if (pages1 != checkListIdentifiersSetExpectedPages) { System.out.println(pages1 + " != " + checkListIdentifiersSetExpectedPages); } System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS - NO PREFIX"); - if (checkListIdentifiersSet.startsWith(HDL_PREFIX)) { + if (checkListIdentifiersSet.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { uri = checkListIdentifiersSet.substring(4); } System.out.println("uri: " + uri); - int pages2 = testList("ListIdentifiers", "oai_dc", uri); + int pages2 = OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_dc", uri); if (pages2 != checkListIdentifiersSetExpectedPages) { System.out.println(pages2 + " != " + checkListIdentifiersSetExpectedPages); } // Check pages count. - if (pages1 != checkListRecordsSetExpectedPages || pages2 != checkListRecordsSetExpectedPages) { + if (pages1 != checkListIdentifiersSetExpectedPages + || pages2 != checkListIdentifiersSetExpectedPages) { assertTrue("pages count does not match", false); } } @@ -477,11 +345,14 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS - STAR PREFIX"); - if (checkListIdentifiersSet.startsWith(HDL_PREFIX)) { - checkListIdentifiersSet = STAR_PREFIX + checkListIdentifiersSet.substring(4); + if (checkListIdentifiersSet.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { + checkListIdentifiersSet = + OAIPMHUtilitiesOnline.STAR_PREFIX + checkListIdentifiersSet.substring(4); } System.out.println("uri: " + checkListIdentifiersSet); - int pages3 = testList("ListIdentifiers", "oai_dc", checkListIdentifiersSet); + int pages3 = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_dc", + checkListIdentifiersSet); if (pages3 != checkListIdentifiersSetExpectedPages) { System.out.println(pages3 + " != " + checkListIdentifiersSetExpectedPages); } @@ -495,189 +366,14 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTIDENTIFIERS"); - testList("ListIdentifiers", "oai_datacite", OAIPMHUtilitiesOnline.NO_SET); + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, "ListIdentifiers", "oai_datacite", + OAIPMHUtilitiesOnline.NO_SET); } // ** // PRIVATE METHODS // ** - /** - * TODO Generalise in OaiPmhTestUtilities! - * - * @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(String theResponseString, - String recordOrHeader, String oldtok) throws IOException { - - // Test for OAIPMH errors. - if (theResponseString.contains("<error code=\"badArgument\">")) { - System.err.println(OAIPMHUtilitiesOnline.ERROR + " IN OAIPMH RESPONSE: " + theResponseString); - assertTrue(false); - } - - // Count response objects at first. - int recordCount = 0; - int i = theResponseString.indexOf("<" + recordOrHeader + ">", 0); - while (i != -1) { - recordCount++; - i++; - i = theResponseString.indexOf("<" + recordOrHeader + ">", i); - } - - System.out.println("\t" + recordOrHeader + "s: " + recordCount); - - // Check if token tag is existing. - int tokStart = theResponseString.indexOf("<resumptionToken"); - int tokEnd = theResponseString.indexOf("</resumptionToken"); - - if (tokStart == -1 && tokEnd == -1) { - System.out.println("\ttoken: no token"); - - return "-1"; - } - - String restokTmp = theResponseString.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 (TestDHOAIPMHOnline.class) { - // Check <record> or <header> count, must be 100! - if (recordCount != 100) { - System.err.println(OAIPMHUtilitiesOnline.ERROR + ": " - + recordOrHeader - + " count mismatch, must be 100 if token is provided, but is " - + recordCount); - } - if (size <= 100) { - System.err.println(OAIPMHUtilitiesOnline.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(OAIPMHUtilitiesOnline.ERROR + ": " - + recordOrHeader + " count mismatch, should be " - + size % 100 + ", but is " + recordCount); - assertTrue(false); - } - - // No resumption token available in response. - return "-1"; - } - - return restok; - } - - /** - * @param theVerb - * @param theMetadataPrefix - * @param theSet - * @return - * @throws IOException - */ - private static int testList(String theVerb, String theMetadataPrefix, - String theSet) throws IOException { - return testList(theVerb, theMetadataPrefix, theSet, - OAIPMHUtilitiesOnline.NO_FROM, OAIPMHUtilitiesOnline.NO_UNTIL); - } - - /** - * TODO Generalise in OaiPmhTestUtilities! - * - * @param theVerb - * @param theMetadataPrefix - * @param theSet - * @param from - * @param until - * @return How many pages were delivered - * @throws IOException - */ - private static int testList(String theVerb, String theMetadataPrefix, - String theSet, String from, String until) throws IOException { - - int result; - - long startTime = System.currentTimeMillis(); - - String testOccurance = "header"; - if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { - testOccurance = "record"; - } - - String path = "verb=" + theVerb + "&metadataPrefix=" - + theMetadataPrefix; - - if (theSet != null && !theSet.isEmpty()) { - path += "&set=" + theSet; - } - - if (from != null && until != null) { - path += "&from=" + from + "&until=" + until; - } - - Response response = OAIPMHUtilitiesOnline.getHttpResponse(oaipmhWebClient, - path); - String responseString = IOUtils.readStringFromStream((InputStream) response.getEntity()); - int status = response.getStatus(); - - long timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning)); - - String restok = examineResumptionTokenTag(responseString, testOccurance, ""); - examineIdentifiers(responseString); - - result = 1; - while (status == HttpStatus.SC_OK && !restok.equals("-1")) { - path = "verb=" + theVerb + "&resumptionToken=" + restok; - response = OAIPMHUtilitiesOnline.getHttpResponse(oaipmhWebClient, path); - responseString = IOUtils.readStringFromStream((InputStream) response.getEntity()); - timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\ttime: " + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning)); - restok = examineResumptionTokenTag(responseString, testOccurance, restok); - examineIdentifiers(responseString); - result++; - } - - System.out.println("\tpage amount: " + result); - System.out.println(OAIPMHUtilitiesOnline.OK); - - return result; - } - /** * @param theSet * @throws IOException @@ -729,7 +425,8 @@ public class TestDHOAIPMHOnline { if (status != HttpStatus.SC_OK || !responseString.contains(shouldContain)) { System.err.println("\tstatus: " + status); System.err - .println(OAIPMHUtilitiesOnline.ERROR + ": response should contain '" + shouldContain + "'"); + .println( + OAIPMHUtilitiesOnline.ERROR + ": response should contain '" + shouldContain + "'"); assertTrue(false); } @@ -737,17 +434,4 @@ public class TestDHOAIPMHOnline { System.out.println(OAIPMHUtilitiesOnline.OK); } - /** - * @param theResponseString - * @return - * @throws IOException - */ - private static void examineIdentifiers(String theResponseString) throws IOException { - if (theResponseString.contains("<identifier>hdl:21</identifier>")) { - System.err.println(OAIPMHUtilitiesOnline.ERROR - + " IN OAIPMH RESPONSE: identifier tag is corrupt!" + theResponseString); - assertTrue(false); - } - } - } diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestGetRecordOnline.java b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestGetRecordOnline.java index b66166819f25ffeaf0ff9a7b09fa0da1d6ba739c..8f70a3944f39f85bf26622db0410d85f3d4cb459 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestGetRecordOnline.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestGetRecordOnline.java @@ -1,7 +1,6 @@ package info.textgrid.middleware.test.online; import static org.junit.Assert.assertTrue; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -15,31 +14,25 @@ import java.util.concurrent.Future; import javax.ws.rs.core.Response; import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.jaxrs.client.Client; -import org.apache.cxf.jaxrs.client.JAXRSClientFactory; -import org.apache.cxf.jaxrs.client.WebClient; -import org.apache.cxf.transport.http.HTTPConduit; -import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.apache.http.HttpStatus; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; -import info.textgrid.middleware.OAIPMHProducer; /** * <p> - * Some online tests for the TextGrid OAIMPH service --> verb=GetRecord. + * Some online tests for the TextGrid OAIMPH service --> verb=GetRecord <-- * </p> * * @author Stefan E. Funk, SUB Göttingen - * @version 2022-09-09 + * @version 2022-09-12 * @since 2022-09-08 */ -// @Ignore +@Ignore public class TestGetRecordOnline { - // + // ** // FINALS // ** @@ -47,7 +40,9 @@ public class TestGetRecordOnline { // private static final String PROPERTIES_FILE = "oaipmh.test.textgridlab-org.properties"; // TODO There is NO IDIOM test data for testing on dev.textgridlab.org! So eventually all // IDIOM tests will fail! - private static final String PROPERTIES_FILE = "oaipmh.test.dev-textgridlab-org.properties"; + protected static final String PROPERTIES_FILE = "oaipmh.test.dev-textgridlab-org.properties"; + + private static final String VERB = OAIPMHUtilitiesOnline.VERB_GET_RECORD; // ** // STATICS @@ -55,7 +50,6 @@ public class TestGetRecordOnline { // Some JAXRS things. private static String oaipmhEndpoint; - private static OAIPMHProducer JAXRSClient; static Client oaipmhWebClient; // Properties @@ -87,14 +81,10 @@ public class TestGetRecordOnline { @BeforeClass public static void setUpBeforeClass() throws Exception { - // Load properties file. - Properties p = new Properties(); - p.load(new FileInputStream(OAIPMHUtilitiesOnline.getResource(PROPERTIES_FILE))); - - System.out.println("Properties file: " + PROPERTIES_FILE); - p.list(System.out); + // Get properties. + Properties p = OAIPMHUtilitiesOnline.getPropertiesFromFile(PROPERTIES_FILE); - // Get other needed properties. + // Set properties. oaipmhEndpoint = p.getProperty("oaipmhEndpoint"); checkGetRecordDC = p.getProperty("checkGetRecordDC"); @@ -116,18 +106,8 @@ public class TestGetRecordOnline { checkGetRecordIDList = OAIPMHUtilitiesOnline.getListFromProperties((String) p.get("checkGetRecordIDList")); - // Get OAI-PMH REST endpoint and HTTP client. - System.out.println("Getting OAI-PMH HTTP client --> " + oaipmhEndpoint + " <--"); - - // Get proxy first, set policy. - JAXRSClient = JAXRSClientFactory.create(oaipmhEndpoint, OAIPMHProducer.class); - HTTPConduit conduit = WebClient.getConfig(JAXRSClient).getHttpConduit(); - HTTPClientPolicy policy = new HTTPClientPolicy(); - policy.setReceiveTimeout(OAIPMHUtilitiesOnline.OAIPMH_CLIENT_TIMEOUT); - conduit.setClient(policy); - - // Create Web Client from Web Proxy. - oaipmhWebClient = WebClient.client(JAXRSClient); + // Get web client from endpoint. + oaipmhWebClient = OAIPMHUtilitiesOnline.getOAIPMHWEebClient(oaipmhEndpoint); } /** @@ -538,288 +518,4 @@ public class TestGetRecordOnline { } } - // ** - // PRIVATE METHODS - // ** - - /** - * TODO Generalise in OaiPmhTestUtilities! - * - * @param theClient - * @param theVerb - * @param theSet - * @param theMetadataPrefix - * @param maxNumberOfPagesToTest - * @param recordsExpectedPerRequest - * @param theThreadName - * @param from - * @param until - * @param resumptionTokenANDMetadataPrefix - * @throws IOException - */ - protected static void testList(Client theClient, final String theVerb, final String theSet, - final String theMetadataPrefix, final int maxNumberOfPagesToTest, - final int recordsExpectedPerRequest, final String theThreadName, final String from, - final String until, final boolean resumptionTokenANDMetadataPrefix) throws IOException { - - String threadName = tn(theThreadName); - - long startTime = System.currentTimeMillis(); - - String testOccurance = "header"; - if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { - testOccurance = "record"; - } - - String path = "verb=" + theVerb + "&metadataPrefix=" + theMetadataPrefix; - - if (theSet != null) { - path += "&set=" + theSet; - } - - if (from != null && until != null) { - path += "&from=" + from + "&until=" + until; - } - - Response httpResponse = OAIPMHUtilitiesOnline.getHttpResponse(theClient, path, threadName); - int status = httpResponse.getStatus(); - - int loopCount = 1; - long timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\t" + threadName + "time: " - + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning) + " (loop " + loopCount - + (maxNumberOfPagesToTest != 0 ? "/" + maxNumberOfPagesToTest : "") + ")"); - - String responseString = IOUtils.readStringFromStream((InputStream) httpResponse.getEntity()); - - // Test resumption token tags. - String restok = examineResumptionTokenTag(responseString, testOccurance, - OAIPMHUtilitiesOnline.NO_TOKEN, recordsExpectedPerRequest, loopCount, threadName); - - // Test general metadata content (if verb is listRecords!), must go conform with the metadata - // prefix setting. - if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { - examineContent(responseString, theMetadataPrefix); - } - - while (status == HttpStatus.SC_OK && !restok.equals(OAIPMHUtilitiesOnline.NO_TOKEN)) { - loopCount += 1; - synchronized (threadName) { - if (maxNumberOfPagesToTest > 0 && loopCount > maxNumberOfPagesToTest) { - System.out - .println("\t" + threadName + "TESTING ONLY " + maxNumberOfPagesToTest + " pages!"); - break; - } - path = "verb=" + theVerb + "&resumptionToken=" + restok; - if (resumptionTokenANDMetadataPrefix) { - path += "&metadataPrefix=" + theMetadataPrefix; - } - httpResponse = OAIPMHUtilitiesOnline.getHttpResponse(theClient, path, threadName); - timeRunning = System.currentTimeMillis() - startTime; - System.out.println("\t" + threadName + "time: " - + OAIPMHUtilitiesOnline.getDurationInSecs(timeRunning) + " (loop " + loopCount - + (maxNumberOfPagesToTest != 0 ? "/" + maxNumberOfPagesToTest : "") + ")"); - - // Test resumption token tags. - responseString = IOUtils.readStringFromStream((InputStream) httpResponse.getEntity()); - restok = examineResumptionTokenTag(responseString, testOccurance, restok, - recordsExpectedPerRequest, loopCount, threadName); - - // Test general metadata content (if verb is listRecords!), must go conform with the - // metadata // prefix setting. - if (theVerb.equals(OAIPMHUtilitiesOnline.VERB_LIST_RECORDS)) { - examineContent(responseString, theMetadataPrefix); - } - } - } - - // Only check for max loops, if we do have less loops and no resumption token, it will be fine, - // too! - if (!restok.equals(OAIPMHUtilitiesOnline.NO_TOKEN) && loopCount < maxNumberOfPagesToTest) { - System.out.println(threadName + OAIPMHUtilitiesOnline.ERROR + ": Must have done " - + maxNumberOfPagesToTest + " loops, but did only " + loopCount); - assertTrue(false); - } - - // Check for single loop, we need MORE! - if (loopCount == 1) { - System.out - .println(threadName + OAIPMHUtilitiesOnline.ERROR + ": Must have more than one loop!"); - assertTrue(false); - } - - System.out.println("\t" + threadName + OAIPMHUtilitiesOnline.OK); - } - - /** - * TODO Generalise in OaiPmhTestUtilities! - * - * @param theResponse - * @param recordOrHeader - * @param oldtok - * @param recordsExpectedPerRequest - * @param loopCount - * @param theThreadName - * @return Resumption token string, if existing, -1 if either no <resumptionToken> tag is - * existing, tag is existing and has no token value. - * @throws IOException - */ - public static String examineResumptionTokenTag(final String theResponseString, - final String recordOrHeader, final String oldtok, final int recordsExpectedPerRequest, - final int loopCount, final String theThreadName) throws IOException { - - // Test for OAIPMH errors. - if (theResponseString.contains("<error code=\"badArgument\">")) { - String message = - theThreadName + OAIPMHUtilitiesOnline.ERROR + " IN OAIPMH RESPONSE: " - + theResponseString; - System.out.println(message); - throw new IOException(message); - } - - // Count response objects at first. - int recordCount = 0; - int i = theResponseString.indexOf("<" + recordOrHeader + ">", 0); - while (i != -1) { - recordCount++; - i++; - i = theResponseString.indexOf("<" + recordOrHeader + ">", i); - } - - System.out.println("\t" + theThreadName + recordOrHeader + "s: " + recordCount); - - // Check if token tag is existing. - int tokStart = theResponseString.indexOf("<resumptionToken"); - int tokEnd = theResponseString.indexOf("</resumptionToken"); - - if (tokStart == -1 && tokEnd == -1) { - System.out.println("\t" + theThreadName + "token: no token"); - - return OAIPMHUtilitiesOnline.NO_TOKEN; - } - - String restokTmp = theResponseString.substring(tokStart, tokEnd); - // Get token tag. - String toktag = restokTmp.substring(0, restokTmp.indexOf(">") + 1).trim(); - System.out.println("\t" + theThreadName + "tokentag: " + toktag); - - // Get token. - String restok = restokTmp.substring(restokTmp.indexOf(">") + 1).trim(); - System.out.println("\t" + theThreadName + "token: " + restok); - - // Check if old and new token are equal or not. - boolean tokchanged = !oldtok.equals(restok); - System.out.println("\t" + theThreadName + "tokchngd: " + 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("\t" + theThreadName + "size: " + size + " / " + cursor); - - // If token is provided, test cursor and element count! - if (!restok.isEmpty()) { - synchronized (TestGetRecordOnline.class) { - // Check <record> or <header> count, must be recordsExpectedPerRequest! - if (recordCount != recordsExpectedPerRequest) { - String message = - OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader + " count mismatch, must be " - + recordsExpectedPerRequest + " if token is provided, but is " + recordCount; - assertTrue(message, false); - } - if (size <= recordsExpectedPerRequest) { - String message = - OAIPMHUtilitiesOnline.ERROR + ": completeListSize count mismatch, must be > " - + recordsExpectedPerRequest + " if token is provided, but is " + size; - assertTrue(message, false); - } - if (cursor != recordsExpectedPerRequest * loopCount) { - String message = OAIPMHUtilitiesOnline.ERROR + ": cursor must be " - + (loopCount * recordsExpectedPerRequest) + " in loop " + loopCount + ", but is " - + cursor; - assertTrue(message, false); - } - } - } - - // If no token is provided, stop querying. - else { - // Check <record> count, must be completeListSize % recordsExpectedPerRequest. - if (recordCount != size % recordsExpectedPerRequest) { - System.err.println(theThreadName + OAIPMHUtilitiesOnline.ERROR + ": " + recordOrHeader - + " count mismatch, should be " + size % recordsExpectedPerRequest + ", but is " - + recordCount); - assertTrue(false); - } - - // No resumption token available in response. - return OAIPMHUtilitiesOnline.NO_TOKEN; - } - - return restok; - } - - /** - * @param theThreadName - * @return - */ - private static String tn(String theThreadName) { - - String result = theThreadName; - - if (theThreadName != null && !theThreadName.equals("") && !theThreadName.endsWith(" ")) { - result = "[" + theThreadName + "] "; - } - - return result; - } - - /** - * @param theResponseString - * @param theMetadataFormat - */ - private static void examineContent(String theResponseString, String theMetadataFormat) { - - // Check for correct metadata content according to metadata prefix. - if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_DC_PREFIX) - && !theResponseString - .contains("metadataPrefix=\"" + OAIPMHUtilitiesOnline.OAI_DC_PREFIX + "\"") - && !theResponseString.contains(OAIPMHUtilitiesOnline.EXPECTED_OAIDC_FORMAT_CONTENT)) { - System.out - .println(OAIPMHUtilitiesOnline.OAI_DC_PREFIX + " needs to deliver content with schema: " - + OAIPMHUtilitiesOnline.EXPECTED_OAIDC_FORMAT_CONTENT + "!"); - - System.out.println("ERROR:\n" + theResponseString); - - assertTrue(false); - } else if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX) - && !theResponseString.contains( - "metadataPrefix=\"" + OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT - + "\"") - && !theResponseString - .contains(OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT)) { - System.out.println( - OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX + " needs to deliver content with schema: " - + OAIPMHUtilitiesOnline.EXPECTED_IDIOMMETS_FORMAT_CONTENT + "!"); - - System.out.println("ERROR:\n" + theResponseString); - - assertTrue(false); - } else if (theMetadataFormat.equals(OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX) - && !theResponseString - .contains("metadataPrefix=\"" + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX + "\"") - && !theResponseString.contains(OAIPMHUtilitiesOnline.EXPECTED_DATACITE_FORMAT_CONTENT)) { - System.out.println( - OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX + " needs to deliver content with schema: " - + OAIPMHUtilitiesOnline.EXPECTED_DATACITE_FORMAT_CONTENT + "!"); - - System.out.println("ERROR:\n" + theResponseString); - - assertTrue(false); - } else { - System.out.println(theResponseString); - } - } - } diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestListRecordsOnline.java b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestListRecordsOnline.java new file mode 100644 index 0000000000000000000000000000000000000000..10aa9b7a14cd19dc31f787b9f79bfa722192ab4f --- /dev/null +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/test/online/TestListRecordsOnline.java @@ -0,0 +1,290 @@ +package info.textgrid.middleware.test.online; + +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.util.Properties; +import org.apache.cxf.jaxrs.client.Client; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * <p> + * Some online tests for the TextGrid OAIMPH service --> verb=ListRecords <-- + * </p> + * + * @author Stefan E. Funk, SUB Göttingen + * @version 2022-09-12 + * @since 2022-09-12 + */ +@Ignore +public class TestListRecordsOnline { + + // ** + // FINALS + // ** + + // private static final String PROPERTIES_FILE = "oaipmh.test.textgridlab-org.properties"; + // TODO There is NO IDIOM test data for testing on dev.textgridlab.org! So eventually all + // IDIOM tests will fail! + protected static final String PROPERTIES_FILE = "oaipmh.test.dev-textgridlab-org.properties"; + + // ** + // STATICS + // ** + + // Some JAXRS things. + private static String oaipmhEndpoint; + static Client oaipmhWebClient; + + // Properties + private static String checkListRecordsDCFrom; + private static String checkListRecordsDCUntil; + private static String checkListRecordsDATACITEFrom; + private static String checkListRecordsDATACITEUntil; + private static String checkListRecordsSet; + private static Integer checkListRecordsSetExpectedPages; + private static Integer checkListRecordsSetExpectedPagesIDIOM; + + // ** + // PREPARATIONS + // ** + + /** + * @throws Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + // Get properties. + Properties p = OAIPMHUtilitiesOnline.getPropertiesFromFile(PROPERTIES_FILE); + + // Set properties. + oaipmhEndpoint = p.getProperty("oaipmhEndpoint"); + + checkListRecordsDCFrom = p.getProperty("checkListRecordsDCFrom"); + checkListRecordsDCUntil = p.getProperty("checkListRecordsDCUntil"); + checkListRecordsDATACITEFrom = p.getProperty("checkListRecordsDATACITEFrom"); + checkListRecordsDATACITEUntil = p.getProperty("checkListRecordsDATACITEUntil"); + checkListRecordsSet = p.getProperty("checkListRecordsSet"); + checkListRecordsSetExpectedPages = + Integer.parseInt(p.getProperty("checkListRecordsSetExpectedPages").trim()); + checkListRecordsSetExpectedPagesIDIOM = + Integer.parseInt(p.getProperty("checkListRecordsSetExpectedPagesIDIOM").trim()); + + // Get web client from endpoint. + oaipmhWebClient = OAIPMHUtilitiesOnline.getOAIPMHWEebClient(oaipmhEndpoint); + } + + /** + * @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 testListRecordsSetDC() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); + + int pages = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DC_PREFIX, checkListRecordsSet); + + if (pages != checkListRecordsSetExpectedPages) { + assertTrue(pages + " != " + checkListRecordsSetExpectedPages, false); + } + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsSetDATACITE() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "testListRecordsSetDATACITE()"); + + int pages = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX, checkListRecordsSet); + + if (pages != checkListRecordsSetExpectedPages) { + assertTrue(pages + " != " + checkListRecordsSetExpectedPages, false); + } + } + + /** + * <p> + * Sets are not implemented in IDIOM requests, we always only ask for ONE project here! + * </p> + * + * @throws IOException + */ + @Test + @Ignore + public void testListRecordsSetIDIOM() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "testListRecordsSetIDIOM()"); + + int pages = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX, checkListRecordsSet); + + if (pages != checkListRecordsSetExpectedPagesIDIOM) { + assertTrue(pages + " != " + checkListRecordsSetExpectedPagesIDIOM, false); + } + } + + /** + * FIXME I don't understand this test. We do not have hdl: prefixes in TextGrid OAI-PMH, do we? + * + * @throws IOException + */ + @Test + @Ignore + public void testListRecordsSetDCDifferentPrefixes() throws IOException { + + String uri = checkListRecordsSet; + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - HDL PREFIX"); + + if (!uri.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { + assertTrue("missing '" + OAIPMHUtilitiesOnline.HDL_PREFIX + "' prefix!", false); + } + int pages1 = OAIPMHUtilitiesOnline.testList(oaipmhWebClient, + OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, OAIPMHUtilitiesOnline.OAI_DC_PREFIX, uri); + if (pages1 != checkListRecordsSetExpectedPages) { + System.out.println(pages1 + " != " + checkListRecordsSetExpectedPages); + } + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - NO PREFIX"); + + if (checkListRecordsSet.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { + uri = checkListRecordsSet.substring(4); + } + int pages2 = OAIPMHUtilitiesOnline.testList(oaipmhWebClient, + OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, OAIPMHUtilitiesOnline.OAI_DC_PREFIX, uri); + if (pages2 != checkListRecordsSetExpectedPages) { + System.out.println(pages2 + " != " + checkListRecordsSetExpectedPages); + } + + // Check pages count. + if (pages1 != checkListRecordsSetExpectedPages || pages2 != checkListRecordsSetExpectedPages) { + assertTrue("pages count does not match", false); + } + } + + /** + * TODO Un-ignore if identifier check is implemented! + * + * @throws IOException + */ + @Test + @Ignore + public void testListRecordsSetDCStarPrefix() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS - STAR PREFIX"); + + if (checkListRecordsSet.startsWith(OAIPMHUtilitiesOnline.HDL_PREFIX)) { + checkListRecordsSet = OAIPMHUtilitiesOnline.STAR_PREFIX + checkListRecordsSet.substring(4); + } + + int pages3 = + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DC_PREFIX, checkListRecordsSet); + if (pages3 != 1) { + System.out.println(pages3 + " != " + 1); + } + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsDCFromUntil() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); + + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DC_PREFIX, OAIPMHUtilitiesOnline.NO_SET, checkListRecordsDCFrom, + checkListRecordsDCUntil); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsDATASITE() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "testListRecordsDATASITE()"); + + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX, OAIPMHUtilitiesOnline.NO_SET); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsIDIOM() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "testListRecordsIDIOM()"); + + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_IDIOMMETS_PREFIX, OAIPMHUtilitiesOnline.NO_SET); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsDC() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "testListRecordsDC()"); + + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DC_PREFIX, OAIPMHUtilitiesOnline.NO_SET); + } + + /** + * @throws IOException + */ + @Test + public void testListRecordsDATASITEFromUntil() throws IOException { + + System.out.println(OAIPMHUtilitiesOnline.TESTING + "#LISTRECORDS"); + + OAIPMHUtilitiesOnline.testList(oaipmhWebClient, OAIPMHUtilitiesOnline.VERB_LIST_RECORDS, + OAIPMHUtilitiesOnline.OAI_DATACITE_PREFIX, OAIPMHUtilitiesOnline.NO_SET, + checkListRecordsDATACITEFrom, checkListRecordsDATACITEUntil); + } + +} diff --git a/oaipmh-core/src/test/resources/oaipmh.test.dev-textgridlab-org.properties b/oaipmh-core/src/test/resources/oaipmh.test.dev-textgridlab-org.properties index f8a16ed9e0fc9fc109495d12e0b47e71bfe789b6..a8cc168f4379f32d1c0fd5906d7cff8572495b1e 100644 --- a/oaipmh-core/src/test/resources/oaipmh.test.dev-textgridlab-org.properties +++ b/oaipmh-core/src/test/resources/oaipmh.test.dev-textgridlab-org.properties @@ -16,9 +16,6 @@ expectedGetRecordIDIOMImage = Aguateca, Miscellaneous Text 11 expectedGetRecordIDIOMImageNoPrefix = The request includes illegal arguments or is missing required arguments: [metadataPrefix] checkGetRecordIDList = textgrid:mq05.0, textgrid:jgv6.0, textgrid:w7rz.0, textgrid:w36b.0, textgrid:mcdv.0, textgrid:v0qx.0, textgrid:xkck.0, textgrid:t3m1.0, textgrid:10rsq.0, textgrid:mq05.0, textgrid:jgv6.0, textgrid:w7rz.0, textgrid:w36b.0, textgrid:mcdv.0, textgrid:v0qx.0, textgrid:xkck.0, textgrid:t3m1.0, textgrid:10rsq.0 -# ListSets -expectedListSets = project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c - # ListRecords checkListRecordsDC = project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c checkListRecordsDCFrom = 2012-01-04T01:00:00 @@ -27,8 +24,14 @@ checkListRecordsIDIOMFrom = 2012-01-04T01:00:00 checkListRecordsIDIOMUntil = 2012-01-04T12:00:00 checkListRecordsDATACITEFrom = 2012-01-04T01:00:00 checkListRecordsDATACITEUntil = 2012-01-04T12:00:00 +checkListRecordsSet = project:TGPR-59722e29-efcb-330f-b9b4-5ef2fb08edab +checkListRecordsSetExpectedPages = 2 +checkListRecordsSetExpectedPagesIDIOM = 1 # ListIdentifiers checkListIdentifiersSet = project:TGPR-59722e29-efcb-330f-b9b4-5ef2fb08edab checkListIdentifiersPagesToTestIDIOM = 10 checkListIdentifiersRecordsPerPageIDIOM = 30 + +# ListSets +expectedListSets = project:TGPR-372fe6dc-57f2-6cd4-01b5-2c4bbefcfd3c