1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.rest; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
8 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
9 | import clarin.cmdi.componentregistry.ItemNotFoundException; |
---|
10 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; |
---|
11 | import clarin.cmdi.componentregistry.impl.database.GroupService; |
---|
12 | import clarin.cmdi.componentregistry.model.Comment; |
---|
13 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
14 | import clarin.cmdi.componentregistry.model.Group; |
---|
15 | import clarin.cmdi.componentregistry.model.Ownership; |
---|
16 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
17 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
18 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
19 | import com.sun.jersey.api.client.ClientResponse; |
---|
20 | import java.text.ParseException; |
---|
21 | import java.util.List; |
---|
22 | import javax.ws.rs.core.MediaType; |
---|
23 | import javax.ws.rs.core.MultivaluedMap; |
---|
24 | import javax.xml.bind.JAXBException; |
---|
25 | import org.junit.Before; |
---|
26 | import org.springframework.beans.factory.annotation.Autowired; |
---|
27 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
28 | |
---|
29 | import static org.junit.Assert.*; |
---|
30 | import org.junit.Test; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * @author olhsha |
---|
35 | */ |
---|
36 | public class RestGroupServiceTest extends ComponentRegistryRestServiceTestCase { |
---|
37 | |
---|
38 | @Autowired |
---|
39 | private ComponentRegistryFactory componentRegistryFactory; |
---|
40 | |
---|
41 | @Autowired |
---|
42 | private JdbcTemplate jdbcTemplate; |
---|
43 | @Autowired |
---|
44 | private GroupService groupService; |
---|
45 | @Autowired |
---|
46 | private UserDao userDao; |
---|
47 | |
---|
48 | private ComponentRegistry baseRegistry; |
---|
49 | |
---|
50 | @Before |
---|
51 | public void init() { |
---|
52 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
53 | createUserRecord(); |
---|
54 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
55 | } |
---|
56 | |
---|
57 | private String expectedUserId(String principal) { |
---|
58 | return getUserDao().getByPrincipalName(principal).getId().toString(); |
---|
59 | } |
---|
60 | private ComponentDescription component1; |
---|
61 | private ComponentDescription component2; |
---|
62 | private ProfileDescription profile1; |
---|
63 | private ProfileDescription profile2; |
---|
64 | private ComponentDescription component3; |
---|
65 | private ProfileDescription profile3; |
---|
66 | private Comment profile1Comment1; |
---|
67 | private Comment profile1Comment2; |
---|
68 | private Comment component1Comment3; |
---|
69 | private Comment component1Comment4; |
---|
70 | private Comment profile3Comment5; |
---|
71 | private Comment component3Comment7; |
---|
72 | |
---|
73 | private void fillUpPublicItems() throws Exception { |
---|
74 | |
---|
75 | profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true); |
---|
76 | profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true); |
---|
77 | component1 = RegistryTestHelper.addComponent(baseRegistry, |
---|
78 | "component2", true); |
---|
79 | component2 = RegistryTestHelper.addComponent(baseRegistry, |
---|
80 | "component1", true); |
---|
81 | profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2", |
---|
82 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
83 | "JUnit@test.com"); |
---|
84 | profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1", |
---|
85 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
86 | "JUnit@test.com"); |
---|
87 | component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3", |
---|
88 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
89 | "JUnit@test.com"); |
---|
90 | component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4", |
---|
91 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
92 | "JUnit@test.com"); |
---|
93 | } |
---|
94 | |
---|
95 | private void fillUpPrivateItems() throws Exception { |
---|
96 | profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false); |
---|
97 | component3 = RegistryTestHelper.addComponent(baseRegistry, |
---|
98 | "component3", false); |
---|
99 | profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5", |
---|
100 | ProfileDescription.PROFILE_PREFIX + "profile3", |
---|
101 | "JUnit@test.com"); |
---|
102 | component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7", |
---|
103 | ComponentDescription.COMPONENT_PREFIX + "component3", |
---|
104 | "JUnit@test.com"); |
---|
105 | } |
---|
106 | |
---|
107 | protected void createAntherUserRecord() { |
---|
108 | RegistryUser user = new RegistryUser(); |
---|
109 | user.setName("Another database test user"); |
---|
110 | user.setPrincipalName("anotherPrincipal"); |
---|
111 | userDao.save(user); |
---|
112 | } |
---|
113 | |
---|
114 | private void MakeGroupA(){ |
---|
115 | groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
116 | } |
---|
117 | |
---|
118 | private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
119 | |
---|
120 | MakeGroupA(); |
---|
121 | |
---|
122 | RegistryTestHelper.addProfile(baseRegistry, "profile-1", false); |
---|
123 | RegistryTestHelper.addComponent(baseRegistry, "component-1", false); |
---|
124 | RegistryTestHelper.addComponent(baseRegistry, "component-2", false); |
---|
125 | |
---|
126 | Ownership ownership = new Ownership(); |
---|
127 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1"); |
---|
128 | ownership.setGroupId(1); |
---|
129 | ownership.setUserId(0); |
---|
130 | groupService.addOwnership(ownership); |
---|
131 | |
---|
132 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1"); |
---|
133 | ownership.setGroupId(1); |
---|
134 | ownership.setUserId(0); |
---|
135 | groupService.addOwnership(ownership); |
---|
136 | |
---|
137 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2"); |
---|
138 | ownership.setGroupId(1); |
---|
139 | ownership.setUserId(0); |
---|
140 | groupService.addOwnership(ownership); |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | private void MakeGroupB() throws ItemNotFoundException{ |
---|
147 | createAntherUserRecord(); |
---|
148 | groupService.createNewGroup("group B", "anotherPrincipal"); |
---|
149 | groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B"); |
---|
150 | } |
---|
151 | |
---|
152 | private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
153 | |
---|
154 | MakeGroupB(); |
---|
155 | |
---|
156 | RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); |
---|
157 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); |
---|
158 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); |
---|
159 | |
---|
160 | Ownership ownership = new Ownership(); |
---|
161 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); |
---|
162 | ownership.setGroupId(2); |
---|
163 | ownership.setUserId(0); |
---|
164 | groupService.addOwnership(ownership); |
---|
165 | |
---|
166 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); |
---|
167 | ownership.setGroupId(2); |
---|
168 | ownership.setUserId(0); |
---|
169 | groupService.addOwnership(ownership); |
---|
170 | |
---|
171 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); |
---|
172 | ownership.setGroupId(2); |
---|
173 | ownership.setUserId(0); |
---|
174 | groupService.addOwnership(ownership); |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | // Response createNewGroup(String groupName) throws IOException; |
---|
179 | |
---|
180 | @Test |
---|
181 | public void testCreateNewGroup() { |
---|
182 | System.out.println("test createNewGroup"); |
---|
183 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
184 | .path("/registry/groups/create").queryParam("groupName", "newGroup")) |
---|
185 | .accept(MediaType.TEXT_XML).post(ClientResponse.class); |
---|
186 | assertEquals(200, cr.getStatus()); |
---|
187 | assertEquals("Group with the name newGroup is created and given an id 1", cr.getEntity(String.class)); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | // List<Group> getGroupsOwnedByUser(String pricipalName) throws IOException; |
---|
192 | |
---|
193 | @Test |
---|
194 | public void testGetGroupsOwnedByUser() { |
---|
195 | System.out.println("test GetGroupsOwnedByUser"); |
---|
196 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
197 | .path("/registry/groups/create").queryParam("groupName", "newGroup1")) |
---|
198 | .accept(MediaType.TEXT_XML).post(ClientResponse.class); |
---|
199 | assertEquals(200, cr.getStatus()); |
---|
200 | ClientResponse cr1 = this.getAuthenticatedResource(getResource() |
---|
201 | .path("/registry/groups/create").queryParam("groupName", "newGroup2")) |
---|
202 | .accept(MediaType.TEXT_XML).post(ClientResponse.class); |
---|
203 | assertEquals(200, cr1.getStatus()); |
---|
204 | |
---|
205 | // test itself |
---|
206 | |
---|
207 | List<Group> result = this.getAuthenticatedResource(getResource() |
---|
208 | .path("/registry/groups/principal").queryParam("principalName", DummyPrincipal.DUMMY_PRINCIPAL.getName())) |
---|
209 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
210 | |
---|
211 | assertEquals(2, result.size()); |
---|
212 | assertEquals("newGroup1", result.get(0).getName()); |
---|
213 | assertEquals("newGroup2", result.get(1).getName()); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | // List<Group> getGroupsTheCurrentUserIsAMemberOf(); |
---|
218 | |
---|
219 | @Test |
---|
220 | public void testGetGroupsTheCurrentUserIsAMemberOf() throws ItemNotFoundException{ |
---|
221 | System.out.println("test getGroupsTheCurrentUserIsAMemberOfr"); |
---|
222 | |
---|
223 | MakeGroupA(); |
---|
224 | MakeGroupB(); |
---|
225 | |
---|
226 | // test itself |
---|
227 | |
---|
228 | List<Group> result = this.getAuthenticatedResource(getResource() |
---|
229 | .path("/registry/groups/usermembership")) |
---|
230 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
231 | |
---|
232 | assertEquals(1, result.size()); |
---|
233 | assertEquals("group B", result.get(0).getName()); |
---|
234 | } |
---|
235 | // |
---|
236 | // List<Group> getGroupsTheItemIsAMemberOf(String itemId); |
---|
237 | |
---|
238 | @Test |
---|
239 | public void testGetGroupsTheItemIsAMemberOf() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
240 | System.out.println("test getGroupsTheItemIsAMemberOf"); |
---|
241 | |
---|
242 | fillUpGroupA(); |
---|
243 | fillUpGroupB(); |
---|
244 | // test itself |
---|
245 | |
---|
246 | List<Group> result = this.getAuthenticatedResource(getResource() |
---|
247 | .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"component-1/groups")) |
---|
248 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
249 | |
---|
250 | assertEquals(1, result.size()); |
---|
251 | assertEquals("group A", result.get(0).getName()); |
---|
252 | |
---|
253 | result = this.getAuthenticatedResource(getResource() |
---|
254 | .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1/groups")) |
---|
255 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
256 | |
---|
257 | assertEquals(1, result.size()); |
---|
258 | assertEquals("group B", result.get(0).getName()); |
---|
259 | |
---|
260 | result = this.getAuthenticatedResource(getResource() |
---|
261 | .path("/registry/items/"+ProfileDescription.PROFILE_PREFIX+"Bprofile-1/groups")) |
---|
262 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
263 | |
---|
264 | assertEquals(1, result.size()); |
---|
265 | assertEquals("group B", result.get(0).getName()); |
---|
266 | |
---|
267 | result = this.getAuthenticatedResource(getResource() |
---|
268 | .path("/registry/items/"+ProfileDescription.PROFILE_PREFIX+"profile-1/groups")) |
---|
269 | .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE); |
---|
270 | |
---|
271 | assertEquals(1, result.size()); |
---|
272 | assertEquals("group A", result.get(0).getName()); |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | @Test |
---|
280 | public void testListGroupNames() throws ItemNotFoundException{ |
---|
281 | System.out.println("test listGroupNames"); |
---|
282 | |
---|
283 | MakeGroupA(); |
---|
284 | MakeGroupB(); |
---|
285 | |
---|
286 | // test itself |
---|
287 | |
---|
288 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
289 | .path("/registry/groups/names")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
290 | assertEquals(200, cr.getStatus()); |
---|
291 | List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings; |
---|
292 | assertEquals(2, result.size()); |
---|
293 | assertEquals("group A", result.get(0)); |
---|
294 | assertEquals("group B", result.get(1)); |
---|
295 | } |
---|
296 | |
---|
297 | // |
---|
298 | // Response isOwner(String groupName) throws IOException; |
---|
299 | |
---|
300 | @Test |
---|
301 | public void testIsOwner() throws ItemNotFoundException{ |
---|
302 | System.out.println("test isOwner"); |
---|
303 | |
---|
304 | MakeGroupA(); |
---|
305 | MakeGroupB(); |
---|
306 | |
---|
307 | // test itself |
---|
308 | |
---|
309 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
310 | .path("/registry/groups/ownership").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
311 | assertEquals(200, cr.getStatus()); |
---|
312 | String result = cr.getEntity(String.class); |
---|
313 | assertEquals("true", result); |
---|
314 | |
---|
315 | cr = this.getAuthenticatedResource(getResource() |
---|
316 | .path("/registry/groups/ownership").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
317 | assertEquals(200, cr.getStatus()); |
---|
318 | result = cr.getEntity(String.class); |
---|
319 | assertEquals("false", result); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | // |
---|
324 | // Response makeGroupMember(String groupName, String principalName) throws IOException; |
---|
325 | |
---|
326 | @Test |
---|
327 | public void testMakeGroupMember() throws ItemNotFoundException{ |
---|
328 | System.out.println("test makeGroupMember"); |
---|
329 | |
---|
330 | MakeGroupA(); |
---|
331 | MakeGroupB(); |
---|
332 | |
---|
333 | // test itself |
---|
334 | |
---|
335 | //MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>(); |
---|
336 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
337 | .path("/registry/groups/makemember").queryParam("groupName", "group A").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class); |
---|
338 | assertEquals(200, cr.getStatus()); |
---|
339 | |
---|
340 | assertTrue(groupService.userGroupMember("anotherPrincipal", "1")); |
---|
341 | |
---|
342 | ; |
---|
343 | cr = this.getAuthenticatedResource(getResource() |
---|
344 | .path("/registry/groups/makemember").queryParam("groupName", "group B").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class); |
---|
345 | assertEquals(403, cr.getStatus()); |
---|
346 | } |
---|
347 | |
---|
348 | // |
---|
349 | // Response listProfiles(String groupId) throws IOException; |
---|
350 | |
---|
351 | @Test |
---|
352 | public void testListProfilesAndComponents() throws Exception{ |
---|
353 | System.out.println("test listProfilesAndComponents"); |
---|
354 | |
---|
355 | fillUpGroupA(); |
---|
356 | |
---|
357 | // test itself |
---|
358 | |
---|
359 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
360 | .path("/registry/groups/profiles").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
361 | assertEquals(200, cr.getStatus()); |
---|
362 | List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings; |
---|
363 | assertEquals(1, result.size()); |
---|
364 | assertEquals(ProfileDescription.PROFILE_PREFIX+"profile-1", result.get(0)); |
---|
365 | |
---|
366 | cr = this.getAuthenticatedResource(getResource() |
---|
367 | .path("/registry/groups/components").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
368 | assertEquals(200, cr.getStatus()); |
---|
369 | result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings; |
---|
370 | assertEquals(2, result.size()); |
---|
371 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-1", result.get(0)); |
---|
372 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-2", result.get(1)); |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | // |
---|
378 | // Response getGroupNameById(String groupId) throws IOException; |
---|
379 | |
---|
380 | @Test |
---|
381 | public void testGetGroupNamebyId() throws Exception{ |
---|
382 | System.out.println("test getGroupNamebyId"); |
---|
383 | |
---|
384 | MakeGroupA(); |
---|
385 | MakeGroupB(); |
---|
386 | // test itself |
---|
387 | |
---|
388 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
389 | .path("/registry/groups/nameById").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
390 | assertEquals(200, cr.getStatus()); |
---|
391 | String result = cr.getEntity(String.class); |
---|
392 | assertEquals("group A", result); |
---|
393 | |
---|
394 | cr = this.getAuthenticatedResource(getResource() |
---|
395 | .path("/registry/groups/nameById").queryParam("groupId", "2")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
396 | assertEquals(200, cr.getStatus()); |
---|
397 | result = cr.getEntity(String.class); |
---|
398 | assertEquals("group B", result); |
---|
399 | } |
---|
400 | |
---|
401 | // |
---|
402 | // Response getGroupIdByName(String groupName) throws IOException; |
---|
403 | |
---|
404 | @Test |
---|
405 | public void testGetIdByGroupName() throws Exception{ |
---|
406 | System.out.println("test getIdByGroupName"); |
---|
407 | |
---|
408 | MakeGroupA(); |
---|
409 | MakeGroupB(); |
---|
410 | // test itself |
---|
411 | |
---|
412 | ClientResponse cr = this.getAuthenticatedResource(getResource() |
---|
413 | .path("/registry/groups/idByName").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
414 | assertEquals(200, cr.getStatus()); |
---|
415 | String result = cr.getEntity(String.class); |
---|
416 | assertEquals("2", result); |
---|
417 | |
---|
418 | cr = this.getAuthenticatedResource(getResource() |
---|
419 | .path("/registry/groups/idByName").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class); |
---|
420 | assertEquals(200, cr.getStatus()); |
---|
421 | result = cr.getEntity(String.class); |
---|
422 | assertEquals("1", result); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | // Response transferItemOwnershipToGroup(String itemId, long groupId) throws IOException; |
---|
427 | // |
---|
428 | |
---|
429 | |
---|
430 | } |
---|