1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
4 | import org.junit.Before; |
---|
5 | |
---|
6 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
7 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
8 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
9 | import clarin.cmdi.componentregistry.ComponentStatus; |
---|
10 | import clarin.cmdi.componentregistry.OwnerUser; |
---|
11 | import clarin.cmdi.componentregistry.UserCredentials; |
---|
12 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
13 | import clarin.cmdi.componentregistry.rest.DummyPrincipal; |
---|
14 | |
---|
15 | import org.springframework.beans.factory.annotation.Autowired; |
---|
16 | import org.junit.Test; |
---|
17 | |
---|
18 | import static org.junit.Assert.fail; |
---|
19 | import static org.junit.Assert.assertNotNull; |
---|
20 | import static org.junit.Assert.assertEquals; |
---|
21 | import static org.junit.Assert.assertNotSame; |
---|
22 | |
---|
23 | /** |
---|
24 | * |
---|
25 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
26 | * @author George.Georgovassilis@mpi.nl |
---|
27 | */ |
---|
28 | public class ComponentRegistryFactoryDbImplTest extends BaseUnitTest { |
---|
29 | |
---|
30 | @Autowired |
---|
31 | private ComponentRegistryFactoryDbImpl componentRegistryFactory; |
---|
32 | @Autowired |
---|
33 | private JdbcTemplate jdbcTemplate; |
---|
34 | |
---|
35 | @Before |
---|
36 | public void init() { |
---|
37 | ComponentRegistryTestDatabase.resetDatabase(jdbcTemplate); |
---|
38 | ComponentRegistryTestDatabase.createTableRegistryUser(jdbcTemplate); |
---|
39 | } |
---|
40 | |
---|
41 | @Test |
---|
42 | public void testInjection() { |
---|
43 | assertNotNull(componentRegistryFactory); |
---|
44 | assertNotNull(jdbcTemplate); |
---|
45 | } |
---|
46 | |
---|
47 | @Test |
---|
48 | public void testGetPublicRegistry() { |
---|
49 | ComponentRegistry registry = componentRegistryFactory |
---|
50 | .getPublicRegistry(); |
---|
51 | assertNotNull(registry); |
---|
52 | } |
---|
53 | |
---|
54 | @Test |
---|
55 | public void getComponentRegistry() throws UserUnauthorizedException { |
---|
56 | // Get public |
---|
57 | assertNotNull(componentRegistryFactory.getComponentRegistry( |
---|
58 | ComponentStatus.PUBLISHED, null, null)); |
---|
59 | |
---|
60 | // Get for non-existing user |
---|
61 | final RegistryUser testUser = UserDaoTest.createTestUser(); |
---|
62 | UserCredentials credentials = new DummyPrincipal( |
---|
63 | testUser.getPrincipalName()).getCredentials(); |
---|
64 | |
---|
65 | ComponentRegistryDbImpl cr1 = (ComponentRegistryDbImpl) componentRegistryFactory |
---|
66 | .getComponentRegistry(ComponentStatus.PRIVATE, null, |
---|
67 | credentials); |
---|
68 | assertNotNull(cr1); |
---|
69 | // Get for existing user |
---|
70 | ComponentRegistryDbImpl cr2 = (ComponentRegistryDbImpl) componentRegistryFactory |
---|
71 | .getComponentRegistry(ComponentStatus.PRIVATE, null, |
---|
72 | credentials); |
---|
73 | ; |
---|
74 | assertNotNull(cr2); |
---|
75 | assertEquals(cr1.getOwner(), cr2.getOwner()); |
---|
76 | |
---|
77 | // Get for another new user |
---|
78 | UserCredentials credentials2 = new DummyPrincipal( |
---|
79 | testUser.getPrincipalName() + "2").getCredentials(); |
---|
80 | ComponentRegistryDbImpl cr3 = (ComponentRegistryDbImpl) componentRegistryFactory |
---|
81 | .getComponentRegistry(ComponentStatus.PRIVATE, null, |
---|
82 | credentials2); |
---|
83 | assertNotNull(cr3); |
---|
84 | assertNotSame(cr1.getOwner(), cr3.getOwner()); |
---|
85 | } |
---|
86 | |
---|
87 | @Test |
---|
88 | public void testGetOtherUserComponentRegistry() |
---|
89 | throws UserUnauthorizedException { |
---|
90 | UserCredentials userCredentials = DummyPrincipal.DUMMY_PRINCIPAL |
---|
91 | .getCredentials(); |
---|
92 | |
---|
93 | // Create registry for new user |
---|
94 | ComponentRegistryDbImpl cr1 = (ComponentRegistryDbImpl) componentRegistryFactory |
---|
95 | .getComponentRegistry(ComponentStatus.PRIVATE, null, |
---|
96 | userCredentials); |
---|
97 | |
---|
98 | Number id = cr1.getOwner().getId(); |
---|
99 | |
---|
100 | // Get it as admin |
---|
101 | ComponentRegistryDbImpl cr2 = (ComponentRegistryDbImpl) componentRegistryFactory |
---|
102 | .getOtherUserComponentRegistry( |
---|
103 | DummyPrincipal.DUMMY_ADMIN_PRINCIPAL, |
---|
104 | ComponentStatus.PRIVATE, new OwnerUser(id)); |
---|
105 | assertNotNull(cr2); |
---|
106 | // Should be this user's registry |
---|
107 | assertEquals(cr1.getOwner(), cr2.getOwner()); |
---|
108 | |
---|
109 | // Try get it as non-admin |
---|
110 | try { |
---|
111 | componentRegistryFactory.getOtherUserComponentRegistry( |
---|
112 | DummyPrincipal.DUMMY_PRINCIPAL, ComponentStatus.PRIVATE, |
---|
113 | new OwnerUser(id)); |
---|
114 | fail("Non-admin can get other user's component registry"); |
---|
115 | } catch (Exception ex) { |
---|
116 | // Exception should occur |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|