JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / jalview / io / BioJsHTMLOutputTest.java
1 package jalview.io;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.util.TreeMap;
10
11 import org.testng.Assert;
12 import org.testng.AssertJUnit;
13 import org.testng.annotations.Test;
14
15 import jalview.json.binding.v1.BioJSReleasePojo;
16 import jalview.json.binding.v1.BioJSRepositoryPojo;
17
18
19 public class BioJsHTMLOutputTest
20 {
21
22   @Test
23   public void getJalviewAlignmentAsJsonString()
24   {
25     String bjsTemplate = null;
26     try
27     {
28       BioJsHTMLOutput.updateBioJS();
29       try
30       {
31         // allow the update some three seconds to complete before getting latest
32         // version of BioJS template
33         Thread.sleep(1000 * 3);
34       } catch (InterruptedException e)
35       {
36         e.printStackTrace();
37       }
38       bjsTemplate = BioJsHTMLOutput.getBioJsTemplateAsString();
39       // System.out.println(bjsTemplate);
40     } catch (IOException e)
41     {
42       e.printStackTrace();
43     }
44     Assert.assertNotNull(bjsTemplate);
45   }
46
47   @Test(expectedExceptions = NullPointerException.class)
48   public void expectedNullPointerException()
49   {
50     try
51     {
52       BioJsHTMLOutput.refreshBioJSVersionsInfo(null);
53     } catch (URISyntaxException e)
54     {
55       AssertJUnit.fail("Expception occured while testing!");
56       e.printStackTrace();
57     }
58   }
59
60   @Test
61   public void getBioJsMSAVersions()
62   {
63     TreeMap<String, File> versions = null;
64     try
65     {
66       BioJsHTMLOutput
67               .refreshBioJSVersionsInfo(BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY);
68       versions = BioJsHTMLOutput.getBioJsMSAVersions();
69     } catch (URISyntaxException e)
70     {
71       AssertJUnit.fail("Expception occured while testing!");
72       e.printStackTrace();
73     }
74     AssertJUnit.assertNotNull("No versions found", versions);
75     AssertJUnit.assertTrue("One or more Templates required", versions.size() > 0);
76     System.out
77             .println("Number of discovered versions : "
78             + versions.size());
79     for (String v : versions.keySet())
80     {
81       System.out.println("version : " + v);
82       System.out.println("File : " + versions.get(v));
83     }
84
85     System.out.println("\nCurrent latest version : "
86             + BioJsHTMLOutput.getCurrentBJSTemplateFile());
87     AssertJUnit.assertNotNull("Latest BioJsMSA version NOT found!",
88             BioJsHTMLOutput.getCurrentBJSTemplateFile());
89
90   }
91
92   @Test
93   public void testBioJsUpdate()
94   {
95     String url = BioJsHTMLOutput.BJS_TEMPLATE_GIT_REPO;
96     AssertJUnit.assertTrue("URL not reacable : " + url, urlIsReachable(url));
97     String response = BioJsHTMLOutput.getURLContentAsString(url);
98     AssertJUnit.assertNotNull("Null response read from url!", response);
99     BioJSRepositoryPojo repository = new BioJSRepositoryPojo(response);
100     System.out.println(">>> description : " + repository.getDescription());
101     System.out
102 .println(">>> latest version : "
103             + repository.getLatestReleaseVersion());
104     System.out.println(">>> repo count : "
105             + repository.getReleases().size());
106     for (BioJSReleasePojo release : repository.getReleases())
107     {
108       System.out.println("repo type : " + release.getType());
109       System.out.println("url : " + release.getUrl());
110       System.out.println("release version : " + release.getVersion());
111     }
112   }
113
114   private static boolean urlIsReachable(String urlString)
115   {
116     try
117     {
118       final URL url = new URL(urlString);
119       final URLConnection conn = url.openConnection();
120       conn.connect();
121       return true;
122     } catch (MalformedURLException e)
123     {
124       throw new RuntimeException(e);
125     } catch (IOException e)
126     {
127       return false;
128     }
129   }
130 }