b751b7745d1c9e395e1eeb541cd30a9cdd0a92e7
[jalview.git] / test / jalview / fts / core / FTSRestClientTest.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.fts.core;
22
23 import jalview.fts.api.FTSDataColumnI;
24 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
25
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import org.testng.Assert;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 public class FTSRestClientTest
35 {
36   private FTSRestClient ftsRestClient;
37
38   @BeforeMethod(alwaysRun = true)
39   public void setup()
40   {
41     ftsRestClient = new FTSRestClient()
42     {
43
44       @Override
45       public String getColumnDataConfigFileName()
46       {
47         return "/fts/uniprot_data_columns.txt";
48       }
49
50       @Override
51       public FTSRestResponse executeRequest(FTSRestRequest ftsRequest)
52               throws Exception
53       {
54         return null;
55       }
56     };
57   }
58
59   @Test(groups = { "Functional" })
60   public void getPrimaryKeyColumIndexTest()
61   {
62     Collection<FTSDataColumnI> wantedFields = ftsRestClient
63             .getAllDefaultDisplayedFTSDataColumns();
64     int foundIndex = -1;
65     try
66     {
67       Assert.assertEquals(foundIndex, -1);
68       foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
69               false);
70       Assert.assertEquals(foundIndex, 0);
71       foundIndex = ftsRestClient
72               .getPrimaryKeyColumIndex(wantedFields, true);
73       Assert.assertEquals(foundIndex, 1);
74     } catch (Exception e)
75     {
76       e.printStackTrace();
77       Assert.fail("Exception thrown while testing...");
78     }
79   }
80
81   @Test(groups = { "Functional" })
82   public void getAllDefaulDisplayedDataColumns()
83   {
84     Assert.assertNotNull(ftsRestClient
85             .getAllDefaultDisplayedFTSDataColumns());
86     Assert.assertTrue(!ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
87             .isEmpty());
88     Assert.assertEquals(ftsRestClient
89             .getAllDefaultDisplayedFTSDataColumns().size(), 7);
90   }
91
92   @Test(groups = { "Functional" })
93   public void getDataColumnsFieldsAsCommaDelimitedString()
94   {
95     Collection<FTSDataColumnI> wantedFields = ftsRestClient
96             .getAllDefaultDisplayedFTSDataColumns();
97     String actual = ftsRestClient
98             .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
99     Assert.assertEquals(actual,
100             "id,entry name,protein names,genes,organism,reviewed,length");
101   }
102
103   @Test(groups = { "Functional" })
104   public void getAllFTSDataColumns()
105   {
106     Collection<FTSDataColumnI> allFields = ftsRestClient
107             .getAllFTSDataColumns();
108     Assert.assertNotNull(allFields);
109     Assert.assertEquals(allFields.size(), 117);
110   }
111
112   @Test(groups = { "Functional" })
113   public void getSearchableDataColumns()
114   {
115     Collection<FTSDataColumnI> searchalbeFields = ftsRestClient
116             .getSearchableDataColumns();
117     Assert.assertNotNull(searchalbeFields);
118     Assert.assertEquals(searchalbeFields.size(), 22);
119   }
120
121   @Test(groups = { "Functional" })
122   public void getPrimaryKeyColumn()
123   {
124     FTSDataColumnI expectedPKColumn;
125     try
126     {
127       expectedPKColumn = ftsRestClient
128               .getDataColumnByNameOrCode("Uniprot Id");
129       Assert.assertNotNull(ftsRestClient.getPrimaryKeyColumn());
130       Assert.assertEquals(ftsRestClient.getPrimaryKeyColumn(),
131               expectedPKColumn);
132     } catch (Exception e)
133     {
134       e.printStackTrace();
135       Assert.fail("Exception thrown while testing...");
136     }
137   }
138
139   @Test(groups = { "Functional" })
140   public void getDataColumnByNameOrCode()
141   {
142     try
143     {
144       FTSDataColumnI foundDataCol = ftsRestClient
145               .getDataColumnByNameOrCode("genes");
146       Assert.assertNotNull(foundDataCol);
147       Assert.assertEquals(foundDataCol.getName(), "Gene Names");
148     } catch (Exception e)
149     {
150       e.printStackTrace();
151       Assert.fail("Exception thrown while testing...");
152     }
153   }
154
155   @Test(groups = { "Functional" })
156   public void getDataColumnGroupById()
157   {
158     FTSDataColumnGroupI foundDataColGroup;
159     try
160     {
161       foundDataColGroup = ftsRestClient.getDataColumnGroupById("g3");
162       Assert.assertNotNull(foundDataColGroup);
163       Assert.assertEquals(foundDataColGroup.getName(), "Names & Taxonomy");
164     } catch (Exception e)
165     {
166       e.printStackTrace();
167     }
168   }
169
170   @Test(groups = { "Functional" })
171   public void getDefaultResponsePageSize()
172   {
173     int defaultResSize = ftsRestClient.getDefaultResponsePageSize();
174     Assert.assertEquals(defaultResSize, 500);
175   }
176
177   @Test(groups = { "Functional" })
178   public void getColumnMinWidthTest()
179   {
180     try
181     {
182       FTSDataColumnI foundDataCol = ftsRestClient
183               .getDataColumnByNameOrCode("Protein names");
184       Assert.assertNotNull(foundDataCol);
185       int actualColMinWidth = foundDataCol.getMinWidth();
186       Assert.assertEquals(actualColMinWidth, 300);
187     } catch (Exception e)
188     {
189       e.printStackTrace();
190       Assert.fail("Exception thrown while testing...");
191     }
192   }
193
194   @Test(groups = { "Functional" })
195   public void getColumnMaxWidthTest()
196   {
197     try
198     {
199       FTSDataColumnI foundDataCol = ftsRestClient
200               .getDataColumnByNameOrCode("Protein names");
201       Assert.assertNotNull(foundDataCol);
202       int actualColMinWidth = foundDataCol.getMaxWidth();
203       Assert.assertEquals(actualColMinWidth, 1500);
204     } catch (Exception e)
205     {
206       e.printStackTrace();
207       Assert.fail("Exception thrown while testing...");
208     }
209   }
210
211   @Test(groups = { "Functional" })
212   public void getColumnPreferredWidthTest()
213   {
214     try
215     {
216       FTSDataColumnI foundDataCol = ftsRestClient
217               .getDataColumnByNameOrCode("Protein names");
218       Assert.assertNotNull(foundDataCol);
219       int actualColMinWidth = foundDataCol.getPreferredWidth();
220       Assert.assertEquals(actualColMinWidth, 500);
221     } catch (Exception e)
222     {
223       e.printStackTrace();
224       Assert.fail("Exception thrown while testing...");
225     }
226   }
227
228   @Test(groups = { "Functional" })
229   public void getColumnClassTest()
230   {
231     try
232     {
233       FTSDataColumnI foundDataCol = ftsRestClient
234               .getDataColumnByNameOrCode("Protein names");
235       Assert.assertNotNull(foundDataCol);
236       Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
237               String.class);
238       foundDataCol = ftsRestClient.getDataColumnByNameOrCode("length");
239       Assert.assertNotNull(foundDataCol);
240       Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
241               Integer.class);
242       // foundDataCol = ftsRestClient.getDataColumnByNameOrCode("length");
243       // Assert.assertNotNull(foundDataCol);
244       // Assert.assertEquals(foundDataCol.getDataColumnClass(), Double.class);
245     } catch (Exception e)
246     {
247       e.printStackTrace();
248       Assert.fail("Exception thrown while testing...");
249     }
250   }
251
252   @Test(groups = { "Functional" })
253   public void coverageForEqualsAndHashFunction()
254   {
255     Set<FTSDataColumnI> uniqueSet = new HashSet<FTSDataColumnI>();
256     Collection<FTSDataColumnI> searchableCols = ftsRestClient
257             .getSearchableDataColumns();
258     for (FTSDataColumnI foundCol : searchableCols)
259     {
260       System.out.println(foundCol.toString());
261       uniqueSet.add(foundCol);
262       uniqueSet.add(foundCol);
263     }
264     Assert.assertTrue(!uniqueSet.isEmpty());
265     Assert.assertEquals(uniqueSet.size(), 22);
266   }
267
268   @Test(groups = { "Functional" })
269   public void coverageForMiscellaneousBranches()
270   {
271     String actual = ftsRestClient.getPrimaryKeyColumn().toString();
272     Assert.assertEquals(actual, "Uniprot Id");
273
274     String actualGroupStr;
275     try
276     {
277       actualGroupStr = ftsRestClient.getDataColumnGroupById("g4")
278               .toString();
279       Assert.assertEquals(actualGroupStr, "Procedures & Softwares");
280       actualGroupStr = ftsRestClient.getDataColumnGroupById(
281               "unavailable group").toString();
282     } catch (Exception e)
283     {
284       Assert.assertTrue(true);
285     }
286
287     String actualResourseFile = ftsRestClient
288             .getResourceFile("/fts/uniprot_data_columns.txt");
289     Assert.assertNotNull(actualResourseFile);
290     Assert.assertTrue(actualResourseFile.length() > 31);
291   }
292
293   @Test(groups = { "Functional" }, expectedExceptions = Exception.class)
294   public void coverageForExceptionBranches() throws Exception
295   {
296     try
297     {
298       ftsRestClient.getDataColumnByNameOrCode("unavailable column");
299     } catch (Exception e)
300     {
301       System.out.println(e.getMessage());
302       String expectedMessage = "Couldn't find data column with name : unavailable column";
303       Assert.assertEquals(e.getMessage(), expectedMessage);
304     }
305     try
306     {
307       ftsRestClient.getDataColumnGroupById("unavailable column group Id");
308     } catch (Exception e)
309     {
310       System.out.println(e.getMessage());
311       String expectedMessage = "Couldn't find data column group with id : unavailable column group Id";
312       Assert.assertEquals(e.getMessage(), expectedMessage);
313     }
314
315     ftsRestClient.getDataColumnByNameOrCode("unavailable column");
316
317     ftsRestClient.getResourceFile("unavailable resource file");
318
319   }
320
321 }