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.RegistrySpace; |
---|
11 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
12 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; |
---|
13 | import clarin.cmdi.componentregistry.impl.database.GroupService; |
---|
14 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
15 | import clarin.cmdi.componentregistry.model.Comment; |
---|
16 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
17 | import clarin.cmdi.componentregistry.model.Ownership; |
---|
18 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
19 | import clarin.cmdi.componentregistry.model.RegisterResponse; |
---|
20 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
21 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
22 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
23 | import com.sun.jersey.api.client.ClientResponse; |
---|
24 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
25 | import java.text.ParseException; |
---|
26 | import java.util.Date; |
---|
27 | import java.util.List; |
---|
28 | import javax.ws.rs.core.MediaType; |
---|
29 | import javax.xml.bind.JAXBException; |
---|
30 | import org.junit.Before; |
---|
31 | import org.springframework.beans.factory.annotation.Autowired; |
---|
32 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
33 | |
---|
34 | import static org.junit.Assert.*; |
---|
35 | import org.junit.Test; |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @author olhsha |
---|
40 | */ |
---|
41 | public class SanboxTest extends ComponentRegistryRestServiceTestCase { |
---|
42 | |
---|
43 | @Autowired |
---|
44 | private ComponentRegistryFactory componentRegistryFactory; |
---|
45 | |
---|
46 | @Autowired |
---|
47 | private JdbcTemplate jdbcTemplate; |
---|
48 | @Autowired |
---|
49 | private GroupService groupService; |
---|
50 | @Autowired |
---|
51 | private UserDao userDao; |
---|
52 | @Autowired |
---|
53 | private CommentsDao commentsDao; |
---|
54 | private ComponentRegistry baseRegistry; |
---|
55 | |
---|
56 | @Before |
---|
57 | public void init() { |
---|
58 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
59 | createUserRecord(); |
---|
60 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | private ComponentDescription component1; |
---|
65 | private ComponentDescription component2; |
---|
66 | private ProfileDescription profile1; |
---|
67 | private ProfileDescription profile2; |
---|
68 | private ComponentDescription component3; |
---|
69 | private ProfileDescription profile3; |
---|
70 | private Comment profile1Comment1; |
---|
71 | private Comment profile1Comment2; |
---|
72 | private Comment component1Comment3; |
---|
73 | private Comment component1Comment4; |
---|
74 | private Comment profile3Comment5; |
---|
75 | private Comment component3Comment7; |
---|
76 | |
---|
77 | private void fillUpPublicItems() throws Exception { |
---|
78 | |
---|
79 | profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true); |
---|
80 | profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true); |
---|
81 | component1 = RegistryTestHelper.addComponent(baseRegistry, |
---|
82 | "component2", true); |
---|
83 | component2 = RegistryTestHelper.addComponent(baseRegistry, |
---|
84 | "component1", true); |
---|
85 | profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2", |
---|
86 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
87 | "JUnit@test.com"); |
---|
88 | profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1", |
---|
89 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
90 | "JUnit@test.com"); |
---|
91 | component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3", |
---|
92 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
93 | "JUnit@test.com"); |
---|
94 | component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4", |
---|
95 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
96 | "JUnit@test.com"); |
---|
97 | } |
---|
98 | |
---|
99 | private void fillUpPrivateItems() throws Exception { |
---|
100 | profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false); |
---|
101 | component3 = RegistryTestHelper.addComponent(baseRegistry, |
---|
102 | "component3", false); |
---|
103 | profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5", |
---|
104 | ProfileDescription.PROFILE_PREFIX + "profile3", |
---|
105 | "JUnit@test.com"); |
---|
106 | component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7", |
---|
107 | ComponentDescription.COMPONENT_PREFIX + "component3", |
---|
108 | "JUnit@test.com"); |
---|
109 | } |
---|
110 | |
---|
111 | protected void createAntherUserRecord() { |
---|
112 | RegistryUser user = new RegistryUser(); |
---|
113 | user.setName("Another database test user"); |
---|
114 | user.setPrincipalName("anotherPrincipal"); |
---|
115 | userDao.save(user); |
---|
116 | } |
---|
117 | |
---|
118 | private void MakeGroupA(){ |
---|
119 | groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
120 | } |
---|
121 | |
---|
122 | private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
123 | |
---|
124 | MakeGroupA(); |
---|
125 | |
---|
126 | RegistryTestHelper.addProfile(baseRegistry, "profile-1", false); |
---|
127 | RegistryTestHelper.addComponent(baseRegistry, "component-1", false); |
---|
128 | RegistryTestHelper.addComponent(baseRegistry, "component-2", false); |
---|
129 | |
---|
130 | Ownership ownership = new Ownership(); |
---|
131 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1"); |
---|
132 | ownership.setGroupId(1); |
---|
133 | ownership.setUserId(0); |
---|
134 | groupService.addOwnership(ownership); |
---|
135 | |
---|
136 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1"); |
---|
137 | ownership.setGroupId(1); |
---|
138 | ownership.setUserId(0); |
---|
139 | groupService.addOwnership(ownership); |
---|
140 | |
---|
141 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2"); |
---|
142 | ownership.setGroupId(1); |
---|
143 | ownership.setUserId(0); |
---|
144 | groupService.addOwnership(ownership); |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | private void MakeGroupB() throws ItemNotFoundException{ |
---|
151 | createAntherUserRecord(); |
---|
152 | groupService.createNewGroup("group B", "anotherPrincipal"); |
---|
153 | groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B"); |
---|
154 | } |
---|
155 | |
---|
156 | private void MakeGroupC() throws ItemNotFoundException{ |
---|
157 | groupService.createNewGroup("group C", "anotherPrincipal"); |
---|
158 | } |
---|
159 | |
---|
160 | private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
161 | |
---|
162 | MakeGroupB(); |
---|
163 | |
---|
164 | RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); |
---|
165 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); |
---|
166 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); |
---|
167 | |
---|
168 | Ownership ownership = new Ownership(); |
---|
169 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); |
---|
170 | ownership.setGroupId(2); |
---|
171 | ownership.setUserId(0); |
---|
172 | groupService.addOwnership(ownership); |
---|
173 | |
---|
174 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); |
---|
175 | ownership.setGroupId(2); |
---|
176 | ownership.setUserId(0); |
---|
177 | groupService.addOwnership(ownership); |
---|
178 | |
---|
179 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); |
---|
180 | ownership.setGroupId(2); |
---|
181 | ownership.setUserId(0); |
---|
182 | groupService.addOwnership(ownership); |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
187 | |
---|
188 | MakeGroupC(); |
---|
189 | |
---|
190 | RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false); |
---|
191 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false); |
---|
192 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false); |
---|
193 | |
---|
194 | Ownership ownership = new Ownership(); |
---|
195 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1"); |
---|
196 | ownership.setGroupId(3); |
---|
197 | ownership.setUserId(0); |
---|
198 | groupService.addOwnership(ownership); |
---|
199 | |
---|
200 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"); |
---|
201 | ownership.setGroupId(3); |
---|
202 | ownership.setUserId(0); |
---|
203 | groupService.addOwnership(ownership); |
---|
204 | |
---|
205 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2"); |
---|
206 | ownership.setGroupId(3); |
---|
207 | ownership.setUserId(0); |
---|
208 | groupService.addOwnership(ownership); |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | private FormDataMultiPart createFormData(Object content) { |
---|
215 | return createFormData(content, "My Test"); |
---|
216 | } |
---|
217 | |
---|
218 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
219 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
220 | form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, |
---|
221 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
222 | form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); |
---|
223 | form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, |
---|
224 | description); |
---|
225 | form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); |
---|
226 | form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
227 | return form; |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | @Test |
---|
233 | public void testPublishGroupProfileAndComponent() throws Exception { |
---|
234 | |
---|
235 | System.out.println("testPublishProfile"); |
---|
236 | |
---|
237 | fillUpGroupA(); |
---|
238 | fillUpGroupB(); |
---|
239 | fillUpGroupC(); |
---|
240 | |
---|
241 | baseRegistry.setRegistrySpace(RegistrySpace.PUBLISHED); |
---|
242 | |
---|
243 | FormDataMultiPart form = createFormData( |
---|
244 | RegistryTestHelper.getTestProfileContent("publishedName"), |
---|
245 | "Published"); |
---|
246 | RegisterResponse response = getAuthenticatedResource(getResource().path( |
---|
247 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"profile-1" + "/publish")) |
---|
248 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
249 | RegisterResponse.class, form); |
---|
250 | assertFalse(response.isInUserSpace()); |
---|
251 | assertTrue(response.isProfile()); |
---|
252 | assertEquals(ProfileDescription.PROFILE_PREFIX+"profile-1", response.getDescription().getId()); |
---|
253 | |
---|
254 | |
---|
255 | List<ProfileDescription> profiles = baseRegistry.getProfileDescriptions(); |
---|
256 | assertEquals(1, profiles.size()); |
---|
257 | ProfileDescription profileDescription = profiles.get(0); |
---|
258 | assertEquals(ProfileDescription.PROFILE_PREFIX+"profile-1", profileDescription.getId()); |
---|
259 | assertEquals("link:" + ProfileDescription.PROFILE_PREFIX+"profile-1", |
---|
260 | profileDescription.getHref()); |
---|
261 | assertEquals("Published", profileDescription.getDescription()); |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | // not my profile from "my" group |
---|
267 | |
---|
268 | form = createFormData( |
---|
269 | RegistryTestHelper.getTestProfileContent("publishedName"), |
---|
270 | "Published"); |
---|
271 | response = getAuthenticatedResource(getResource().path( |
---|
272 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1" + "/publish")) |
---|
273 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
274 | RegisterResponse.class, form); |
---|
275 | assertFalse(response.isInUserSpace()); |
---|
276 | assertTrue(response.isProfile()); |
---|
277 | assertEquals(ProfileDescription.PROFILE_PREFIX+"Bprofile-1", response.getDescription().getId()); |
---|
278 | |
---|
279 | profiles = baseRegistry.getProfileDescriptions(); |
---|
280 | assertEquals(2, profiles.size()); |
---|
281 | profileDescription = profiles.get(0); |
---|
282 | assertEquals(ProfileDescription.PROFILE_PREFIX+"Bprofile-1", profileDescription.getId()); |
---|
283 | assertEquals("link:" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1", |
---|
284 | profileDescription.getHref()); |
---|
285 | assertEquals("Published", profileDescription.getDescription()); |
---|
286 | |
---|
287 | // not my profile, not my group |
---|
288 | |
---|
289 | form = createFormData( |
---|
290 | RegistryTestHelper.getTestProfileContent("publishedName"), |
---|
291 | "Published"); |
---|
292 | ClientResponse cr = getAuthenticatedResource(getResource().path( |
---|
293 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1" + "/publish")) |
---|
294 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
295 | ClientResponse.class, form); |
---|
296 | assertEquals(403, cr.getStatus()); |
---|
297 | profiles = baseRegistry.getProfileDescriptions(); |
---|
298 | assertEquals(2, profiles.size()); |
---|
299 | |
---|
300 | /// components |
---|
301 | |
---|
302 | |
---|
303 | form = createFormData( |
---|
304 | RegistryTestHelper.getComponentTestContentAsStream("publishedName"), |
---|
305 | "Published"); |
---|
306 | response = getAuthenticatedResource(getResource().path( |
---|
307 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"component-1" + "/publish")) |
---|
308 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
309 | RegisterResponse.class, form); |
---|
310 | assertFalse(response.isInUserSpace()); |
---|
311 | assertFalse(response.isProfile()); |
---|
312 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-1", response.getDescription().getId()); |
---|
313 | |
---|
314 | |
---|
315 | List<ComponentDescription> components = baseRegistry.getComponentDescriptions(); |
---|
316 | assertEquals(1, components.size()); |
---|
317 | ComponentDescription componentDescription = components.get(0); |
---|
318 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-1", componentDescription.getId()); |
---|
319 | assertEquals("link:" + ComponentDescription.COMPONENT_PREFIX+"component-1", |
---|
320 | componentDescription.getHref()); |
---|
321 | assertEquals("Published", componentDescription.getDescription()); |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | // not my profile from "my" group |
---|
327 | |
---|
328 | |
---|
329 | form = createFormData( |
---|
330 | RegistryTestHelper.getComponentTestContentAsStream("publishedName"), |
---|
331 | "Published"); |
---|
332 | response = getAuthenticatedResource(getResource().path( |
---|
333 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1" + "/publish")) |
---|
334 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
335 | RegisterResponse.class, form); |
---|
336 | assertFalse(response.isInUserSpace()); |
---|
337 | assertFalse(response.isProfile()); |
---|
338 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1", response.getDescription().getId()); |
---|
339 | |
---|
340 | |
---|
341 | components = baseRegistry.getComponentDescriptions(); |
---|
342 | assertEquals(2, components.size()); |
---|
343 | componentDescription = components.get(0); |
---|
344 | assertEquals(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1", componentDescription.getId()); |
---|
345 | assertEquals("link:" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1", |
---|
346 | componentDescription.getHref()); |
---|
347 | assertEquals("Published", componentDescription.getDescription()); |
---|
348 | |
---|
349 | |
---|
350 | // not my profile, not my group |
---|
351 | |
---|
352 | form = createFormData( |
---|
353 | RegistryTestHelper.getComponentTestContentAsStream("publishedName"), |
---|
354 | "Published"); |
---|
355 | cr = getAuthenticatedResource(getResource().path( |
---|
356 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1" + "/publish")) |
---|
357 | .type(MediaType.MULTIPART_FORM_DATA).post( |
---|
358 | ClientResponse.class, form); |
---|
359 | assertEquals(403, cr.getStatus()); |
---|
360 | components = baseRegistry.getComponentDescriptions(); |
---|
361 | assertEquals(2, components.size()); |
---|
362 | } |
---|
363 | |
---|
364 | } |
---|