2525: call method invocation should match arvados api documentation, instead of disco...
[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.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.junit.Test;
9
10 import static org.junit.Assert.*;
11
12 import com.google.api.services.discovery.model.RestDescription;
13 import com.google.api.services.discovery.model.RestResource;
14
15 import org.json.simple.JSONObject;
16 import org.json.simple.parser.JSONParser;
17
18 /**
19  * Unit test for Arvados.
20  */
21 public class ArvadosTest {
22
23   /**
24    * test discover method
25    * @throws Exception
26    */
27   @Test
28   public void testDiscover() throws Exception {
29     Arvados arv = new Arvados("arvados", "v1");
30
31     RestDescription restDescription = arv.discover();
32
33     // The discover method returns the supported methods
34     Map<String, RestResource> resources = restDescription.getResources();
35     assertNotNull("Expected resources", resources);
36
37     Object users = resources.get("users");
38     assertNotNull ("Expected users.list method", users);
39     assertEquals("Exepcted users.list to be a RestResource type", RestResource.class, users.getClass());
40
41     assertTrue("Root URL expected to match ARVADOS_API_HOST env paramdeter", 
42         restDescription.getRootUrl().contains(System.getenv().get("ARVADOS_API_HOST")));
43   }
44
45   /**
46    * Test users.list api
47    * @throws Exception
48    */
49   @Test
50   public void testCallUsersList() throws Exception {
51     Arvados arv = new Arvados("arvados", "v1");
52
53     Map<String, Object> params = new HashMap<String, Object>();
54
55     String response = arv.call("users", "list", params);
56     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
57     assertTrue("Expected users.list in response", response.contains("uuid"));
58
59     JSONParser parser = new JSONParser();
60     Object obj = parser.parse(response);
61     JSONObject jsonObject = (JSONObject) obj;
62
63     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
64
65     List items = (List)jsonObject.get("items");
66     assertNotNull("expected users list items", items);
67     assertTrue("expected at least one item in users list", items.size()>0);
68
69     JSONObject firstUser = (JSONObject)items.get(0);
70     assertNotNull ("Expcted at least one user", firstUser);
71
72     assertEquals("Expected kind to be user", "arvados#user", firstUser.get("kind"));
73     assertNotNull("Expected uuid for first user", firstUser.get("uuid"));
74   }
75
76   /**
77    * Test users.get <uuid> api
78    * @throws Exception
79    */
80   @Test
81   public void testCallUsersGet() throws Exception {
82     Arvados arv = new Arvados("arvados", "v1");
83
84     // call user.system and get uuid of this user
85     Map<String, Object> params = new HashMap<String, Object>();
86
87     String response = arv.call("users", "list", params);
88     JSONParser parser = new JSONParser();
89     Object obj = parser.parse(response);
90     JSONObject jsonObject = (JSONObject) obj;
91     assertNotNull("expected users list", jsonObject);
92     List items = (List)jsonObject.get("items");
93     assertNotNull("expected users list items", items);
94
95     JSONObject firstUser = (JSONObject)items.get(0);
96     String userUuid = (String)firstUser.get("uuid");
97
98     // invoke users.get with the system user uuid
99     params = new HashMap<String, Object>();
100     params.put("uuid", userUuid);
101
102     response = arv.call("users", "get", params);
103
104     //JSONParser parser = new JSONParser();
105     jsonObject = (JSONObject) parser.parse(response);;
106     assertNotNull("Expected uuid for first user", jsonObject.get("uuid"));
107     assertEquals("Expected system user uuid", userUuid, jsonObject.get("uuid"));
108   }
109
110   /**
111    * Test users.create api
112    * @throws Exception
113    */
114   @Test
115   public void testCreateUser() throws Exception {
116     Arvados arv = new Arvados("arvados", "v1");
117
118     File file = new File(getClass().getResource( "/create_user.json" ).toURI());
119     String filePath = file.getPath();
120
121     Map<String, Object> params = new HashMap<String, Object>();
122     params.put("user", "{}");
123     String response = arv.call("users", "create", params);
124
125     JSONParser parser = new JSONParser();
126     JSONObject jsonObject = (JSONObject) parser.parse(response);
127     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
128     assertNotNull("Expected uuid for first user", jsonObject.get("uuid"));
129   }
130
131   @Test
132   public void testCreateUserWithMissingRequiredParam() throws Exception {
133     Arvados arv = new Arvados("arvados", "v1");
134
135     Map<String, Object> params = new HashMap<String, Object>();
136
137     Exception caught = null;
138     try {
139       arv.call("users", "create", params);
140     } catch (Exception e) {
141       caught = e;
142     }
143
144     assertNotNull ("expected exception", caught);
145     assertTrue ("Expected POST method requires content object user", 
146         caught.getMessage().contains("ERROR: POST method requires content object user"));
147   }
148
149   /**
150    * Test users.create api
151    * @throws Exception
152    */
153   @Test
154   public void testCreateAndUpdateUser() throws Exception {
155     Arvados arv = new Arvados("arvados", "v1");
156
157     Map<String, Object> params = new HashMap<String, Object>();
158     params.put("user", "{}");
159     String response = arv.call("users", "create", params);
160
161     JSONParser parser = new JSONParser();
162     JSONObject jsonObject = (JSONObject) parser.parse(response);
163     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
164     
165     Object uuid = jsonObject.get("uuid");
166     assertNotNull("Expected uuid for first user", uuid);
167     
168     // update this user
169     params = new HashMap<String, Object>();
170     params.put("user", "{}");
171     params.put("uuid", uuid);
172     response = arv.call("users", "update", params);
173
174     parser = new JSONParser();
175     jsonObject = (JSONObject) parser.parse(response);
176     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
177     
178     uuid = jsonObject.get("uuid");
179     assertNotNull("Expected uuid for first user", uuid);
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     String filePath = file.getPath();
268
269     Map<String, Object> params = new HashMap<String, Object>();
270     params.put("pipeline_template", "{}");                          // TBD - read file and send
271     String response = arv.call("pipeline_templates", "create", params);
272
273     JSONParser parser = new JSONParser();
274     JSONObject jsonObject = (JSONObject) parser.parse(response);
275     assertEquals("Expected kind to be user", "arvados#pipelineTemplate", jsonObject.get("kind"));
276     String uuid = (String)jsonObject.get("uuid");
277     assertNotNull("Expected uuid for pipeline template", uuid);
278     
279     // get the pipeline
280     params = new HashMap<String, Object>();
281     params.put("uuid", uuid);
282     response = arv.call("pipeline_templates", "get", params);
283
284     parser = new JSONParser();
285     jsonObject = (JSONObject) parser.parse(response);
286     assertEquals("Expected kind to be user", "arvados#pipelineTemplate", jsonObject.get("kind"));
287     assertEquals("Expected uuid for pipeline template", uuid, jsonObject.get("uuid"));
288   }
289
290
291   /**
292    * Test users.list api
293    * @throws Exception
294    */
295   @Test
296   public void testArvadosWithTokenPassed() throws Exception {
297     String token = System.getenv().get("ARVADOS_API_TOKEN");
298     String host = System.getenv().get("ARVADOS_API_HOST");      
299     String hostInsecure = System.getenv().get("ARVADOS_API_HOST_INSECURE");
300
301     Arvados arv = new Arvados("arvados", "v1", token, host, hostInsecure);
302
303     Map<String, Object> params = new HashMap<String, Object>();
304
305     String response = arv.call("users", "list", params);
306     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
307     assertTrue("Expected users.list in response", response.contains("uuid"));
308
309     JSONParser parser = new JSONParser();
310     Object obj = parser.parse(response);
311     JSONObject jsonObject = (JSONObject) obj;
312     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
313   }
314
315   /**
316    * Test users.list api
317    * @throws Exception
318    */
319   @Test
320   public void testCallUsersListWithLimit() throws Exception {
321     Arvados arv = new Arvados("arvados", "v1");
322
323     Map<String, Object> params = new HashMap<String, Object>();
324
325     String response = arv.call("users", "list", params);
326     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
327     assertTrue("Expected users.list in response", response.contains("uuid"));
328
329     JSONParser parser = new JSONParser();
330     Object obj = parser.parse(response);
331     JSONObject jsonObject = (JSONObject) obj;
332
333     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
334
335     List items = (List)jsonObject.get("items");
336     assertNotNull("expected users list items", items);
337     assertTrue("expected at least one item in users list", items.size()>0);
338
339     int numUsersListItems = items.size();
340
341     // make the request again with limit
342     params = new HashMap<String, Object>();
343     params.put("limit", numUsersListItems-1);
344     
345     response = arv.call("users", "list", params);
346
347     parser = new JSONParser();
348     obj = parser.parse(response);
349     jsonObject = (JSONObject) obj;
350
351     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
352
353     items = (List)jsonObject.get("items");
354     assertNotNull("expected users list items", items);
355     assertTrue("expected at least one item in users list", items.size()>0);
356
357     int numUsersListItems2 = items.size();
358     assertEquals ("Got more users than requested", numUsersListItems-1, numUsersListItems2);
359   }
360
361 }