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