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.RegisterResponse; |
---|
17 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
18 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
19 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
20 | import com.sun.jersey.api.client.ClientResponse; |
---|
21 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
22 | import java.text.ParseException; |
---|
23 | import java.util.Date; |
---|
24 | import javax.ws.rs.core.MediaType; |
---|
25 | import javax.xml.bind.JAXBException; |
---|
26 | import org.junit.Before; |
---|
27 | import org.springframework.beans.factory.annotation.Autowired; |
---|
28 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
29 | |
---|
30 | import static org.junit.Assert.*; |
---|
31 | import org.junit.Test; |
---|
32 | |
---|
33 | /** |
---|
34 | * |
---|
35 | * @author olhsha |
---|
36 | */ |
---|
37 | public class SanboxTest extends ComponentRegistryRestServiceTestCase { |
---|
38 | |
---|
39 | @Autowired |
---|
40 | private ComponentRegistryFactory componentRegistryFactory; |
---|
41 | |
---|
42 | @Autowired |
---|
43 | private JdbcTemplate jdbcTemplate; |
---|
44 | @Autowired |
---|
45 | private GroupService groupService; |
---|
46 | @Autowired |
---|
47 | private UserDao userDao; |
---|
48 | @Autowired |
---|
49 | private CommentsDao commentsDao; |
---|
50 | private ComponentRegistry baseRegistry; |
---|
51 | |
---|
52 | @Before |
---|
53 | public void init() { |
---|
54 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
55 | createUserRecord(); |
---|
56 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
57 | } |
---|
58 | |
---|
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 MakeGroupC() throws ItemNotFoundException{ |
---|
153 | groupService.createNewGroup("group C", "anotherPrincipal"); |
---|
154 | } |
---|
155 | |
---|
156 | private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
157 | |
---|
158 | MakeGroupB(); |
---|
159 | |
---|
160 | RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); |
---|
161 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); |
---|
162 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); |
---|
163 | |
---|
164 | Ownership ownership = new Ownership(); |
---|
165 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); |
---|
166 | ownership.setGroupId(2); |
---|
167 | ownership.setUserId(0); |
---|
168 | groupService.addOwnership(ownership); |
---|
169 | |
---|
170 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); |
---|
171 | ownership.setGroupId(2); |
---|
172 | ownership.setUserId(0); |
---|
173 | groupService.addOwnership(ownership); |
---|
174 | |
---|
175 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); |
---|
176 | ownership.setGroupId(2); |
---|
177 | ownership.setUserId(0); |
---|
178 | groupService.addOwnership(ownership); |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
183 | |
---|
184 | MakeGroupC(); |
---|
185 | |
---|
186 | RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false); |
---|
187 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false); |
---|
188 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false); |
---|
189 | |
---|
190 | Ownership ownership = new Ownership(); |
---|
191 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1"); |
---|
192 | ownership.setGroupId(3); |
---|
193 | ownership.setUserId(0); |
---|
194 | groupService.addOwnership(ownership); |
---|
195 | |
---|
196 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"); |
---|
197 | ownership.setGroupId(3); |
---|
198 | ownership.setUserId(0); |
---|
199 | groupService.addOwnership(ownership); |
---|
200 | |
---|
201 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2"); |
---|
202 | ownership.setGroupId(3); |
---|
203 | ownership.setUserId(0); |
---|
204 | groupService.addOwnership(ownership); |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | private FormDataMultiPart createFormData(Object content) { |
---|
211 | return createFormData(content, "My Test"); |
---|
212 | } |
---|
213 | |
---|
214 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
215 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
216 | form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, |
---|
217 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
218 | form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); |
---|
219 | form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, |
---|
220 | description); |
---|
221 | form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); |
---|
222 | form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
223 | return form; |
---|
224 | } |
---|
225 | |
---|
226 | @Test |
---|
227 | public void testUpdateGroupComponentAndProfile() throws Exception { |
---|
228 | |
---|
229 | System.out.println("test updateGorupComponent"); |
---|
230 | |
---|
231 | fillUpGroupA(); |
---|
232 | fillUpGroupB(); |
---|
233 | fillUpGroupC(); |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | FormDataMultiPart form = createFormData( |
---|
238 | RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"), |
---|
239 | "UPDATE DESCRIPTION!"); |
---|
240 | ClientResponse cResponse = getAuthenticatedResource(getResource().path( |
---|
241 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"component-1" + "/update")).type( |
---|
242 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
243 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
---|
244 | cResponse.getStatus()); |
---|
245 | RegisterResponse response = cResponse.getEntity(RegisterResponse.class); |
---|
246 | assertTrue(response.isRegistered()); |
---|
247 | assertFalse(response.isProfile()); |
---|
248 | assertTrue(response.isInUserSpace()); |
---|
249 | ComponentDescription desc = (ComponentDescription) response.getDescription(); |
---|
250 | assertNotNull(desc); |
---|
251 | assertEquals("Test1", desc.getName()); |
---|
252 | assertEquals("UPDATE DESCRIPTION!", desc.getDescription()); |
---|
253 | |
---|
254 | form = createFormData( |
---|
255 | RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"), |
---|
256 | "UPDATE DESCRIPTION!"); |
---|
257 | cResponse = getAuthenticatedResource(getResource().path( |
---|
258 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1" + "/update")).type( |
---|
259 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
260 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
---|
261 | cResponse.getStatus()); |
---|
262 | response = cResponse.getEntity(RegisterResponse.class); |
---|
263 | assertTrue(response.isRegistered()); |
---|
264 | assertFalse(response.isProfile()); |
---|
265 | assertTrue(response.isInUserSpace()); |
---|
266 | desc = (ComponentDescription) response.getDescription(); |
---|
267 | assertNotNull(desc); |
---|
268 | assertEquals("Test1", desc.getName()); |
---|
269 | assertEquals("UPDATE DESCRIPTION!", desc.getDescription()); |
---|
270 | |
---|
271 | form = createFormData( |
---|
272 | RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"), |
---|
273 | "UPDATE DESCRIPTION!"); |
---|
274 | cResponse = getAuthenticatedResource(getResource().path( |
---|
275 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1" + "/update")).type( |
---|
276 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
277 | assertEquals(403, cResponse.getStatus()); |
---|
278 | |
---|
279 | // profile |
---|
280 | |
---|
281 | form = createFormData(RegistryTestHelper.getTestProfileContent("TESTNAME"), |
---|
282 | "UPDATE DESCRIPTION!"); |
---|
283 | cResponse = getAuthenticatedResource(getResource().path( |
---|
284 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"profile-1" + "/update")).type( |
---|
285 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
286 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
---|
287 | cResponse.getStatus()); |
---|
288 | response = cResponse.getEntity(RegisterResponse.class); |
---|
289 | assertTrue(response.isRegistered()); |
---|
290 | assertTrue(response.isProfile()); |
---|
291 | assertTrue(response.isInUserSpace()); |
---|
292 | ProfileDescription pdesc = (ProfileDescription) response.getDescription(); |
---|
293 | assertNotNull(pdesc); |
---|
294 | assertEquals("Test1", pdesc.getName()); |
---|
295 | assertEquals("UPDATE DESCRIPTION!", pdesc.getDescription()); |
---|
296 | |
---|
297 | form = createFormData( |
---|
298 | RegistryTestHelper.getTestProfileContent("TESTNAME"), |
---|
299 | "UPDATE DESCRIPTION!"); |
---|
300 | cResponse = getAuthenticatedResource(getResource().path( |
---|
301 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1" + "/update")).type( |
---|
302 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
303 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
---|
304 | cResponse.getStatus()); |
---|
305 | response = cResponse.getEntity(RegisterResponse.class); |
---|
306 | assertTrue(response.isRegistered()); |
---|
307 | assertTrue(response.isProfile()); |
---|
308 | assertTrue(response.isInUserSpace()); |
---|
309 | pdesc = (ProfileDescription) response.getDescription(); |
---|
310 | assertNotNull(pdesc); |
---|
311 | assertEquals("Test1", pdesc.getName()); |
---|
312 | assertEquals("UPDATE DESCRIPTION!", pdesc.getDescription()); |
---|
313 | |
---|
314 | form = createFormData( |
---|
315 | RegistryTestHelper.getTestProfileContent("TESTNAME"), |
---|
316 | "UPDATE DESCRIPTION!"); |
---|
317 | cResponse = getAuthenticatedResource(getResource().path( |
---|
318 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1" + "/update")).type( |
---|
319 | MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); |
---|
320 | assertEquals(403, cResponse.getStatus()); |
---|
321 | |
---|
322 | } |
---|
323 | |
---|
324 | } |
---|