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