1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import static clarin.cmdi.componentregistry.rest.ComponentRegistryRestService.USERSPACE_PARAM; |
---|
4 | import static org.junit.Assert.assertEquals; |
---|
5 | import static org.junit.Assert.assertFalse; |
---|
6 | import static org.junit.Assert.assertNotNull; |
---|
7 | import static org.junit.Assert.assertTrue; |
---|
8 | import static org.junit.Assert.fail; |
---|
9 | |
---|
10 | import java.io.ByteArrayInputStream; |
---|
11 | import java.util.Date; |
---|
12 | import java.util.List; |
---|
13 | |
---|
14 | import javax.ws.rs.core.MediaType; |
---|
15 | import javax.ws.rs.core.Response.Status; |
---|
16 | |
---|
17 | import org.junit.Before; |
---|
18 | import org.junit.Test; |
---|
19 | import org.junit.runner.RunWith; |
---|
20 | import org.springframework.beans.factory.annotation.Autowired; |
---|
21 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
22 | import org.springframework.test.context.ContextConfiguration; |
---|
23 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
24 | |
---|
25 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
26 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
27 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
28 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory; |
---|
29 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; |
---|
30 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
31 | import clarin.cmdi.componentregistry.model.Comment; |
---|
32 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
33 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
34 | import clarin.cmdi.componentregistry.model.RegisterResponse; |
---|
35 | |
---|
36 | import com.sun.jersey.api.client.ClientResponse; |
---|
37 | import com.sun.jersey.api.client.UniformInterfaceException; |
---|
38 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
39 | |
---|
40 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
41 | @ContextConfiguration(locations = {"/applicationContext.xml"}) |
---|
42 | public class ComponentRegistryRestServiceTest extends ComponentRegistryRestServiceTestCase { |
---|
43 | |
---|
44 | @Autowired |
---|
45 | private ComponentRegistryFactory componentRegistryFactory; |
---|
46 | @Autowired |
---|
47 | private ComponentRegistryBeanFactory componentRegistryBeanFactory; |
---|
48 | @Autowired |
---|
49 | private JdbcTemplate jdbcTemplate; |
---|
50 | private ComponentRegistry testRegistry; |
---|
51 | |
---|
52 | @Before |
---|
53 | public void init() { |
---|
54 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
55 | createUserRecord(); |
---|
56 | // Get public component registry |
---|
57 | testRegistry = componentRegistryBeanFactory.getNewComponentRegistry(); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | protected String getApplicationContextFile() { |
---|
62 | return "classpath:applicationContext.xml"; |
---|
63 | } |
---|
64 | |
---|
65 | private ComponentRegistry getTestRegistry() { |
---|
66 | return testRegistry; |
---|
67 | } |
---|
68 | |
---|
69 | private String expectedUserId(String principal) { |
---|
70 | return getUserDao().getByPrincipalName(principal).getId().toString(); |
---|
71 | } |
---|
72 | |
---|
73 | private void fillUp() throws Exception { |
---|
74 | RegistryTestHelper.addProfile(getTestRegistry(), "profile2"); |
---|
75 | RegistryTestHelper.addProfile(getTestRegistry(), "profile1"); |
---|
76 | RegistryTestHelper.addComponent(getTestRegistry(), "component2"); |
---|
77 | RegistryTestHelper.addComponent(getTestRegistry(), "component1"); |
---|
78 | RegistryTestHelper.addComment(getTestRegistry(), "comment2", "profile1", "JUnit@test.com"); |
---|
79 | RegistryTestHelper.addComment(getTestRegistry(), "comment1", "profile1", "JUnit@test.com"); |
---|
80 | RegistryTestHelper.addComment(getTestRegistry(), "comment3", "component1", "JUnit@test.com"); |
---|
81 | RegistryTestHelper.addComment(getTestRegistry(), "comment4", "component1", "JUnit@test.com"); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | ; |
---|
86 | |
---|
87 | @Test |
---|
88 | public void testGetRegisteredProfiles() throws Exception { |
---|
89 | fillUp(); |
---|
90 | RegistryTestHelper.addProfile(getTestRegistry(), "PROFILE2"); |
---|
91 | List<ProfileDescription> response = getResource().path("/registry/profiles").accept(MediaType.APPLICATION_XML).get( |
---|
92 | PROFILE_LIST_GENERICTYPE); |
---|
93 | assertEquals(3, response.size()); |
---|
94 | response = getResource().path("/registry/profiles").accept(MediaType.APPLICATION_JSON).get(PROFILE_LIST_GENERICTYPE); |
---|
95 | assertEquals(3, response.size()); |
---|
96 | assertEquals("profile1", response.get(0).getName()); |
---|
97 | assertEquals("PROFILE2", response.get(1).getName()); |
---|
98 | assertEquals("profile2", response.get(2).getName()); |
---|
99 | } |
---|
100 | |
---|
101 | @Test |
---|
102 | public void testGetRegisteredComponents() throws Exception { |
---|
103 | fillUp(); |
---|
104 | RegistryTestHelper.addComponent(getTestRegistry(), "COMPONENT2"); |
---|
105 | List<ComponentDescription> response = getResource().path("/registry/components").accept(MediaType.APPLICATION_XML).get( |
---|
106 | COMPONENT_LIST_GENERICTYPE); |
---|
107 | assertEquals(3, response.size()); |
---|
108 | response = getResource().path("/registry/components").accept(MediaType.APPLICATION_JSON).get(COMPONENT_LIST_GENERICTYPE); |
---|
109 | assertEquals(3, response.size()); |
---|
110 | assertEquals("component1", response.get(0).getName()); |
---|
111 | assertEquals("COMPONENT2", response.get(1).getName()); |
---|
112 | assertEquals("component2", response.get(2).getName()); |
---|
113 | } |
---|
114 | |
---|
115 | @Test |
---|
116 | public void testGetUserComponents() throws Exception { |
---|
117 | fillUp(); |
---|
118 | List<ComponentDescription> response = getUserComponents(); |
---|
119 | assertEquals(0, response.size()); |
---|
120 | response = getAuthenticatedResource(getResource().path("/registry/components")).accept(MediaType.APPLICATION_JSON).get( |
---|
121 | COMPONENT_LIST_GENERICTYPE); |
---|
122 | assertEquals("Get public components", 2, response.size()); |
---|
123 | ClientResponse cResponse = getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true").accept( |
---|
124 | MediaType.APPLICATION_JSON).get(ClientResponse.class); |
---|
125 | assertEquals("Trying to get userspace without credentials", 500, cResponse.getStatus()); |
---|
126 | } |
---|
127 | |
---|
128 | @Test |
---|
129 | public void testGetRegisteredComponent() throws Exception { |
---|
130 | fillUp(); |
---|
131 | CMDComponentSpec component = getResource().path("/registry/components/clarin.eu:cr1:component1").accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class); |
---|
132 | assertNotNull(component); |
---|
133 | assertEquals("Access", component.getCMDComponent().get(0).getName()); |
---|
134 | component = getResource().path("/registry/components/clarin.eu:cr1:component2").accept(MediaType.APPLICATION_XML).get( |
---|
135 | CMDComponentSpec.class); |
---|
136 | assertNotNull(component); |
---|
137 | assertEquals("Access", component.getCMDComponent().get(0).getName()); |
---|
138 | |
---|
139 | assertEquals("clarin.eu:cr1:component2", component.getHeader().getID()); |
---|
140 | assertEquals("component2", component.getHeader().getName()); |
---|
141 | assertEquals("Test Description", component.getHeader().getDescription()); |
---|
142 | } |
---|
143 | |
---|
144 | @Test |
---|
145 | public void testGetRegisteredCommentsInProfile() throws Exception { |
---|
146 | fillUp(); |
---|
147 | RegistryTestHelper.addComment(getTestRegistry(), "COMMENT1", "profile1", "JUnit@test.com"); |
---|
148 | List<Comment> response = getResource().path("/registry/profiles/profile1/comments/").accept(MediaType.APPLICATION_XML). |
---|
149 | get(COMMENT_LIST_GENERICTYPE); |
---|
150 | assertEquals(3, response.size()); |
---|
151 | response = getResource().path("/registry/profiles/profile1/comments").accept(MediaType.APPLICATION_JSON).get( |
---|
152 | COMMENT_LIST_GENERICTYPE); |
---|
153 | assertEquals(3, response.size()); |
---|
154 | assertEquals("comment2", response.get(0).getComment()); |
---|
155 | assertEquals("comment1", response.get(1).getComment()); |
---|
156 | assertEquals("COMMENT1", response.get(2).getComment()); |
---|
157 | } |
---|
158 | |
---|
159 | @Test |
---|
160 | public void testGetRegisteredCommentsInComponent() throws Exception { |
---|
161 | fillUp(); |
---|
162 | RegistryTestHelper.addComment(getTestRegistry(), "COMMENT2", "component1", "JUnit@test.com"); |
---|
163 | List<Comment> response = getResource().path("/registry/components/component1/comments/").accept(MediaType.APPLICATION_XML). |
---|
164 | get(COMMENT_LIST_GENERICTYPE); |
---|
165 | assertEquals(3, response.size()); |
---|
166 | response = getResource().path("/registry/components/component1/comments").accept(MediaType.APPLICATION_JSON).get( |
---|
167 | COMMENT_LIST_GENERICTYPE); |
---|
168 | assertEquals(3, response.size()); |
---|
169 | assertEquals("comment3", response.get(0).getComment()); |
---|
170 | assertEquals("comment4", response.get(1).getComment()); |
---|
171 | assertEquals("COMMENT2", response.get(2).getComment()); |
---|
172 | } |
---|
173 | |
---|
174 | @Test |
---|
175 | public void testGetSpecifiedCommentInComponent() throws Exception { |
---|
176 | fillUp(); |
---|
177 | Comment comment = getResource().path("/registry/components/component1/comments/2").accept(MediaType.APPLICATION_JSON).get(Comment.class); |
---|
178 | assertNotNull(comment); |
---|
179 | assertEquals("comment3", comment.getComment()); |
---|
180 | assertEquals("2", comment.getId()); |
---|
181 | comment = getResource().path("/registry/components/component1/comments/3").accept(MediaType.APPLICATION_JSON).get(Comment.class); |
---|
182 | assertNotNull(comment); |
---|
183 | assertEquals("comment4", comment.getComment()); |
---|
184 | assertEquals("3", comment.getId()); |
---|
185 | assertEquals("component1", comment.getComponentDescriptionId()); |
---|
186 | } |
---|
187 | |
---|
188 | @Test |
---|
189 | public void testGetSpecifiedCommentInProfile() throws Exception { |
---|
190 | fillUp(); |
---|
191 | Comment comment = getResource().path("/registry/profiles/profile1/comments/0").accept(MediaType.APPLICATION_JSON).get(Comment.class); |
---|
192 | assertNotNull(comment); |
---|
193 | assertEquals("comment2", comment.getComment()); |
---|
194 | assertEquals("0", comment.getId()); |
---|
195 | comment = getResource().path("/registry/profiles/profile1/comments/1").accept(MediaType.APPLICATION_JSON).get(Comment.class); |
---|
196 | assertNotNull(comment); |
---|
197 | assertEquals("comment1", comment.getComment()); |
---|
198 | assertEquals("1", comment.getId()); |
---|
199 | assertEquals("profile1", comment.getProfileDescriptionId()); |
---|
200 | } |
---|
201 | |
---|
202 | @Test |
---|
203 | public void testDeleteCommentFromComponent() throws Exception { |
---|
204 | fillUp(); |
---|
205 | List<Comment> comments = getResource().path("/registry/components/component1/comments").get(COMMENT_LIST_GENERICTYPE); |
---|
206 | assertEquals(2, comments.size()); |
---|
207 | Comment aComment = getResource().path("/registry/components/component1/comments/2").get(Comment.class); |
---|
208 | assertNotNull(aComment); |
---|
209 | ClientResponse response = getAuthenticatedResource("/registry/components/component1/comments/2").delete(ClientResponse.class); |
---|
210 | assertEquals(200, response.getStatus()); |
---|
211 | |
---|
212 | comments = getResource().path("/registry/components/component1/comments/").get(COMMENT_LIST_GENERICTYPE); |
---|
213 | assertEquals(1, comments.size()); |
---|
214 | |
---|
215 | response = getAuthenticatedResource("/registry/components/component1/comments/3").delete(ClientResponse.class); |
---|
216 | assertEquals(200, response.getStatus()); |
---|
217 | |
---|
218 | comments = getResource().path("/registry/components/component1/comments").get(COMMENT_LIST_GENERICTYPE); |
---|
219 | assertEquals(0, comments.size()); |
---|
220 | } |
---|
221 | |
---|
222 | @Test |
---|
223 | public void testDeleteCommentFromProfile() throws Exception { |
---|
224 | fillUp(); |
---|
225 | List<Comment> comments = getResource().path("/registry/profiles/profile1/comments").get(COMMENT_LIST_GENERICTYPE); |
---|
226 | assertEquals(2, comments.size()); |
---|
227 | Comment aComment = getResource().path("/registry/profiles/profile1/comments/0").get(Comment.class); |
---|
228 | assertNotNull(aComment); |
---|
229 | ClientResponse response = getAuthenticatedResource("/registry/profiles/profile1/comments/0").delete(ClientResponse.class); |
---|
230 | assertEquals(200, response.getStatus()); |
---|
231 | |
---|
232 | comments = getResource().path("/registry/profiles/profile1/comments/").get(COMMENT_LIST_GENERICTYPE); |
---|
233 | assertEquals(1, comments.size()); |
---|
234 | |
---|
235 | response = getAuthenticatedResource("/registry/profiles/profile1/comments/1").delete(ClientResponse.class); |
---|
236 | assertEquals(200, response.getStatus()); |
---|
237 | |
---|
238 | comments = getResource().path("/registry/profiles/profile1/comments").get(COMMENT_LIST_GENERICTYPE); |
---|
239 | assertEquals(0, comments.size()); |
---|
240 | } |
---|
241 | |
---|
242 | @Test |
---|
243 | public void testDeleteRegisteredComponent() throws Exception { |
---|
244 | fillUp(); |
---|
245 | List<ComponentDescription> components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
246 | assertEquals(2, components.size()); |
---|
247 | CMDComponentSpec profile = getResource().path("/registry/components/clarin.eu:cr1:component1").get(CMDComponentSpec.class); |
---|
248 | assertNotNull(profile); |
---|
249 | ClientResponse response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1").delete(ClientResponse.class); |
---|
250 | assertEquals(200, response.getStatus()); |
---|
251 | |
---|
252 | components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
253 | assertEquals(1, components.size()); |
---|
254 | |
---|
255 | response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component2").delete(ClientResponse.class); |
---|
256 | assertEquals(200, response.getStatus()); |
---|
257 | |
---|
258 | components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
259 | assertEquals(0, components.size()); |
---|
260 | |
---|
261 | response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1").delete(ClientResponse.class); |
---|
262 | assertEquals(200, response.getStatus()); |
---|
263 | } |
---|
264 | |
---|
265 | @Test |
---|
266 | public void testDeleteRegisteredComponentStillUsed() throws Exception { |
---|
267 | String content = ""; |
---|
268 | content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
269 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
270 | content += " <Header/>\n"; |
---|
271 | content += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
272 | content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
273 | content += " </CMD_Component>\n"; |
---|
274 | content += "</CMD_ComponentSpec>\n"; |
---|
275 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(getTestRegistry(), "XXX1", content); |
---|
276 | |
---|
277 | content = ""; |
---|
278 | content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
279 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
280 | content += " <Header/>\n"; |
---|
281 | content += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
282 | content += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
283 | content += " </CMD_Component>\n"; |
---|
284 | content += " </CMD_Component>\n"; |
---|
285 | content += "</CMD_ComponentSpec>\n"; |
---|
286 | ComponentDescription compDesc2 = RegistryTestHelper.addComponent(getTestRegistry(), "YYY1", content); |
---|
287 | |
---|
288 | content = ""; |
---|
289 | content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
290 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
291 | content += " <Header/>\n"; |
---|
292 | content += " <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
293 | content += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
294 | content += " </CMD_Component>\n"; |
---|
295 | content += " </CMD_Component>\n"; |
---|
296 | content += "</CMD_ComponentSpec>\n"; |
---|
297 | ProfileDescription profile = RegistryTestHelper.addProfile(getTestRegistry(), "TestProfile3", content); |
---|
298 | |
---|
299 | List<ComponentDescription> components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
300 | assertEquals(2, components.size()); |
---|
301 | |
---|
302 | ClientResponse response = getAuthenticatedResource("/registry/components/" + compDesc1.getId()).delete(ClientResponse.class); |
---|
303 | assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus()); |
---|
304 | assertEquals( |
---|
305 | "Still used by the following profiles: \n - TestProfile3\nStill used by the following components: \n - YYY1\nTry to change above mentioned references first.", |
---|
306 | response.getEntity(String.class)); |
---|
307 | |
---|
308 | components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
309 | assertEquals(2, components.size()); |
---|
310 | |
---|
311 | response = getAuthenticatedResource("/registry/profiles/" + profile.getId()).delete(ClientResponse.class); |
---|
312 | assertEquals(200, response.getStatus()); |
---|
313 | response = getAuthenticatedResource("/registry/components/" + compDesc2.getId()).delete(ClientResponse.class); |
---|
314 | assertEquals(200, response.getStatus()); |
---|
315 | response = getAuthenticatedResource("/registry/components/" + compDesc1.getId()).delete(ClientResponse.class); |
---|
316 | assertEquals(200, response.getStatus()); |
---|
317 | components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE); |
---|
318 | assertEquals(0, components.size()); |
---|
319 | } |
---|
320 | |
---|
321 | @Test |
---|
322 | public void testGetRegisteredProfile() throws Exception { |
---|
323 | fillUp(); |
---|
324 | CMDComponentSpec profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1").accept(MediaType.APPLICATION_JSON).get( |
---|
325 | CMDComponentSpec.class); |
---|
326 | assertNotNull(profile); |
---|
327 | assertEquals("Actor", profile.getCMDComponent().get(0).getName()); |
---|
328 | profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile2").accept(MediaType.APPLICATION_XML).get( |
---|
329 | CMDComponentSpec.class); |
---|
330 | assertNotNull(profile); |
---|
331 | assertEquals("Actor", profile.getCMDComponent().get(0).getName()); |
---|
332 | |
---|
333 | assertEquals("clarin.eu:cr1:profile2", profile.getHeader().getID()); |
---|
334 | assertEquals("profile2", profile.getHeader().getName()); |
---|
335 | assertEquals("Test Description", profile.getHeader().getDescription()); |
---|
336 | |
---|
337 | try { |
---|
338 | profile = getResource().path("/registry/profiles/clarin.eu:cr1:profileXXXX").accept(MediaType.APPLICATION_XML).get( |
---|
339 | CMDComponentSpec.class); |
---|
340 | fail("Exception should have been thrown resource does not exist, HttpStatusCode 204"); |
---|
341 | } catch (UniformInterfaceException e) { |
---|
342 | assertEquals(204, e.getResponse().getStatus()); |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | @Test |
---|
347 | public void testGetRegisteredProfileRawData() throws Exception { |
---|
348 | fillUp(); |
---|
349 | String profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/xsd").accept(MediaType.TEXT_XML).get(String.class); |
---|
350 | assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema")); |
---|
351 | assertTrue(profile.endsWith("</xs:schema>")); |
---|
352 | |
---|
353 | profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/xml").accept(MediaType.TEXT_XML).get(String.class); |
---|
354 | assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec")); |
---|
355 | assertTrue(profile.endsWith("</CMD_ComponentSpec>\n")); |
---|
356 | assertTrue(profile.contains("xsi:schemaLocation")); |
---|
357 | |
---|
358 | try { |
---|
359 | getResource().path("/registry/components/clarin.eu:cr1:component1/xsl").accept(MediaType.TEXT_XML).get(String.class); |
---|
360 | fail("Should have thrown exception, unsupported path parameter"); |
---|
361 | } catch (UniformInterfaceException e) {//server error |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | @Test |
---|
366 | public void testPrivateProfileXsd() throws Exception { |
---|
367 | FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent()); |
---|
368 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
369 | assertTrue(response.isProfile()); |
---|
370 | assertEquals(1, getUserProfiles().size()); |
---|
371 | AbstractDescription desc = response.getDescription(); |
---|
372 | String profile = getResource().path("/registry/profiles/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(String.class); |
---|
373 | assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema")); |
---|
374 | assertTrue(profile.endsWith("</xs:schema>")); |
---|
375 | } |
---|
376 | |
---|
377 | @Test |
---|
378 | public void testPrivateComponentXsd() throws Exception { |
---|
379 | FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent()); |
---|
380 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
381 | assertFalse(response.isProfile()); |
---|
382 | assertEquals(1, getUserComponents().size()); |
---|
383 | AbstractDescription desc = response.getDescription(); |
---|
384 | String profile = getResource().path("/registry/components/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(String.class); |
---|
385 | assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema")); |
---|
386 | assertTrue(profile.endsWith("</xs:schema>")); |
---|
387 | } |
---|
388 | |
---|
389 | @Test |
---|
390 | public void testDeleteRegisteredProfile() throws Exception { |
---|
391 | fillUp(); |
---|
392 | List<ProfileDescription> profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE); |
---|
393 | assertEquals(2, profiles.size()); |
---|
394 | CMDComponentSpec profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1").get(CMDComponentSpec.class); |
---|
395 | assertNotNull(profile); |
---|
396 | |
---|
397 | ClientResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1").delete(ClientResponse.class); |
---|
398 | assertEquals(200, response.getStatus()); |
---|
399 | |
---|
400 | profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE); |
---|
401 | assertEquals(1, profiles.size()); |
---|
402 | |
---|
403 | response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile2").delete(ClientResponse.class); |
---|
404 | assertEquals(200, response.getStatus()); |
---|
405 | |
---|
406 | profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE); |
---|
407 | assertEquals(0, profiles.size()); |
---|
408 | |
---|
409 | response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1").delete(ClientResponse.class); |
---|
410 | assertEquals(200, response.getStatus()); |
---|
411 | } |
---|
412 | |
---|
413 | @Test |
---|
414 | public void testGetRegisteredComponentRawData() throws Exception { |
---|
415 | fillUp(); |
---|
416 | String component = getResource().path("/registry/components/clarin.eu:cr1:component1/xsd").accept(MediaType.TEXT_XML).get( |
---|
417 | String.class); |
---|
418 | assertTrue(component.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema")); |
---|
419 | assertTrue(component.endsWith("</xs:schema>")); |
---|
420 | |
---|
421 | component = getResource().path("/registry/components/clarin.eu:cr1:component1/xml").accept(MediaType.TEXT_XML).get(String.class); |
---|
422 | assertTrue(component.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec")); |
---|
423 | assertTrue(component.endsWith("</CMD_ComponentSpec>\n")); |
---|
424 | assertTrue(component.contains("xsi:schemaLocation")); |
---|
425 | |
---|
426 | try { |
---|
427 | getResource().path("/registry/components/clarin.eu:cr1:component1/jpg").accept(MediaType.TEXT_XML).get(String.class); |
---|
428 | fail("Should have thrown exception, unsopported path parameter"); |
---|
429 | } catch (UniformInterfaceException e) { |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | @Test |
---|
434 | public void testRegisterProfile() throws Exception { |
---|
435 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
436 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getTestProfileContent(), |
---|
437 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
438 | form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1"); |
---|
439 | form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain"); |
---|
440 | form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile"); |
---|
441 | form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Test Group"); |
---|
442 | RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
443 | RegisterResponse.class, form); |
---|
444 | assertTrue(response.isProfile()); |
---|
445 | assertFalse(response.isInUserSpace()); |
---|
446 | ProfileDescription profileDesc = (ProfileDescription) response.getDescription(); |
---|
447 | assertNotNull(profileDesc); |
---|
448 | assertEquals("ProfileTest1", profileDesc.getName()); |
---|
449 | assertEquals("My Test Profile", profileDesc.getDescription()); |
---|
450 | assertEquals("TestDomain", profileDesc.getDomainName()); |
---|
451 | assertEquals("My Test Group", profileDesc.getGroupName()); |
---|
452 | assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId()); |
---|
453 | assertEquals("JUnit@test.com", profileDesc.getCreatorName()); |
---|
454 | assertTrue(profileDesc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "p_")); |
---|
455 | assertNotNull(profileDesc.getRegistrationDate()); |
---|
456 | assertEquals("http://localhost:9998/registry/profiles/" + profileDesc.getId(), profileDesc.getHref()); |
---|
457 | } |
---|
458 | |
---|
459 | @Test |
---|
460 | public void testPublishProfile() throws Exception { |
---|
461 | assertEquals("user registered profiles", 0, getUserProfiles().size()); |
---|
462 | assertEquals("public registered profiles", 0, getPublicProfiles().size()); |
---|
463 | FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent(), "Unpublished"); |
---|
464 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
465 | assertTrue(response.isProfile()); |
---|
466 | AbstractDescription desc = response.getDescription(); |
---|
467 | assertEquals("Unpublished", desc.getDescription()); |
---|
468 | assertEquals(1, getUserProfiles().size()); |
---|
469 | assertEquals(0, getPublicProfiles().size()); |
---|
470 | form = createFormData(RegistryTestHelper.getTestProfileContent("publishedName"), "Published"); |
---|
471 | response = getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId() + "/publish")).type( |
---|
472 | MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
473 | |
---|
474 | assertEquals(0, getUserProfiles().size()); |
---|
475 | List<ProfileDescription> profiles = getPublicProfiles(); |
---|
476 | assertEquals(1, profiles.size()); |
---|
477 | ProfileDescription profileDescription = profiles.get(0); |
---|
478 | assertNotNull(profileDescription.getId()); |
---|
479 | assertEquals(desc.getId(), profileDescription.getId()); |
---|
480 | assertEquals("http://localhost:9998/registry/profiles/" + desc.getId(), profileDescription.getHref()); |
---|
481 | assertEquals("Published", profileDescription.getDescription()); |
---|
482 | CMDComponentSpec spec = getPublicSpec(profileDescription); |
---|
483 | assertEquals("publishedName", spec.getCMDComponent().get(0).getName()); |
---|
484 | } |
---|
485 | |
---|
486 | private CMDComponentSpec getPublicSpec(AbstractDescription desc) { |
---|
487 | if (desc.isProfile()) { |
---|
488 | return getResource().path("/registry/profiles/" + desc.getId()).get(CMDComponentSpec.class); |
---|
489 | } else { |
---|
490 | return getResource().path("/registry/components/" + desc.getId()).get(CMDComponentSpec.class); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | @Test |
---|
495 | public void testPublishComponent() throws Exception { |
---|
496 | assertEquals(0, getUserComponents().size()); |
---|
497 | assertEquals(0, getPublicComponents().size()); |
---|
498 | FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent(), "Unpublished"); |
---|
499 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
500 | assertFalse(response.isProfile()); |
---|
501 | AbstractDescription desc = response.getDescription(); |
---|
502 | assertEquals("Unpublished", desc.getDescription()); |
---|
503 | assertEquals(1, getUserComponents().size()); |
---|
504 | assertEquals(0, getPublicComponents().size()); |
---|
505 | form = createFormData(RegistryTestHelper.getComponentTestContent("publishedName"), "Published"); |
---|
506 | response = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId() + "/publish")).type( |
---|
507 | MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
508 | |
---|
509 | assertEquals(0, getUserComponents().size()); |
---|
510 | List<ComponentDescription> components = getPublicComponents(); |
---|
511 | assertEquals(1, components.size()); |
---|
512 | ComponentDescription componentDescription = components.get(0); |
---|
513 | assertNotNull(componentDescription.getId()); |
---|
514 | assertEquals(desc.getId(), componentDescription.getId()); |
---|
515 | assertEquals("http://localhost:9998/registry/components/" + desc.getId(), componentDescription.getHref()); |
---|
516 | assertEquals("Published", componentDescription.getDescription()); |
---|
517 | CMDComponentSpec spec = getPublicSpec(componentDescription); |
---|
518 | assertEquals("publishedName", spec.getCMDComponent().get(0).getName()); |
---|
519 | } |
---|
520 | |
---|
521 | @Test |
---|
522 | public void testRegisterUserspaceProfile() throws Exception { |
---|
523 | List<ProfileDescription> profiles = getUserProfiles(); |
---|
524 | assertEquals("user registered profiles", 0, profiles.size()); |
---|
525 | assertEquals("public registered profiles", 0, getPublicProfiles().size()); |
---|
526 | FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent()); |
---|
527 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
528 | assertTrue(response.isProfile()); |
---|
529 | assertTrue(response.isInUserSpace()); |
---|
530 | ProfileDescription profileDesc = (ProfileDescription) response.getDescription(); |
---|
531 | assertNotNull(profileDesc); |
---|
532 | assertEquals("Test1", profileDesc.getName()); |
---|
533 | assertEquals("My Test", profileDesc.getDescription()); |
---|
534 | assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId()); |
---|
535 | assertEquals("JUnit@test.com", profileDesc.getCreatorName()); |
---|
536 | assertTrue(profileDesc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "p_")); |
---|
537 | assertNotNull(profileDesc.getRegistrationDate()); |
---|
538 | assertEquals("http://localhost:9998/registry/profiles/" + profileDesc.getId() + "?userspace=true", profileDesc.getHref()); |
---|
539 | |
---|
540 | profiles = getUserProfiles(); |
---|
541 | assertEquals(1, profiles.size()); |
---|
542 | assertEquals(0, getPublicProfiles().size()); |
---|
543 | // Try get from public registry |
---|
544 | ClientResponse cResponse = getResource().path("/registry/profiles/" + profileDesc.getId()).accept(MediaType.APPLICATION_XML).get( |
---|
545 | ClientResponse.class); |
---|
546 | // Should return 204 = no content |
---|
547 | assertEquals(204, cResponse.getStatus()); |
---|
548 | CMDComponentSpec spec = getAuthenticatedResource( |
---|
549 | getResource().path("/registry/profiles/" + profileDesc.getId()).queryParam(USERSPACE_PARAM, "true")).accept( |
---|
550 | MediaType.APPLICATION_XML).get(CMDComponentSpec.class); |
---|
551 | assertNotNull(spec); |
---|
552 | |
---|
553 | cResponse = getResource().path("/registry/profiles/" + profileDesc.getId() + "/xsd").accept(MediaType.TEXT_XML).get( |
---|
554 | ClientResponse.class); |
---|
555 | assertEquals(200, cResponse.getStatus()); |
---|
556 | String profile = cResponse.getEntity(String.class); |
---|
557 | assertTrue(profile.length() > 0); |
---|
558 | |
---|
559 | profile = getAuthenticatedResource(getResource().path("/registry/profiles/" + profileDesc.getId() + "/xml")).accept( |
---|
560 | MediaType.TEXT_XML).get(String.class); |
---|
561 | assertTrue(profile.length() > 0); |
---|
562 | |
---|
563 | cResponse = getAuthenticatedResource( |
---|
564 | getResource().path("/registry/profiles/" + profileDesc.getId()).queryParam(USERSPACE_PARAM, "true")).delete( |
---|
565 | ClientResponse.class); |
---|
566 | assertEquals(200, cResponse.getStatus()); |
---|
567 | |
---|
568 | profiles = getUserProfiles(); |
---|
569 | assertEquals(0, profiles.size()); |
---|
570 | } |
---|
571 | |
---|
572 | private List<ProfileDescription> getPublicProfiles() { |
---|
573 | return getAuthenticatedResource("/registry/profiles").accept(MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE); |
---|
574 | } |
---|
575 | |
---|
576 | private List<ProfileDescription> getUserProfiles() { |
---|
577 | return getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).accept( |
---|
578 | MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE); |
---|
579 | } |
---|
580 | |
---|
581 | private FormDataMultiPart createFormData(Object content) { |
---|
582 | return createFormData(content, "My Test"); |
---|
583 | } |
---|
584 | |
---|
585 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
586 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
587 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, content, MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
588 | form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); |
---|
589 | form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, description); |
---|
590 | form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); |
---|
591 | form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
592 | return form; |
---|
593 | } |
---|
594 | |
---|
595 | @Test |
---|
596 | public void testRegisterWithUserComponents() throws Exception { |
---|
597 | ComponentRegistry userRegistry = componentRegistryFactory.getComponentRegistry(true, DummyPrincipal.DUMMY_CREDENTIALS); |
---|
598 | String content = ""; |
---|
599 | content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
600 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
601 | content += " <Header/>\n"; |
---|
602 | content += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
603 | content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
604 | content += " </CMD_Component>\n"; |
---|
605 | content += "</CMD_ComponentSpec>\n"; |
---|
606 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(userRegistry, "XXX1", content); |
---|
607 | |
---|
608 | content = ""; |
---|
609 | content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
610 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
611 | content += " <Header/>\n"; |
---|
612 | content += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
613 | content += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
614 | content += " </CMD_Component>\n"; |
---|
615 | content += " </CMD_Component>\n"; |
---|
616 | content += "</CMD_ComponentSpec>\n"; |
---|
617 | FormDataMultiPart form = createFormData(content); |
---|
618 | RegisterResponse response = getAuthenticatedResource("/registry/components").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
619 | RegisterResponse.class, form); |
---|
620 | assertFalse(response.isRegistered()); |
---|
621 | assertEquals(1, response.getErrors().size()); |
---|
622 | assertEquals("referenced component cannot be found in the published components: " + compDesc1.getName() + " (" + compDesc1.getId() |
---|
623 | + ")", response.getErrors().get(0)); |
---|
624 | |
---|
625 | response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type( |
---|
626 | MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
627 | assertTrue(response.isRegistered()); |
---|
628 | ComponentDescription comp2 = (ComponentDescription) response.getDescription(); |
---|
629 | |
---|
630 | content = ""; |
---|
631 | content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
632 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
633 | content += " <Header/>\n"; |
---|
634 | content += " <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
635 | content += " <CMD_Component ComponentId=\"" + comp2.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
636 | content += " </CMD_Component>\n"; |
---|
637 | content += " </CMD_Component>\n"; |
---|
638 | content += "</CMD_ComponentSpec>\n"; |
---|
639 | |
---|
640 | form = createFormData(content); |
---|
641 | response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
642 | assertFalse(response.isRegistered()); |
---|
643 | assertEquals(1, response.getErrors().size()); |
---|
644 | assertEquals("referenced component cannot be found in the published components: " + comp2.getName() + " (" + comp2.getId() + ")", |
---|
645 | response.getErrors().get(0)); |
---|
646 | |
---|
647 | response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type( |
---|
648 | MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
649 | assertTrue(response.isRegistered()); |
---|
650 | } |
---|
651 | |
---|
652 | @Test |
---|
653 | public void testRegisterUserspaceComponent() throws Exception { |
---|
654 | List<ComponentDescription> components = getUserComponents(); |
---|
655 | assertEquals("user registered components", 0, components.size()); |
---|
656 | assertEquals("public registered components", 0, getPublicComponents().size()); |
---|
657 | FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent()); |
---|
658 | |
---|
659 | RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form); |
---|
660 | assertTrue(response.isRegistered()); |
---|
661 | assertFalse(response.isProfile()); |
---|
662 | assertTrue(response.isInUserSpace()); |
---|
663 | ComponentDescription desc = (ComponentDescription) response.getDescription(); |
---|
664 | assertNotNull(desc); |
---|
665 | assertEquals("Test1", desc.getName()); |
---|
666 | assertEquals("My Test", desc.getDescription()); |
---|
667 | assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId()); |
---|
668 | assertEquals("JUnit@test.com", desc.getCreatorName()); |
---|
669 | assertEquals("TestGroup", desc.getGroupName()); |
---|
670 | assertTrue(desc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "c_")); |
---|
671 | assertNotNull(desc.getRegistrationDate()); |
---|
672 | String url = getResource().getUriBuilder().build().toString(); |
---|
673 | assertEquals(url + "registry/components/" + desc.getId() + "?userspace=true", desc.getHref()); |
---|
674 | |
---|
675 | components = getUserComponents(); |
---|
676 | assertEquals(1, components.size()); |
---|
677 | assertEquals(0, getPublicComponents().size()); |
---|
678 | |
---|
679 | // Try to get from public registry |
---|
680 | ClientResponse cResponse = getResource().path("/registry/components/" + desc.getId()).accept(MediaType.APPLICATION_XML).get( |
---|
681 | ClientResponse.class); |
---|
682 | // Should return 204 = no content |
---|
683 | assertEquals(204, cResponse.getStatus()); |
---|
684 | CMDComponentSpec spec = getUserComponent(desc); |
---|
685 | assertNotNull(spec); |
---|
686 | |
---|
687 | cResponse = getResource().path("/registry/components/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(ClientResponse.class); |
---|
688 | assertEquals(200, cResponse.getStatus()); |
---|
689 | String result = cResponse.getEntity(String.class); |
---|
690 | assertTrue(result.length() > 0); |
---|
691 | |
---|
692 | result = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId() + "/xml")).accept(MediaType.TEXT_XML).get(String.class); |
---|
693 | assertTrue(result.length() > 0); |
---|
694 | |
---|
695 | cResponse = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).delete(ClientResponse.class); |
---|
696 | assertEquals(200, cResponse.getStatus()); |
---|
697 | |
---|
698 | components = getUserComponents(); |
---|
699 | assertEquals(0, components.size()); |
---|
700 | } |
---|
701 | |
---|
702 | @Test |
---|
703 | public void testUpdateComponent() throws Exception { |
---|
704 | List<ComponentDescription> components = getUserComponents(); |
---|
705 | assertEquals("user registered components", 0, components.size()); |
---|
706 | assertEquals("public registered components", 0, getPublicComponents().size()); |
---|
707 | |
---|
708 | FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent()); |
---|
709 | ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
710 | assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus()); |
---|
711 | RegisterResponse response = cResponse.getEntity(RegisterResponse.class); |
---|
712 | assertTrue(response.isRegistered()); |
---|
713 | assertFalse(response.isProfile()); |
---|
714 | assertTrue(response.isInUserSpace()); |
---|
715 | ComponentDescription desc = (ComponentDescription) response.getDescription(); |
---|
716 | assertNotNull(desc); |
---|
717 | assertEquals("Test1", desc.getName()); |
---|
718 | assertEquals("My Test", desc.getDescription()); |
---|
719 | Date firstDate = AbstractDescription.getDate(desc.getRegistrationDate()); |
---|
720 | CMDComponentSpec spec = getUserComponent(desc); |
---|
721 | assertNotNull(spec); |
---|
722 | assertEquals("Access", spec.getCMDComponent().get(0).getName()); |
---|
723 | components = getUserComponents(); |
---|
724 | assertEquals(1, components.size()); |
---|
725 | assertEquals(0, getPublicComponents().size()); |
---|
726 | |
---|
727 | //Now update |
---|
728 | form = createFormData(RegistryTestHelper.getComponentTestContent("TESTNAME"), "UPDATE DESCRIPTION!"); |
---|
729 | cResponse = getAuthenticatedResource( |
---|
730 | getResource().path("/registry/components/" + desc.getId() + "/update").queryParam(USERSPACE_PARAM, "true")).type( |
---|
731 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
732 | assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus()); |
---|
733 | response = cResponse.getEntity(RegisterResponse.class); |
---|
734 | assertTrue(response.isRegistered()); |
---|
735 | assertFalse(response.isProfile()); |
---|
736 | assertTrue(response.isInUserSpace()); |
---|
737 | desc = (ComponentDescription) response.getDescription(); |
---|
738 | assertNotNull(desc); |
---|
739 | assertEquals("Test1", desc.getName()); |
---|
740 | assertEquals("UPDATE DESCRIPTION!", desc.getDescription()); |
---|
741 | Date secondDate = AbstractDescription.getDate(desc.getRegistrationDate()); |
---|
742 | assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate)); |
---|
743 | |
---|
744 | spec = getUserComponent(desc); |
---|
745 | assertNotNull(spec); |
---|
746 | assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName()); |
---|
747 | components = getUserComponents(); |
---|
748 | assertEquals(1, components.size()); |
---|
749 | assertEquals(0, getPublicComponents().size()); |
---|
750 | } |
---|
751 | |
---|
752 | @Test |
---|
753 | public void testUpdateProfile() throws Exception { |
---|
754 | List<ProfileDescription> profiles = getUserProfiles(); |
---|
755 | assertEquals(0, profiles.size()); |
---|
756 | assertEquals(0, getPublicProfiles().size()); |
---|
757 | |
---|
758 | FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent()); |
---|
759 | ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
760 | assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus()); |
---|
761 | RegisterResponse response = cResponse.getEntity(RegisterResponse.class); |
---|
762 | assertTrue(response.isRegistered()); |
---|
763 | assertTrue(response.isProfile()); |
---|
764 | assertTrue(response.isInUserSpace()); |
---|
765 | ProfileDescription desc = (ProfileDescription) response.getDescription(); |
---|
766 | assertNotNull(desc); |
---|
767 | assertEquals("Test1", desc.getName()); |
---|
768 | assertEquals("My Test", desc.getDescription()); |
---|
769 | assertEquals("TestGroup", desc.getGroupName()); |
---|
770 | Date firstDate = AbstractDescription.getDate(desc.getRegistrationDate()); |
---|
771 | CMDComponentSpec spec = getUserProfile(desc); |
---|
772 | assertNotNull(spec); |
---|
773 | assertEquals("Actor", spec.getCMDComponent().get(0).getName()); |
---|
774 | profiles = getUserProfiles(); |
---|
775 | assertEquals(1, profiles.size()); |
---|
776 | assertEquals(0, getPublicComponents().size()); |
---|
777 | |
---|
778 | //Now update |
---|
779 | form = createFormData(RegistryTestHelper.getTestProfileContent("TESTNAME"), "UPDATE DESCRIPTION!"); |
---|
780 | cResponse = getAuthenticatedResource( |
---|
781 | getResource().path("/registry/profiles/" + desc.getId() + "/update").queryParam(USERSPACE_PARAM, "true")).type( |
---|
782 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
783 | assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus()); |
---|
784 | response = cResponse.getEntity(RegisterResponse.class); |
---|
785 | assertTrue(response.isRegistered()); |
---|
786 | assertTrue(response.isProfile()); |
---|
787 | assertTrue(response.isInUserSpace()); |
---|
788 | desc = (ProfileDescription) response.getDescription(); |
---|
789 | assertNotNull(desc); |
---|
790 | assertEquals("Test1", desc.getName()); |
---|
791 | assertEquals("UPDATE DESCRIPTION!", desc.getDescription()); |
---|
792 | Date secondDate = AbstractDescription.getDate(desc.getRegistrationDate()); |
---|
793 | assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate)); |
---|
794 | |
---|
795 | spec = getUserProfile(desc); |
---|
796 | assertNotNull(spec); |
---|
797 | assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName()); |
---|
798 | profiles = getUserProfiles(); |
---|
799 | assertEquals(1, profiles.size()); |
---|
800 | assertEquals(0, getPublicComponents().size()); |
---|
801 | } |
---|
802 | |
---|
803 | private CMDComponentSpec getUserComponent(ComponentDescription desc) { |
---|
804 | return getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class); |
---|
805 | } |
---|
806 | |
---|
807 | private CMDComponentSpec getUserProfile(ProfileDescription desc) { |
---|
808 | return getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class); |
---|
809 | } |
---|
810 | |
---|
811 | private List<ComponentDescription> getPublicComponents() { |
---|
812 | return getAuthenticatedResource("/registry/components").accept(MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE); |
---|
813 | } |
---|
814 | |
---|
815 | private List<ComponentDescription> getUserComponents() { |
---|
816 | return getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).accept( |
---|
817 | MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE); |
---|
818 | } |
---|
819 | |
---|
820 | @Test |
---|
821 | public void testRegisterComponent() throws Exception { |
---|
822 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
823 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getComponentTestContent(), |
---|
824 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
825 | form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ComponentTest1"); |
---|
826 | form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Component"); |
---|
827 | form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain"); |
---|
828 | form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
829 | RegisterResponse response = getAuthenticatedResource("/registry/components").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
830 | RegisterResponse.class, form); |
---|
831 | assertTrue(response.isRegistered()); |
---|
832 | assertFalse(response.isProfile()); |
---|
833 | assertFalse(response.isInUserSpace()); |
---|
834 | ComponentDescription desc = (ComponentDescription) response.getDescription(); |
---|
835 | assertNotNull(desc); |
---|
836 | assertEquals("ComponentTest1", desc.getName()); |
---|
837 | assertEquals("My Test Component", desc.getDescription()); |
---|
838 | assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId()); |
---|
839 | assertEquals("JUnit@test.com", desc.getCreatorName()); |
---|
840 | assertEquals("TestGroup", desc.getGroupName()); |
---|
841 | assertEquals("TestDomain", desc.getDomainName()); |
---|
842 | assertTrue(desc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "c_")); |
---|
843 | assertNotNull(desc.getRegistrationDate()); |
---|
844 | String url = getResource().getUriBuilder().build().toString(); |
---|
845 | assertEquals(url + "registry/components/" + desc.getId(), desc.getHref()); |
---|
846 | } |
---|
847 | |
---|
848 | @Test |
---|
849 | public void testRegisterComment() throws Exception { |
---|
850 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
851 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getCommentTestContent(), |
---|
852 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
853 | fillUp(); |
---|
854 | RegisterResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
855 | RegisterResponse.class, form); |
---|
856 | assertTrue(response.isRegistered()); |
---|
857 | assertFalse(response.isInUserSpace()); |
---|
858 | Comment comment = response.getComment(); |
---|
859 | assertNotNull(comment); |
---|
860 | assertEquals("Actual", comment.getComment()); |
---|
861 | assertEquals(expectedUserId("JUnit@test.com"), comment.getUserId()); |
---|
862 | assertNotNull(comment.getCommentDate()); |
---|
863 | } |
---|
864 | |
---|
865 | @Test |
---|
866 | public void testRegisterProfileInvalidData() throws Exception { |
---|
867 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
868 | String notAValidProfile = "<CMD_ComponentSpec> </CMD_ComponentSpec>"; |
---|
869 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, new ByteArrayInputStream(notAValidProfile.getBytes()), |
---|
870 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
871 | form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1"); |
---|
872 | form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "Domain"); |
---|
873 | form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "Group"); |
---|
874 | form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile"); |
---|
875 | RegisterResponse postResponse = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
876 | RegisterResponse.class, form); |
---|
877 | assertTrue(postResponse.isProfile()); |
---|
878 | assertFalse(postResponse.isRegistered()); |
---|
879 | assertEquals(1, postResponse.getErrors().size()); |
---|
880 | assertTrue(postResponse.getErrors().get(0).contains("SAXParseException")); |
---|
881 | } |
---|
882 | |
---|
883 | @Test |
---|
884 | public void testRegisterProfileInvalidDescriptionAndContent() throws Exception { |
---|
885 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
886 | String profileContent = ""; |
---|
887 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
888 | profileContent += "<CMD_ComponentSpec> \n"; //No isProfile attribute |
---|
889 | profileContent += " <Header />\n"; |
---|
890 | profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
891 | profileContent += " <AttributeList>\n"; |
---|
892 | profileContent += " <Attribute>\n"; |
---|
893 | profileContent += " <Name>Name</Name>\n"; |
---|
894 | profileContent += " <Type>string</Type>\n"; |
---|
895 | profileContent += " </Attribute>\n"; |
---|
896 | profileContent += " </AttributeList>\n"; |
---|
897 | profileContent += " </CMD_Component>\n"; |
---|
898 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
899 | form.field("data", new ByteArrayInputStream(profileContent.getBytes()), MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
900 | form.field("name", "");//Empty name so invalid |
---|
901 | form.field("description", "My Test Profile"); |
---|
902 | RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
903 | RegisterResponse.class, form); |
---|
904 | assertFalse(response.isRegistered()); |
---|
905 | assertEquals(2, response.getErrors().size()); |
---|
906 | assertNotNull(response.getErrors().get(0)); |
---|
907 | assertEquals(MDValidator.PARSE_ERROR, response.getErrors().get(1).substring(0, MDValidator.PARSE_ERROR.length())); |
---|
908 | } |
---|
909 | |
---|
910 | @Test |
---|
911 | public void testRegisterComponentAsProfile() throws Exception { |
---|
912 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
913 | form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getComponentTestContent(), |
---|
914 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
915 | form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "t"); |
---|
916 | form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "domain"); |
---|
917 | form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test"); |
---|
918 | form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Group"); |
---|
919 | RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post( |
---|
920 | RegisterResponse.class, form); |
---|
921 | assertFalse(response.isRegistered()); |
---|
922 | assertTrue(response.isProfile()); |
---|
923 | assertEquals(1, response.getErrors().size()); |
---|
924 | assertEquals(MDValidator.MISMATCH_ERROR, response.getErrors().get(0)); |
---|
925 | } |
---|
926 | } |
---|