Skip to content

Commit

Permalink
FELIX-4399, FELIX-4400: UserAdmin MongoDB store:
Browse files Browse the repository at this point in the history
- when creating a new role, we should return the created role if we
  have good indications that the creation was successful;
- MongoDB returns null for empty properties, causing NPEs on several
  occassions.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1560612 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Jan Willem Janssen committed Jan 23, 2014
1 parent ddc72e4 commit dd6edec
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 239 deletions.
18 changes: 12 additions & 6 deletions useradmin/itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
<packaging>jar</packaging>
<description>Integration tests for the UserAdmin OSGi compendium service.</description>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>${osgi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
Expand All @@ -62,6 +56,18 @@
<version>1.0.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.useradmin.mongodb</artifactId>
<version>1.0.2-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.dependencymanager</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,26 @@
*
* @author <a href="mailto:[email protected]">Felix Project Team</a>
*/
public abstract class BaseIntegrationTest {
public abstract class BaseIntegrationTest
{

private static final int DEFAULT_TIMEOUT = 10000;

protected static final String ORG_APACHE_FELIX_USERADMIN = "org.apache.felix.useradmin";
protected static final String ORG_APACHE_FELIX_USERADMIN_FILESTORE = "org.apache.felix.useradmin.filestore";
private static final int DEFAULT_TIMEOUT = 10000;

protected static final String ORG_APACHE_FELIX_USERADMIN = "org.apache.felix.useradmin";
protected static final String ORG_APACHE_FELIX_USERADMIN_FILESTORE = "org.apache.felix.useradmin.filestore";
protected static final String ORG_APACHE_FELIX_USERADMIN_MONGODBSTORE = "org.apache.felix.useradmin.mongodb";
protected static final String ORG_MONGODB_MONGO_JAVA_DRIVER = "org.mongodb.mongo-java-driver";

@Inject
protected volatile BundleContext m_context;

@Configuration
public Option[] config() {
public Option[] config()
{
return options(
bootDelegationPackage("sun.*"),
cleanCaches(),
CoreOptions.systemProperty("logback.configurationFile").value("file:src/test/resources/logback.xml"),
CoreOptions.systemProperty("logback.configurationFile").value("file:src/test/resources/logback.xml"), //
// CoreOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8787"),

mavenBundle("org.slf4j", "slf4j-api").version("1.6.5").startLevel(START_LEVEL_SYSTEM_BUNDLES),
Expand All @@ -79,15 +83,15 @@ public Option[] config() {
url("link:classpath:META-INF/links/org.apache.geronimo.specs.atinject.link").startLevel(START_LEVEL_SYSTEM_BUNDLES),

mavenBundle("org.apache.felix", ORG_APACHE_FELIX_USERADMIN).versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
mavenBundle("org.apache.felix", ORG_APACHE_FELIX_USERADMIN_FILESTORE).versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),

junitBundles(),
frameworkStartLevel(START_LEVEL_TEST_BUNDLE),
felix());
mavenBundle("org.apache.felix", ORG_APACHE_FELIX_USERADMIN_FILESTORE).versionAsInProject().noStart(),
mavenBundle("org.apache.felix", ORG_APACHE_FELIX_USERADMIN_MONGODBSTORE).versionAsInProject().noStart(), mavenBundle("org.mongodb", "mongo-java-driver").versionAsInProject().noStart(),

junitBundles(), frameworkStartLevel(START_LEVEL_TEST_BUNDLE), felix());
}

@Before
public void setUp() throws Exception {
public void setUp() throws Exception
{
assertNotNull("No bundle context?!", m_context);
}

Expand All @@ -97,55 +101,86 @@ public void setUp() throws Exception {
* @return
* @throws Exception
*/
protected <T> T awaitService(String serviceName) throws Exception {
protected <T> T awaitService(String serviceName) throws Exception
{
ServiceTracker tracker = new ServiceTracker(m_context, serviceName, null);
tracker.open();
T result;
try {
try
{
result = (T) tracker.waitForService(DEFAULT_TIMEOUT);
}
finally {
finally
{
tracker.close();
}
return result;
}

/**
* @param bsn
* @return
*/
protected Bundle findBundle(String bsn)
{
for (Bundle bundle : m_context.getBundles())
{
if (bsn.equals(bundle.getSymbolicName()))
{
return bundle;
}
}
return null;
}

protected Bundle getFileStoreBundle()
{
Bundle b = findBundle(ORG_APACHE_FELIX_USERADMIN_FILESTORE);
assertNotNull("Filestore bundle not found?!", b);
return b;
}

protected Bundle getMongoDBStoreBundle()
{
Bundle b = findBundle(ORG_APACHE_FELIX_USERADMIN_MONGODBSTORE);
assertNotNull("MongoDB store bundle not found?!", b);
return b;
}

protected Bundle getMongoDBBundle()
{
Bundle b = findBundle(ORG_MONGODB_MONGO_JAVA_DRIVER);
assertNotNull("MongoDB bundle not found?!", b);
return b;
}

/**
* Obtains a service without waiting for it to become available.
* @param serviceName
* @return
* @throws Exception
*/
protected <T> T getService(String serviceName) throws Exception {
protected <T> T getService(String serviceName) throws Exception
{
ServiceTracker tracker = new ServiceTracker(m_context, serviceName, null);
tracker.open();
T result;
try {
try
{
result = (T) tracker.getService();
}
finally {
finally
{
tracker.close();
}
return result;
}

/**
* @return the {@link UserAdmin} service instance.
*/
protected UserAdmin getUserAdmin() throws Exception {
protected UserAdmin getUserAdmin() throws Exception
{
return getService(UserAdmin.class.getName());
}

/**
* @param bsn
* @return
*/
protected Bundle findBundle(String bsn) {
for (Bundle bundle : m_context.getBundles()) {
if (bsn.equals(bundle.getSymbolicName())) {
return bundle;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,51 +36,54 @@
* @author <a href="mailto:[email protected]">Felix Project Team</a>
*/
@RunWith(JUnit4TestRunner.class)
public class FileStoreInitializationTest extends BaseIntegrationTest {

/**
* Tests that initialization and closing of the repository store is
* performed correctly.
*/
@Test
public void testStoreIsInitializedAndClosedProperlyOk() throws Exception {
UserAdmin ua = getUserAdmin();

// Create two roles...
User user = (User) ua.createRole("user1", Role.USER);
assertNotNull(user);

Group group = (Group) ua.createRole("group1", Role.GROUP);
assertNotNull(group);

group.addMember(user);
group.addRequiredMember(ua.getRole(Role.USER_ANYONE));

// Stop the file store; should persist the two roles...
Bundle fileStoreBundle = findBundle(ORG_APACHE_FELIX_USERADMIN_FILESTORE);
assertNotNull(fileStoreBundle);
fileStoreBundle.stop();

Thread.sleep(100); // Wait a little until the bundle is really stopped...

// Retrieve the roles again; should both yield null due to the store not being available...
user = (User) ua.getRole("user1");
assertNull(user);

group = (Group) ua.getRole("group1");
assertNull(group);

// This will not succeed: no backend to store the user in...
assertNull(ua.createRole("user2", Role.USER));

fileStoreBundle.start();

awaitService(ORG_APACHE_FELIX_USERADMIN_FILESTORE);

public class FileStoreInitializationTest extends BaseIntegrationTest
{

/**
* Tests that initialization and closing of the repository store is
* performed correctly.
*/
@Test
public void testStoreIsInitializedAndClosedProperlyOk() throws Exception
{
UserAdmin ua = getUserAdmin();
// Start the file store bundle...
Bundle fileStoreBundle = getFileStoreBundle();
fileStoreBundle.start();

// Create two roles...
User user = (User) ua.createRole("user1", Role.USER);
assertNotNull(user);

Group group = (Group) ua.createRole("group1", Role.GROUP);
assertNotNull(group);

group.addMember(user);
group.addRequiredMember(ua.getRole(Role.USER_ANYONE));

// Stop the file store; should persist the two roles...
fileStoreBundle.stop();

Thread.sleep(100); // Wait a little until the bundle is really stopped...

// Retrieve the roles again; should both yield null due to the store not being available...
user = (User) ua.getRole("user1");
assertNull(user);

group = (Group) ua.getRole("group1");
assertNull(group);

// This will not succeed: no backend to store the user in...
assertNull(ua.createRole("user2", Role.USER));

fileStoreBundle.start();

awaitService(ORG_APACHE_FELIX_USERADMIN_FILESTORE);

// Retrieve the roles again; should both yield valid values...
user = (User) ua.getRole("user1");
assertNotNull(user);

group = (Group) ua.getRole("group1");
assertNotNull(group);

Expand All @@ -93,8 +96,8 @@ public void testStoreIsInitializedAndClosedProperlyOk() throws Exception {
assertNotNull(members);
assertEquals(1, members.length);
assertEquals(Role.USER_ANYONE, members[0].getName());

user = (User) ua.getRole("user2");
assertNull(user);
}
}
}
Loading

0 comments on commit dd6edec

Please sign in to comment.