JAL-2316 Restructure identifiers.org download
[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.io.File;
29 import java.io.FileWriter;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.Vector;
33
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class IdentifiersUrlProviderTest
38 {
39   // Test identifiers.org download file
40   private static final String testIdOrgFile = "[{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
41           + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
42           + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
43           + "\"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+)?$\","
44           + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
45           + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
46           + "\"url\":\"http://identifiers.org/interpro\"},"
47           + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
48           + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]";
49   
50   private static final String[] dlinks = {
51       "UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$",
52       "InterPro|http://identifiers.org/interpro/$DB_ACCESSION$",
53       "ENA|http://identifiers.org/ena.embl/$DB_ACCESSION$" };
54
55   private static final String[] dlinks1 = {
56       "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$",
57       "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$" };
58
59   private static final String[] dlinks2 = {
60       "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$",
61       "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$" };
62
63   private static final String stringLinks = "MIR:00000005|http://identifiers.org/uniprot/$DB_ACCESSION$"
64           + "MIR:00000011|http://identifiers.org/interpro/$DB_ACCESSION$"
65           + "MIR:00000372|http://identifiers.org/ena.embl/$DB_ACCESSION$";
66
67   private static final String[] unselDlinks = { "ChEBI|http://identifiers.org/chebi/$DB_ACCESSION$" };
68
69   private static final Vector<String> displayLinks = new Vector<String>(
70         Arrays.asList(dlinks));
71   
72   private static final Vector<String> unselDisplayLinks = new Vector<String>(
73           Arrays.asList(unselDlinks));
74
75   private static final Vector<String> displayLinks1 = new Vector<String>(
76           Arrays.asList(dlinks1));
77
78   private static final Vector<String> displayLinks2 = new Vector<String>(
79           Arrays.asList(dlinks2));
80
81   private static final HashMap<String, String> urlMap = new HashMap<String, String>()
82   {
83     {
84       put("MIR:00000005", "http://identifiers.org/uniprot/$DB_ACCESSION$");
85       put("MIR:00000011", "http://identifiers.org/interpro/$DB_ACCESSION$");
86       put("MIR:00000372", "http://identifiers.org/ena.embl/$DB_ACCESSION$");
87     }
88   };
89
90   private String testfile = "";
91
92
93   @BeforeClass(alwaysRun = true)
94   public void setup()
95   {
96     // setup test ids in a file
97     File outFile = null;
98     try
99     {
100       outFile = File.createTempFile("testidsfile", "txt");
101       outFile.deleteOnExit();
102
103       FileWriter fw = new FileWriter(outFile);
104       fw.write(testIdOrgFile);
105       fw.close();
106
107       testfile = outFile.getAbsolutePath();
108
109     } catch (Exception ex)
110     {
111       System.err.println(ex);
112     }
113
114     IdOrgSettings.setDownloadLocation(testfile);
115   }
116
117   /*
118    * Test urls are set and returned correctly
119    */
120   @Test(groups = { "Functional" })
121   public void testUrlLinks()
122   {
123     // creation from cached id list
124     String idList = "MIR:00000005|MIR:00000011|MIR:00000372";
125     UrlProviderI idProv = new IdentifiersUrlProvider(idList);
126     
127     assertTrue(displayLinks.containsAll(idProv.getLinksForMenu()));
128
129     // because UrlProvider does not guarantee order of links, we can't just
130     // compare the output of writeUrlsAsString to a string, hence the hoops here
131     String result = idProv.writeUrlsAsString(true);
132     UrlProviderI up = new IdentifiersUrlProvider(result);
133     assertTrue(displayLinks.containsAll(up.getLinksForMenu()));
134
135     result = idProv.writeUrlsAsString(false);
136     up = new IdentifiersUrlProvider(result);
137     assertTrue(unselDisplayLinks.containsAll(up.getLinksForMenu()));
138
139   }
140
141   /*
142    * Test default is set and returned correctly
143    */
144   @Test(groups = { "Functional" })
145   public void testDefaultUrl()
146   {
147     // creation from cached id list
148     String idList = "MIR:00000005|MIR:00000011|MIR:00000372";
149     UrlProviderI idProv = new IdentifiersUrlProvider(idList);
150     
151     // initially no default
152     assertEquals(null, idProv.getDefaultUrl("seqid"));
153     
154     // set and then retrieve default
155     assertTrue(idProv.setDefaultUrl("MIR:00000005"));
156     assertEquals("http://identifiers.org/uniprot/seqid",
157             idProv.getDefaultUrl("seqid"));
158
159     // ids less than length 4 return null
160     assertEquals(null,
161             idProv.getDefaultUrl("123"));
162
163     // attempt to set bad default
164     assertFalse(idProv.setDefaultUrl("MIR:00001234"));
165     // default set to null (as default should have been set elsewhere)
166     assertEquals(null, idProv.getDefaultUrl("seqid"));
167
168     // chooseDefaultUrl not implemented for IdentifiersUrlProvider
169     assertEquals(null, idProv.chooseDefaultUrl());
170   }
171 }