Changeset 1324
- Timestamp:
- 05/17/11 08:39:02 (13 years ago)
- 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 17 17 */ 18 18 public class ComponentRegistryFactoryDbImpl implements ComponentRegistryFactory { 19 19 20 @Autowired 20 21 private ComponentRegistryBeanFactory componentRegistryBeanFactory; … … 38 39 @Override 39 40 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; 41 56 } 42 57 … … 57 72 return componentRegistry; 58 73 } 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 } 59 89 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImplTest.java
r1293 r1324 1 1 package clarin.cmdi.componentregistry.impl.database; 2 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 import org.junit.Before; 5 import clarin.cmdi.componentregistry.model.UserMapping.User; 3 6 import clarin.cmdi.componentregistry.ComponentRegistry; 7 import clarin.cmdi.componentregistry.UserCredentials; 8 import clarin.cmdi.componentregistry.rest.DummyPrincipal; 4 9 import org.springframework.beans.factory.annotation.Autowired; 5 10 import org.junit.Test; … … 9 14 10 15 import static org.junit.Assert.assertNotNull; 16 import static org.junit.Assert.assertEquals; 17 import static org.junit.Assert.assertNotSame; 11 18 12 19 /** … … 19 26 20 27 @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 } 22 37 23 38 @Test 24 39 public void testInjection() { 25 40 assertNotNull(componentRegistryFactory); 26 41 } 27 42 28 43 @Test 29 44 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()); 32 75 } 33 76 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/UserDaoTest.java
r1323 r1324 69 69 } 70 70 71 p rivateUser createTestUser(){71 public static User createTestUser(){ 72 72 User testUser = new User(); 73 73 testUser.setName(TEST_USER_NAME); … … 76 76 } 77 77 78 p rivatefinal static String TEST_USER_NAME = "Aap";79 p rivatefinal 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"; 80 80 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/DummyPrincipal.java
r622 r1324 6 6 7 7 public final class DummyPrincipal implements Principal { 8 8 9 public static final DummyPrincipal DUMMY_PRINCIPAL = new DummyPrincipal("JUnit@test.com"); 9 10 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 } 14 16 }; 15 16 17 private final String username; 17 18 18 19 public DummyPrincipal(String username) { 19 20 this.username = username; 20 21 } 21 22 23 @Override 22 24 public String getName() { 23 return username; 25 return username; 26 } 27 28 public UserCredentials getCredentials() { 29 return new UserCredentials(this); 24 30 } 25 31 }
Note: See TracChangeset
for help on using the changeset viewer.