2525: Add pipeline template create testing. Use maven resources for input files.
[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.ArrayList;
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   @Test(expected=Exception.class)
24   public void testMainWithNoParams() throws Exception {
25     String[] args = new String[0];
26     Arvados.main(args);
27   }
28
29   @Test(expected=Exception.class)
30   public void testHelp() throws Exception {
31     String[] args = new String[1];
32
33     args[0] = "help";
34     Arvados.help(args); // expect this to succeed with no problems
35
36     args = new String[2];
37
38     args[0] = "help";
39     args[1] = "call";
40     Arvados.main(args); // call via main
41     
42     args[0] = "help";
43     args[1] = "discover";
44     Arvados.help(args); // call help directly
45
46     args[0] = "help";
47     args[1] = "unknown";
48     Arvados.help(args); // expect exception
49   }
50
51   /**
52    * test discover method
53    * @throws Exception
54    */
55   @Test
56   public void testDiscover() throws Exception {
57     Arvados arv = new Arvados("arvados");
58
59     List<String> params = new ArrayList<String>();
60     params.add("discover");
61     params.add("arvados");
62     params.add("v1");
63
64     RestDescription restDescription = arv.discover(params);
65
66     // The discover method returns the supported methods
67     Map<String, RestResource> resources = restDescription.getResources();
68     assertNotNull("Expected resources", resources);
69
70     Object users = resources.get("users");
71     assertNotNull ("Expected users.list method", users);
72     assertEquals("Exepcted users.list to be a RestResource type", RestResource.class, users.getClass());
73
74     assertTrue("Root URL expected to match ARVADOS_API_HOST env paramdeter", 
75         restDescription.getRootUrl().contains(System.getenv().get("ARVADOS_API_HOST")));
76   }
77
78   /**
79    * Test users.list api
80    * @throws Exception
81    */
82   @Test
83   public void testCallUsersList() throws Exception {
84     Arvados arv = new Arvados("arvados");
85
86     List<String> params = new ArrayList<String>();
87     params.add("call");
88     params.add("arvados");
89     params.add("v1");
90     params.add("users.list");
91
92     String response = arv.call(params);
93     assertTrue("Expected users.list in response", response.contains("arvados#userList"));
94     assertTrue("Expected users.list in response", response.contains("uuid"));
95
96     JSONParser parser = new JSONParser();
97     Object obj = parser.parse(response);
98     JSONObject jsonObject = (JSONObject) obj;
99
100     assertEquals("Expected kind to be users.list", "arvados#userList", jsonObject.get("kind"));
101
102     List items = (List)jsonObject.get("items");
103     assertNotNull("expected users list items", items);
104     assertTrue("expected at least one item in users list", items.size()>0);
105
106     JSONObject firstUser = (JSONObject)items.get(0);
107     assertNotNull ("Expcted at least one user", firstUser);
108
109     assertEquals("Expected kind to be user", "arvados#user", firstUser.get("kind"));
110     assertNotNull("Expected uuid for first user", firstUser.get("uuid"));
111   }
112
113   /**
114    * Test users.get <uuid> api
115    * @throws Exception
116    */
117   @Test
118   public void testCallUsersGet() throws Exception {
119     Arvados arv = new Arvados("arvados");
120
121     // call user.system and get uuid of this user
122     List<String> params = new ArrayList<String>();
123     params.add("call");
124     params.add("arvados");
125     params.add("v1");
126     params.add("users.list");
127
128     String response = arv.call(params);
129     JSONParser parser = new JSONParser();
130     Object obj = parser.parse(response);
131     JSONObject jsonObject = (JSONObject) obj;
132     assertNotNull("expected users list", jsonObject);
133     List items = (List)jsonObject.get("items");
134     assertNotNull("expected users list items", items);
135
136     JSONObject firstUser = (JSONObject)items.get(0);
137     String userUuid = (String)firstUser.get("uuid");
138
139     // invoke users.get with the system user uuid
140     params = new ArrayList<String>();
141     params.add("call");
142     params.add("arvados");
143     params.add("v1");
144     params.add("users.get");
145     params.add(userUuid);
146
147     response = arv.call(params);
148
149     //JSONParser parser = new JSONParser();
150     jsonObject = (JSONObject) parser.parse(response);;
151     assertNotNull("Expected uuid for first user", jsonObject.get("uuid"));
152     assertEquals("Expected system user uuid", userUuid, jsonObject.get("uuid"));
153   }
154
155   /**
156    * Test users.create api
157    * @throws Exception
158    */
159   @Test
160   public void testCreateUser() throws Exception {
161     Arvados arv = new Arvados("arvados");
162
163     File file = new File(getClass().getResource( "/create_user.json" ).toURI());
164     String filePath = file.getPath();
165
166     List<String> params = new ArrayList<String>();
167     params.add("call");
168     params.add("arvados");
169     params.add("v1");
170     params.add("users.create");
171     params.add(filePath);
172     String response = arv.call(params);
173
174     JSONParser parser = new JSONParser();
175     JSONObject jsonObject = (JSONObject) parser.parse(response);
176     assertEquals("Expected kind to be user", "arvados#user", jsonObject.get("kind"));
177     assertNotNull("Expected uuid for first user", jsonObject.get("uuid"));
178   }
179
180   /**
181    * Test unsupported api version api
182    * @throws Exception
183    */
184   @Test
185   public void testUnsupportedApiName() throws Exception {
186     Arvados arv = new Arvados("not_arvados");
187
188     List<String> params = new ArrayList<String>();
189     params.add("call");
190     params.add("not_arvados");
191     params.add("v1");
192     params.add("users.list");
193
194     Exception caught = null;
195     try {
196       arv.call(params);
197     } catch (Exception e) {
198       caught = e;
199     }
200
201     assertNotNull ("expected exception", caught);
202     assertTrue ("Expected 404 when unsupported api 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 testUnsupportedVersion() throws Exception {
211     Arvados arv = new Arvados("arvados");
212
213     List<String> params = new ArrayList<String>();
214     params.add("call");
215     params.add("arvados");
216     params.add("v2");         // no such version
217     params.add("users.list");
218
219     Exception caught = null;
220     try {
221       arv.call(params);
222     } catch (Exception e) {
223       caught = e;
224     }
225
226     assertNotNull ("expected exception", caught);
227     assertTrue ("Expected 404 when unsupported version is used", caught.getMessage().contains("404 Not Found"));
228   }
229   
230   /**
231    * Test unsupported api version api
232    * @throws Exception
233    */
234   @Test
235   public void testCallWithTooFewParams() throws Exception {
236     Arvados arv = new Arvados("arvados");
237
238     List<String> params = new ArrayList<String>();
239     params.add("call");
240     params.add("arvados");
241     params.add("v1");
242
243     Exception caught = null;
244     try {
245       arv.call(params);
246     } catch (Exception e) {
247       caught = e;
248     }
249
250     assertNotNull ("expected exception", caught);
251     assertTrue ("Expected ERROR: missing method name", caught.getMessage().contains("ERROR: missing method name"));
252   }
253   
254   /**
255    * Test pipeline_tempates.create api
256    * @throws Exception
257    */
258   @Test
259   public void testCreateAndGetPipelineTemplate() throws Exception {
260     Arvados arv = new Arvados("arvados");
261
262     File file = new File(getClass().getResource( "/first_pipeline.json" ).toURI());
263     String filePath = file.getPath();
264
265     List<String> params = new ArrayList<String>();
266     params.add("call");
267     params.add("arvados");
268     params.add("v1");
269     params.add("pipeline_templates.create");
270     params.add(filePath);
271     String response = arv.call(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 ArrayList<String>();
281     params.add("call");
282     params.add("arvados");
283     params.add("v1");
284     params.add("pipeline_templates.get");
285     params.add(uuid);
286     response = arv.call(params);
287
288     parser = new JSONParser();
289     jsonObject = (JSONObject) parser.parse(response);
290     assertEquals("Expected kind to be user", "arvados#pipelineTemplate", jsonObject.get("kind"));
291     assertEquals("Expected uuid for pipeline template", uuid, jsonObject.get("uuid"));
292   }
293
294
295 }