Merge branch 'features/mchmmer' into merge/wsinterfaces_mchmmer_JAL-3070_JAL-1950
[jalview.git] / test / jalview / ws / jws2 / ParameterUtilsTest.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.ws.jws2;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.bin.Cache;
28 import jalview.gui.JvOptionPane;
29 import jalview.ws.api.UIinfo;
30 import jalview.ws.jabaws.JalviewJabawsTestUtils;
31 import jalview.ws.jws2.jabaws2.Jws2Instance;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40 import compbio.metadata.Option;
41 import compbio.metadata.Parameter;
42 import compbio.metadata.Preset;
43 import compbio.metadata.PresetManager;
44 import compbio.metadata.WrongParameterException;
45
46 /*
47  * All methods in this class are set to the Network group because setUpBeforeClass will fail
48  * if there is no network.
49  */
50 @Test(singleThreaded = true)
51 public class ParameterUtilsTest
52 {
53
54   @BeforeClass(alwaysRun = true)
55   public void setUpJvOptionPane()
56   {
57     JvOptionPane.setInteractiveMode(false);
58     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
59   }
60
61   /*
62    * To limit tests to specify services, add them to this list; leave list empty
63    * to test all
64    */
65   private static List<String> serviceTests = new ArrayList<>();
66
67   private static Jws2Discoverer disc = null;
68
69   @BeforeClass(alwaysRun = true)
70   public static void setUpBeforeClass() throws Exception
71   {
72     serviceTests.add("AAConWS".toLowerCase());
73     Cache.loadProperties("test/jalview/io/testProps.jvprops");
74     Cache.initLogger();
75     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
76   }
77
78   @Test(groups = { "Network" })
79   public void testWriteParameterSet() throws WrongParameterException
80   {
81     for (Jws2Instance service : disc.getServices())
82     {
83       if (isForTesting(service))
84       {
85
86         List<Preset> prl = null;
87         PresetManager prman = service.getPresets();
88         if (prman == null)
89         {
90           continue;
91         }
92         prl = prman.getPresets();
93         if (prl == null)
94         {
95           continue;
96         }
97         for (Preset pr : prl)
98         {
99           List<String> writeparam = null;
100           List<String> readparam = null;
101           writeparam = ParameterUtils.writeParameterSet(
102                   pr.getArguments(service.getRunnerConfig()), " ");
103           List<Option> pset = ParameterUtils.processParameters(writeparam,
104                   service.getRunnerConfig(), " ");
105           readparam = ParameterUtils.writeParameterSet(pset, " ");
106           Iterator<String> o = pr.getOptions().iterator();
107           Iterator<String> s = writeparam.iterator();
108           Iterator<String> t = readparam.iterator();
109           boolean failed = false;
110           while (s.hasNext() && t.hasNext())
111           {
112             String on = o.next();
113             String sn = s.next();
114             String st = t.next();
115             final String errorMsg = "Original was " + on
116                     + " Phase 1 wrote " + sn + "\tPhase 2 wrote " + st;
117             assertEquals(errorMsg, sn, st);
118             assertEquals(errorMsg, sn, on);
119           }
120         }
121       }
122     }
123   }
124
125   /**
126    * Returns true if the service is in the list of the ones we chose to test,
127    * _or_ the list is empty (test all)
128    * 
129    * @param service
130    * @return
131    */
132   public boolean isForTesting(UIinfo service)
133   {
134     return serviceTests.size() == 0
135             || serviceTests.contains(service.getName().toLowerCase());
136   }
137
138   @Test(groups = { "Network" })
139   public void testCopyOption()
140   {
141     for (Jws2Instance service : disc.getServices())
142     {
143       if (isForTesting(service))
144       {
145         List<Option<?>> options = service.getRunnerConfig().getOptions();
146         for (Option<?> o : options)
147         {
148           System.out.println("Testing copyOption for option " + o.getName()
149                   + " of " + service.getActionText());
150           Option<?> cpy = ParameterUtils.copyOption(o);
151           assertTrue(cpy.equals(o));
152           assertEquals(cpy.getName(), o.getName());
153           assertFalse(cpy == o);
154           // todo more assertions?
155         }
156       }
157     }
158   }
159
160   /**
161    */
162   @Test(groups = { "Network" })
163   public void testCopyParameter()
164   {
165     for (Jws2Instance service : disc.getServices())
166     {
167       if (isForTesting(service))
168       {
169         List<Parameter> parameters = service.getRunnerConfig()
170                 .getParameters();
171         for (Parameter o : parameters)
172         {
173           System.out.println("Testing copyParameter for parameter "
174                   + o.getName() + " of " + service.getActionText());
175           Parameter cpy = ParameterUtils.copyParameter(o);
176           assertTrue(cpy.equals(o));
177           assertFalse(cpy == o);
178           assertEquals(cpy.getName(), o.getName());
179           // todo more assertions?
180         }
181       }
182     }
183   }
184 }