I'm trying to write some test cases to validate a custom handler. One of
the requirements of the custom handler is that the rampart receivers
first run and populate the messageContext appropriate
(WSHandlerConstants.RECV_RESULTS).

I'm trying to right test cases to validate my handler. Rather than mock
a messageContext to pass into my handler and set all the associated
properties manually, I'd like to programmatically run the portion of
rampart that generates the security header and processes the soap
message. I would then take the resultant message context and run it
through my handler.

>From looking at the sample test cases included with rampart, this seems
like it would be easy enough. I perform the following steps:

(Generate the Request Stage)
1. Create a message context similar to
rampart-tests\src\test\java\org\apache\rampart\MessageBuilderTestBase

2. Load my policy into the context similar to
rampart-tests\src\test\java\org\apache\rampart\
AsymmetricBindingBuilderTest

3. Create a MessageBuilder and build the context
MessageBuilder builder = new MessageBuilder();
builder.build(ctx);

At this step I have what appears to be a valid SOAP envelope in the
message context that's populated with the SOAP headers. Now I need to
process this SOAP message so the context is populated with the
appropriate properties (WSHandlerConstants.RECV_RESULTS).

(Process Stage)
4. Create an RampartReceiver and process the message context

5. Call my handler with the message context

However, I'm getting an error in Step 4. 
java.lang.ClassCastException: org.apache.axiom.om.impl.dom.ElementImpl
incompatible with org.apache.axiom.soap.SOAPHeaderBlock
        at
org.apache.rampart.RampartEngine.process(RampartEngine.java:108)

Line 108 of RampartEngine is:
SOAPHeaderBlock elem = (SOAPHeaderBlock) headerBlocksIterator.next();

More context includes:
RampartMessageData rmd = new RampartMessageData(msgCtx, false);
...
SOAPHeader header = rmd.getMsgContext().getEnvelope().getHeader();
if(header == null) {
    throw new RampartException("missingSOAPHeader");
}               
ArrayList headerBlocks =
header.getHeaderBlocksWithNSURI(WSConstants.WSSE_NS);
SOAPHeaderBlock secHeader = null;
//Issue is axiom - a returned collection must not be null
if(headerBlocks != null) {
        Iterator headerBlocksIterator = headerBlocks.iterator();
        while (headerBlocksIterator.hasNext()) {
                SOAPHeaderBlock elem = (SOAPHeaderBlock)
headerBlocksIterator.next();
                if(elem.getLocalName().equals(WSConstants.WSSE_LN)) {
                        secHeader = elem;
                        break;
                }
        }
}

It appears that the iterator returned from
getHeaderBlocksWithNSURI(WSConstants.WSSE_NS) returns an iterator
consisting of ElementImpl, not SOAPHeaderBlocks. 

I assume that I'm doing something wrong, but am out of ideas at this
point. I don't see any rampart test cases that test the valid processing
of the results, just a single one that test that RampartEngine fails
when no security header is present. I can provide a test case if anyone
is interested.

Any help would be greatly appreciated.

Thanks,
Bob

Reply via email to