/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.urls; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertTrue; import java.io.File; import java.io.FileWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Vector; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class IdentifiersUrlProviderTest { // Test identifiers.org download file private static final String testIdOrgFile = "[{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\"," + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\"," + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\"," + "\"pattern\":\"^([A-N,R-Z][0-9]([A-Z][A-Z, 0-9][A-Z, 0-9][0-9]){1,2})|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])(\\.\\d+)?$\"," + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"}," + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\"," + "\"url\":\"http://identifiers.org/interpro\"}," + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\"" + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]"; private static final String[] dlinks = { "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$", "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$", "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$"}; private static final String[] dlinks1 = { "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$", "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$" }; private static final String[] dlinks2 = { "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$", "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$" }; private static final String stringLinks = "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$" + "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$" + "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$"; private static final Vector displayLinks = new Vector( Arrays.asList(dlinks)); private static final Vector displayLinks1 = new Vector( Arrays.asList(dlinks1)); private static final Vector displayLinks2 = new Vector( Arrays.asList(dlinks2)); private static final HashMap urlMap = new HashMap() { { put("MIR:00000005", "http://identifiers.org/uniprot/$DB_ACCESSION$"); put("MIR:00000011", "http://identifiers.org/interpro/$DB_ACCESSION$"); put("MIR:00000372", "http://identifiers.org/ena.embl/$DB_ACCESSION$"); } }; private String testfile = ""; @BeforeClass(alwaysRun = true) public void setup() { // setup test ids in a file File outFile = null; try { outFile = File.createTempFile("testidsfile", "txt"); outFile.deleteOnExit(); FileWriter fw = new FileWriter(outFile); fw.write(testIdOrgFile); fw.close(); testfile = outFile.getAbsolutePath(); } catch (Exception ex) { System.err.println(ex); } } /* * Test urls are set and returned correctly */ @Test(groups = { "Functional" }) public void testUrlLinks() { // creation from cached id list String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; UrlProviderI idProv = new IdentifiersUrlProvider(idList, testfile); assertTrue(displayLinks.containsAll(idProv.getLinksForDisplay())); // because UrlProvider does not guarantee order of links, we can't just // compare the output of writeUrlsAsString to a string, hence the hoops here String result = idProv.writeUrlsAsString(); UrlProviderI up = new IdentifiersUrlProvider(result, testfile); assertTrue(displayLinks.containsAll(up.getLinksForDisplay())); } /* * Test default is set and returned correctly */ @Test(groups = { "Functional" }) public void testDefaultUrl() { // creation from cached id list String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; UrlProviderI idProv = new IdentifiersUrlProvider(idList, testfile); // initially no default assertEquals(null, idProv.getDefaultUrl()); // set and then retrieve default assertTrue(idProv.setDefaultUrl("MIR:00000005")); assertEquals("MIR:00000005", idProv.getDefaultUrl()); assertEquals("http://identifiers.org/uniprot/id", idProv.getDefaultUrl("id")); // attempt to set bad default assertFalse(idProv.setDefaultUrl("MIR:00001234")); // default set to null (as default should have been set elsewhere) assertEquals(null, idProv.getDefaultUrl()); // chooseDefaultUrl not implemented for IdentifiersUrlProvider assertEquals(null, idProv.chooseDefaultUrl()); } /* * Test url setting works */ @Test(groups = { "Functional" }) public void testSetUrlLinks() { // creation from cached id list String idList = "MIR:00000005|MIR:00000011|MIR:00000372"; UrlProviderI idProv = new IdentifiersUrlProvider(idList, testfile); // set url links String[] ids = { "MIR:00000372", "MIR:00000011" }; Vector names = new Vector(Arrays.asList(ids)); Vector urls = null; idProv.setUrlLinks(names, urls); Vector result = idProv.getLinksForDisplay(); assertEquals(displayLinks1.size(), result.size()); assertTrue(displayLinks1.containsAll(result)); //set default url then reset url links, default should be unset if not present String[] moreids = { "MIR:00000005", "MIR:00000011" }; names = new Vector(Arrays.asList(moreids)); idProv.setDefaultUrl("MIR:00000372"); idProv.setUrlLinks(names, urls); result = idProv.getLinksForDisplay(); assertEquals(displayLinks2.size(), result.size()); assertTrue(displayLinks2.containsAll(result)); assertEquals(null, idProv.getDefaultUrl()); } }