Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / test / jalview / urls / UrlProviderTest.java
1 package jalview.urls;
2
3 import jalview.urls.api.UrlProviderI;
4 import jalview.urls.desktop.DesktopUrlProviderFactory;
5 import jalview.util.UrlConstants;
6
7 import java.io.BufferedWriter;
8 import java.io.File;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.util.List;
12
13 import org.testng.Assert;
14 import org.testng.annotations.BeforeMethod;
15 import org.testng.annotations.Test;
16
17
18 public class UrlProviderTest {
19   
20   // Test identifiers.org download file
21   private static final String testIdOrgString = "{\"Local\": [{\"id\":\"MIR:00000002\",\"name\":\"ChEBI\",\"pattern\":\"^CHEBI:\\d+$\","
22          + "\"definition\":\"Chemical Entities of Biological Interest (ChEBI)\",\"prefix\":\"chebi\","
23          + "\"url\":\"http://identifiers.org/chebi\"},{\"id\":\"MIR:00000005\",\"name\":\"UniProt Knowledgebase\","
24          + "\"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+)?$\","
25          + "\"definition\":\"The UniProt Knowledgebase (UniProtKB)\",\"prefix\":\"uniprot\",\"url\":\"http://identifiers.org/uniprot\"},"
26          + "{\"id\":\"MIR:00000011\",\"name\":\"InterPro\",\"pattern\":\"^IPR\\d{6}$\",\"definition\":\"InterPro\",\"prefix\":\"interpro\","
27          + "\"url\":\"http://identifiers.org/interpro\"},"
28          + "{\"id\":\"MIR:00000372\",\"name\":\"ENA\",\"pattern\":\"^[A-Z]+[0-9]+(\\.\\d+)?$\",\"definition\":\"The European Nucleotide Archive (ENA),\""
29           + "\"prefix\":\"ena.embl\",\"url\":\"http://identifiers.org/ena.embl\"}]}";
30
31   private UrlProviderI prov;
32
33   @BeforeMethod(alwaysRun = true)
34   public void setup()
35   {
36    // make a dummy identifiers.org download file
37    File temp = null;
38
39    try
40    {
41      temp = File.createTempFile("tempfile", ".tmp");
42      temp.deleteOnExit();
43      BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
44      bw.write(testIdOrgString);
45      bw.close();
46    } catch (IOException e)
47    {
48       System.out.println("Error initialising UrlProviderTest test: "
49               + e.getMessage());
50    }
51
52    IdOrgSettings.setDownloadLocation(temp.getPath());
53
54     String defaultUrlString = "No default";
55     String cachedUrlList = "MIR:00000005|MIR:00000011|Test1|http://blah.blah/$SEQUENCE_ID$|"
56             + "Test2|http://test2/$DB_ACCESSION$|Test3|http://test3/$SEQUENCE_ID$";
57     String userUrlList = "MIR:00000372|Test4|httpL//another.url/$SEQUENCE_ID$";
58
59     DesktopUrlProviderFactory factory = new DesktopUrlProviderFactory(
60             defaultUrlString, cachedUrlList, userUrlList);
61     prov = factory.createUrlProvider();
62  }
63
64   @Test(groups = { "Functional" })
65   public void testInitUrlProvider()
66   {
67     String emblUrl = UrlConstants.DEFAULT_STRING.substring(
68             UrlConstants.DEFAULT_STRING.indexOf(UrlConstants.SEP) + 1,
69             UrlConstants.DEFAULT_STRING.length());
70
71     // chooses EMBL url when default Url id does not exist in provided url lists
72     Assert.assertEquals(prov.getPrimaryUrlId(), UrlConstants.DEFAULT_LABEL);
73     Assert.assertEquals(prov.getPrimaryUrl("FER_CAPAN"),
74             emblUrl.replace("$SEQUENCE_ID$", "FER_CAPAN"));
75
76     List<String> menulinks = prov.getLinksForMenu();
77     List<UrlLinkDisplay> allLinks = prov.getLinksForTable();
78
79     // 9 links in provider - 4 from id file, 4 custom links, 1 additional
80     // default
81     Assert.assertEquals(allLinks.size(), 9);
82
83     // 6 links in menu (cachedUrlList) + new default
84     Assert.assertEquals(menulinks.size(), 6);
85
86     Assert.assertTrue(menulinks
87             .contains("Test1|http://blah.blah/$SEQUENCE_ID$"));
88     Assert.assertTrue(menulinks
89             .contains("Test2|http://test2/$DB_ACCESSION$"));
90     Assert.assertTrue(menulinks
91             .contains("Test3|http://test3/$SEQUENCE_ID$"));
92     Assert.assertTrue(menulinks
93             .contains("UniProt Knowledgebase|http://identifiers.org/uniprot/$DB_ACCESSION$|uniprot"));
94     Assert.assertTrue(menulinks
95             .contains("InterPro|http://identifiers.org/interpro/$DB_ACCESSION$|interpro"));
96     Assert.assertTrue(menulinks.contains(UrlConstants.DEFAULT_LABEL
97             + UrlConstants.SEP + emblUrl));
98   }
99
100   @Test(groups = { "Functional" })
101   public void testSetDefaultUrl()
102   {
103     // set custom url as default
104     Assert.assertTrue(prov.setPrimaryUrl("Test1"));
105     Assert.assertEquals(prov.getPrimaryUrlId(), "Test1");
106
107     // set identifiers url as default
108     Assert.assertTrue(prov.setPrimaryUrl("MIR:00000011"));
109     Assert.assertEquals(prov.getPrimaryUrlId(), "MIR:00000011");
110   }
111
112   @Test(
113     groups = { "Functional" },
114     expectedExceptions = { IllegalArgumentException.class })
115   public void testSetDefaultUrlWrongly()
116   {
117     // don't allow default to be a non-key
118     prov.setPrimaryUrl("not-a-key");
119   }
120 }