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