summaryrefslogtreecommitdiffstats
path: root/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java
blob: 6517c6bf1d89ac2f63310aaff59b50f8dcb2a041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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));
    }
}