9712f4bc40ff56eab602cd731a8eaa48bda4c32b
[arvados.git] / sdk / java / src / test / java / org / arvados / sdk / java / ArvadosTest.java
1 package org.arvados.sdk.java;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.junit.Test;
11
12 import static org.junit.Assert.*;
13
14 import org.json.simple.JSONObject;
15 import org.json.simple.parser.JSONParser;
16
17 /**
18  * Unit test for Arvados.
19  */
20 public class ArvadosTest {
21
22   /**
23    * Test users.list api
24    * @throws Exception
25    */
26   @Test
27   public void testCallUsersList() throws Exception {
28     Arvados arv = new Arvados("arvados", "v1");
29
30     Map<String, Object> params = new HashMap<String, Object>();
31
32     String response = arv.call("users", "list", params);
33     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
34     assertTrue("Expected users.list in response", response.contains("uuid"));
35
36     JSONParser parser = new JSONParser();
37     Object obj = parser.parse(response);
38     JSONObject jsonObject = (JSONObject) obj;
39
40     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
41
42     List items = (List)jsonObject.get("items");
43     assertNotNull("expected users list items", items);
44     assertTrue("expected at least one item in users list", items.size()>0);
45
46     JSONObject firstUser = (JSONObject)items.get(0);
47     assertNotNull ("Expcted at least one user", firstUser);
48
49     assertEquals("Expected kind to be user", "arvados#user", firstUser.get("kind"));
50     assertNotNull("Expected uuid for first user", firstUser.get("uuid"));
51   }
52
53   /**
54    * Test users.get <uuid> api
55    * @throws Exception
56    */
57   @Test
58   public void testCallUsersGet() throws Exception {
59     Arvados arv = new Arvados("arvados", "v1");
60
61     // call user.system and get uuid of this user
62     Map<String, Object> params = new HashMap<String, Object>();
63
64     String response = arv.call("users", "list", params);
65     JSONParser parser = new JSONParser();
66     Object obj = parser.parse(response);
67     JSONObject jsonObject = (JSONObject) obj;
68     assertNotNull("expected users list", jsonObject);
69     List items = (List)jsonObject.get("items");
70     assertNotNull("expected users list items", items);
71
72     JSONObject firstUser = (JSONObject)items.get(0);
73     String userUuid = (String)firstUser.get("uuid");
74
75     // invoke users.get with the system user uuid
76     params = new HashMap<String, Object>();
77     params.put("uuid", userUuid);
78
79     response = arv.call("users", "get", params);
80
81     //JSONParser parser = new JSONParser();
82     jsonObject = (JSONObject) parser.parse(response);;
83     assertNotNull("Expected uuid for first user", jsonObject.get("uuid"));
84     assertEquals("Expected system user uuid", userUuid, jsonObject.get("uuid"));
85   }
86
87   /**
88    * Test users.create api
89    * @throws Exception
90    */
91   @Test
92   public void testCreateUser() throws Exception {
93     Arvados arv = new Arvados("arvados", "v1");
94
95     Map<String, Object> params = new HashMap<String, Object>();
96     params.put("user", "{}");
97     String response = arv.call("users", "create", params);
98
99     JSONParser parser = new JSONParser();
100     JSONObject jsonObject = (JSONObject) parser.parse(response);
101     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
102
103     Object uuid = jsonObject.get("uuid");
104     assertNotNull("Expected uuid for first user", uuid);
105
106     // delete the object
107     params = new HashMap<String, Object>();
108     params.put("uuid", uuid);
109     response = arv.call("users", "delete", params);
110
111     // invoke users.get with the system user uuid
112     params = new HashMap<String, Object>();
113     params.put("uuid", uuid);
114
115     Exception caught = null;
116     try {
117       arv.call("users", "get", params);
118     } catch (Exception e) {
119       caught = e;
120     }
121
122     assertNotNull ("expected exception", caught);
123     assertTrue ("Expected 404", caught.getMessage().contains("Path not found"));
124   }
125
126   @Test
127   public void testCreateUserWithMissingRequiredParam() throws Exception {
128     Arvados arv = new Arvados("arvados", "v1");
129
130     Map<String, Object> params = new HashMap<String, Object>();
131
132     Exception caught = null;
133     try {
134       arv.call("users", "create", params);
135     } catch (Exception e) {
136       caught = e;
137     }
138
139     assertNotNull ("expected exception", caught);
140     assertTrue ("Expected POST method requires content object user", 
141         caught.getMessage().contains("ERROR: POST method requires content object user"));
142   }
143
144   /**
145    * Test users.create api
146    * @throws Exception
147    */
148   @Test
149   public void testCreateAndUpdateUser() throws Exception {
150     Arvados arv = new Arvados("arvados", "v1");
151
152     Map<String, Object> params = new HashMap<String, Object>();
153     params.put("user", "{}");
154     String response = arv.call("users", "create", params);
155
156     JSONParser parser = new JSONParser();
157     JSONObject jsonObject = (JSONObject) parser.parse(response);
158     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
159
160     Object uuid = jsonObject.get("uuid");
161     assertNotNull("Expected uuid for first user", uuid);
162
163     // update this user
164     params = new HashMap<String, Object>();
165     params.put("user", "{}");
166     params.put("uuid", uuid);
167     response = arv.call("users", "update", params);
168
169     parser = new JSONParser();
170     jsonObject = (JSONObject) parser.parse(response);
171     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
172
173     uuid = jsonObject.get("uuid");
174     assertNotNull("Expected uuid for first user", uuid);
175
176     // delete the object
177     params = new HashMap<String, Object>();
178     params.put("uuid", uuid);
179     response = arv.call("users", "delete", params);
180   }
181
182   /**
183    * Test unsupported api version api
184    * @throws Exception
185    */
186   @Test
187   public void testUnsupportedApiName() throws Exception {
188     Arvados arv = new Arvados("not_arvados", "v1");
189
190     Exception caught = null;
191     try {
192       arv.call("users", "list", null);
193     } catch (Exception e) {
194       caught = e;
195     }
196
197     assertNotNull ("expected exception", caught);
198     assertTrue ("Expected 404 when unsupported api is used", caught.getMessage().contains("404 Not Found"));
199   }
200
201   /**
202    * Test unsupported api version api
203    * @throws Exception
204    */
205   @Test
206   public void testUnsupportedVersion() throws Exception {
207     Arvados arv = new Arvados("arvados", "v2");
208
209     Exception caught = null;
210     try {
211       arv.call("users", "list", null);
212     } catch (Exception e) {
213       caught = e;
214     }
215
216     assertNotNull ("expected exception", caught);
217     assertTrue ("Expected 404 when unsupported version is used", caught.getMessage().contains("404 Not Found"));
218   }
219
220   /**
221    * Test unsupported api version api
222    * @throws Exception
223    */
224   @Test
225   public void testCallForNoSuchResrouce() throws Exception {
226     Arvados arv = new Arvados("arvados", "v1");
227
228     Exception caught = null;
229     try {
230       arv.call("abcd", "list", null);
231     } catch (Exception e) {
232       caught = e;
233     }
234
235     assertNotNull ("expected exception", caught);
236     assertTrue ("Expected ERROR: 404 not found", caught.getMessage().contains("ERROR: resource not found"));
237   }
238
239   /**
240    * Test unsupported api version api
241    * @throws Exception
242    */
243   @Test
244   public void testCallForNoSuchResrouceMethod() throws Exception {
245     Arvados arv = new Arvados("arvados", "v1");
246
247     Exception caught = null;
248     try {
249       arv.call("users", "abcd", null);
250     } catch (Exception e) {
251       caught = e;
252     }
253
254     assertNotNull ("expected exception", caught);
255     assertTrue ("Expected ERROR: 404 not found", caught.getMessage().contains("ERROR: method not found"));
256   }
257
258   /**
259    * Test pipeline_tempates.create api
260    * @throws Exception
261    */
262   @Test
263   public void testCreateAndGetPipelineTemplate() throws Exception {
264     Arvados arv = new Arvados("arvados", "v1");
265
266     File file = new File(getClass().getResource( "/first_pipeline.json" ).toURI());
267     byte[] data = new byte[(int)file.length()];
268     try {
269       FileInputStream is = new FileInputStream(file);
270       is.read(data);
271       is.close();
272     }catch(Exception e) {
273       e.printStackTrace();
274     }
275
276     Map<String, Object> params = new HashMap<String, Object>();
277     params.put("pipeline_template", new String(data));
278     String response = arv.call("pipeline_templates", "create", params);
279
280     JSONParser parser = new JSONParser();
281     JSONObject jsonObject = (JSONObject) parser.parse(response);
282     assertEquals("Expected kind to be user", "arvados#pipelineTemplate", jsonObject.get("kind"));
283     String uuid = (String)jsonObject.get("uuid");
284     assertNotNull("Expected uuid for pipeline template", uuid);
285
286     // get the pipeline
287     params = new HashMap<String, Object>();
288     params.put("uuid", uuid);
289     response = arv.call("pipeline_templates", "get", params);
290
291     parser = new JSONParser();
292     jsonObject = (JSONObject) parser.parse(response);
293     assertEquals("Expected kind to be user", "arvados#pipelineTemplate", jsonObject.get("kind"));
294     assertEquals("Expected uuid for pipeline template", uuid, jsonObject.get("uuid"));
295
296     // delete the object
297     params = new HashMap<String, Object>();
298     params.put("uuid", uuid);
299     response = arv.call("pipeline_templates", "delete", params);
300   }
301
302   /**
303    * Test users.list api
304    * @throws Exception
305    */
306   @Test
307   public void testArvadosWithTokenPassed() throws Exception {
308     String token = System.getenv().get("ARVADOS_API_TOKEN");
309     String host = System.getenv().get("ARVADOS_API_HOST");      
310     String hostInsecure = System.getenv().get("ARVADOS_API_HOST_INSECURE");
311
312     Arvados arv = new Arvados("arvados", "v1", token, host, hostInsecure);
313
314     Map<String, Object> params = new HashMap<String, Object>();
315
316     String response = arv.call("users", "list", params);
317     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
318     assertTrue("Expected users.list in response", response.contains("uuid"));
319
320     JSONParser parser = new JSONParser();
321     Object obj = parser.parse(response);
322     JSONObject jsonObject = (JSONObject) obj;
323     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
324   }
325
326   /**
327    * Test users.list api
328    * @throws Exception
329    */
330   @Test
331   public void testCallUsersListWithLimit() throws Exception {
332     Arvados arv = new Arvados("arvados", "v1");
333
334     Map<String, Object> params = new HashMap<String, Object>();
335
336     String response = arv.call("users", "list", params);
337     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
338     assertTrue("Expected users.list in response", response.contains("uuid"));
339
340     JSONParser parser = new JSONParser();
341     Object obj = parser.parse(response);
342     JSONObject jsonObject = (JSONObject) obj;
343
344     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
345
346     List items = (List)jsonObject.get("items");
347     assertNotNull("expected users list items", items);
348     assertTrue("expected at least one item in users list", items.size()>0);
349
350     int numUsersListItems = items.size();
351
352     // make the request again with limit
353     params = new HashMap<String, Object>();
354     params.put("limit", numUsersListItems-1);
355
356     response = arv.call("users", "list", params);
357
358     parser = new JSONParser();
359     obj = parser.parse(response);
360     jsonObject = (JSONObject) obj;
361
362     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
363
364     items = (List)jsonObject.get("items");
365     assertNotNull("expected users list items", items);
366     assertTrue("expected at least one item in users list", items.size()>0);
367
368     int numUsersListItems2 = items.size();
369     assertEquals ("Got more users than requested", numUsersListItems-1, numUsersListItems2);
370   }
371
372   @Test
373   public void testGetLinksWithFilters() throws Exception {
374     Arvados arv = new Arvados("arvados", "v1");
375
376     Map<String, Object> params = new HashMap<String, Object>();
377
378     String response = arv.call("links", "list", params);
379     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
380
381     String[] filters = new String[3];
382     filters[0] = "name";
383     filters[1] = "is_a";
384     filters[2] = "can_manage";
385     
386     params.put("filters", filters);
387     
388     response = arv.call("links", "list", params);
389     
390     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
391     assertFalse("Expected no can_manage in response", response.contains("\"name\":\"can_manage\""));
392   }
393
394   @Test
395   public void testGetLinksWithFiltersAsList() throws Exception {
396     Arvados arv = new Arvados("arvados", "v1");
397
398     Map<String, Object> params = new HashMap<String, Object>();
399
400     String response = arv.call("links", "list", params);
401     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
402
403     List<String> filters = new ArrayList<String>();
404     filters.add("name");
405     filters.add("is_a");
406     filters.add("can_manage");
407     
408     params.put("filters", filters);
409     
410     response = arv.call("links", "list", params);
411     
412     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
413     assertFalse("Expected no can_manage in response", response.contains("\"name\":\"can_manage\""));
414   }
415
416   @Test
417   public void testGetLinksWithWhereClause() throws Exception {
418     Arvados arv = new Arvados("arvados", "v1");
419
420     Map<String, Object> params = new HashMap<String, Object>();
421
422     String response = arv.call("links", "list", params);
423     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
424
425     Map<String, String> where = new HashMap<String, String>();
426     where.put("where", "updated_at > '2014-05-01'");
427     
428     params.put("where", where);
429     
430     response = arv.call("links", "list", params);
431     
432     assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
433   }
434
435 }