JAL-1517 fix copyright for 2.8.2
[jalview.git] / test / jalview / ws / rest / ShmmrRSBSService.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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 should come as 2
60   }
61
62   @Test
63   public void testShmmrService()
64   {
65
66     assertTrue(
67             "Test Rsd Exchange using using default Shmmr service failed.",
68             testRsdExchange("Test using default Shmmr service",
69                     RestClient.makeShmmrRestClient().service));
70   }
71   @Test
72   public void testShmmrServiceDataprep() throws Exception
73   {
74     RestClient _rc = RestClient.makeShmmrRestClient();
75     assertNotNull(_rc);
76     AlignFrame alf = new jalview.io.FileLoader(false).LoadFileWaitTillLoaded("examples/testdata/smad.fa", jalview.io.FormatAdapter.FILE);
77     assertNotNull("Couldn't find test data.",alf);
78     alf.loadJalviewDataFile("examples/testdata/smad_groups.jva",
79             jalview.io.FormatAdapter.FILE, null, null);
80     assertTrue("Couldn't load the test data's annotation file (should be 5 groups but found "+alf.getViewport().getAlignment().getGroups().size()+").", alf.getViewport().getAlignment().getGroups().size()==5);
81     
82     RestClient rc = new RestClient(_rc.service, alf, true);
83     
84     
85     
86     assertNotNull("Couldn't creat RestClient job.",rc);
87     jalview.bin.Cache.initLogger();
88     RestJob rjb = new RestJob(0, new RestJobThread(rc),rc.av.getAlignment(),null);
89     rjb.setAlignmentForInputs(rc.service.getInputParams().values(), rc.av.getAlignment());
90     for (Map.Entry<String,InputType> e:rc.service.getInputParams().entrySet()) {
91       System.out.println("For Input '"+e.getKey()+":\n"+e.getValue().formatForInput(rjb).getContentLength());
92     }
93   }
94
95   private static boolean testRsdExchange(String desc, String servicestring)
96   {
97     try
98     {
99       RestServiceDescription newService = new RestServiceDescription(
100               servicestring);
101       if (!newService.isValid())
102       {
103         throw new Error("Failed to create service from '" + servicestring
104                 + "'.\n" + newService.getInvalidMessage());
105       }
106       return testRsdExchange(desc, newService);
107     } catch (Throwable x)
108     {
109       System.err.println("Failed for service (" + desc + "): "
110               + servicestring);
111       x.printStackTrace();
112       return false;
113     }
114   }
115
116   private static boolean testRsdExchange(String desc,
117           RestServiceDescription service)
118   {
119     try
120     {
121       String fromservicetostring = service.toString();
122       RestServiceDescription newService = new RestServiceDescription(
123               fromservicetostring);
124       if (!newService.isValid())
125       {
126         throw new Error("Failed to create service from '"
127                 + fromservicetostring + "'.\n"
128                 + newService.getInvalidMessage());
129       }
130
131       if (!service.equals(newService))
132       {
133         System.err.println("Failed for service (" + desc + ").");
134         System.err.println("Original service and parsed service differ.");
135         System.err.println("Original: " + fromservicetostring);
136         System.err.println("Parsed  : " + newService.toString());
137         return false;
138       }
139     } catch (Throwable x)
140     {
141       System.err.println("Failed for service (" + desc + "): "
142               + service.toString());
143       x.printStackTrace();
144       return false;
145     }
146     return true;
147   }
148
149 }