1 package org.arvados.sdk.java;
4 import java.io.FileInputStream;
5 import java.util.ArrayList;
6 import java.util.HashMap;
10 import org.junit.Test;
12 import static org.junit.Assert.*;
14 import org.json.simple.JSONObject;
15 import org.json.simple.parser.JSONParser;
18 * Unit test for Arvados.
20 public class ArvadosTest {
27 public void testCallUsersList() throws Exception {
28 Arvados arv = new Arvados("arvados", "v1");
30 Map<String, Object> params = new HashMap<String, Object>();
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"));
36 JSONParser parser = new JSONParser();
37 Object obj = parser.parse(response);
38 JSONObject jsonObject = (JSONObject) obj;
40 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
46 JSONObject firstUser = (JSONObject)items.get(0);
47 assertNotNull ("Expcted at least one user", firstUser);
49 assertEquals("Expected kind to be user", "arvados#user", firstUser.get("kind"));
50 assertNotNull("Expected uuid for first user", firstUser.get("uuid"));
54 * Test users.get <uuid> api
58 public void testCallUsersGet() throws Exception {
59 Arvados arv = new Arvados("arvados", "v1");
61 // call user.system and get uuid of this user
62 Map<String, Object> params = new HashMap<String, Object>();
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);
72 JSONObject firstUser = (JSONObject)items.get(0);
73 String userUuid = (String)firstUser.get("uuid");
75 // invoke users.get with the system user uuid
76 params = new HashMap<String, Object>();
77 params.put("uuid", userUuid);
79 response = arv.call("users", "get", params);
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"));
88 * Test users.create api
92 public void testCreateUser() throws Exception {
93 Arvados arv = new Arvados("arvados", "v1");
95 Map<String, Object> params = new HashMap<String, Object>();
96 params.put("user", "{}");
97 String response = arv.call("users", "create", params);
99 JSONParser parser = new JSONParser();
100 JSONObject jsonObject = (JSONObject) parser.parse(response);
101 assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
103 Object uuid = jsonObject.get("uuid");
104 assertNotNull("Expected uuid for first user", uuid);
107 params = new HashMap<String, Object>();
108 params.put("uuid", uuid);
109 response = arv.call("users", "delete", params);
111 // invoke users.get with the system user uuid
112 params = new HashMap<String, Object>();
113 params.put("uuid", uuid);
115 Exception caught = null;
117 arv.call("users", "get", params);
118 } catch (Exception e) {
122 assertNotNull ("expected exception", caught);
123 assertTrue ("Expected 404", caught.getMessage().contains("Path not found"));
127 public void testCreateUserWithMissingRequiredParam() throws Exception {
128 Arvados arv = new Arvados("arvados", "v1");
130 Map<String, Object> params = new HashMap<String, Object>();
132 Exception caught = null;
134 arv.call("users", "create", params);
135 } catch (Exception e) {
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"));
145 * Test users.create api
149 public void testCreateAndUpdateUser() throws Exception {
150 Arvados arv = new Arvados("arvados", "v1");
152 Map<String, Object> params = new HashMap<String, Object>();
153 params.put("user", "{}");
154 String response = arv.call("users", "create", params);
156 JSONParser parser = new JSONParser();
157 JSONObject jsonObject = (JSONObject) parser.parse(response);
158 assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
160 Object uuid = jsonObject.get("uuid");
161 assertNotNull("Expected uuid for first user", uuid);
164 params = new HashMap<String, Object>();
165 params.put("user", "{}");
166 params.put("uuid", uuid);
167 response = arv.call("users", "update", params);
169 parser = new JSONParser();
170 jsonObject = (JSONObject) parser.parse(response);
171 assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
173 uuid = jsonObject.get("uuid");
174 assertNotNull("Expected uuid for first user", uuid);
177 params = new HashMap<String, Object>();
178 params.put("uuid", uuid);
179 response = arv.call("users", "delete", params);
183 * Test unsupported api version api
187 public void testUnsupportedApiName() throws Exception {
188 Arvados arv = new Arvados("not_arvados", "v1");
190 Exception caught = null;
192 arv.call("users", "list", null);
193 } catch (Exception e) {
197 assertNotNull ("expected exception", caught);
198 assertTrue ("Expected 404 when unsupported api is used", caught.getMessage().contains("404 Not Found"));
202 * Test unsupported api version api
206 public void testUnsupportedVersion() throws Exception {
207 Arvados arv = new Arvados("arvados", "v2");
209 Exception caught = null;
211 arv.call("users", "list", null);
212 } catch (Exception e) {
216 assertNotNull ("expected exception", caught);
217 assertTrue ("Expected 404 when unsupported version is used", caught.getMessage().contains("404 Not Found"));
221 * Test unsupported api version api
225 public void testCallForNoSuchResrouce() throws Exception {
226 Arvados arv = new Arvados("arvados", "v1");
228 Exception caught = null;
230 arv.call("abcd", "list", null);
231 } catch (Exception e) {
235 assertNotNull ("expected exception", caught);
236 assertTrue ("Expected ERROR: 404 not found", caught.getMessage().contains("ERROR: resource not found"));
240 * Test unsupported api version api
244 public void testCallForNoSuchResrouceMethod() throws Exception {
245 Arvados arv = new Arvados("arvados", "v1");
247 Exception caught = null;
249 arv.call("users", "abcd", null);
250 } catch (Exception e) {
254 assertNotNull ("expected exception", caught);
255 assertTrue ("Expected ERROR: 404 not found", caught.getMessage().contains("ERROR: method not found"));
259 * Test pipeline_tempates.create api
263 public void testCreateAndGetPipelineTemplate() throws Exception {
264 Arvados arv = new Arvados("arvados", "v1");
266 File file = new File(getClass().getResource( "/first_pipeline.json" ).toURI());
267 byte[] data = new byte[(int)file.length()];
269 FileInputStream is = new FileInputStream(file);
272 }catch(Exception e) {
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);
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);
287 params = new HashMap<String, Object>();
288 params.put("uuid", uuid);
289 response = arv.call("pipeline_templates", "get", params);
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"));
297 params = new HashMap<String, Object>();
298 params.put("uuid", uuid);
299 response = arv.call("pipeline_templates", "delete", params);
303 * Test users.list api
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");
312 Arvados arv = new Arvados("arvados", "v1", token, host, hostInsecure);
314 Map<String, Object> params = new HashMap<String, Object>();
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"));
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"));
327 * Test users.list api
331 public void testCallUsersListWithLimit() throws Exception {
332 Arvados arv = new Arvados("arvados", "v1");
334 Map<String, Object> params = new HashMap<String, Object>();
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"));
340 JSONParser parser = new JSONParser();
341 Object obj = parser.parse(response);
342 JSONObject jsonObject = (JSONObject) obj;
344 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
350 int numUsersListItems = items.size();
352 // make the request again with limit
353 params = new HashMap<String, Object>();
354 params.put("limit", numUsersListItems-1);
356 response = arv.call("users", "list", params);
358 parser = new JSONParser();
359 obj = parser.parse(response);
360 jsonObject = (JSONObject) obj;
362 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
368 int numUsersListItems2 = items.size();
369 assertEquals ("Got more users than requested", numUsersListItems-1, numUsersListItems2);
373 public void testGetLinksWithFilters() throws Exception {
374 Arvados arv = new Arvados("arvados", "v1");
376 Map<String, Object> params = new HashMap<String, Object>();
378 String response = arv.call("links", "list", params);
379 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
381 String[] filters = new String[3];
384 filters[2] = "can_manage";
386 params.put("filters", filters);
388 response = arv.call("links", "list", params);
390 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
391 assertFalse("Expected no can_manage in response", response.contains("\"name\":\"can_manage\""));
395 public void testGetLinksWithFiltersAsList() throws Exception {
396 Arvados arv = new Arvados("arvados", "v1");
398 Map<String, Object> params = new HashMap<String, Object>();
400 String response = arv.call("links", "list", params);
401 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
403 List<String> filters = new ArrayList<String>();
406 filters.add("can_manage");
408 params.put("filters", filters);
410 response = arv.call("links", "list", params);
412 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
413 assertFalse("Expected no can_manage in response", response.contains("\"name\":\"can_manage\""));
417 public void testGetLinksWithWhereClause() throws Exception {
418 Arvados arv = new Arvados("arvados", "v1");
420 Map<String, Object> params = new HashMap<String, Object>();
422 String response = arv.call("links", "list", params);
423 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));
425 Map<String, String> where = new HashMap<String, String>();
426 where.put("where", "updated_at > '2014-05-01'");
428 params.put("where", where);
430 response = arv.call("links", "list", params);
432 assertTrue("Expected links.list in response", response.contains("arvados#linkList"));