1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
4 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
5 | import clarin.cmdi.componentregistry.ComponentStatus; |
---|
6 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
7 | import clarin.cmdi.componentregistry.OwnerGroup; |
---|
8 | import clarin.cmdi.componentregistry.OwnerUser; |
---|
9 | import clarin.cmdi.componentregistry.UserCredentials; |
---|
10 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
11 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
12 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
13 | import clarin.cmdi.componentregistry.model.Comment; |
---|
14 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
15 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
16 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
17 | import clarin.cmdi.componentregistry.rest.DummyPrincipal; |
---|
18 | import clarin.cmdi.componentregistry.rest.RegistryTestHelper; |
---|
19 | import java.io.ByteArrayOutputStream; |
---|
20 | import java.io.OutputStream; |
---|
21 | import java.util.Calendar; |
---|
22 | import java.util.Date; |
---|
23 | import java.util.List; |
---|
24 | import org.apache.commons.lang.time.DateFormatUtils; |
---|
25 | import org.junit.Before; |
---|
26 | import org.junit.Test; |
---|
27 | import org.junit.runner.RunWith; |
---|
28 | import org.springframework.beans.factory.annotation.Autowired; |
---|
29 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
30 | import org.springframework.test.context.ContextConfiguration; |
---|
31 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
32 | |
---|
33 | import static org.junit.Assert.*; |
---|
34 | |
---|
35 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
36 | @ContextConfiguration(locations = {"/applicationContext.xml"}) |
---|
37 | public class ComponentRegistryDbImplTest { |
---|
38 | |
---|
39 | protected final static UserCredentials USER_CREDS = DummyPrincipal.DUMMY_CREDENTIALS; |
---|
40 | protected static final DummyPrincipal PRINCIPAL_ADMIN = DummyPrincipal.DUMMY_ADMIN_PRINCIPAL; |
---|
41 | @Autowired |
---|
42 | private ComponentRegistryBeanFactory componentRegistryBeanFactory; |
---|
43 | @Autowired |
---|
44 | private UserDao userDao; |
---|
45 | @Autowired |
---|
46 | private JdbcTemplate jdbcTemplate; |
---|
47 | |
---|
48 | @Before |
---|
49 | public void init() { |
---|
50 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
51 | } |
---|
52 | |
---|
53 | @Test |
---|
54 | public void testRegisterComponent() throws Exception { |
---|
55 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
56 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
57 | |
---|
58 | description.setName("Aap"); |
---|
59 | description.setDescription("MyDescription"); |
---|
60 | |
---|
61 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
62 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
63 | |
---|
64 | CMDComponentSpec testComponent = RegistryTestHelper.getTestComponent(); |
---|
65 | assertNull(testComponent.getHeader().getID()); |
---|
66 | assertNull(testComponent.getHeader().getName()); |
---|
67 | assertNull(testComponent.getHeader().getDescription()); |
---|
68 | testComponent.getHeader().setDescription("Will not be overwritten"); |
---|
69 | |
---|
70 | register.register(description, testComponent); |
---|
71 | |
---|
72 | assertEquals(1, register.getComponentDescriptions().size()); |
---|
73 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
74 | |
---|
75 | ComponentDescription desc = register.getComponentDescriptions().get(0); |
---|
76 | assertNotNull(desc); |
---|
77 | CMDComponentSpec component = register.getMDComponent(desc.getId()); |
---|
78 | assertNotNull(component); |
---|
79 | |
---|
80 | assertNull(register.getMDProfile(desc.getId())); |
---|
81 | |
---|
82 | ComponentDescription componentDescription = register.getComponentDescription(description.getId()); |
---|
83 | assertNotNull(componentDescription); |
---|
84 | |
---|
85 | assertEquals("Header id should be set from description id", description.getId(), component.getHeader().getID()); |
---|
86 | assertEquals("Aap", component.getHeader().getName()); |
---|
87 | assertEquals("Will not be overwritten", component.getHeader(). |
---|
88 | getDescription()); |
---|
89 | } |
---|
90 | |
---|
91 | @Test |
---|
92 | public void testRegisterProfile() throws Exception { |
---|
93 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
94 | ProfileDescription description = ProfileDescription.createNewDescription(); |
---|
95 | description.setName("Aap"); |
---|
96 | description.setDescription("MyDescription"); |
---|
97 | |
---|
98 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
99 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
100 | |
---|
101 | CMDComponentSpec testProfile = RegistryTestHelper.getTestProfile(); |
---|
102 | assertNull(testProfile.getHeader().getID()); |
---|
103 | assertNull(testProfile.getHeader().getName()); |
---|
104 | assertNull(testProfile.getHeader().getDescription()); |
---|
105 | |
---|
106 | register.register(description, testProfile); |
---|
107 | |
---|
108 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
109 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
110 | ProfileDescription desc = register.getProfileDescriptions().get(0); |
---|
111 | assertNull(register.getMDComponent(desc.getId())); |
---|
112 | |
---|
113 | CMDComponentSpec profile = register.getMDProfile(desc.getId()); |
---|
114 | |
---|
115 | ProfileDescription profileDescription = register.getProfileDescription(description.getId()); |
---|
116 | assertNotNull(profileDescription); |
---|
117 | |
---|
118 | assertNotNull(profile); |
---|
119 | assertEquals("Header id should be set from description id", description.getId(), profile.getHeader().getID()); |
---|
120 | assertEquals("Aap", profile.getHeader().getName()); |
---|
121 | assertEquals("MyDescription", profile.getHeader().getDescription()); |
---|
122 | } |
---|
123 | |
---|
124 | @Test |
---|
125 | public void testDeletePublicProfile() throws Exception { |
---|
126 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
127 | ProfileDescription description = createProfile(register); |
---|
128 | |
---|
129 | // Delete as admin |
---|
130 | register.deleteMDProfile(description.getId(), PRINCIPAL_ADMIN); |
---|
131 | |
---|
132 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
133 | assertNull(register.getMDProfile(description.getId())); |
---|
134 | } |
---|
135 | |
---|
136 | @Test |
---|
137 | public void testDeleteUserProfile() throws Exception { |
---|
138 | RegistryUser user = createUser(); |
---|
139 | Number userId = userDao.insertUser(user); |
---|
140 | ComponentRegistry registry = getComponentRegistryForUser(userId); |
---|
141 | ProfileDescription description = createProfile(registry); |
---|
142 | // Delete as user |
---|
143 | registry.deleteMDProfile(description.getId(), USER_CREDS.getPrincipal()); |
---|
144 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
145 | assertNull(registry.getMDProfile(description.getId())); |
---|
146 | // Delete as admin |
---|
147 | description = createProfile(registry); |
---|
148 | |
---|
149 | registry.deleteMDProfile(description.getId(), PRINCIPAL_ADMIN); |
---|
150 | assertEquals(0, registry.getProfileDescriptions().size()); |
---|
151 | assertNull(registry.getMDProfile(description.getId())); |
---|
152 | } |
---|
153 | |
---|
154 | @Test |
---|
155 | public void testGetDeletedDescriptions() throws Exception { |
---|
156 | RegistryUser user = createUser(); |
---|
157 | Number userId = userDao.insertUser(user); |
---|
158 | ComponentRegistry registry = getComponentRegistryForUser(userId); |
---|
159 | ComponentRegistry publicReg = getComponentRegistryForUser(null); |
---|
160 | ProfileDescription desc1 = createProfile(registry); |
---|
161 | ProfileDescription desc2 = createProfile(publicReg); |
---|
162 | ComponentDescription desc3 = createComponent(registry); |
---|
163 | ComponentDescription desc4 = createComponent(publicReg); |
---|
164 | |
---|
165 | assertEquals(0, registry.getDeletedProfileDescriptions().size()); |
---|
166 | assertEquals(0, publicReg.getDeletedProfileDescriptions().size()); |
---|
167 | assertEquals(0, registry.getDeletedComponentDescriptions().size()); |
---|
168 | assertEquals(0, publicReg.getDeletedComponentDescriptions().size()); |
---|
169 | |
---|
170 | registry.deleteMDProfile(desc1.getId(), USER_CREDS.getPrincipal()); |
---|
171 | |
---|
172 | assertEquals(1, registry.getDeletedProfileDescriptions().size()); |
---|
173 | assertEquals(0, publicReg.getDeletedProfileDescriptions().size()); |
---|
174 | assertEquals(0, registry.getDeletedComponentDescriptions().size()); |
---|
175 | assertEquals(0, publicReg.getDeletedComponentDescriptions().size()); |
---|
176 | |
---|
177 | publicReg.deleteMDProfile(desc2.getId(), USER_CREDS.getPrincipal()); |
---|
178 | |
---|
179 | assertEquals(1, registry.getDeletedProfileDescriptions().size()); |
---|
180 | assertEquals(1, publicReg.getDeletedProfileDescriptions().size()); |
---|
181 | assertEquals(0, registry.getDeletedComponentDescriptions().size()); |
---|
182 | assertEquals(0, publicReg.getDeletedComponentDescriptions().size()); |
---|
183 | |
---|
184 | registry.deleteMDComponent(desc3.getId(), USER_CREDS.getPrincipal(), false); |
---|
185 | |
---|
186 | assertEquals(1, registry.getDeletedProfileDescriptions().size()); |
---|
187 | assertEquals(1, publicReg.getDeletedProfileDescriptions().size()); |
---|
188 | assertEquals(1, registry.getDeletedComponentDescriptions().size()); |
---|
189 | assertEquals(0, publicReg.getDeletedComponentDescriptions().size()); |
---|
190 | |
---|
191 | publicReg.deleteMDComponent(desc4.getId(), USER_CREDS.getPrincipal(), false); |
---|
192 | |
---|
193 | assertEquals(1, registry.getDeletedProfileDescriptions().size()); |
---|
194 | assertEquals(1, publicReg.getDeletedProfileDescriptions().size()); |
---|
195 | assertEquals(1, registry.getDeletedComponentDescriptions().size()); |
---|
196 | assertEquals(1, publicReg.getDeletedComponentDescriptions().size()); |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | private Comment createComment(ComponentRegistry register, ProfileDescription description) throws Exception { |
---|
201 | Comment comment = createComment(description); |
---|
202 | register.registerComment(comment, USER_CREDS.getPrincipalName()); |
---|
203 | assertEquals(1, register.getCommentsInProfile(description.getId(), null).size()); |
---|
204 | return comment; |
---|
205 | } |
---|
206 | |
---|
207 | private Comment createComment(AbstractDescription description) { |
---|
208 | Comment comment = Comment.createANewComment(); |
---|
209 | if (description instanceof ComponentDescription) { |
---|
210 | comment.setComponentDescriptionId(description.getId()); |
---|
211 | } else if (description instanceof ProfileDescription) { |
---|
212 | comment.setProfileDescriptionId(description.getId()); |
---|
213 | } |
---|
214 | comment.setComment("Test comment at " + new Date().toString()); |
---|
215 | comment.setUserName(USER_CREDS.getDisplayName()); |
---|
216 | return comment; |
---|
217 | } |
---|
218 | |
---|
219 | private ProfileDescription createProfile(ComponentRegistry register) throws Exception { |
---|
220 | ProfileDescription description = getProfileDesc(); |
---|
221 | |
---|
222 | CMDComponentSpec testProfile = RegistryTestHelper.getTestProfile(); |
---|
223 | register.register(description, testProfile); |
---|
224 | |
---|
225 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
226 | assertNotNull(register.getMDProfile(description.getId())); |
---|
227 | // Non authorized user should never be able to delete |
---|
228 | try { |
---|
229 | register.deleteMDProfile(description.getId(), new DummyPrincipal("Fake User")); |
---|
230 | fail("Should have thrown exception"); |
---|
231 | } catch (UserUnauthorizedException e) { |
---|
232 | } |
---|
233 | |
---|
234 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
235 | assertNotNull(register.getMDProfile(description.getId())); |
---|
236 | return description; |
---|
237 | } |
---|
238 | |
---|
239 | private ProfileDescription getProfileDesc() { |
---|
240 | ProfileDescription description = ProfileDescription.createNewDescription(); |
---|
241 | description.setName("Aap"); |
---|
242 | description.setCreatorName(USER_CREDS.getDisplayName()); |
---|
243 | description.setUserId(USER_CREDS.getPrincipalName()); |
---|
244 | description.setDescription("MyDescription"); |
---|
245 | return description; |
---|
246 | } |
---|
247 | |
---|
248 | // @Test |
---|
249 | // public void testPostComponentcomment() throws Exception { |
---|
250 | // ComponentRegistry registry = getComponentRegistryForUser(null); |
---|
251 | // ComponentDescription description = createComponent(registry); |
---|
252 | // |
---|
253 | // } |
---|
254 | @Test |
---|
255 | public void testDeletePublicComponent() throws Exception { |
---|
256 | ComponentRegistry registry = getComponentRegistryForUser(null); |
---|
257 | ComponentDescription description = createComponent(registry); |
---|
258 | // Delete as admin |
---|
259 | registry.deleteMDComponent(description.getId(), PRINCIPAL_ADMIN, false); |
---|
260 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
261 | assertNull(registry.getMDComponent(description.getId())); |
---|
262 | } |
---|
263 | |
---|
264 | @Test |
---|
265 | public void testDeleteUserComponent() throws Exception { |
---|
266 | RegistryUser user = createUser(); |
---|
267 | Number userId = userDao.insertUser(user); |
---|
268 | ComponentRegistry registry = getComponentRegistryForUser(userId); |
---|
269 | ComponentDescription description = createComponent(registry); |
---|
270 | // Delete as user |
---|
271 | registry.deleteMDComponent(description.getId(), USER_CREDS.getPrincipal(), false); |
---|
272 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
273 | assertNull(registry.getMDProfile(description.getId())); |
---|
274 | |
---|
275 | // Delete as admin |
---|
276 | description = createComponent(registry); |
---|
277 | |
---|
278 | registry.deleteMDComponent(description.getId(), PRINCIPAL_ADMIN, false); |
---|
279 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
280 | assertNull(registry.getMDComponent(description.getId())); |
---|
281 | } |
---|
282 | |
---|
283 | @Test |
---|
284 | public void testDoNotDeleteOldPublicComponent() throws Exception { |
---|
285 | Number userId = userDao.insertUser(createUser()); |
---|
286 | ComponentRegistry registry = getComponentRegistryForUser(userId); |
---|
287 | |
---|
288 | ComponentDescription description = getComponentDesc(); |
---|
289 | Calendar calendar = Calendar.getInstance(); |
---|
290 | calendar.set(Calendar.YEAR, 1999); |
---|
291 | description.setRegistrationDate(DateFormatUtils.formatUTC(calendar.getTime(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern())); |
---|
292 | CMDComponentSpec testComp = RegistryTestHelper.getTestComponent(); |
---|
293 | |
---|
294 | registry.register(description, testComp); |
---|
295 | registry.publish(description, testComp, USER_CREDS.getPrincipal()); |
---|
296 | |
---|
297 | // Switch to public registry |
---|
298 | registry = getComponentRegistryForUser(null); |
---|
299 | |
---|
300 | try { |
---|
301 | registry.deleteMDComponent(description.getId(), USER_CREDS.getPrincipal(), false); |
---|
302 | fail("Should have thrown exception"); |
---|
303 | } catch (DeleteFailedException e) { |
---|
304 | } |
---|
305 | assertEquals(1, registry.getComponentDescriptions().size()); |
---|
306 | registry.deleteMDComponent(description.getId(), PRINCIPAL_ADMIN, false); |
---|
307 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
308 | |
---|
309 | registry = getComponentRegistryForUser(userId); // ComponentRegistryFactoryImpl.getInstance().getComponentRegistry(true, USER_CREDS); //user registry |
---|
310 | description = getComponentDesc(); |
---|
311 | registry.register(description, testComp); |
---|
312 | assertEquals(1, registry.getComponentDescriptions().size()); |
---|
313 | registry.deleteMDComponent(description.getId(), USER_CREDS.getPrincipal(), false); //user workspace can always delete |
---|
314 | assertEquals(0, registry.getComponentDescriptions().size()); |
---|
315 | } |
---|
316 | |
---|
317 | @Test |
---|
318 | public void testDoNotDeleteOldPublicProfile() throws Exception { |
---|
319 | Number userId = userDao.insertUser(createUser()); |
---|
320 | ComponentRegistry registry = getComponentRegistryForUser(userId); |
---|
321 | |
---|
322 | ProfileDescription description = getProfileDesc(); |
---|
323 | Calendar calendar = Calendar.getInstance(); |
---|
324 | calendar.set(Calendar.YEAR, 1999); |
---|
325 | description.setRegistrationDate(DateFormatUtils.formatUTC(calendar.getTime(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern())); |
---|
326 | CMDComponentSpec testComp = RegistryTestHelper.getTestProfile(); |
---|
327 | |
---|
328 | registry.register(description, testComp); |
---|
329 | registry.publish(description, testComp, USER_CREDS.getPrincipal()); |
---|
330 | |
---|
331 | // Switch to public registry |
---|
332 | registry = getComponentRegistryForUser(null); |
---|
333 | try { |
---|
334 | registry.deleteMDProfile(description.getId(), USER_CREDS.getPrincipal()); |
---|
335 | fail("Should have thrown exception"); |
---|
336 | } catch (DeleteFailedException e) { |
---|
337 | } |
---|
338 | assertEquals(1, registry.getProfileDescriptions().size()); |
---|
339 | registry.deleteMDProfile(description.getId(), PRINCIPAL_ADMIN); |
---|
340 | assertEquals(0, registry.getProfileDescriptions().size()); |
---|
341 | |
---|
342 | registry = getComponentRegistryForUser(userId); |
---|
343 | description = getProfileDesc();//Need to create a new one for a new id |
---|
344 | registry.register(description, testComp); |
---|
345 | assertEquals(1, registry.getProfileDescriptions().size()); |
---|
346 | registry.deleteMDProfile(description.getId(), USER_CREDS.getPrincipal()); //user workspace can always delete |
---|
347 | assertEquals(0, registry.getProfileDescriptions().size()); |
---|
348 | } |
---|
349 | |
---|
350 | @Test(expected = DeleteFailedException.class) |
---|
351 | public void testDoNotDeleteUsedComponent() throws Exception { |
---|
352 | RegistryUser user = createUser(); |
---|
353 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
354 | |
---|
355 | String comp1Id = "component1"; |
---|
356 | String comp2Id = "component2"; |
---|
357 | |
---|
358 | String comp1Content = ""; |
---|
359 | comp1Content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
360 | comp1Content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
361 | comp1Content += " <Header/>\n"; |
---|
362 | comp1Content += " <CMD_Component name=\"Recursion\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
363 | comp1Content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
364 | comp1Content += " </CMD_Component>\n"; |
---|
365 | comp1Content += "</CMD_ComponentSpec>\n"; |
---|
366 | |
---|
367 | ComponentDescription comp1Desc = RegistryTestHelper.addComponent(register, comp1Id, comp1Content); |
---|
368 | |
---|
369 | // Component2 references component1 |
---|
370 | |
---|
371 | String comp2Content = ""; |
---|
372 | comp2Content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
373 | comp2Content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
374 | comp2Content += " <Header/>\n"; |
---|
375 | comp2Content += " <CMD_Component name=\"Recursion\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
376 | comp2Content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
377 | comp2Content += " <CMD_Component ComponentId=\"" + comp1Desc.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"5\"/>\n"; |
---|
378 | comp2Content += " </CMD_Component>\n"; |
---|
379 | comp2Content += "</CMD_ComponentSpec>\n"; |
---|
380 | |
---|
381 | RegistryTestHelper.addComponent(register, comp2Id, comp2Content); |
---|
382 | |
---|
383 | register.deleteMDComponent(comp1Desc.getId(), PRINCIPAL_ADMIN, false); |
---|
384 | } |
---|
385 | |
---|
386 | private ComponentDescription createComponent(ComponentRegistry registry) throws Exception { |
---|
387 | ComponentDescription description = getComponentDesc(); |
---|
388 | CMDComponentSpec testComp = RegistryTestHelper.getTestComponent(); |
---|
389 | |
---|
390 | registry.register(description, testComp); |
---|
391 | |
---|
392 | // Non authorized user should never be able to delete |
---|
393 | try { |
---|
394 | registry.deleteMDComponent(description.getId(), new DummyPrincipal("Fake User"), false); |
---|
395 | fail("Should have thrown exception"); |
---|
396 | } catch (UserUnauthorizedException e) { |
---|
397 | } |
---|
398 | |
---|
399 | assertEquals(1, registry.getComponentDescriptions().size()); |
---|
400 | assertNotNull(registry.getMDComponent(description.getId())); |
---|
401 | return description; |
---|
402 | } |
---|
403 | |
---|
404 | private ComponentDescription getComponentDesc() { |
---|
405 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
406 | description.setName("Aap"); |
---|
407 | description.setCreatorName(USER_CREDS.getDisplayName()); |
---|
408 | description.setUserId(USER_CREDS.getPrincipalName()); |
---|
409 | description.setDescription("MyDescription"); |
---|
410 | return description; |
---|
411 | } |
---|
412 | |
---|
413 | private ComponentRegistry getComponentRegistryForUser(Number userId) { |
---|
414 | ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry(); |
---|
415 | if (userId != null) { |
---|
416 | componentRegistry.setStatus(ComponentStatus.PRIVATE, new OwnerUser(userId)); |
---|
417 | } |
---|
418 | return componentRegistry; |
---|
419 | } |
---|
420 | |
---|
421 | private RegistryUser createUser() { |
---|
422 | return createUser(USER_CREDS); |
---|
423 | } |
---|
424 | |
---|
425 | private RegistryUser createUser(UserCredentials credentials) { |
---|
426 | RegistryUser user = new RegistryUser(); |
---|
427 | user.setName(credentials.getDisplayName()); |
---|
428 | user.setPrincipalName(credentials.getPrincipalName()); |
---|
429 | return user; |
---|
430 | } |
---|
431 | |
---|
432 | @Test |
---|
433 | public void testGetProfileAsXsd() throws Exception { |
---|
434 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
435 | String profileContent = ""; |
---|
436 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
437 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
438 | profileContent += " <Header />\n"; |
---|
439 | profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
440 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
441 | profileContent += " <ValueScheme>\n"; |
---|
442 | profileContent += " <pattern>[0-9][0-9]</pattern>\n"; |
---|
443 | profileContent += " </ValueScheme>\n"; |
---|
444 | profileContent += " </CMD_Element>\n"; |
---|
445 | profileContent += " </CMD_Component>\n"; |
---|
446 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
447 | |
---|
448 | String id = "profile1"; |
---|
449 | ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); |
---|
450 | |
---|
451 | OutputStream output = new ByteArrayOutputStream(); |
---|
452 | register.getMDProfileAsXsd(description.getId(), output); |
---|
453 | String xsd = output.toString().trim(); |
---|
454 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
455 | |
---|
456 | assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "unbounded")); |
---|
457 | } |
---|
458 | |
---|
459 | @Test |
---|
460 | public void testGetNestedRecursiveComponentAsXsd() throws Exception { |
---|
461 | RegistryUser user = createUser(); |
---|
462 | Number userId = userDao.insertUser(user); |
---|
463 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
464 | |
---|
465 | String comp1Id = "component1"; |
---|
466 | String comp2Id = "component2"; |
---|
467 | |
---|
468 | // Component1 references component2 |
---|
469 | |
---|
470 | String comp1Content = ""; |
---|
471 | comp1Content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
472 | comp1Content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
473 | comp1Content += " <Header/>\n"; |
---|
474 | comp1Content += " <CMD_Component name=\"Recursion\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
475 | comp1Content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
476 | comp1Content += " <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + comp2Id + "\" CardinalityMin=\"0\" CardinalityMax=\"5\"/>\n"; |
---|
477 | comp1Content += " </CMD_Component>\n"; |
---|
478 | comp1Content += "</CMD_ComponentSpec>\n"; |
---|
479 | |
---|
480 | ComponentDescription comp1Desc = RegistryTestHelper.addComponent(register, comp1Id, comp1Content); |
---|
481 | |
---|
482 | // Component2 references component1 |
---|
483 | |
---|
484 | String comp2Content = ""; |
---|
485 | comp2Content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
486 | comp2Content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
487 | comp2Content += " <Header/>\n"; |
---|
488 | comp2Content += " <CMD_Component name=\"Recursion\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
489 | comp2Content += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
490 | comp2Content += " <CMD_Component ComponentId=\"" + comp1Desc.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"5\"/>\n"; |
---|
491 | comp2Content += " </CMD_Component>\n"; |
---|
492 | comp2Content += "</CMD_ComponentSpec>\n"; |
---|
493 | |
---|
494 | RegistryTestHelper.addComponent(register, comp2Id, comp2Content); |
---|
495 | |
---|
496 | String profileContent = ""; |
---|
497 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
498 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
499 | profileContent += " <Header />\n"; |
---|
500 | profileContent += " <CMD_Component ComponentId=\"" + comp1Desc.getId() |
---|
501 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"5\">\n"; |
---|
502 | profileContent += " </CMD_Component>\n"; |
---|
503 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
504 | |
---|
505 | String id = "profile1"; |
---|
506 | ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); |
---|
507 | |
---|
508 | OutputStream output = new ByteArrayOutputStream(); |
---|
509 | try { |
---|
510 | register.getMDProfileAsXml(description.getId(), output); |
---|
511 | // This should throw a ComponentRegistryException for recursion |
---|
512 | fail(); |
---|
513 | } catch (ComponentRegistryException ex) { |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | @Test |
---|
518 | public void testGetNestedComponentAsXsd() throws Exception { |
---|
519 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
520 | |
---|
521 | String compId = "component1"; |
---|
522 | String compContent = ""; |
---|
523 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
524 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
525 | compContent += " <Header/>\n"; |
---|
526 | compContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
527 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
528 | compContent += " </CMD_Component>\n"; |
---|
529 | compContent += "</CMD_ComponentSpec>\n"; |
---|
530 | |
---|
531 | ComponentDescription compDesc = RegistryTestHelper.addComponent(register, compId, compContent); |
---|
532 | |
---|
533 | String profileContent = ""; |
---|
534 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
535 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
536 | profileContent += " <Header />\n"; |
---|
537 | profileContent += " <CMD_Component ComponentId=\"" + compDesc.getId() |
---|
538 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"5\">\n"; |
---|
539 | profileContent += " </CMD_Component>\n"; |
---|
540 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
541 | |
---|
542 | String id = "profile1"; |
---|
543 | ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); |
---|
544 | |
---|
545 | OutputStream output = new ByteArrayOutputStream(); |
---|
546 | register.getMDProfileAsXsd(description.getId(), output); |
---|
547 | String xsd = output.toString().trim(); |
---|
548 | |
---|
549 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
550 | assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "5")); |
---|
551 | } |
---|
552 | |
---|
553 | @Test |
---|
554 | public void testGetNestedComponentAsXsdComplex() throws Exception { |
---|
555 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
556 | |
---|
557 | String compContent = ""; |
---|
558 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
559 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
560 | compContent += " <Header/>\n"; |
---|
561 | compContent += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
562 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
563 | compContent += " </CMD_Component>\n"; |
---|
564 | compContent += "</CMD_ComponentSpec>\n"; |
---|
565 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(register, "component1", compContent); |
---|
566 | |
---|
567 | compContent = ""; |
---|
568 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
569 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
570 | compContent += " <Header/>\n"; |
---|
571 | compContent += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; |
---|
572 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
573 | compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" filename=\"component-test-file\">\n"; |
---|
574 | compContent += " </CMD_Component>\n"; |
---|
575 | compContent += " </CMD_Component>\n"; |
---|
576 | compContent += "</CMD_ComponentSpec>\n"; |
---|
577 | ComponentDescription compDesc2 = RegistryTestHelper.addComponent(register, "component2", compContent); |
---|
578 | |
---|
579 | compContent = ""; |
---|
580 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
581 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
582 | compContent += " <Header/>\n"; |
---|
583 | compContent += " <CMD_Component name=\"ZZZ\u00e9\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
584 | compContent += " <CMD_Component ComponentId=\"" + compDesc2.getId() |
---|
585 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"2\">\n"; |
---|
586 | compContent += " </CMD_Component>\n"; |
---|
587 | compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() |
---|
588 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
589 | compContent += " </CMD_Component>\n"; |
---|
590 | compContent += " </CMD_Component>\n"; |
---|
591 | compContent += "</CMD_ComponentSpec>\n"; |
---|
592 | ComponentDescription compDesc3 = RegistryTestHelper.addComponent(register, "component3", compContent); |
---|
593 | |
---|
594 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
---|
595 | register.getMDComponentAsXsd(compDesc3.getId(), output); |
---|
596 | String xsd = output.toString("UTF-8").trim(); |
---|
597 | |
---|
598 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
599 | // System.out.println(xsd); |
---|
600 | |
---|
601 | assertTrue(RegistryTestHelper.hasComponent(xsd, "YYY", "0", "2")); |
---|
602 | assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "1", "10")); |
---|
603 | assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "0", "99")); |
---|
604 | assertTrue(RegistryTestHelper.hasComponent(xsd, "ZZZ\u00e9", "1", "unbounded")); |
---|
605 | } |
---|
606 | |
---|
607 | @Test |
---|
608 | public void testUpdate() throws Exception { |
---|
609 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
610 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
611 | description.setName("Aap"); |
---|
612 | description.setDescription("MyDescription"); |
---|
613 | |
---|
614 | CMDComponentSpec testComponent = RegistryTestHelper.getTestComponent("Test1"); |
---|
615 | register.register(description, testComponent); |
---|
616 | |
---|
617 | // Change values |
---|
618 | description.setName("Noot"); |
---|
619 | description.setDescription("AnotherDescription"); |
---|
620 | // Update in db |
---|
621 | register.update(description, testComponent, PRINCIPAL_ADMIN, false); |
---|
622 | description = register.getComponentDescription(description.getId()); |
---|
623 | // Test if new values are there |
---|
624 | assertNotNull(description); |
---|
625 | assertEquals("Noot", description.getName()); |
---|
626 | assertEquals("AnotherDescription", description.getDescription()); |
---|
627 | |
---|
628 | // Update content |
---|
629 | CMDComponentSpec testComponent2 = RegistryTestHelper.getTestComponent("Test2"); |
---|
630 | register.update(description, testComponent2, PRINCIPAL_ADMIN, false); |
---|
631 | // Test if new content is there |
---|
632 | assertEquals(RegistryTestHelper.getXml(testComponent2), |
---|
633 | RegistryTestHelper.getXml(register.getMDComponent(description.getId()))); |
---|
634 | |
---|
635 | |
---|
636 | // Update both |
---|
637 | description.setName("Mies"); |
---|
638 | description.setDescription("YetAnotherDescription"); |
---|
639 | CMDComponentSpec testComponent3 = RegistryTestHelper.getTestComponent("Test3"); |
---|
640 | |
---|
641 | // Update in db |
---|
642 | register.update(description, testComponent3, PRINCIPAL_ADMIN, false); |
---|
643 | description = register.getComponentDescription(description.getId()); |
---|
644 | // Test if new values are there |
---|
645 | assertNotNull(description); |
---|
646 | assertEquals("Mies", description.getName()); |
---|
647 | assertEquals("YetAnotherDescription", description.getDescription()); |
---|
648 | // Test if new content is there |
---|
649 | assertEquals(RegistryTestHelper.getXml(testComponent3), |
---|
650 | RegistryTestHelper.getXml(register.getMDComponent(description.getId()))); |
---|
651 | } |
---|
652 | |
---|
653 | @Test |
---|
654 | public void testPublish() throws Exception { |
---|
655 | Number userId = userDao.insertUser(createUser()); |
---|
656 | |
---|
657 | ComponentRegistry userRegistry = getComponentRegistryForUser(userId); |
---|
658 | ComponentRegistry publicRegistry = getComponentRegistryForUser(null); |
---|
659 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
660 | description.setName("Aap"); |
---|
661 | description.setDescription("MyDescription"); |
---|
662 | description.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); |
---|
663 | |
---|
664 | CMDComponentSpec testComponent = RegistryTestHelper.getTestComponent("Test1"); |
---|
665 | userRegistry.register(description, testComponent); |
---|
666 | description = userRegistry.getComponentDescription(description.getId()); |
---|
667 | assertNotNull(description); |
---|
668 | assertEquals("Aap", description.getName()); |
---|
669 | assertEquals("MyDescription", description.getDescription()); |
---|
670 | |
---|
671 | // Change values |
---|
672 | description.setName("Noot"); |
---|
673 | description.setDescription("AnotherDescription"); |
---|
674 | CMDComponentSpec testComponent2 = RegistryTestHelper.getTestComponent("Test2"); |
---|
675 | |
---|
676 | // Publish |
---|
677 | int result = userRegistry.publish(description, testComponent2, DummyPrincipal.DUMMY_PRINCIPAL); |
---|
678 | assertEquals(0, result); |
---|
679 | |
---|
680 | // Should not be in user registry |
---|
681 | assertNull(userRegistry.getComponentDescription(description.getId())); |
---|
682 | // Get from public registry |
---|
683 | description = publicRegistry.getComponentDescription(description.getId()); |
---|
684 | // Test if new values are there |
---|
685 | assertNotNull(description); |
---|
686 | assertEquals("Noot", description.getName()); |
---|
687 | assertEquals("AnotherDescription", description.getDescription()); |
---|
688 | // Test if new content is there |
---|
689 | assertEquals(RegistryTestHelper.getXml(testComponent2), |
---|
690 | RegistryTestHelper.getXml(publicRegistry.getMDComponent(description.getId()))); |
---|
691 | assertNull(userRegistry.getMDComponent(description.getId())); |
---|
692 | } |
---|
693 | |
---|
694 | @Test |
---|
695 | public void testRegisterPublicComment() throws Exception { |
---|
696 | // Associated user should exist when posting a comment |
---|
697 | RegistryUser user = createUser(); |
---|
698 | userDao.insertUser(user); |
---|
699 | |
---|
700 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
701 | ProfileDescription description = createProfile(register); |
---|
702 | Comment comment = createComment(register, description); |
---|
703 | assertNotNull(comment); |
---|
704 | assertEquals(0, description.getCommentsCount()); |
---|
705 | |
---|
706 | List<Comment> comments = register.getCommentsInProfile(description.getId(), null); |
---|
707 | assertEquals(1, comments.size()); |
---|
708 | |
---|
709 | comment = comments.get(0); |
---|
710 | assertEquals("0", comment.getUserId()); |
---|
711 | assertEquals(USER_CREDS.getDisplayName(), comment.getUserName()); |
---|
712 | |
---|
713 | // Should not be allowed to delete, not authenticated |
---|
714 | assertFalse(comment.isCanDelete()); |
---|
715 | |
---|
716 | description = register.getProfileDescription(description.getId()); |
---|
717 | assertEquals(1, description.getCommentsCount()); |
---|
718 | |
---|
719 | // Test if owner can delete |
---|
720 | comments = register.getCommentsInProfile(description.getId(), DummyPrincipal.DUMMY_PRINCIPAL); |
---|
721 | assertTrue(comments.get(0).isCanDelete()); |
---|
722 | |
---|
723 | // Test if admin can delete |
---|
724 | userDao.insertUser(createUser(DummyPrincipal.DUMMY_ADMIN_CREDENTIALS)); |
---|
725 | comments = register.getCommentsInProfile(description.getId(), PRINCIPAL_ADMIN); |
---|
726 | assertTrue(comments.get(0).isCanDelete()); |
---|
727 | } |
---|
728 | |
---|
729 | @Test |
---|
730 | public void testRegisterPrivateComment() throws Exception { |
---|
731 | // Associated user should exist when posting a comment |
---|
732 | RegistryUser user = createUser(); |
---|
733 | Number uid = userDao.insertUser(user); |
---|
734 | |
---|
735 | ComponentRegistry register = getComponentRegistryForUser(uid); |
---|
736 | ProfileDescription description = createProfile(register); |
---|
737 | Comment comment = createComment(register, description); |
---|
738 | assertNotNull(comment); |
---|
739 | assertEquals(0, description.getCommentsCount()); |
---|
740 | |
---|
741 | List<Comment> comments = register.getCommentsInProfile(description.getId(), null); |
---|
742 | assertEquals(1, comments.size()); |
---|
743 | |
---|
744 | comment = comments.get(0); |
---|
745 | assertEquals("0", comment.getUserId()); |
---|
746 | assertEquals(USER_CREDS.getDisplayName(), comment.getUserName()); |
---|
747 | |
---|
748 | description = register.getProfileDescription(description.getId()); |
---|
749 | assertEquals(1, description.getCommentsCount()); |
---|
750 | } |
---|
751 | |
---|
752 | @Test |
---|
753 | public void testDeleteComment() throws Exception { |
---|
754 | // Associated user should exist when posting a comment |
---|
755 | RegistryUser user = createUser(); |
---|
756 | userDao.insertUser(user); |
---|
757 | |
---|
758 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
759 | ProfileDescription description = createProfile(register); |
---|
760 | Comment comment = createComment(register, description); |
---|
761 | assertNotNull(comment); |
---|
762 | |
---|
763 | List<Comment> comments = register.getCommentsInProfile(description.getId(), null); |
---|
764 | assertEquals(1, comments.size()); |
---|
765 | description = register.getProfileDescription(description.getId()); |
---|
766 | assertEquals(1, description.getCommentsCount()); |
---|
767 | |
---|
768 | comment = comments.get(0); |
---|
769 | register.deleteComment(comment.getId(), USER_CREDS.getPrincipal()); |
---|
770 | comments = register.getCommentsInProfile(description.getId(), null); |
---|
771 | assertEquals(0, comments.size()); |
---|
772 | description = register.getProfileDescription(description.getId()); |
---|
773 | assertEquals(0, description.getCommentsCount()); |
---|
774 | } |
---|
775 | |
---|
776 | @Test |
---|
777 | public void testDeleteSomeoneElsesComment() throws Exception { |
---|
778 | // User 1 posts a comment... |
---|
779 | |
---|
780 | RegistryUser user1 = createUser(USER_CREDS); |
---|
781 | userDao.insertUser(user1); |
---|
782 | |
---|
783 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
784 | ProfileDescription description = createProfile(register); |
---|
785 | Comment comment = createComment(register, description); |
---|
786 | assertNotNull(comment); |
---|
787 | |
---|
788 | List<Comment> comments = register.getCommentsInProfile(description.getId(), null); |
---|
789 | assertEquals(1, comments.size()); |
---|
790 | comment = comments.get(0); |
---|
791 | |
---|
792 | // Another user comes along.... |
---|
793 | |
---|
794 | DummyPrincipal user2 = new DummyPrincipal(USER_CREDS.getPrincipalName() + "2"); |
---|
795 | userDao.insertUser(createUser(user2.getCredentials())); |
---|
796 | |
---|
797 | try { |
---|
798 | register.deleteComment(comment.getId(), user2); |
---|
799 | // Should not get beyond this |
---|
800 | fail("Could delete someone else's comment!"); |
---|
801 | } catch (UserUnauthorizedException ex) { |
---|
802 | System.err.print(ex); |
---|
803 | } |
---|
804 | } |
---|
805 | |
---|
806 | @Test |
---|
807 | public void testGetName() throws Exception { |
---|
808 | ComponentRegistry register = getComponentRegistryForUser(null); |
---|
809 | assertEquals(ComponentRegistry.PUBLIC_NAME, register.getName()); |
---|
810 | |
---|
811 | RegistryUser user1 = createUser(USER_CREDS); |
---|
812 | Number id = userDao.insertUser(user1); |
---|
813 | |
---|
814 | register = getComponentRegistryForUser(id); |
---|
815 | assertEquals("Registry of J.Unit", register.getName()); |
---|
816 | } |
---|
817 | |
---|
818 | @Test |
---|
819 | public void testGetStatus() throws Exception { |
---|
820 | ComponentRegistry register = new ComponentRegistryDbImpl(); |
---|
821 | assertEquals(ComponentStatus.PUBLISHED, register.getStatus()); |
---|
822 | register = new ComponentRegistryDbImpl(ComponentStatus.PRIVATE, null); |
---|
823 | assertEquals(ComponentStatus.PRIVATE, register.getStatus()); |
---|
824 | } |
---|
825 | |
---|
826 | @Test |
---|
827 | public void testGetOwner() throws Exception { |
---|
828 | ComponentRegistry register = new ComponentRegistryDbImpl(); |
---|
829 | assertNull(register.getOwner()); |
---|
830 | register = new ComponentRegistryDbImpl(ComponentStatus.PRIVATE, new OwnerUser(101)); |
---|
831 | assertEquals(new OwnerUser(101), register.getOwner()); |
---|
832 | |
---|
833 | register = new ComponentRegistryDbImpl(ComponentStatus.PRIVATE, new OwnerGroup(101)); |
---|
834 | assertEquals(new OwnerGroup(101), register.getOwner()); |
---|
835 | } |
---|
836 | |
---|
837 | @Test |
---|
838 | public void testSetStatus() throws Exception { |
---|
839 | // Construct with an owner |
---|
840 | ComponentRegistryDbImpl register = new ComponentRegistryDbImpl(ComponentStatus.PUBLISHED, new OwnerUser(101)); |
---|
841 | register.setStatus(ComponentStatus.PRIVATE); |
---|
842 | assertEquals(ComponentStatus.PRIVATE, register.getStatus()); |
---|
843 | // Owner should remain unchanged |
---|
844 | assertEquals(new OwnerUser(101), register.getOwner()); |
---|
845 | |
---|
846 | // Construct with no owner |
---|
847 | register = new ComponentRegistryDbImpl(ComponentStatus.PUBLISHED, null); |
---|
848 | register.setStatus(ComponentStatus.PRIVATE); |
---|
849 | assertEquals(ComponentStatus.PRIVATE, register.getStatus()); |
---|
850 | // Owner should remain unchanged |
---|
851 | assertNull(register.getOwner()); |
---|
852 | } |
---|
853 | |
---|
854 | @Test |
---|
855 | public void testSetStatusAndOwner() throws Exception { |
---|
856 | ComponentRegistryDbImpl register = new ComponentRegistryDbImpl(ComponentStatus.PUBLISHED, new OwnerUser(101)); |
---|
857 | |
---|
858 | register.setStatus(ComponentStatus.PRIVATE, new OwnerGroup(102)); |
---|
859 | assertEquals(ComponentStatus.PRIVATE, register.getStatus()); |
---|
860 | assertEquals(new OwnerGroup(102), register.getOwner()); |
---|
861 | } |
---|
862 | } |
---|