JAL-1756 fix failed test as a result of runing the BioJS updater from a new thread
[jalview.git] / test / jalview / io / BioJsHTMLOutputTest.java
1 package jalview.io;
2
3 import jalview.json.binding.v1.BioJSReleasePojo;
4 import jalview.json.binding.v1.BioJSRepositoryPojo;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
9 import java.net.URISyntaxException;
10 import java.net.URL;
11 import java.net.URLConnection;
12 import java.util.TreeMap;
13
14 import org.junit.Assert;
15 import org.junit.Test;
16
17
18 public class BioJsHTMLOutputTest
19 {
20
21   @Test
22   public void getJalviewAlignmentAsJsonString()
23   {
24     BioJsHTMLOutput bioJsHtmlOutput = new BioJsHTMLOutput(null, null);
25     String bjsTemplate = null;
26     try
27     {
28       bioJsHtmlOutput.updateBioJS();
29       try
30       {
31         // allow the update some three seconds to complete before getting latest
32         // version of BioJS template
33         Thread.sleep(1000 * 3);
34       } catch (InterruptedException e)
35       {
36         e.printStackTrace();
37       }
38       bjsTemplate = BioJsHTMLOutput.getBioJsTemplateAsString();
39       // System.out.println(bjsTemplate);
40     } catch (IOException e)
41     {
42       e.printStackTrace();
43     }
44     Assert.assertNotNull(bjsTemplate);
45   }
46
47   @Test(expected = NullPointerException.class)
48   public void expectedNullPointerException()
49   {
50     BioJsHTMLOutput bjs = new BioJsHTMLOutput(null, null);
51     try
52     {
53       bjs.refreshBioJSVersionsInfo(null);
54     } catch (URISyntaxException e)
55     {
56       Assert.fail("Expception occured while testing!");
57       e.printStackTrace();
58     }
59   }
60
61   @Test
62   public void getBioJsMSAVersions()
63   {
64     BioJsHTMLOutput bjs = new BioJsHTMLOutput(null, null);
65     TreeMap<String, File> versions = null;
66     try
67     {
68       bjs.refreshBioJSVersionsInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
69       versions = BioJsHTMLOutput.getBioJsMSAVersions();
70     } catch (URISyntaxException e)
71     {
72       Assert.fail("Expception occured while testing!");
73       e.printStackTrace();
74     }
75     Assert.assertNotNull("No versions found", versions);
76     Assert.assertTrue("One or more Templates required", versions.size() > 0);
77     System.out
78             .println("Number of discovered versions : "
79             + versions.size());
80     for (String v : versions.keySet())
81     {
82       System.out.println("version : " + v);
83       System.out.println("File : " + versions.get(v));
84     }
85
86     System.out.println("\nCurrent latest version : "
87             + BioJsHTMLOutput.getCurrentBJSTemplateFile());
88     Assert.assertNotNull("Latest BioJsMSA version NOT found!",
89             BioJsHTMLOutput.getCurrentBJSTemplateFile());
90
91   }
92
93   @Test
94   public void testBioJsUpdate()
95   {
96     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
97     Assert.assertTrue("URL not reacable : " + url, urlIsReachable(url));
98     String response = BioJsHTMLOutput.getURLContentAsString(url);
99     Assert.assertNotNull("Null response read from url!", response);
100     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
101     System.out.println(">>> description : " + repository.getDescription());
102     System.out
103 .println(">>> latest version : "
104             + repository.getLatestReleaseVersion());
105     System.out.println(">>> repo count : "
106             + repository.getReleases().size());
107     for (BioJSReleasePojo release : repository.getReleases())
108     {
109       System.out.println("repo type : " + release.getType());
110       System.out.println("url : " + release.getUrl());
111       System.out.println("release version : " + release.getVersion());
112     }
113   }
114
115   private static boolean urlIsReachable(String urlString)
116   {
117     try
118     {
119       final URL url = new URL(urlString);
120       final URLConnection conn = url.openConnection();
121       conn.connect();
122       return true;
123     } catch (MalformedURLException e)
124     {
125       throw new RuntimeException(e);
126     } catch (IOException e)
127     {
128       return false;
129     }
130   }
131 }