JAL-1645 source formatting and organise imports
[jalview.git] / test / jalview / io / BioJsHTMLOutputTest.java
1 package jalview.io;
2
3 import jalview.json.binding.biojs.BioJSReleasePojo;
4 import jalview.json.binding.biojs.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 public class BioJsHTMLOutputTest
19 {
20
21   @Test(groups = { "Functional" })
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(
47     groups = { "Functional" },
48     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",
77             versions.size() > 0);
78     System.out
79             .println("Number of discovered versions : " + 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 = { "Network" })
94   public void testBioJsUpdate()
95   {
96     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
97     AssertJUnit
98             .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.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 }