24e6cf7b2155d0d78173d33eff83eeb4f3837e7f
[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.gui.JvOptionPane;
24 import jalview.json.binding.biojs.BioJSReleasePojo;
25 import jalview.json.binding.biojs.BioJSRepositoryPojo;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.net.URISyntaxException;
31 import java.net.URL;
32 import java.net.URLConnection;
33 import java.util.TreeMap;
34
35 import org.testng.Assert;
36 import org.testng.AssertJUnit;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40 public class BioJsHTMLOutputTest
41 {
42
43   @BeforeClass(alwaysRun = true)
44   public void setUpJvOptionPane()
45   {
46     JvOptionPane.setInteractiveMode(false);
47     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48   }
49
50   @Test(groups = { "Functional" })
51   public void getJalviewAlignmentAsJsonString()
52   {
53     String bjsTemplate = null;
54     try
55     {
56       BioJsHTMLOutput.updateBioJS();
57       try
58       {
59         // allow the update some three seconds to complete before getting latest
60         // version of BioJS template
61         Thread.sleep(1000 * 3);
62       } catch (InterruptedException e)
63       {
64         e.printStackTrace();
65       }
66       bjsTemplate = HTMLOutput.readFileAsString(BioJsHTMLOutput
67               .getCurrentBJSTemplateFile());
68       // System.out.println(bjsTemplate);
69     } catch (IOException e)
70     {
71       e.printStackTrace();
72     }
73     Assert.assertNotNull(bjsTemplate);
74   }
75
76   @Test(
77     groups = { "Functional" },
78     expectedExceptions = NullPointerException.class)
79   public void expectedNullPointerException()
80   {
81     try
82     {
83       BioJsHTMLOutput.refreshVersionInfo(null);
84     } catch (URISyntaxException e)
85     {
86       AssertJUnit.fail("Expception occured while testing!");
87       e.printStackTrace();
88     }
89   }
90
91   @Test(groups = { "Functional" })
92   public void getBioJsMSAVersions()
93   {
94     TreeMap<String, File> versions = null;
95     try
96     {
97       BioJsHTMLOutput
98               .refreshVersionInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
99       versions = BioJsHTMLOutput.getBioJsMSAVersions();
100     } catch (URISyntaxException e)
101     {
102       AssertJUnit.fail("Expception occured while testing!");
103       e.printStackTrace();
104     }
105     AssertJUnit.assertNotNull("No versions found", versions);
106     AssertJUnit.assertTrue("One or more Templates required",
107             versions.size() > 0);
108     System.out
109             .println("Number of discovered versions : " + versions.size());
110     for (String v : versions.keySet())
111     {
112       System.out.println("version : " + v);
113       System.out.println("File : " + versions.get(v));
114     }
115
116     System.out.println("\nCurrent latest version : "
117             + BioJsHTMLOutput.getCurrentBJSTemplateFile());
118     AssertJUnit.assertNotNull("Latest BioJsMSA version NOT found!",
119             BioJsHTMLOutput.getCurrentBJSTemplateFile());
120
121   }
122
123   @Test(groups = { "Network" })
124   public void testBioJsUpdate()
125   {
126     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
127     AssertJUnit
128             .assertTrue("URL not reacable : " + url, urlIsReachable(url));
129     String response = BioJsHTMLOutput.getURLContentAsString(url);
130     AssertJUnit.assertNotNull("Null response read from url!", response);
131     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
132     System.out.println(">>> description : " + repository.getDescription());
133     System.out.println(">>> latest version : "
134             + repository.getLatestReleaseVersion());
135     System.out.println(">>> repo count : "
136             + repository.getReleases().size());
137     for (BioJSReleasePojo release : repository.getReleases())
138     {
139       System.out.println("repo type : " + release.getType());
140       System.out.println("url : " + release.getUrl());
141       System.out.println("release version : " + release.getVersion());
142     }
143   }
144
145   private static boolean urlIsReachable(String urlString)
146   {
147     try
148     {
149       final URL url = new URL(urlString);
150       final URLConnection conn = url.openConnection();
151       conn.connect();
152       return true;
153     } catch (MalformedURLException e)
154     {
155       throw new RuntimeException(e);
156     } catch (IOException e)
157     {
158       return false;
159     }
160   }
161 }