diff options
author | mharmsen <mharmsen@c9f7a03b-bd48-0410-a16d-cbbf54688b0b> | 2011-10-04 01:17:41 +0000 |
---|---|---|
committer | mharmsen <mharmsen@c9f7a03b-bd48-0410-a16d-cbbf54688b0b> | 2011-10-04 01:17:41 +0000 |
commit | a4682ceae6774956461edd03b2485bbacea445f4 (patch) | |
tree | 94c475a125441da63101738220ce3972cf37db61 /pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java | |
parent | 0c775428675d2cb1be9551f84e6b741ca813f77e (diff) | |
download | pki-IPA_v2_RHEL_6_2_20111003.tar.gz pki-IPA_v2_RHEL_6_2_20111003.tar.xz pki-IPA_v2_RHEL_6_2_20111003.zip |
Bugzilla Bug #688225 - (dogtagIPAv2.1) TRACKER: of the Dogtag fixes for freeIPA 2.1IPA_v2_RHEL_6_2_20111003
git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pki/tags/IPA_v2_RHEL_6_2_20111003@2252 c9f7a03b-bd48-0410-a16d-cbbf54688b0b
Diffstat (limited to 'pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java')
-rw-r--r-- | pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java new file mode 100644 index 000000000..6517c6bf1 --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java @@ -0,0 +1,170 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.EDBException; +import com.netscape.certsrv.dbs.IDBObj; +import com.netscape.certsrv.dbs.IDBRegistry; +import com.netscape.certsrv.request.IRequestRecord; +import com.netscape.cmscore.request.DBDynAttrMapperDefaultStub; +import com.netscape.cmscore.request.RequestRecord; +import com.netscape.cmscore.test.CMSBaseTestCase; +import com.netscape.cmscore.test.TestHelper; +import junit.framework.Test; +import junit.framework.TestSuite; +import netscape.ldap.LDAPAttributeSet; +import netscape.ldap.LDAPAttribute; + +import java.util.Enumeration; +import java.util.Collections; +import java.util.Arrays; + +public class DBRegistryTest extends CMSBaseTestCase { + + DBSubsystemStub db; + DBRegistry registry; + DBDynAttrMapperStub extAttrMapper; + RequestRecordStub requestRecordStub = new RequestRecordStub(); + + public DBRegistryTest(String name) { + super(name); + } + + public void cmsTestSetUp() { + db = new DBSubsystemStub(); + registry = new DBRegistry(); + db.registry = registry; + + // Emulate the registration of mappers. + // Normally RequestRepository calls RequestRecord.register() as part + // of a long chain of initialization calls. + extAttrMapper = new DBDynAttrMapperStub(); + try { + registry.registerObjectClass(requestRecordStub.getClass().getName(), + new String[] {"ocvalue"} ); + registry.registerAttribute(IRequestRecord.ATTR_EXT_DATA, extAttrMapper); + registry.registerAttribute(IRequestRecord.ATTR_SOURCE_ID, + new StringMapper("sourceIdOut")); + registry.registerDynamicMapper(extAttrMapper); + } catch (EDBException e) { + e.printStackTrace(); + } + } + + public void cmsTestTearDown() { + } + + public static Test suite() { + return new TestSuite(DBRegistryTest.class); + } + + public void testMapObject() throws EBaseException { + assertFalse(extAttrMapper.mapObjectCalled); + registry.mapObject(null, IRequestRecord.ATTR_EXT_DATA, null, + new LDAPAttributeSet()); + assertTrue(extAttrMapper.mapObjectCalled); + } + + public void testGetLDAPAttributesForExtData() throws EBaseException { + String inAttrs[] = new String[] { + "extData-foo", + "extData-foo12", + "EXTDATA-bar;baz", + IRequestRecord.ATTR_SOURCE_ID + }; + String outAttrs[] = registry.getLDAPAttributes(inAttrs); + + assertTrue(TestHelper.contains(outAttrs, inAttrs[0])); + assertTrue(TestHelper.contains(outAttrs, inAttrs[1])); + assertTrue(TestHelper.contains(outAttrs, inAttrs[2])); + assertTrue(TestHelper.contains(outAttrs, "sourceIdOut")); + + try { + registry.getLDAPAttributes(new String[] {"badattr"}); + fail("Should not be able to map badattr"); + } catch (EBaseException e) { /* good */ } + } + + public void testCreateLDAPAttributeSet() throws EBaseException { + assertFalse(extAttrMapper.mapObjectCalled); + + registry.createLDAPAttributeSet(requestRecordStub); + assertTrue(requestRecordStub.getCalled); + assertEquals(requestRecordStub.getCalledWith, + IRequestRecord.ATTR_EXT_DATA); + + // This asserts that mapObject() is called and makes it down to the + // extDataDynAttrMapper.mapObjectToLDAPAttributeSet() call. + assertTrue(extAttrMapper.mapObjectCalled); + } + + public void testCreateObject() throws EBaseException { + LDAPAttributeSet attrs = new LDAPAttributeSet(); + attrs.add(new LDAPAttribute("objectclass", "ocvalue")); + attrs.add(new LDAPAttribute("extdata-foo")); + + assertFalse(extAttrMapper.mapLDAPAttrsCalled); + + registry.createObject(attrs); + + assertTrue(extAttrMapper.mapLDAPAttrsCalled); + } + + + class DBSubsystemStub extends DBSubsystemDefaultStub { + DBRegistry registry; + + public IDBRegistry getRegistry() { + return registry; + } + } + + + class DBDynAttrMapperStub extends DBDynAttrMapperDefaultStub { + boolean mapObjectCalled = false; + Object mapObjectCalledWithObject = null; + boolean mapLDAPAttrsCalled = false; + + public boolean supportsLDAPAttributeName(String attrName) { + return (attrName != null) && + attrName.toLowerCase().startsWith("extdata-"); + } + + public void mapLDAPAttributeSetToObject(LDAPAttributeSet attrs, String name, IDBObj parent) throws EBaseException { + mapLDAPAttrsCalled = true; + } + + public void mapObjectToLDAPAttributeSet(IDBObj parent, String name, + Object obj, + LDAPAttributeSet attrs) + throws EBaseException { + mapObjectCalled = true; + mapObjectCalledWithObject = obj; + } + } + +} + +/* + * This class is purposefully placed outside the test because + * DBRegistry.createObject() calls Class.newInstance() to create + * this stub. This fails if the class is nested. + */ + class RequestRecordStub extends RequestRecordDefaultStub { + + String[] attrs = new String[] { IRequestRecord.ATTR_EXT_DATA }; + + boolean getCalled = false; + String getCalledWith = null; + boolean getSerializedAttrNamesCalled = false; + + public Object get(String name) { + getCalled = true; + getCalledWith = name; + return "foo"; + } + + public Enumeration getSerializableAttrNames() { + getSerializedAttrNamesCalled = true; + return Collections.enumeration(Arrays.asList(attrs)); + } +} |