diff --git a/annotation/src/main/java/lcsb/mapviewer/annotation/services/MiriamConnector.java b/annotation/src/main/java/lcsb/mapviewer/annotation/services/MiriamConnector.java index f90874d5d3da486df6dce2f6513da87be67b5e9d..6cf4f1819003508f194c5b31dd5d6d2311df6c78 100644 --- a/annotation/src/main/java/lcsb/mapviewer/annotation/services/MiriamConnector.java +++ b/annotation/src/main/java/lcsb/mapviewer/annotation/services/MiriamConnector.java @@ -21,261 +21,262 @@ import uk.ac.ebi.miriam.lib.MiriamLink; /** * Class responsible for connection to Miriam DB. It allows to update URI of - * database being used and retreive urls for miriam uri. + * database being used and retrieve urls for miriam uri. * * @author Piotr Gawron * */ public final class MiriamConnector extends CachableInterface implements IExternalService { - /** - * String used to distinguish cached data for links. - */ - static final String LINK_DB_PREFIX = "Link: "; + /** + * String used to distinguish cached data for links. + */ + static final String LINK_DB_PREFIX = "Link: "; - /** - * String used to distinguish cached data for checking if uri is valid. - */ - protected static final String VALID_URI_PREFIX = "Validity: "; + /** + * String used to distinguish cached data for checking if uri is valid. + */ + protected static final String VALID_URI_PREFIX = "Validity: "; - /** - * String describing invalid miriam entries that will be put into db (instead - * of null). - */ - private static final String INVALID_LINK = "INVALID"; + /** + * String describing invalid miriam entries that will be put into db (instead of + * null). + */ + private static final String INVALID_LINK = "INVALID"; - /** - * Default class logger. - */ - private Logger logger = Logger.getLogger(MiriamConnector.class.getName()); + /** + * Default class logger. + */ + private Logger logger = Logger.getLogger(MiriamConnector.class.getName()); - /** - * Miriam Registry API. - */ - private MiriamLink link; + /** + * Miriam Registry API. + */ + private MiriamLink link; - /** - * Default class constructor. Prevent initialization. - */ - public MiriamConnector() { - super(MiriamConnector.class); - link = new MiriamLink(); - } + /** + * Default class constructor. Prevent initialization. + */ + public MiriamConnector() { + super(MiriamConnector.class); + link = new MiriamLink(); + link.setAddress("https://www.ebi.ac.uk/miriamws/main/MiriamWebServices"); + } - /** - * Returns url to the webpage represented by {@link MiriamData} param. - * - * @param miriamData - * miriam data - * @return url to resource pointed by miriam data - */ - public String getUrlString(MiriamData miriamData) { - if (miriamData.getDataType() == null) { - throw new InvalidArgumentException("Data type cannot be empty."); - } else if (miriamData.getDataType().getUris().size() == 0) { - throw new InvalidArgumentException("Url for " + miriamData.getDataType() + " cannot be retreived."); - } - String query = LINK_DB_PREFIX + miriamData.getDataType().getUris().get(0) + "\n" + miriamData.getResource(); - String result = getCacheValue(query); - if (result != null) { - if (INVALID_LINK.equals(result)) { - return null; - } - return result; - } - // hardcoded specific treatment for mesh - if (MiriamType.MESH_2012.equals(miriamData.getDataType())) { - result = "http://bioportal.bioontology.org/ontologies/1351?p=terms&conceptid=" + miriamData.getResource(); - } else { - String uri = miriamData.getDataType().getUris().get(0) + ":" + miriamData.getResource(); - String[] urls = getLink().getLocations(uri); - if (urls == null) { - result = null; - } else if (urls.length > 0) { - result = urls[0]; - } - } - if (result != null) { - setCacheValue(query, result); - return result; - } else { - logger.warn("Cannot find url for miriam: " + miriamData); - setCacheValue(query, INVALID_LINK); - return null; - } - } + /** + * Returns url to the web page represented by {@link MiriamData} parameter. + * + * @param miriamData + * miriam data + * @return url to resource pointed by miriam data + */ + public String getUrlString(MiriamData miriamData) { + if (miriamData.getDataType() == null) { + throw new InvalidArgumentException("Data type cannot be empty."); + } else if (miriamData.getDataType().getUris().size() == 0) { + throw new InvalidArgumentException("Url for " + miriamData.getDataType() + " cannot be retreived."); + } + String query = LINK_DB_PREFIX + miriamData.getDataType().getUris().get(0) + "\n" + miriamData.getResource(); + String result = getCacheValue(query); + if (result != null) { + if (INVALID_LINK.equals(result)) { + return null; + } + return result; + } + // hard-coded specific treatment for mesh + if (MiriamType.MESH_2012.equals(miriamData.getDataType())) { + result = "http://bioportal.bioontology.org/ontologies/1351?p=terms&conceptid=" + miriamData.getResource(); + } else { + String uri = miriamData.getDataType().getUris().get(0) + ":" + miriamData.getResource(); + String[] urls = getLink().getLocations(uri); + if (urls == null) { + result = null; + } else if (urls.length > 0) { + result = urls[0]; + } + } + if (result != null) { + setCacheValue(query, result); + return result; + } else { + logger.warn("Cannot find url for miriam: " + miriamData); + setCacheValue(query, INVALID_LINK); + return null; + } + } - @Override - public ExternalServiceStatus getServiceStatus() { - ExternalServiceStatus status = new ExternalServiceStatus("MIRIAM Registry", "http://www.ebi.ac.uk/miriam/main/"); - GeneralCacheInterface cacheCopy = getCache(); - this.setCache(null); + @Override + public ExternalServiceStatus getServiceStatus() { + ExternalServiceStatus status = new ExternalServiceStatus("MIRIAM Registry", "https://www.ebi.ac.uk/miriam/main/"); + GeneralCacheInterface cacheCopy = getCache(); + this.setCache(null); - try { - String url = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.PUBMED, "3453")); - status.setStatus(ExternalServiceStatusType.OK); - if (url == null) { - status.setStatus(ExternalServiceStatusType.DOWN); - } - } catch (Exception e) { - logger.error(status.getName() + " is down", e); - status.setStatus(ExternalServiceStatusType.DOWN); - } - this.setCache(cacheCopy); - return status; - } + try { + String url = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.PUBMED, "3453")); + status.setStatus(ExternalServiceStatusType.OK); + if (url == null) { + status.setStatus(ExternalServiceStatusType.DOWN); + } + } catch (Exception e) { + logger.error(status.getName() + " is down", e); + status.setStatus(ExternalServiceStatusType.DOWN); + } + this.setCache(cacheCopy); + return status; + } - /** - * Check if identifier can be transformed into {@link MiriamData}. - * - * @param string - * identifier in the format NAME:IDENTIFIER. Where NAME is the name - * from {@link MiriamType#commonName} and IDENTIFIER is resource - * identifier. - * @return <code>true</code> if identifier can be transformed into - * {@link MiriamData} - */ - public boolean isValidIdentifier(String string) { - try { - MiriamType.getMiriamDataFromIdentifier(string); - return true; - } catch (InvalidArgumentException e) { - return false; - } - } + /** + * Check if identifier can be transformed into {@link MiriamData}. + * + * @param string + * identifier in the format NAME:IDENTIFIER. Where NAME is the name + * from {@link MiriamType#commonName} and IDENTIFIER is resource + * identifier. + * @return <code>true</code> if identifier can be transformed into + * {@link MiriamData} + */ + public boolean isValidIdentifier(String string) { + try { + MiriamType.getMiriamDataFromIdentifier(string); + return true; + } catch (InvalidArgumentException e) { + return false; + } + } - @Override - public String refreshCacheQuery(Object query) throws SourceNotAvailable { - String result = null; - if (query instanceof String) { - String name = (String) query; - if (name.startsWith(LINK_DB_PREFIX)) { - String tmp = name.substring(LINK_DB_PREFIX.length()); - String[] rows = tmp.split("\n"); - if (rows.length != 2) { - throw new InvalidArgumentException("Miriam link query is invalid: " + query); - } - MiriamType dataType = MiriamType.getTypeByUri(rows[0]); - String resource = rows[1]; - result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource)); - } else if (name.startsWith(VALID_URI_PREFIX)) { - String tmp = name.substring(VALID_URI_PREFIX.length()); - result = "" + isValid(tmp); - } else if (name.startsWith("http")) { - try { - result = getWebPageContent(name); - } catch (IOException e) { - throw new SourceNotAvailable(e); - } - } else { - throw new InvalidArgumentException("Don't know what to do with string \"" + query + "\""); - } - } else { - throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); - } + @Override + public String refreshCacheQuery(Object query) throws SourceNotAvailable { + String result = null; + if (query instanceof String) { + String name = (String) query; + if (name.startsWith(LINK_DB_PREFIX)) { + String tmp = name.substring(LINK_DB_PREFIX.length()); + String[] rows = tmp.split("\n"); + if (rows.length != 2) { + throw new InvalidArgumentException("Miriam link query is invalid: " + query); + } + MiriamType dataType = MiriamType.getTypeByUri(rows[0]); + String resource = rows[1]; + result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource)); + } else if (name.startsWith(VALID_URI_PREFIX)) { + String tmp = name.substring(VALID_URI_PREFIX.length()); + result = "" + isValid(tmp); + } else if (name.startsWith("http")) { + try { + result = getWebPageContent(name); + } catch (IOException e) { + throw new SourceNotAvailable(e); + } + } else { + throw new InvalidArgumentException("Don't know what to do with string \"" + query + "\""); + } + } else { + throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass()); + } - return result; - } + return result; + } - /** - * Checks if uri (like the one defined in {@link MiriamType#uris}) is valid. - * - * @param uri - * uri to check - * @return <code>true</code> if uri in parameter is valid, <code>false</code> - * otherwise - */ - public boolean isValid(String uri) { - String query = VALID_URI_PREFIX + uri; - String val = getCacheValue(query); - if (val != null) { - return "true".equalsIgnoreCase(val); - } + /** + * Checks if uri (like the one defined in {@link MiriamType#uris}) is valid. + * + * @param uri + * uri to check + * @return <code>true</code> if uri in parameter is valid, <code>false</code> + * otherwise + */ + public boolean isValid(String uri) { + String query = VALID_URI_PREFIX + uri; + String val = getCacheValue(query); + if (val != null) { + return "true".equalsIgnoreCase(val); + } - boolean result = false; - String name = getLink().getName(uri); - if (name == null) { - result = false; - } else { - result = !name.isEmpty(); - } - setCacheValue(query, "" + result); - return result; - } + boolean result = false; + String name = getLink().getName(uri); + if (name == null) { + result = false; + } else { + result = !name.isEmpty(); + } + setCacheValue(query, "" + result); + return result; + } - /** - * This is alternative versopm od {@link #getUrlString(MiriamData)} method - * that is around 30% fatsre. But it refers to beta version of - * <a href="http://www.ebi.ac.uk/miriamws/main/rest/">miriam Rest API</a>. - * - * @param md - * miriam data for which access url will be returned - * @return url to resource pointed by miriam data - * @throws AnnotationException - * thrown when there is a problem with accessing miriam REST API - */ - protected String getUrlString2(MiriamData md) throws AnnotationException { - try { - String result = null; - String uri = md.getDataType().getUris().get(0) + ":" + md.getResource(); - String query = "http://www.ebi.ac.uk/miriamws/main/rest/resolve/" + uri; - String page; - page = getWebPageContent(query); - Document document = getXmlDocumentFromString(page); - Node uris = document.getFirstChild(); - NodeList nodes = uris.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (Node.ELEMENT_NODE == node.getNodeType()) { - if (getNodeAttr("deprecated", node).equals("")) { - result = node.getTextContent(); - } - } - } - return result; - } catch (IOException e) { - throw new AnnotationException("Problem with accessing miriam REST API", e); - } catch (InvalidXmlSchemaException e) { - throw new AnnotationException("Problem with parsing miriam REST API response", e); - } - } + /** + * This is alternative version of {@link #getUrlString(MiriamData)} method that + * is around 30% faster. But it refers to beta version of + * <a href="https://www.ebi.ac.uk/miriamws/main/rest/">miriam Rest API</a>. + * + * @param md + * miriam data for which access url will be returned + * @return url to resource pointed by miriam data + * @throws AnnotationException + * thrown when there is a problem with accessing miriam REST API + */ + protected String getUrlString2(MiriamData md) throws AnnotationException { + try { + String result = null; + String uri = md.getDataType().getUris().get(0) + ":" + md.getResource(); + String query = "https://www.ebi.ac.uk/miriamws/main/rest/resolve/" + uri; + String page; + page = getWebPageContent(query); + Document document = getXmlDocumentFromString(page); + Node uris = document.getFirstChild(); + NodeList nodes = uris.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (Node.ELEMENT_NODE == node.getNodeType()) { + if (getNodeAttr("deprecated", node).equals("")) { + result = node.getTextContent(); + } + } + } + return result; + } catch (IOException e) { + throw new AnnotationException("Problem with accessing miriam REST API", e); + } catch (InvalidXmlSchemaException e) { + throw new AnnotationException("Problem with parsing miriam REST API response", e); + } + } - /** - * Returns uri to miriam resource. - * - * @param md - * {@link MiriamData} object for which uri should be returned - * @return uri to miriam resource - */ - public String miriamDataToUri(MiriamData md) { - return md.getDataType().getUris().get(0) + ":" + md.getResource(); - } + /** + * Returns uri to miriam resource. + * + * @param md + * {@link MiriamData} object for which uri should be returned + * @return uri to miriam resource + */ + public String miriamDataToUri(MiriamData md) { + return md.getDataType().getUris().get(0) + ":" + md.getResource(); + } - @Override - protected WebPageDownloader getWebPageDownloader() { - return super.getWebPageDownloader(); - } + @Override + protected WebPageDownloader getWebPageDownloader() { + return super.getWebPageDownloader(); + } - @Override - protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { - super.setWebPageDownloader(webPageDownloader); - } + @Override + protected void setWebPageDownloader(WebPageDownloader webPageDownloader) { + super.setWebPageDownloader(webPageDownloader); + } - /** - * @return the link - * @see #link - */ - MiriamLink getLink() { - return link; - } + /** + * @return the link + * @see #link + */ + MiriamLink getLink() { + return link; + } - /** - * @param link - * the link to set - * @see #link - */ - void setLink(MiriamLink link) { - this.link = link; - } + /** + * @param link + * the link to set + * @see #link + */ + void setLink(MiriamLink link) { + this.link = link; + } } diff --git a/annotation/src/test/java/lcsb/mapviewer/annotation/services/MiriamConnectorTest.java b/annotation/src/test/java/lcsb/mapviewer/annotation/services/MiriamConnectorTest.java index 8814930e8ddd1ff50b723d84098f482f71d18d8c..e9dff58fee609e7fcc5e18ec14b658aaf3f9783e 100644 --- a/annotation/src/test/java/lcsb/mapviewer/annotation/services/MiriamConnectorTest.java +++ b/annotation/src/test/java/lcsb/mapviewer/annotation/services/MiriamConnectorTest.java @@ -33,350 +33,349 @@ import lcsb.mapviewer.model.map.MiriamType; import uk.ac.ebi.miriam.lib.MiriamLink; public class MiriamConnectorTest extends AnnotationTestFunctions { - Logger logger = Logger.getLogger(MiriamConnectorTest.class); - - @Autowired - MiriamConnector miriamConnector; - - GeneralCacheInterface cache; - - @Before - public void setUp() throws Exception { - cache = miriamConnector.getCache(); - } - - @After - public void tearDown() throws Exception { - miriamConnector.setCache(cache); - } - - @Test - public void testGoUri() throws Exception { - try { - assertFalse(MiriamType.GO.getUris().size() == 1); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test(timeout = 15000) - public void testMeshUrl() throws Exception { - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.MESH_2012, "D004249"); - - String pageContent = getWebpage(miriamConnector.getUrlString(md)); - assertTrue(pageContent.contains("DNA Damage")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrl() throws Exception { - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; - - String url = miriamConnector.getUrlString(md); - assertNotNull(url); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrlForInvalidMiriam() throws Exception { - try { - miriamConnector.getUrlString(new MiriamData()); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Data type cannot be empty")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrlForInvalidMiriam2() throws Exception { - try { - miriamConnector.getUrlString(new MiriamData(MiriamType.UNKNOWN,"")); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("cannot be retreived")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrlForInvalidMiriam3() throws Exception { - try { - String url = miriamConnector.getUrlString(new MiriamData(MiriamType.ENTREZ,"abc")); - assertNull(url); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testIsValidIdentifier() throws Exception { - try { - assertTrue(miriamConnector.isValidIdentifier(MiriamType.ENTREZ.getCommonName()+":"+"1234")); - assertFalse(miriamConnector.isValidIdentifier("blablabla")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testResolveUrl() throws Exception { - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - for (MiriamType mt : MiriamType.values()) { - if (!MiriamType.UNKNOWN.equals(mt)) { - assertTrue("Invalid URI for MiriamType: " + mt, miriamConnector.isValid(mt.getUris().get(0))); - } - } - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testRefresh() throws Exception { - String query = MiriamConnector.VALID_URI_PREFIX + MiriamType.HGNC.getUris().get(0); - try { - String res = miriamConnector.refreshCacheQuery(query); - assertNotNull(res); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testRefresh2() throws Exception { - MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; - String query = MiriamConnector.LINK_DB_PREFIX + md.getDataType().getUris().get(0) + "\n" + md.getResource(); - try { - String res = miriamConnector.refreshCacheQuery(query); - assertNotNull(res); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testRefresh3() throws Exception { - try { - String res = miriamConnector.refreshCacheQuery("https://www.google.pl/"); - assertNotNull(res); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrl2() throws Exception { - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; - - String url = miriamConnector.getUrlString2(md); - assertNotNull(url); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testMiriamDataToUri() throws Exception { - try { - assertNotNull(miriamConnector.miriamDataToUri(TaxonomyBackend.HUMAN_TAXONOMY)); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetUrl2WithInvalidApiResponse() throws Exception { - WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); - GeneralCacheInterface cache = miriamConnector.getCache(); - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException()); - miriamConnector.setWebPageDownloader(mockDownloader); - - miriamConnector.getUrlString2(TaxonomyBackend.HUMAN_TAXONOMY); - - } catch (AnnotationException e) { - assertTrue(e.getMessage().contains("Problem with accessing miriam REST API")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - miriamConnector.setWebPageDownloader(downloader); - miriamConnector.setCache(cache); - } - } - - @Test - public void testGetUrl2WithInvalidApiResponse2() throws Exception { - WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); - GeneralCacheInterface cache = miriamConnector.getCache(); - try { - // exclude first cached value - miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString())).thenReturn(""); - miriamConnector.setWebPageDownloader(mockDownloader); - - miriamConnector.getUrlString2(TaxonomyBackend.HUMAN_TAXONOMY); - - } catch (AnnotationException e) { - assertTrue(e.getMessage().contains("Problem with parsing miriam REST API response")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - miriamConnector.setWebPageDownloader(downloader); - miriamConnector.setCache(cache); - } - } - - - @Test - public void testRefreshCacheQueryNotAvailable() throws Exception { - WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); - try { - WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); - when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException()); - miriamConnector.setWebPageDownloader(mockDownloader); - miriamConnector.refreshCacheQuery("http://google.pl/"); - fail("Exception expected"); - } catch (SourceNotAvailable e) { - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - miriamConnector.setWebPageDownloader(downloader); - } - } - - @Test - public void testRefreshInvalidCacheQuery() throws Exception { - try { - miriamConnector.refreshCacheQuery("invalid_query"); - fail("Exception expected"); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Don't know what to do")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testRefreshInvalidCacheQuery2() throws Exception { - try { - miriamConnector.refreshCacheQuery(new Object()); - fail("Exception expected"); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Don't know what to do")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testRefreshInvalidCacheQuery3() throws Exception { - try { - miriamConnector.refreshCacheQuery(MiriamConnector.LINK_DB_PREFIX); - fail("Exception expected"); - } catch (InvalidArgumentException e) { - assertTrue(e.getMessage().contains("Miriam link query is invalid")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testCheckValidyOfEmptyUri() throws Exception { - try { - //user connector without cache - boolean value = new MiriamConnector().isValid(""); - assertFalse(value); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testStatus() throws Exception { - try { - assertEquals(ExternalServiceStatusType.OK, miriamConnector.getServiceStatus().getStatus()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testSimulateDownStatus() throws Exception { - MiriamLink link = miriamConnector.getLink(); - try { - MiriamLink mockDownloader = Mockito.mock(MiriamLink.class); - when(mockDownloader.getLocations(any())).thenThrow(new InvalidStateException()); - miriamConnector.setLink(mockDownloader); - assertEquals(ExternalServiceStatusType.DOWN, miriamConnector.getServiceStatus().getStatus()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - miriamConnector.setLink(link); - } - } - - @Test - public void testSimulateDownStatus2() throws Exception { - MiriamLink link = miriamConnector.getLink(); - try { - MiriamLink mockDownloader = Mockito.mock(MiriamLink.class); - when(mockDownloader.getLocations(any())).thenReturn(null); - miriamConnector.setLink(mockDownloader); - assertEquals(ExternalServiceStatusType.DOWN, miriamConnector.getServiceStatus().getStatus()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - miriamConnector.setLink(link); - } - } + Logger logger = Logger.getLogger(MiriamConnectorTest.class); + + @Autowired + MiriamConnector miriamConnector; + + GeneralCacheInterface cache; + + @Before + public void setUp() throws Exception { + cache = miriamConnector.getCache(); + } + + @After + public void tearDown() throws Exception { + miriamConnector.setCache(cache); + } + + @Test + public void testGoUri() throws Exception { + try { + assertFalse(MiriamType.GO.getUris().size() == 1); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test(timeout = 15000) + public void testMeshUrl() throws Exception { + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.MESH_2012, "D004249"); + + String pageContent = getWebpage(miriamConnector.getUrlString(md)); + assertTrue(pageContent.contains("DNA Damage")); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrl() throws Exception { + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; + + String url = miriamConnector.getUrlString(md); + assertNotNull(url); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrlForInvalidMiriam() throws Exception { + try { + miriamConnector.getUrlString(new MiriamData()); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Data type cannot be empty")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrlForInvalidMiriam2() throws Exception { + try { + miriamConnector.getUrlString(new MiriamData(MiriamType.UNKNOWN, "")); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("cannot be retreived")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrlForInvalidMiriam3() throws Exception { + try { + String url = miriamConnector.getUrlString(new MiriamData(MiriamType.ENTREZ, "abc")); + assertNull(url); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testIsValidIdentifier() throws Exception { + try { + assertTrue(miriamConnector.isValidIdentifier(MiriamType.ENTREZ.getCommonName() + ":" + "1234")); + assertFalse(miriamConnector.isValidIdentifier("blablabla")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testResolveUrl() throws Exception { + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + for (MiriamType mt : MiriamType.values()) { + if (!MiriamType.UNKNOWN.equals(mt)) { + assertTrue("Invalid URI for MiriamType: " + mt, miriamConnector.isValid(mt.getUris().get(0))); + } + } + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testRefresh() throws Exception { + String query = MiriamConnector.VALID_URI_PREFIX + MiriamType.HGNC.getUris().get(0); + try { + String res = miriamConnector.refreshCacheQuery(query); + assertNotNull(res); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testRefresh2() throws Exception { + MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; + String query = MiriamConnector.LINK_DB_PREFIX + md.getDataType().getUris().get(0) + "\n" + md.getResource(); + try { + String res = miriamConnector.refreshCacheQuery(query); + assertNotNull(res); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testRefresh3() throws Exception { + try { + String res = miriamConnector.refreshCacheQuery("https://www.google.pl/"); + assertNotNull(res); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrl2() throws Exception { + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + MiriamData md = TaxonomyBackend.HUMAN_TAXONOMY; + + String url = miriamConnector.getUrlString2(md); + assertNotNull(url); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testMiriamDataToUri() throws Exception { + try { + assertNotNull(miriamConnector.miriamDataToUri(TaxonomyBackend.HUMAN_TAXONOMY)); + + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetUrl2WithInvalidApiResponse() throws Exception { + WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); + GeneralCacheInterface cache = miriamConnector.getCache(); + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException()); + miriamConnector.setWebPageDownloader(mockDownloader); + + miriamConnector.getUrlString2(TaxonomyBackend.HUMAN_TAXONOMY); + + } catch (AnnotationException e) { + assertTrue(e.getMessage().contains("Problem with accessing miriam REST API")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + miriamConnector.setWebPageDownloader(downloader); + miriamConnector.setCache(cache); + } + } + + @Test + public void testGetUrl2WithInvalidApiResponse2() throws Exception { + WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); + GeneralCacheInterface cache = miriamConnector.getCache(); + try { + // exclude first cached value + miriamConnector.setCache(new GeneralCacheWithExclusion(cache, 1)); + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString())).thenReturn(""); + miriamConnector.setWebPageDownloader(mockDownloader); + + miriamConnector.getUrlString2(TaxonomyBackend.HUMAN_TAXONOMY); + + } catch (AnnotationException e) { + assertTrue(e.getMessage().contains("Problem with parsing miriam REST API response")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + miriamConnector.setWebPageDownloader(downloader); + miriamConnector.setCache(cache); + } + } + + @Test + public void testRefreshCacheQueryNotAvailable() throws Exception { + WebPageDownloader downloader = miriamConnector.getWebPageDownloader(); + try { + WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class); + when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException()); + miriamConnector.setWebPageDownloader(mockDownloader); + miriamConnector.refreshCacheQuery("http://google.pl/"); + fail("Exception expected"); + } catch (SourceNotAvailable e) { + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + miriamConnector.setWebPageDownloader(downloader); + } + } + + @Test + public void testRefreshInvalidCacheQuery() throws Exception { + try { + miriamConnector.refreshCacheQuery("invalid_query"); + fail("Exception expected"); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Don't know what to do")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testRefreshInvalidCacheQuery2() throws Exception { + try { + miriamConnector.refreshCacheQuery(new Object()); + fail("Exception expected"); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Don't know what to do")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testRefreshInvalidCacheQuery3() throws Exception { + try { + miriamConnector.refreshCacheQuery(MiriamConnector.LINK_DB_PREFIX); + fail("Exception expected"); + } catch (InvalidArgumentException e) { + assertTrue(e.getMessage().contains("Miriam link query is invalid")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testCheckValidyOfEmptyUri() throws Exception { + try { + // user connector without cache + boolean value = new MiriamConnector().isValid(""); + assertFalse(value); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testStatus() throws Exception { + try { + assertEquals(ExternalServiceStatusType.OK, miriamConnector.getServiceStatus().getStatus()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testSimulateDownStatus() throws Exception { + MiriamLink link = miriamConnector.getLink(); + try { + MiriamLink mockDownloader = Mockito.mock(MiriamLink.class); + when(mockDownloader.getLocations(any())).thenThrow(new InvalidStateException()); + miriamConnector.setLink(mockDownloader); + assertEquals(ExternalServiceStatusType.DOWN, miriamConnector.getServiceStatus().getStatus()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + miriamConnector.setLink(link); + } + } + + @Test + public void testSimulateDownStatus2() throws Exception { + MiriamLink link = miriamConnector.getLink(); + try { + MiriamLink mockDownloader = Mockito.mock(MiriamLink.class); + when(mockDownloader.getLocations(any())).thenReturn(null); + miriamConnector.setLink(mockDownloader); + assertEquals(ExternalServiceStatusType.DOWN, miriamConnector.getServiceStatus().getStatus()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + miriamConnector.setLink(link); + } + } } diff --git a/frontend-js/.settings/org.eclipse.wst.validation.prefs b/frontend-js/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000000000000000000000000000000000000..04cad8cb752a9761c4e5167d0301d3a27674430f --- /dev/null +++ b/frontend-js/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,2 @@ +disabled=06target +eclipse.preferences.version=1