/** * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright ${year} * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.dataone.integration.it; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Scanner; import java.util.Vector; import org.apache.commons.io.IOUtils; import org.apache.solr.client.solrj.util.ClientUtils; import org.dataone.client.v1.CNode; import org.dataone.client.D1Node; import org.dataone.integration.it.testImplementations.AbstractAuthorizationTestImplementations; import org.dataone.service.exceptions.BaseException; import org.dataone.service.types.v1.Identifier; import org.dataone.service.types.v1.Node; import org.dataone.service.types.v1.ObjectList; import org.dataone.service.types.v1.Permission; import org.dataone.service.types.v1_1.QueryEngineDescription; import org.dataone.service.types.v1_1.QueryEngineList; import org.dataone.service.util.D1Url; import org.dataone.service.util.EncodingUtilities; import org.junit.Test; /** * Test the DataONE Java client methods that focus on CN services. * @author Rob Nahf */ public class CNodeTier1_search_IT extends AbstractAuthorizationTestImplementations { private static String unicodeIdPrefix = "testCNodeTier1"; private static String identifierEncodingTestFile = "/d1_testdocs/encodingTestSet/testUnicodeStrings.utf8.txt"; // private static String identifierEncodingTestFile = "/d1_testdocs/encodingTestSet/testAsciiStrings.utf8.txt"; private static String currentUrl; @Override protected String getTestDescription() { return "Test the Tier1 CN methods related to the search & query component"; } @Override protected Iterator getNodeIterator() { return getCoordinatingNodeIterator(); } @Override protected CNode instantiateD1Node(String baseUrl) { return new CNode(baseUrl); } @Override protected String getTestServiceEndpoint() { return "/query/solr/?q=id:{pid}"; } @Override protected boolean runTest(Permission p) { if (p.equals(Permission.READ)) return true; return false; } @Override protected String runAuthTest(D1Node d1Node, Identifier pid, Permission permission) { String outcome = null; InputStream is = null; try { is = d1Node.query("solr", "?q=id:" + EncodingUtilities.encodeUrlQuerySegment("\"" + pid.getValue() + "\"")); // + "&fl=id&start=0&rows=1000")); log.info(d1Node.getLatestRequestUrl()); String responseText = IOUtils.toString(is, "UTF-8"); if (responseText.contains(" it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testSearch(...) vs. node: " + currentUrl); try { ObjectList response = cn.search(null,"solr","?q=*:*"); checkTrue(cn.getLatestRequestUrl(),"search(...) returns a ObjectList object", response != null); } catch (IndexOutOfBoundsException e) { handleFail(cn.getLatestRequestUrl(),"No Objects available to test against"); } catch (BaseException e) { handleFail(cn.getLatestRequestUrl(),e.getClass().getSimpleName() + ": " + e.getDetail_code() + ":: " + e.getDescription()); } catch(Exception e) { e.printStackTrace(); handleFail(currentUrl,e.getClass().getName() + ": " + e.getMessage()); } } } /** * This test searches for identifiers containing the representative unicode * characters, and is successful if ObjectList is returned (can be empty). * The goal is to rule out encoding issues with the request and processing. * It does NOT test that an object with those characters are found. */ @Test public void testSearch_Solr_unicodeTests() { // get identifiers to check with Vector unicodeString = new Vector(); Vector escapedString = new Vector(); InputStream is = this.getClass().getResourceAsStream(identifierEncodingTestFile); Scanner s = new Scanner(is,"UTF-8"); String[] temp; int c = 0; try{ while (s.hasNextLine()) { String line = s.nextLine(); if (line.startsWith("common-") || line.startsWith("query-")) { System.out.println(c++ + " " + line); temp = line.split("\t"); unicodeString.add(temp[0]); escapedString.add(temp[1]); } } } finally { s.close(); } Iterator it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testSearch_Solr_unicodeTests(...) vs. node: " + currentUrl); Vector nodeSummary = new Vector(); nodeSummary.add("Node Test Summary for node: " + currentUrl ); for (int i=0; i it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testListQueryEngines(...) vs. node: " + currentUrl); try { QueryEngineList response = cn.listQueryEngines(); checkTrue(cn.getLatestRequestUrl(),"listQueryEngines(...) returns a QueryEngineList object", response != null); } catch (BaseException e) { handleFail(cn.getLatestRequestUrl(),e.getClass().getSimpleName() + ": " + e.getDetail_code() + ":: " + e.getDescription()); } catch(Exception e) { e.printStackTrace(); handleFail(currentUrl,e.getClass().getName() + ": " + e.getMessage()); } } } /** * this test runs the base listQueryEngines request and makes sure we get * a proper response (QueryEngineList) */ @Test public void testGetQueryEngineDescription() { Iterator it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testGetQueryEngineDescription(...) vs. node: " + currentUrl); try { QueryEngineList response = cn.listQueryEngines(); QueryEngineDescription response2 = cn.getQueryEngineDescription(response.getQueryEngine(0)); checkTrue(cn.getLatestRequestUrl(),"getQueryEngineDescription(...)" + " returns a QueryEngineDescription object", response2 != null); } catch (BaseException e) { handleFail(cn.getLatestRequestUrl(),e.getClass().getSimpleName() + ": " + e.getDetail_code() + ":: " + e.getDescription()); } catch(Exception e) { e.printStackTrace(); handleFail(currentUrl,e.getClass().getName() + ": " + e.getMessage()); } } } /** * this test runs the base listQueryEngines request and makes sure we get * a proper response (QueryEngineList) */ @Test public void testQuery() { Iterator it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testQuery(...) vs. node: " + currentUrl); try { InputStream response2 = cn.query("solr","?q=*:*"); checkTrue(cn.getLatestRequestUrl(),"query(...)" + " returns an InputStream object", response2 != null); } catch (BaseException e) { handleFail(cn.getLatestRequestUrl(),e.getClass().getSimpleName() + ": " + e.getDetail_code() + ":: " + e.getDescription()); } catch(Exception e) { e.printStackTrace(); handleFail(currentUrl,e.getClass().getName() + ": " + e.getMessage()); } } } @Test public void testQuery_Authentication() { Iterator it = getCoordinatingNodeIterator(); while (it.hasNext()) { currentUrl = it.next().getBaseURL(); CNode cn = new CNode(currentUrl); currentUrl = cn.getNodeBaseServiceUrl(); printTestHeader("testQuery(...) vs. node: " + currentUrl); try { InputStream response2 = cn.query("solr","?q=*:*"); checkTrue(cn.getLatestRequestUrl(),"query(...)" + " returns an InputStream object", response2 != null); } catch (BaseException e) { handleFail(cn.getLatestRequestUrl(),e.getClass().getSimpleName() + ": " + e.getDetail_code() + ":: " + e.getDescription()); } catch(Exception e) { e.printStackTrace(); handleFail(currentUrl,e.getClass().getName() + ": " + e.getMessage()); } } } }