Changeset 1324


Ignore:
Timestamp:
05/17/11 08:39:02 (13 years ago)
Author:
twagoo
Message:

Implemented getComponentRegistry method for FactoryDbImpl?

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImpl.java

    r1322 r1324  
    1717 */
    1818public class ComponentRegistryFactoryDbImpl implements ComponentRegistryFactory {
     19
    1920    @Autowired
    2021    private ComponentRegistryBeanFactory componentRegistryBeanFactory;
     
    3839    @Override
    3940    public ComponentRegistry getComponentRegistry(boolean userspace, UserCredentials credentials) {
    40         return getComponentRegistryForUser(null);
     41        ComponentRegistry result = null;
     42        if (userspace) {
     43            if (credentials != null && !ANONYMOUS_USER.equals(credentials.
     44                    getPrincipalName())) {
     45                String principalName = credentials.getPrincipalName();
     46                User user = getOrCreateUser(principalName, credentials.
     47                        getDisplayName());
     48                result = getComponentRegistryForUser(user.getId());
     49            } else {
     50                throw new IllegalArgumentException("No user credentials available cannot load userspace.");
     51            }
     52        } else {
     53            result = getPublicRegistry();
     54        }
     55        return result;
    4156    }
    4257
     
    5772        return componentRegistry;
    5873    }
     74
     75    private User getOrCreateUser(String principalName, String displayName) {
     76        // Try getting it from db
     77        User user = userDao.getByPrincipalName(principalName);
     78        if (user == null) {
     79            // Create the new user
     80            user = new User();
     81            user.setPrincipalName(principalName);
     82            user.setName(displayName);
     83            userDao.insertUser(user);
     84            // Retrieve from db
     85            user = userDao.getByPrincipalName(principalName);
     86        }
     87        return user;
     88    }
    5989}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImplTest.java

    r1293 r1324  
    11package clarin.cmdi.componentregistry.impl.database;
    22
     3import org.springframework.jdbc.core.JdbcTemplate;
     4import org.junit.Before;
     5import clarin.cmdi.componentregistry.model.UserMapping.User;
    36import clarin.cmdi.componentregistry.ComponentRegistry;
     7import clarin.cmdi.componentregistry.UserCredentials;
     8import clarin.cmdi.componentregistry.rest.DummyPrincipal;
    49import org.springframework.beans.factory.annotation.Autowired;
    510import org.junit.Test;
     
    914
    1015import static org.junit.Assert.assertNotNull;
     16import static org.junit.Assert.assertEquals;
     17import static org.junit.Assert.assertNotSame;
    1118
    1219/**
     
    1926
    2027    @Autowired
    21     ComponentRegistryFactoryDbImpl componentRegistryFactory;
     28    private ComponentRegistryFactoryDbImpl componentRegistryFactory;
     29    @Autowired
     30    private JdbcTemplate jdbcTemplate;
     31
     32    @Before
     33    public void init() {
     34        ComponentRegistryDatabase.resetDatabase(jdbcTemplate);
     35        ComponentRegistryDatabase.createTableRegistryUser(jdbcTemplate);
     36    }
    2237
    2338    @Test
    2439    public void testInjection() {
    25         assertNotNull(componentRegistryFactory);
     40        assertNotNull(componentRegistryFactory);
    2641    }
    2742
    2843    @Test
    2944    public void testGetPublicRegistry() {
    30         ComponentRegistry registry = componentRegistryFactory.getPublicRegistry();
    31         assertNotNull(registry);
     45        ComponentRegistry registry = componentRegistryFactory.getPublicRegistry();
     46        assertNotNull(registry);
     47    }
     48
     49    @Test
     50    public void getComponentRegistry() {
     51        // Get public
     52        assertNotNull(componentRegistryFactory.getComponentRegistry(false, null));
     53       
     54        // Get for non-existing user
     55        final User testUser = UserDaoTest.createTestUser();
     56        UserCredentials credentials = new DummyPrincipal(testUser.
     57                getPrincipalName()).getCredentials();
     58
     59        ComponentRegistryDbImpl cr1 = (ComponentRegistryDbImpl) componentRegistryFactory.
     60                getComponentRegistry(true, credentials);
     61        assertNotNull(cr1);
     62        // Get for existing user
     63        ComponentRegistryDbImpl cr2 = (ComponentRegistryDbImpl) componentRegistryFactory.
     64                getComponentRegistry(true, credentials);
     65        assertNotNull(cr2);
     66        assertEquals(cr1.getUserId(), cr2.getUserId());
     67
     68        // Get for another new user
     69        UserCredentials credentials2 = new DummyPrincipal(testUser.
     70                getPrincipalName() + "2").getCredentials();
     71        ComponentRegistryDbImpl cr3 = (ComponentRegistryDbImpl) componentRegistryFactory.
     72                getComponentRegistry(true, credentials2);
     73        assertNotNull(cr3);
     74        assertNotSame(cr1.getUserId(), cr3.getUserId());
    3275    }
    3376}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/UserDaoTest.java

    r1323 r1324  
    6969    }
    7070
    71     private User createTestUser(){
     71    public static User createTestUser(){
    7272        User testUser = new User();
    7373        testUser.setName(TEST_USER_NAME);
     
    7676    }
    7777
    78     private final static String TEST_USER_NAME = "Aap";
    79     private final static String TEST_USER_PRINCIPAL_NAME = "aap@clarin.eu";
     78    public final static String TEST_USER_NAME = "Aap";
     79    public final static String TEST_USER_PRINCIPAL_NAME = "aap@clarin.eu";
    8080}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/DummyPrincipal.java

    r622 r1324  
    66
    77public final class DummyPrincipal implements Principal {
     8
    89    public static final DummyPrincipal DUMMY_PRINCIPAL = new DummyPrincipal("JUnit@test.com");
    910    public static final UserCredentials DUMMY_CREDENTIALS = new UserCredentials(DUMMY_PRINCIPAL) {
    10         @Override
    11         public String getDisplayName() {
    12             return "J.Unit";
    13         }
     11
     12        @Override
     13        public String getDisplayName() {
     14            return "J.Unit";
     15        }
    1416    };
    15 
    1617    private final String username;
    1718
    1819    public DummyPrincipal(String username) {
    19         this.username = username;
     20        this.username = username;
    2021    }
    2122
     23    @Override
    2224    public String getName() {
    23         return username;
     25        return username;
     26    }
     27
     28    public UserCredentials getCredentials() {
     29        return new UserCredentials(this);
    2430    }
    2531}
Note: See TracChangeset for help on using the changeset viewer.