Commit 8a9ea56d authored by piotr.gawron's avatar piotr.gawron
Browse files

tests added for pubmed parser

parent c87c68aa
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>annotation</artifactId>
<name>Annotation module</name>
<description>Module used for preparing annotations for model and connect to external resources.</description>
<repositories>
<!-- Main repository of ebi -->
<repository>
<id>central</id>
<url>http://repo.maven.apache.org/maven2/</url>
</repository>
<!-- Repository used for chebi -->
<repository>
<id>ebi-repo</id>
<name>The EBI internal repository</name>
<url>http://www.ebi.ac.uk/~maven/m2repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- dependency from the MapViewer model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
<!-- dependency from the MapViewer dao -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>persist</artifactId>
<version>1.0</version>
</dependency>
<!-- dependency from the MapViewer cell designer parser (we need access
to parsing functions for rdf) -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter-CellDesigner</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter</artifactId>
<version>1.0</version>
</dependency>
<!-- Log4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- XML parser -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>${xercesImp.version}</version>
</dependency>
<!-- Chebi API -->
<dependency>
<groupId>uk.ac.ebi.chebi.webapps.chebiWS.client</groupId>
<artifactId>chebiWS-client</artifactId>
<version>${chebi-ws.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Miriam registry API -->
<dependency>
<groupId>uk.ac.ebi.miriam</groupId>
<artifactId>miriam-lib</artifactId>
<version>${miriam-lib.version}</version>
</dependency>
<!-- Utils used for connection via http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<!-- Java xml serialization -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<!-- mockito used for testing -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>annotation</artifactId>
<name>Annotation module</name>
<description>Module used for preparing annotations for model and connect to external resources.</description>
<repositories>
<!-- Main repository of ebi -->
<repository>
<id>central</id>
<url>http://repo.maven.apache.org/maven2/</url>
</repository>
<!-- Repository used for chebi -->
<repository>
<id>ebi-repo</id>
<name>The EBI internal repository</name>
<url>http://www.ebi.ac.uk/~maven/m2repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- dependency from the MapViewer model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
<!-- dependency from the MapViewer dao -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>persist</artifactId>
<version>1.0</version>
</dependency>
<!-- dependency from the MapViewer cell designer parser (we need access
to parsing functions for rdf) -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter-CellDesigner</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>converter</artifactId>
<version>1.0</version>
</dependency>
<!-- Log4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- XML parser -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>${xercesImp.version}</version>
</dependency>
<!-- Chebi API -->
<dependency>
<groupId>uk.ac.ebi.chebi.webapps.chebiWS.client</groupId>
<artifactId>chebiWS-client</artifactId>
<version>${chebi-ws.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Miriam registry API -->
<dependency>
<groupId>uk.ac.ebi.miriam</groupId>
<artifactId>miriam-lib</artifactId>
<version>${miriam-lib.version}</version>
</dependency>
<!-- Utils used for connection via http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${apache.httpcomponents.version}</version>
<scope>compile</scope>
</dependency>
<!-- Java xml serialization -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<!-- mockito used for testing -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package lcsb.mapviewer.annotation.services;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.log4j.Logger;
......@@ -16,9 +18,11 @@ import org.w3c.dom.NodeList;
import lcsb.mapviewer.annotation.cache.CachableInterface;
import lcsb.mapviewer.annotation.cache.GeneralCacheInterface;
import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
import lcsb.mapviewer.annotation.cache.WebPageDownloader;
import lcsb.mapviewer.annotation.cache.XmlSerializer;
import lcsb.mapviewer.annotation.data.Article;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
......@@ -35,18 +39,23 @@ public class PubmedParser extends CachableInterface implements IExternalService
* Pubmed identifier used to check the status of service (functionality from
* {@link IExternalService}).
*/
private static final int SERVICE_STATUS_PUBMED_ID = 12345;
static final int SERVICE_STATUS_PUBMED_ID = 12345;
/**
* Prefix used for caching elements with pubmed identifier as a key.
*/
private static final String PUBMED_PREFIX = "pubmed: ";
static final String PUBMED_PREFIX = "pubmed: ";
/**
* Length of {@link #PUBMED_PREFIX} string.
*/
private static final int PUBMED_PREFIX_LENGTH = PUBMED_PREFIX.length();
/**
* Version of the remote API thnat is supported by this connecting class.
*/
static final String SUPPORTED_VERSION = "4.5.2";
/**
* Connector used for accessing data from miriam registry.
*/
......@@ -248,13 +257,12 @@ public class PubmedParser extends CachableInterface implements IExternalService
try {
Article art = getPubmedArticleById(SERVICE_STATUS_PUBMED_ID);
status.setStatus(ExternalServiceStatusType.OK);
if (art == null) {
status.setStatus(ExternalServiceStatusType.DOWN);
} else if (art.getTitle() == null) {
status.setStatus(ExternalServiceStatusType.CHANGED);
} else if (art.getTitle().equals("")) {
if (!getApiVersion().equals(SUPPORTED_VERSION)) {
status.setStatus(ExternalServiceStatusType.CHANGED);
} else if (art == null) {
status.setStatus(ExternalServiceStatusType.DOWN);
} else {
status.setStatus(ExternalServiceStatusType.OK);
}
} catch (Exception e) {
logger.error(status.getName() + " is down", e);
......@@ -264,6 +272,35 @@ public class PubmedParser extends CachableInterface implements IExternalService
return status;
}
/**
* Returns current version of the pmc API.
*
* @return version of the api to which this class is connected
* @throws PubmedSearchException
* thrown when there is a problem with accessing external databes
*/
public String getApiVersion() throws PubmedSearchException {
try {
String url = "http://www.ebi.ac.uk/europepmc/webservices/rest/search/resulttype=core&query=src%3Amed+ext_id%3A23644949";
String content = getWebPageContent(url);
Document mainDoc = super.getXmlDocumentFromString(content);
mainDoc.getDocumentElement().normalize();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
NodeList nodeList = (NodeList) xpath.compile("/responseWrapper/version").evaluate(mainDoc, XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
return nodeList.item(0).getTextContent();
}
return null;
} catch (IOException e) {
throw new PubmedSearchException("Problem with accessing pubmed db", e);
} catch (InvalidXmlSchemaException | XPathExpressionException e) {
throw new PubmedSearchException("Invalid response from pubmed db", e);
}
}
/**
* Get the summary of the article.
*
......@@ -311,4 +348,15 @@ public class PubmedParser extends CachableInterface implements IExternalService
protected void setArticleSerializer(XmlSerializer<Article> articleSerializer) {
this.articleSerializer = articleSerializer;
}
@Override
protected WebPageDownloader getWebPageDownloader() {
return super.getWebPageDownloader();
}
@Override
protected void setWebPageDownloader(WebPageDownloader webPageDownloader) {
super.setWebPageDownloader(webPageDownloader);
}
}
......@@ -6,7 +6,10 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -14,10 +17,14 @@ import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.AnnotationTestFunctions;
import lcsb.mapviewer.annotation.cache.GeneralCacheInterface;
import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
import lcsb.mapviewer.annotation.cache.WebPageDownloader;
import lcsb.mapviewer.annotation.cache.XmlSerializer;
import lcsb.mapviewer.annotation.data.Article;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
......@@ -145,16 +152,14 @@ public class PubmedParserTest extends AnnotationTestFunctions {
}
@Test
public void testGetSummary() {
public void testGetSummary() throws Exception {
try {
String summary = pubmedParser.getSummary(18400456);
assertNotNull(summary);
assertFalse(summary.isEmpty());
} catch (Exception e) {
e.printStackTrace();
fail("Unknown exception " + e.getMessage());
throw e;
}
}
......@@ -171,6 +176,18 @@ public class PubmedParserTest extends AnnotationTestFunctions {
}
}
@Test
public void testGetSummary3() throws Exception {
try {
String summary = pubmedParser.getSummary("18400456");
assertNotNull(summary);
assertFalse(summary.isEmpty());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
// wait max 15 second
public void testCachableInterface() throws Exception {
......@@ -209,6 +226,30 @@ public class PubmedParserTest extends AnnotationTestFunctions {
}
}
@Test
public void testRefreshWhenExternalNotAvailable() throws Exception {
WebPageDownloader downloader = pubmedParser.getWebPageDownloader();
try {
pubmedParser.setCache(null);
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException());
pubmedParser.setWebPageDownloader(mockDownloader);
String query = PubmedParser.PUBMED_PREFIX + PubmedParser.SERVICE_STATUS_PUBMED_ID;
pubmedParser.refreshCacheQuery(query);
fail("Exception expected");
} catch (SourceNotAvailable e) {
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pubmedParser.setWebPageDownloader(downloader);
pubmedParser.setCache(cache);
}
}
@Test
public void testRefreshInvalidCacheQuery2() throws Exception {
try {
......@@ -233,4 +274,152 @@ public class PubmedParserTest extends AnnotationTestFunctions {
}
}
@Test
public void testStatus() throws Exception {
try {
assertEquals(ExternalServiceStatusType.OK, pubmedParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testSimulateDownStatus() throws Exception {
WebPageDownloader downloader = pubmedParser.getWebPageDownloader();
try {
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException());
pubmedParser.setWebPageDownloader(mockDownloader);
assertEquals(ExternalServiceStatusType.DOWN, pubmedParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pubmedParser.setWebPageDownloader(downloader);
}
}
@Test
public void testSimulateDownStatus2() throws Exception {
WebPageDownloader downloader = pubmedParser.getWebPageDownloader();
try {
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString()))
.thenReturn("<responseWrapper><version>" + PubmedParser.SUPPORTED_VERSION + "</version></responseWrapper>");
pubmedParser.setWebPageDownloader(mockDownloader);
assertEquals(ExternalServiceStatusType.DOWN, pubmedParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pubmedParser.setWebPageDownloader(downloader);
}
}
@Test
public void testSimulateChangeStatus() throws Exception {
WebPageDownloader downloader = pubmedParser.getWebPageDownloader();
try {
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString()))
.thenReturn("<responseWrapper><version>" + PubmedParser.SUPPORTED_VERSION + "blabla</version></responseWrapper>");
pubmedParser.setWebPageDownloader(mockDownloader);
assertEquals(ExternalServiceStatusType.CHANGED, pubmedParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pubmedParser.setWebPageDownloader(downloader);
}
}
@Test
public void testGetApiVersion() throws Exception {
try {
assertNotNull(pubmedParser.getApiVersion());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}