1 package org.arvados.sdk.java;
4 import java.util.HashMap;
10 import static org.junit.Assert.*;
12 import com.google.api.services.discovery.model.RestDescription;
13 import com.google.api.services.discovery.model.RestResource;
15 import org.json.simple.JSONObject;
16 import org.json.simple.parser.JSONParser;
19 * Unit test for Arvados.
21 public class ArvadosTest {
24 * test discover method
28 public void testDiscover() throws Exception {
29 Arvados arv = new Arvados("arvados", "v1");
31 RestDescription restDescription = arv.discover();
33 // The discover method returns the supported methods
34 Map<String, RestResource> resources = restDescription.getResources();
35 assertNotNull("Expected resources", resources);
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());
41 assertTrue("Root URL expected to match ARVADOS_API_HOST env paramdeter",
42 restDescription.getRootUrl().contains(System.getenv().get("ARVADOS_API_HOST")));
50 public void testCallUsersList() throws Exception {
51 Arvados arv = new Arvados("arvados", "v1");
53 Map<String, Object> params = new HashMap<String, Object>();
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"));
59 JSONParser parser = new JSONParser();
60 Object obj = parser.parse(response);
61 JSONObject jsonObject = (JSONObject) obj;
63 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
69 JSONObject firstUser = (JSONObject)items.get(0);
70 assertNotNull ("Expcted at least one user", firstUser);
72 assertEquals("Expected kind to be user", "arvados#user", firstUser.get("kind"));
73 assertNotNull("Expected uuid for first user", firstUser.get("uuid"));
77 * Test users.get <uuid> api
81 public void testCallUsersGet() throws Exception {
82 Arvados arv = new Arvados("arvados", "v1");
84 // call user.system and get uuid of this user
85 Map<String, Object> params = new HashMap<String, Object>();
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);
95 JSONObject firstUser = (JSONObject)items.get(0);
96 String userUuid = (String)firstUser.get("uuid");
98 // invoke users.get with the system user uuid
99 params = new HashMap<String, Object>();
100 params.put("uuid", userUuid);
102 response = arv.call("users", "get", params);
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"));
111 * Test users.create api
115 public void testCreateUser() throws Exception {
116 Arvados arv = new Arvados("arvados", "v1");
118 File file = new File(getClass().getResource( "/create_user.json" ).toURI());
119 String filePath = file.getPath();
121 Map<String, Object> params = new HashMap<String, Object>();
122 params.put("user", "{}");
123 String response = arv.call("users", "create", params);
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"));
132 public void testCreateUserWithMissingRequiredParam() throws Exception {
133 Arvados arv = new Arvados("arvados", "v1");
135 Map<String, Object> params = new HashMap<String, Object>();
137 Exception caught = null;
139 arv.call("users", "create", params);
140 } catch (Exception e) {
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"));
150 * Test users.create api
154 public void testCreateAndUpdateUser() throws Exception {
155 Arvados arv = new Arvados("arvados", "v1");
157 Map<String, Object> params = new HashMap<String, Object>();
158 params.put("user", "{}");
159 String response = arv.call("users", "create", params);
161 JSONParser parser = new JSONParser();
162 JSONObject jsonObject = (JSONObject) parser.parse(response);
163 assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
165 Object uuid = jsonObject.get("uuid");
166 assertNotNull("Expected uuid for first user", uuid);
169 params = new HashMap<String, Object>();
170 params.put("user", "{}");
171 params.put("uuid", uuid);
172 response = arv.call("users", "update", params);
174 parser = new JSONParser();
175 jsonObject = (JSONObject) parser.parse(response);
176 assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
178 uuid = jsonObject.get("uuid");
179 assertNotNull("Expected uuid for first user", uuid);
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 String filePath = file.getPath();
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);
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);
280 params = new HashMap<String, Object>();
281 params.put("uuid", uuid);
282 response = arv.call("pipeline_templates", "get", params);
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"));
292 * Test users.list api
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");
301 Arvados arv = new Arvados("arvados", "v1", token, host, hostInsecure);
303 Map<String, Object> params = new HashMap<String, Object>();
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"));
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"));
316 * Test users.list api
320 public void testCallUsersListWithLimit() throws Exception {
321 Arvados arv = new Arvados("arvados", "v1");
323 Map<String, Object> params = new HashMap<String, Object>();
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"));
329 JSONParser parser = new JSONParser();
330 Object obj = parser.parse(response);
331 JSONObject jsonObject = (JSONObject) obj;
333 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
339 int numUsersListItems = items.size();
341 // make the request again with limit
342 params = new HashMap<String, Object>();
343 params.put("limit", numUsersListItems-1);
345 response = arv.call("users", "list", params);
347 parser = new JSONParser();
348 obj = parser.parse(response);
349 jsonObject = (JSONObject) obj;
351 assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
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);
357 int numUsersListItems2 = items.size();
358 assertEquals ("Got more users than requested", numUsersListItems-1, numUsersListItems2);