ddf9a15469a4876a99d153be592aa6e17f699b76
[jalview.git] / test / jalview / io / BioJsHTMLOutputTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.json.binding.biojs.BioJSReleasePojo;
24 import jalview.json.binding.biojs.BioJSRepositoryPojo;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.MalformedURLException;
29 import java.net.URISyntaxException;
30 import java.net.URL;
31 import java.net.URLConnection;
32 import java.util.TreeMap;
33
34 import org.testng.Assert;
35 import org.testng.AssertJUnit;
36 import org.testng.annotations.Test;
37
38 public class BioJsHTMLOutputTest
39 {
40
41   @Test(groups = { "Functional" })
42   public void getJalviewAlignmentAsJsonString()
43   {
44     String bjsTemplate = null;
45     try
46     {
47       BioJsHTMLOutput.updateBioJS();
48       try
49       {
50         // allow the update some three seconds to complete before getting latest
51         // version of BioJS template
52         Thread.sleep(1000 * 3);
53       } catch (InterruptedException e)
54       {
55         e.printStackTrace();
56       }
57       bjsTemplate = BioJsHTMLOutput.getBioJsTemplateAsString();
58       // System.out.println(bjsTemplate);
59     } catch (IOException e)
60     {
61       e.printStackTrace();
62     }
63     Assert.assertNotNull(bjsTemplate);
64   }
65
66   @Test(
67     groups = { "Functional" },
68     expectedExceptions = NullPointerException.class)
69   public void expectedNullPointerException()
70   {
71     try
72     {
73       BioJsHTMLOutput.refreshBioJSVersionsInfo(null);
74     } catch (URISyntaxException e)
75     {
76       AssertJUnit.fail("Expception occured while testing!");
77       e.printStackTrace();
78     }
79   }
80
81   @Test(groups = { "Functional" })
82   public void getBioJsMSAVersions()
83   {
84     TreeMap<String, File> versions = null;
85     try
86     {
87       BioJsHTMLOutput
88               .refreshBioJSVersionsInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
89       versions = BioJsHTMLOutput.getBioJsMSAVersions();
90     } catch (URISyntaxException e)
91     {
92       AssertJUnit.fail("Expception occured while testing!");
93       e.printStackTrace();
94     }
95     AssertJUnit.assertNotNull("No versions found", versions);
96     AssertJUnit.assertTrue("One or more Templates required",
97             versions.size() > 0);
98     System.out
99             .println("Number of discovered versions : " + versions.size());
100     for (String v : versions.keySet())
101     {
102       System.out.println("version : " + v);
103       System.out.println("File : " + versions.get(v));
104     }
105
106     System.out.println("\nCurrent latest version : "
107             + BioJsHTMLOutput.getCurrentBJSTemplateFile());
108     AssertJUnit.assertNotNull("Latest BioJsMSA version NOT found!",
109             BioJsHTMLOutput.getCurrentBJSTemplateFile());
110
111   }
112
113   @Test(groups = { "Network" })
114   public void testBioJsUpdate()
115   {
116     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
117     AssertJUnit
118             .assertTrue("URL not reacable : " + url, urlIsReachable(url));
119     String response = BioJsHTMLOutput.getURLContentAsString(url);
120     AssertJUnit.assertNotNull("Null response read from url!", response);
121     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
122     System.out.println(">>> description : " + repository.getDescription());
123     System.out.println(">>> latest version : "
124             + repository.getLatestReleaseVersion());
125     System.out.println(">>> repo count : "
126             + repository.getReleases().size());
127     for (BioJSReleasePojo release : repository.getReleases())
128     {
129       System.out.println("repo type : " + release.getType());
130       System.out.println("url : " + release.getUrl());
131       System.out.println("release version : " + release.getVersion());
132     }
133   }
134
135   private static boolean urlIsReachable(String urlString)
136   {
137     try
138     {
139       final URL url = new URL(urlString);
140       final URLConnection conn = url.openConnection();
141       conn.connect();
142       return true;
143     } catch (MalformedURLException e)
144     {
145       throw new RuntimeException(e);
146     } catch (IOException e)
147     {
148       return false;
149     }
150   }
151 }