JAL-1620 version bump and release notes
[jalview.git] / test / jalview / ws / rest / ShmmrRSBSService.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.ws.rest;
22
23 import static org.junit.Assert.*;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
30 import java.util.Map;
31
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.AlignmentView;
34 import jalview.gui.AlignFrame;
35 import jalview.io.FileParse;
36 import jalview.ws.rest.InputType;
37 import jalview.ws.rest.params.SeqGroupIndexVector;
38
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42
43 /**
44  * @author jimp
45  * 
46  */
47 public class ShmmrRSBSService
48 {
49
50   @Test
51   public void testSeparatorListToArrayForRestServiceDescriptions()
52   {
53     assertTrue(
54             "separatorListToArray is faulty.",
55             RestServiceDescription.separatorListToArray(
56                     "foo=',',min='foo',max='1,2,3',fa=','", ",").length == 4);
57     assertTrue("separatorListToArray is faulty.",
58             RestServiceDescription.separatorListToArray(
59                     "minsize='2', sep=','", ",").length != 2); // probably
60                                                                // should come as
61                                                                // 2
62   }
63
64   @Test
65   public void testShmmrService()
66   {
67
68     assertTrue(
69             "Test Rsd Exchange using using default Shmmr service failed.",
70             testRsdExchange("Test using default Shmmr service",
71                     RestClient.makeShmmrRestClient().service));
72   }
73
74   @Test
75   public void testShmmrServiceDataprep() throws Exception
76   {
77     RestClient _rc = RestClient.makeShmmrRestClient();
78     assertNotNull(_rc);
79     AlignFrame alf = new jalview.io.FileLoader(false)
80             .LoadFileWaitTillLoaded("examples/testdata/smad.fa",
81                     jalview.io.FormatAdapter.FILE);
82     assertNotNull("Couldn't find test data.", alf);
83     alf.loadJalviewDataFile("examples/testdata/smad_groups.jva",
84             jalview.io.FormatAdapter.FILE, null, null);
85     assertTrue(
86             "Couldn't load the test data's annotation file (should be 5 groups but found "
87                     + alf.getViewport().getAlignment().getGroups().size()
88                     + ").", alf.getViewport().getAlignment().getGroups()
89                     .size() == 5);
90
91     RestClient rc = new RestClient(_rc.service, alf, true);
92
93     assertNotNull("Couldn't creat RestClient job.", rc);
94     jalview.bin.Cache.initLogger();
95     RestJob rjb = new RestJob(0, new RestJobThread(rc),
96             rc.av.getAlignment(), null);
97     rjb.setAlignmentForInputs(rc.service.getInputParams().values(),
98             rc.av.getAlignment());
99     for (Map.Entry<String, InputType> e : rc.service.getInputParams()
100             .entrySet())
101     {
102       System.out.println("For Input '" + e.getKey() + ":\n"
103               + e.getValue().formatForInput(rjb).getContentLength());
104     }
105   }
106
107   private static boolean testRsdExchange(String desc, String servicestring)
108   {
109     try
110     {
111       RestServiceDescription newService = new RestServiceDescription(
112               servicestring);
113       if (!newService.isValid())
114       {
115         throw new Error("Failed to create service from '" + servicestring
116                 + "'.\n" + newService.getInvalidMessage());
117       }
118       return testRsdExchange(desc, newService);
119     } catch (Throwable x)
120     {
121       System.err.println("Failed for service (" + desc + "): "
122               + servicestring);
123       x.printStackTrace();
124       return false;
125     }
126   }
127
128   private static boolean testRsdExchange(String desc,
129           RestServiceDescription service)
130   {
131     try
132     {
133       String fromservicetostring = service.toString();
134       RestServiceDescription newService = new RestServiceDescription(
135               fromservicetostring);
136       if (!newService.isValid())
137       {
138         throw new Error("Failed to create service from '"
139                 + fromservicetostring + "'.\n"
140                 + newService.getInvalidMessage());
141       }
142
143       if (!service.equals(newService))
144       {
145         System.err.println("Failed for service (" + desc + ").");
146         System.err.println("Original service and parsed service differ.");
147         System.err.println("Original: " + fromservicetostring);
148         System.err.println("Parsed  : " + newService.toString());
149         return false;
150       }
151     } catch (Throwable x)
152     {
153       System.err.println("Failed for service (" + desc + "): "
154               + service.toString());
155       x.printStackTrace();
156       return false;
157     }
158     return true;
159   }
160
161 }