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.Ownership; |
---|
15 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
16 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
17 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
18 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
19 | import clarin.cmdi.componentregistry.rss.Rss; |
---|
20 | import com.sun.jersey.api.client.ClientResponse; |
---|
21 | import java.text.ParseException; |
---|
22 | import java.util.List; |
---|
23 | import javax.ws.rs.core.MediaType; |
---|
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 SanboxTest 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 | @Autowired |
---|
48 | private CommentsDao commentsDao; |
---|
49 | private ComponentRegistry baseRegistry; |
---|
50 | |
---|
51 | @Before |
---|
52 | public void init() { |
---|
53 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
54 | createUserRecord(); |
---|
55 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | private ComponentDescription component1; |
---|
60 | private ComponentDescription component2; |
---|
61 | private ProfileDescription profile1; |
---|
62 | private ProfileDescription profile2; |
---|
63 | private ComponentDescription component3; |
---|
64 | private ProfileDescription profile3; |
---|
65 | private Comment profile1Comment1; |
---|
66 | private Comment profile1Comment2; |
---|
67 | private Comment component1Comment3; |
---|
68 | private Comment component1Comment4; |
---|
69 | private Comment profile3Comment5; |
---|
70 | private Comment component3Comment7; |
---|
71 | |
---|
72 | private void fillUpPublicItems() throws Exception { |
---|
73 | |
---|
74 | profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true); |
---|
75 | profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true); |
---|
76 | component1 = RegistryTestHelper.addComponent(baseRegistry, |
---|
77 | "component2", true); |
---|
78 | component2 = RegistryTestHelper.addComponent(baseRegistry, |
---|
79 | "component1", true); |
---|
80 | profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2", |
---|
81 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
82 | "JUnit@test.com"); |
---|
83 | profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1", |
---|
84 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
85 | "JUnit@test.com"); |
---|
86 | component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3", |
---|
87 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
88 | "JUnit@test.com"); |
---|
89 | component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4", |
---|
90 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
91 | "JUnit@test.com"); |
---|
92 | } |
---|
93 | |
---|
94 | private void fillUpPrivateItems() throws Exception { |
---|
95 | profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false); |
---|
96 | component3 = RegistryTestHelper.addComponent(baseRegistry, |
---|
97 | "component3", false); |
---|
98 | profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5", |
---|
99 | ProfileDescription.PROFILE_PREFIX + "profile3", |
---|
100 | "JUnit@test.com"); |
---|
101 | component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7", |
---|
102 | ComponentDescription.COMPONENT_PREFIX + "component3", |
---|
103 | "JUnit@test.com"); |
---|
104 | } |
---|
105 | |
---|
106 | protected void createAntherUserRecord() { |
---|
107 | RegistryUser user = new RegistryUser(); |
---|
108 | user.setName("Another database test user"); |
---|
109 | user.setPrincipalName("anotherPrincipal"); |
---|
110 | userDao.save(user); |
---|
111 | } |
---|
112 | |
---|
113 | private void MakeGroupA(){ |
---|
114 | groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
115 | } |
---|
116 | |
---|
117 | private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
118 | |
---|
119 | MakeGroupA(); |
---|
120 | |
---|
121 | RegistryTestHelper.addProfile(baseRegistry, "profile-1", false); |
---|
122 | RegistryTestHelper.addComponent(baseRegistry, "component-1", false); |
---|
123 | RegistryTestHelper.addComponent(baseRegistry, "component-2", false); |
---|
124 | |
---|
125 | Ownership ownership = new Ownership(); |
---|
126 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1"); |
---|
127 | ownership.setGroupId(1); |
---|
128 | ownership.setUserId(0); |
---|
129 | groupService.addOwnership(ownership); |
---|
130 | |
---|
131 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1"); |
---|
132 | ownership.setGroupId(1); |
---|
133 | ownership.setUserId(0); |
---|
134 | groupService.addOwnership(ownership); |
---|
135 | |
---|
136 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2"); |
---|
137 | ownership.setGroupId(1); |
---|
138 | ownership.setUserId(0); |
---|
139 | groupService.addOwnership(ownership); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | private void MakeGroupB() throws ItemNotFoundException{ |
---|
146 | createAntherUserRecord(); |
---|
147 | groupService.createNewGroup("group B", "anotherPrincipal"); |
---|
148 | groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B"); |
---|
149 | } |
---|
150 | |
---|
151 | private void MakeGroupC() throws ItemNotFoundException{ |
---|
152 | groupService.createNewGroup("group C", "anotherPrincipal"); |
---|
153 | } |
---|
154 | |
---|
155 | private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
156 | |
---|
157 | MakeGroupB(); |
---|
158 | |
---|
159 | RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); |
---|
160 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); |
---|
161 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); |
---|
162 | |
---|
163 | Ownership ownership = new Ownership(); |
---|
164 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); |
---|
165 | ownership.setGroupId(2); |
---|
166 | ownership.setUserId(0); |
---|
167 | groupService.addOwnership(ownership); |
---|
168 | |
---|
169 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); |
---|
170 | ownership.setGroupId(2); |
---|
171 | ownership.setUserId(0); |
---|
172 | groupService.addOwnership(ownership); |
---|
173 | |
---|
174 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); |
---|
175 | ownership.setGroupId(2); |
---|
176 | ownership.setUserId(0); |
---|
177 | groupService.addOwnership(ownership); |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
182 | |
---|
183 | MakeGroupC(); |
---|
184 | |
---|
185 | RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false); |
---|
186 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false); |
---|
187 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false); |
---|
188 | |
---|
189 | Ownership ownership = new Ownership(); |
---|
190 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1"); |
---|
191 | ownership.setGroupId(3); |
---|
192 | ownership.setUserId(0); |
---|
193 | groupService.addOwnership(ownership); |
---|
194 | |
---|
195 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"); |
---|
196 | ownership.setGroupId(3); |
---|
197 | ownership.setUserId(0); |
---|
198 | groupService.addOwnership(ownership); |
---|
199 | |
---|
200 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2"); |
---|
201 | ownership.setGroupId(3); |
---|
202 | ownership.setUserId(0); |
---|
203 | groupService.addOwnership(ownership); |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | @Test |
---|
210 | public void testGetGroupRss() throws Exception { |
---|
211 | |
---|
212 | System.out.println("test getGroupRss"); |
---|
213 | |
---|
214 | fillUpGroupA(); |
---|
215 | fillUpGroupB(); |
---|
216 | fillUpGroupC(); |
---|
217 | |
---|
218 | |
---|
219 | // lists of profiles and components |
---|
220 | |
---|
221 | Rss response = this.getAuthenticatedResource(getResource() |
---|
222 | .path("/registry/profiles/rss").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML) |
---|
223 | .get(Rss.class); |
---|
224 | assertEquals(1, response.getChannel().getItem().size()); |
---|
225 | |
---|
226 | response = this.getAuthenticatedResource(getResource() |
---|
227 | .path("/registry/components/rss").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML) |
---|
228 | .get(Rss.class); |
---|
229 | assertEquals(2, response.getChannel().getItem().size()); |
---|
230 | |
---|
231 | response = this.getAuthenticatedResource(getResource() |
---|
232 | .path("/registry/profiles/rss").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML) |
---|
233 | .get(Rss.class); |
---|
234 | assertEquals(1, response.getChannel().getItem().size()); |
---|
235 | |
---|
236 | response = this.getAuthenticatedResource(getResource() |
---|
237 | .path("/registry/components/rss").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML) |
---|
238 | .get(Rss.class); |
---|
239 | assertEquals(2, response.getChannel().getItem().size()); |
---|
240 | |
---|
241 | ClientResponse clientResponse = this.getAuthenticatedResource(getResource() |
---|
242 | .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "3")).accept(MediaType.APPLICATION_XML) |
---|
243 | .get(ClientResponse.class); |
---|
244 | |
---|
245 | assertEquals(403, clientResponse.getStatus()); |
---|
246 | |
---|
247 | RegistryTestHelper.addComment(baseRegistry, "COMMENTc1", ComponentDescription.COMPONENT_PREFIX + "component-1", |
---|
248 | "JUnit@test.com"); |
---|
249 | RegistryTestHelper.addComment(baseRegistry, "COMMENTp1", ProfileDescription.PROFILE_PREFIX + "profile-1", |
---|
250 | "JUnit@test.com"); |
---|
251 | RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1", ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1", |
---|
252 | "anotherPrincipal"); |
---|
253 | RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1", ProfileDescription.PROFILE_PREFIX + "Bprofile-1", |
---|
254 | "anotherPrincipal"); |
---|
255 | (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1", ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1", |
---|
256 | "anotherPrincipal"); |
---|
257 | (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1", ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal"); |
---|
258 | |
---|
259 | |
---|
260 | // lists |
---|
261 | |
---|
262 | response = this.getAuthenticatedResource(getResource() |
---|
263 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/rss")) |
---|
264 | .accept(MediaType.APPLICATION_XML) |
---|
265 | .get(Rss.class); |
---|
266 | assertEquals(1, response.getChannel().getItem().size()); |
---|
267 | |
---|
268 | response = this.getAuthenticatedResource(getResource() |
---|
269 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/rss")) |
---|
270 | .accept(MediaType.APPLICATION_XML) |
---|
271 | .get(Rss.class); |
---|
272 | assertEquals(1, response.getChannel().getItem().size()); |
---|
273 | |
---|
274 | response = this.getAuthenticatedResource(getResource() |
---|
275 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/rss")) |
---|
276 | .accept(MediaType.APPLICATION_XML) |
---|
277 | .get(Rss.class); |
---|
278 | assertEquals(1, response.getChannel().getItem().size()); |
---|
279 | |
---|
280 | response = this.getAuthenticatedResource(getResource() |
---|
281 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/rss")) |
---|
282 | .accept(MediaType.APPLICATION_XML) |
---|
283 | .get(Rss.class); |
---|
284 | assertEquals(1, response.getChannel().getItem().size()); |
---|
285 | |
---|
286 | |
---|
287 | |
---|
288 | clientResponse = this.getAuthenticatedResource(getResource() |
---|
289 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/rss")) |
---|
290 | .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); |
---|
291 | assertEquals(403, clientResponse.getStatus()); |
---|
292 | |
---|
293 | clientResponse = this.getAuthenticatedResource(getResource() |
---|
294 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/rss")) |
---|
295 | .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); |
---|
296 | assertEquals(403, clientResponse.getStatus()); |
---|
297 | |
---|
298 | // particular comments |
---|
299 | |
---|
300 | Comment responseComment = this.getAuthenticatedResource(getResource() |
---|
301 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1")) |
---|
302 | .accept(MediaType.APPLICATION_XML) |
---|
303 | .get(Comment.class); |
---|
304 | assertNotNull(responseComment); |
---|
305 | |
---|
306 | assertEquals(1, Long.parseLong(responseComment.getId())); |
---|
307 | |
---|
308 | responseComment = this.getAuthenticatedResource(getResource() |
---|
309 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2")) |
---|
310 | .accept(MediaType.APPLICATION_XML) |
---|
311 | .get(Comment.class); |
---|
312 | assertNotNull(responseComment); |
---|
313 | |
---|
314 | assertEquals(2, Long.parseLong(responseComment.getId())); |
---|
315 | |
---|
316 | responseComment = this.getAuthenticatedResource(getResource() |
---|
317 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3")) |
---|
318 | .accept(MediaType.APPLICATION_XML) |
---|
319 | .get(Comment.class); |
---|
320 | assertNotNull(responseComment); |
---|
321 | assertEquals(3, Long.parseLong(responseComment.getId())); |
---|
322 | |
---|
323 | responseComment = this.getAuthenticatedResource(getResource() |
---|
324 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4")) |
---|
325 | .accept(MediaType.APPLICATION_XML) |
---|
326 | .get(Comment.class); |
---|
327 | assertNotNull(responseComment); |
---|
328 | assertEquals(4, Long.parseLong(responseComment.getId())); |
---|
329 | |
---|
330 | clientResponse = this.getAuthenticatedResource(getResource() |
---|
331 | .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6")) |
---|
332 | .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); |
---|
333 | assertEquals(403, clientResponse.getStatus()); |
---|
334 | |
---|
335 | clientResponse = this.getAuthenticatedResource(getResource() |
---|
336 | .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5")) |
---|
337 | .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); |
---|
338 | assertEquals(403, clientResponse.getStatus()); |
---|
339 | |
---|
340 | } |
---|
341 | |
---|
342 | } |
---|