77aa0f6e28be2bdb433ac9193f47d8e4eaa42bb0
[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 = HTMLOutput.readFileAsString(BioJsHTMLOutput
58               .getCurrentBJSTemplateFile());
59       // System.out.println(bjsTemplate);
60     } catch (IOException e)
61     {
62       e.printStackTrace();
63     }
64     Assert.assertNotNull(bjsTemplate);
65   }
66
67   @Test(
68     groups = { "Functional" },
69     expectedExceptions = NullPointerException.class)
70   public void expectedNullPointerException()
71   {
72     try
73     {
74       BioJsHTMLOutput.refreshVersionInfo(null);
75     } catch (URISyntaxException e)
76     {
77       AssertJUnit.fail("Expception occured while testing!");
78       e.printStackTrace();
79     }
80   }
81
82   @Test(groups = { "Functional" })
83   public void getBioJsMSAVersions()
84   {
85     TreeMap<String, File> versions = null;
86     try
87     {
88       BioJsHTMLOutput
89               .refreshVersionInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
90       versions = BioJsHTMLOutput.getBioJsMSAVersions();
91     } catch (URISyntaxException e)
92     {
93       AssertJUnit.fail("Expception occured while testing!");
94       e.printStackTrace();
95     }
96     AssertJUnit.assertNotNull("No versions found", versions);
97     AssertJUnit.assertTrue("One or more Templates required",
98             versions.size() > 0);
99     System.out
100             .println("Number of discovered versions : " + versions.size());
101     for (String v : versions.keySet())
102     {
103       System.out.println("version : " + v);
104       System.out.println("File : " + versions.get(v));
105     }
106
107     System.out.println("\nCurrent latest version : "
108             + BioJsHTMLOutput.getCurrentBJSTemplateFile());
109     AssertJUnit.assertNotNull("Latest BioJsMSA version NOT found!",
110             BioJsHTMLOutput.getCurrentBJSTemplateFile());
111
112   }
113
114   @Test(groups = { "Network" })
115   public void testBioJsUpdate()
116   {
117     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
118     AssertJUnit
119             .assertTrue("URL not reacable : " + url, urlIsReachable(url));
120     String response = BioJsHTMLOutput.getURLContentAsString(url);
121     AssertJUnit.assertNotNull("Null response read from url!", response);
122     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
123     System.out.println(">>> description : " + repository.getDescription());
124     System.out.println(">>> latest version : "
125             + repository.getLatestReleaseVersion());
126     System.out.println(">>> repo count : "
127             + repository.getReleases().size());
128     for (BioJSReleasePojo release : repository.getReleases())
129     {
130       System.out.println("repo type : " + release.getType());
131       System.out.println("url : " + release.getUrl());
132       System.out.println("release version : " + release.getVersion());
133     }
134   }
135
136   private static boolean urlIsReachable(String urlString)
137   {
138     try
139     {
140       final URL url = new URL(urlString);
141       final URLConnection conn = url.openConnection();
142       conn.connect();
143       return true;
144     } catch (MalformedURLException e)
145     {
146       throw new RuntimeException(e);
147     } catch (IOException e)
148     {
149       return false;
150     }
151   }
152 }