JAL-1759 merge from develop
[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.testng.Assert;
15 import org.testng.AssertJUnit;
16 import org.testng.annotations.Test;
17
18
19 public class BioJsHTMLOutputTest
20 {
21
22   @Test(groups ={ "Functional" })
23   public void getJalviewAlignmentAsJsonString()
24   {
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(groups =
48   { "Functional" }, expectedExceptions = NullPointerException.class)
49   public void expectedNullPointerException()
50   {
51     try
52     {
53       BioJsHTMLOutput.refreshBioJSVersionsInfo(null);
54     } catch (URISyntaxException e)
55     {
56       AssertJUnit.fail("Expception occured while testing!");
57       e.printStackTrace();
58     }
59   }
60
61   @Test(groups ={ "Functional" })
62   public void getBioJsMSAVersions()
63   {
64     TreeMap<String, File> versions = null;
65     try
66     {
67       BioJsHTMLOutput
68               .refreshBioJSVersionsInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
69       versions = BioJsHTMLOutput.getBioJsMSAVersions();
70     } catch (URISyntaxException e)
71     {
72       AssertJUnit.fail("Expception occured while testing!");
73       e.printStackTrace();
74     }
75     AssertJUnit.assertNotNull("No versions found", versions);
76     AssertJUnit.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     AssertJUnit.assertNotNull("Latest BioJsMSA version NOT found!",
89             BioJsHTMLOutput.getCurrentBJSTemplateFile());
90
91   }
92
93   @Test(groups =
94   { "Network" })
95   public void testBioJsUpdate()
96   {
97     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
98     AssertJUnit.assertTrue("URL not reacable : " + url, urlIsReachable(url));
99     String response = BioJsHTMLOutput.getURLContentAsString(url);
100     AssertJUnit.assertNotNull("Null response read from url!", response);
101     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
102     System.out.println(">>> description : " + repository.getDescription());
103     System.out
104 .println(">>> latest version : "
105             + repository.getLatestReleaseVersion());
106     System.out.println(">>> repo count : "
107             + repository.getReleases().size());
108     for (BioJSReleasePojo release : repository.getReleases())
109     {
110       System.out.println("repo type : " + release.getType());
111       System.out.println("url : " + release.getUrl());
112       System.out.println("release version : " + release.getVersion());
113     }
114   }
115
116   private static boolean urlIsReachable(String urlString)
117   {
118     try
119     {
120       final URL url = new URL(urlString);
121       final URLConnection conn = url.openConnection();
122       conn.connect();
123       return true;
124     } catch (MalformedURLException e)
125     {
126       throw new RuntimeException(e);
127     } catch (IOException e)
128     {
129       return false;
130     }
131   }
132 }