JAL-2316 Unit test for IdentifiersUrlProvider
[jalview.git] / test / jalview / urls / IdentifiersUrlProviderTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.urls;
23
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertFalse;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Vector;
31
32 import org.testng.annotations.Test;
33
34 public class IdentifiersUrlProviderTest
35 {
36   // Test identifiers.org download file
37   private static final String testIdOrgFile = "[{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
38           + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
39           + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
40           + "\"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+)?$\","
41           + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
42           + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
43           + "\"url\":\"http://identifiers.org/interpro\"},"
44           + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
45           + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]";
46   
47   private static final String[] dlinks = {
48     "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$",
49     "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$",
50     "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$"};
51
52   private static final String stringLinks = "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$"
53           + "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$"
54           + "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$";
55
56
57   private static final Vector<String> displayLinks = new Vector<String>(
58         Arrays.asList(dlinks));
59   
60   private static final HashMap<String, String> urlMap = new HashMap<String, String>()
61   {
62     {
63       put("MIR:00000005", "http://identifiers.org/uniprot/$DB_ACCESSION$");
64       put("MIR:00000011", "http://identifiers.org/interpro/$DB_ACCESSION$");
65       put("MIR:00000372", "http://identifiers.org/ena.embl/$DB_ACCESSION$");
66     }
67   };
68
69   /*
70    * Test urls are set and returned correctly
71    */
72   @Test(groups = { "Functional" })
73   public void testUrlLinks()
74   {
75     // creation from cached id list
76     String idList = "MIR:00000005|MIR:00000011|MIR:00000372";
77     UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile);
78     
79     assertTrue(displayLinks.containsAll(idProv.getLinksForDisplay()));
80
81     // because UrlProvider does not guarantee order of links, we can't just
82     // compare the output of writeUrlsAsString to a string, hence the hoops here
83     String result = idProv.writeUrlsAsString();
84     UrlProviderI up = new IdentifiersUrlProvider(result, testIdOrgFile);
85     assertTrue(displayLinks.containsAll(up.getLinksForDisplay()));
86
87   }
88
89   /*
90    * Test default is set and returned correctly
91    */
92   @Test(groups = { "Functional" })
93   public void testDefaultUrl()
94   {
95     // creation from cached id list
96     String idList = "MIR:00000005|MIR:00000011|MIR:00000372";
97     UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile);
98     
99     // initially no default
100     assertEquals(null, idProv.getDefaultUrl());
101     
102     // set and then retrieve default
103     assertTrue(idProv.setDefaultUrl("MIR:00000005"));
104     assertEquals("MIR:00000005", idProv.getDefaultUrl());
105     assertEquals("http://identifiers.org/uniprot/id",
106             idProv.getDefaultUrl("id"));
107
108     // attempt to set bad default
109     assertFalse(idProv.setDefaultUrl("MIR:00001234"));
110     // default set to null (as default should have been set elsewhere)
111     assertEquals(null, idProv.getDefaultUrl());
112
113     // chooseDefaultUrl not implemented for IdentifiersUrlProvider
114     assertEquals(null, idProv.chooseDefaultUrl());
115   }
116
117   /*
118    * Test url setting works
119    */
120   @Test(groups = { "Functional" })
121   public void testSetUrlLinks()
122   {
123     // creation from cached id list
124     String idList = "MIR:00000005|MIR:00000011|MIR:00000372";
125     UrlProviderI idProv = new IdentifiersUrlProvider(idList, testIdOrgFile);
126
127     // set url links
128     String[] ids = { "MIR:00000372", "MIR:00000011" };
129     Vector<String> names = new Vector<String>(Arrays.asList(ids));
130     Vector<String> urls = null;
131     idProv.setUrlLinks(names, urls);
132     assertEquals(names, idProv.getLinksForDisplay());
133     
134     //set default url then reset url links, default should be unset if not present
135     String[] moreids = { "MIR:00000005", "MIR:00000011" };
136     names = new Vector<String>(Arrays.asList(moreids));
137     idProv.setDefaultUrl("MIR:00000372");
138     idProv.setUrlLinks(names, urls);
139     assertEquals(names, idProv.getLinksForDisplay());
140     assertEquals(null, idProv.getDefaultUrl());
141
142   }
143 }