source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImplTest.java @ 3449

Last change on this file since 3449 was 3449, checked in by George.Georgovassilis@mpi.nl, 11 years ago

#360 Refactoring of transaction handling, tests and application context

File size: 3.8 KB
Line 
1package clarin.cmdi.componentregistry.impl.database;
2
3import org.springframework.jdbc.core.JdbcTemplate;
4import org.junit.Before;
5
6import clarin.cmdi.componentregistry.model.RegistryUser;
7import clarin.cmdi.componentregistry.BaseUnitTest;
8import clarin.cmdi.componentregistry.ComponentRegistry;
9import clarin.cmdi.componentregistry.ComponentStatus;
10import clarin.cmdi.componentregistry.OwnerUser;
11import clarin.cmdi.componentregistry.UserCredentials;
12import clarin.cmdi.componentregistry.UserUnauthorizedException;
13import clarin.cmdi.componentregistry.rest.DummyPrincipal;
14
15import org.springframework.beans.factory.annotation.Autowired;
16import org.junit.Test;
17
18import static org.junit.Assert.fail;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertNotSame;
22
23/**
24 *
25 * @author Twan Goosen <twan.goosen@mpi.nl>
26 * @author George.Georgovassilis@mpi.nl
27 */
28public 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}
Note: See TracBrowser for help on using the repository browser.