PubMedSearch { public Collection<BibliographicReference> searchAndRetrieveByHTTP( Collection<String> searchTerms ) throws IOException, SAXException, ParserConfigurationException { StringBuilder builder = new StringBuilder(); builder.append( uri ); builder.append( "&term=" ); for ( String string : searchTerms ) { builder.append( string ); builder.append( "+" ); } URL toBeGotten = new URL( StringUtils.chomp( builder.toString() ) ); log.info( "Fetching " + toBeGotten ); ESearchXMLParser parser = new ESearchXMLParser(); Collection<String> ids = null; int numTries = 0; while ( ids == null && numTries < MAX_TRIES ) { try { numTries++; ids = parser.parse( toBeGotten.openStream() ); } catch ( IOException e ) { if ( numTries == MAX_TRIES ) throw e; log.warn( "Failed attempt (" + numTries + "/" + MAX_TRIES + ") " + e.getMessage() ); try { Thread.sleep( 200 ); } catch ( InterruptedException e1 ) { } } } Collection<BibliographicReference> results = fetchById( ids ); log.info( "Fetched " + results.size() + " references" ); return results; } PubMedSearch(); Collection<BibliographicReference> searchAndRetrieveByHTTP( Collection<String> searchTerms ); Collection<BibliographicReference> searchAndRetrieveIdByHTTP( Collection<String> searchTerms ); Collection<String> searchAndRetrieveIdsByHTTP( Collection<String> searchTerms ); Collection<String> searchAndRetrieveIdsByHTTP( String searchQuery );  }
@Test public void testSearchAndRetrieveByHTTP() throws Exception { try { PubMedSearch pms = new PubMedSearch(); Collection<String> searchTerms = new HashSet<>(); searchTerms.add( "brain" ); searchTerms.add( "hippocampus" ); searchTerms.add( "habenula" ); searchTerms.add( "glucose" ); Collection<BibliographicReference> actualResult = pms.searchAndRetrieveByHTTP( searchTerms ); assertTrue( "Expected at least 5 results, got " + actualResult.size(), actualResult.size() >= 5 ); BibliographicReference r = actualResult.iterator().next(); assertNotNull(r.getAuthorList()); assertNotNull(r.getPublicationDate()); } catch ( java.net.UnknownHostException e ) { PubMedSearchTest.log.warn( "Test skipped due to unknown host exception" ); } catch ( java.io.IOException e ) { if ( e.getMessage().contains( "503" ) || e.getMessage().contains( "502" ) ) { PubMedSearchTest.log.warn( "Test skipped due to a 50X error from NCBI" ); return; } throw e; } }
@Test public void testSearchAndRetrieveByHTTPInChunks() throws Exception { try { PubMedSearch pms = new PubMedSearch(); Collection<String> searchTerms = new HashSet<>(); searchTerms.add( "brain" ); searchTerms.add( "hippocampus" ); searchTerms.add( "habenula" ); Collection<BibliographicReference> actualResult = pms.searchAndRetrieveByHTTP( searchTerms ); assertTrue( "Expected at least 10, got " + actualResult.size(), actualResult.size() >= 10 ); } catch ( java.net.UnknownHostException e ) { PubMedSearchTest.log.warn( "Test skipped due to unknown host exception" ); } catch ( java.io.IOException e ) { this.checkCause( e ); } }