JAL-1432 updated copyright notices
[jalview.git] / test / jalview / ws / rest / ShmmrRSBSService.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.rest;
20
21 import static org.junit.Assert.*;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.Map;
29
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.AlignmentView;
32 import jalview.gui.AlignFrame;
33 import jalview.io.FileParse;
34 import jalview.ws.rest.InputType;
35 import jalview.ws.rest.params.SeqGroupIndexVector;
36
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40
41 /**
42  * @author jimp
43  * 
44  */
45 public class ShmmrRSBSService
46 {
47
48   @Test
49   public void testSeparatorListToArrayForRestServiceDescriptions()
50   {
51     assertTrue(
52             "separatorListToArray is faulty.",
53             RestServiceDescription.separatorListToArray(
54                     "foo=',',min='foo',max='1,2,3',fa=','", ",").length == 4);
55     assertTrue("separatorListToArray is faulty.",
56             RestServiceDescription.separatorListToArray(
57                     "minsize='2', sep=','", ",").length != 2); // probably should come as 2
58   }
59
60   @Test
61   public void testShmmrService()
62   {
63
64     assertTrue(
65             "Test Rsd Exchange using using default Shmmr service failed.",
66             testRsdExchange("Test using default Shmmr service",
67                     RestClient.makeShmmrRestClient().service));
68   }
69   @Test
70   public void testShmmrServiceDataprep() throws Exception
71   {
72     RestClient _rc = RestClient.makeShmmrRestClient();
73     assertNotNull(_rc);
74     AlignFrame alf = new jalview.io.FileLoader(false).LoadFileWaitTillLoaded("examples/testdata/smad.fa", jalview.io.FormatAdapter.FILE);
75     assertNotNull("Couldn't find test data.",alf);
76     alf.loadJalviewDataFile("examples/testdata/smad_groups.jva",
77             jalview.io.FormatAdapter.FILE, null, null);
78     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);
79     
80     RestClient rc = new RestClient(_rc.service, alf, true);
81     
82     
83     
84     assertNotNull("Couldn't creat RestClient job.",rc);
85     jalview.bin.Cache.initLogger();
86     RestJob rjb = new RestJob(0, new RestJobThread(rc),rc.av.getAlignment(),null);
87     rjb.setAlignmentForInputs(rc.service.getInputParams().values(), rc.av.getAlignment());
88     for (Map.Entry<String,InputType> e:rc.service.getInputParams().entrySet()) {
89       System.out.println("For Input '"+e.getKey()+":\n"+e.getValue().formatForInput(rjb).getContentLength());
90     }
91   }
92
93   private static boolean testRsdExchange(String desc, String servicestring)
94   {
95     try
96     {
97       RestServiceDescription newService = new RestServiceDescription(
98               servicestring);
99       if (!newService.isValid())
100       {
101         throw new Error("Failed to create service from '" + servicestring
102                 + "'.\n" + newService.getInvalidMessage());
103       }
104       return testRsdExchange(desc, newService);
105     } catch (Throwable x)
106     {
107       System.err.println("Failed for service (" + desc + "): "
108               + servicestring);
109       x.printStackTrace();
110       return false;
111     }
112   }
113
114   private static boolean testRsdExchange(String desc,
115           RestServiceDescription service)
116   {
117     try
118     {
119       String fromservicetostring = service.toString();
120       RestServiceDescription newService = new RestServiceDescription(
121               fromservicetostring);
122       if (!newService.isValid())
123       {
124         throw new Error("Failed to create service from '"
125                 + fromservicetostring + "'.\n"
126                 + newService.getInvalidMessage());
127       }
128
129       if (!service.equals(newService))
130       {
131         System.err.println("Failed for service (" + desc + ").");
132         System.err.println("Original service and parsed service differ.");
133         System.err.println("Original: " + fromservicetostring);
134         System.err.println("Parsed  : " + newService.toString());
135         return false;
136       }
137     } catch (Throwable x)
138     {
139       System.err.println("Failed for service (" + desc + "): "
140               + service.toString());
141       x.printStackTrace();
142       return false;
143     }
144     return true;
145   }
146
147 }