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

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 4.1 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.RegistrySpace;
12import clarin.cmdi.componentregistry.UserCredentials;
13import clarin.cmdi.componentregistry.UserUnauthorizedException;
14import clarin.cmdi.componentregistry.rest.DummyPrincipal;
15
16import org.springframework.beans.factory.annotation.Autowired;
17import org.junit.Test;
18
19import static org.junit.Assert.fail;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertNotSame;
23
24/**
25 *
26 * @author Twan Goosen <twan.goosen@mpi.nl>
27 * @author George.Georgovassilis@mpi.nl
28 */
29public 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}
Note: See TracBrowser for help on using the repository browser.