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