001: package org.objectweb.celtix.bus.ws.rm.soap;
002:
003: import java.io.IOException;
004: import java.math.BigInteger;
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.Set;
009:
010: import javax.xml.namespace.QName;
011: import javax.xml.soap.MessageFactory;
012: import javax.xml.soap.Name;
013: import javax.xml.soap.SOAPEnvelope;
014: import javax.xml.soap.SOAPException;
015: import javax.xml.soap.SOAPHeader;
016: import javax.xml.soap.SOAPHeaderElement;
017: import javax.xml.soap.SOAPMessage;
018: import javax.xml.ws.handler.soap.SOAPMessageContext;
019:
020: import static javax.xml.ws.handler.MessageContext.MESSAGE_OUTBOUND_PROPERTY;
021:
022: import junit.framework.TestCase;
023:
024: import org.objectweb.celtix.bus.bindings.TestInputStreamContext;
025: import org.objectweb.celtix.bus.bindings.soap.SOAPBindingImpl;
026: import org.objectweb.celtix.bus.bindings.soap.W3CConstants;
027: import org.objectweb.celtix.bus.ws.rm.Names;
028: import org.objectweb.celtix.bus.ws.rm.RMContextUtils;
029: import org.objectweb.celtix.bus.ws.rm.RMEndpointTest;
030: import org.objectweb.celtix.bus.ws.rm.RMPropertiesImpl;
031: import org.objectweb.celtix.bus.ws.rm.RMUtils;
032: import org.objectweb.celtix.context.ObjectMessageContext;
033: import org.objectweb.celtix.context.ObjectMessageContextImpl;
034: import org.objectweb.celtix.ws.rm.AckRequestedType;
035: import org.objectweb.celtix.ws.rm.Identifier;
036: import org.objectweb.celtix.ws.rm.RMProperties;
037: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
038: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement.AcknowledgementRange;
039: import org.objectweb.celtix.ws.rm.SequenceType;
040:
041: public class RMSoapHandlerTest extends TestCase {
042:
043: private static final String SEQ_IDENTIFIER = "http://Business456.com/RM/ABC";
044: private static final BigInteger MSG1_MESSAGE_NUMBER = BigInteger.ONE;
045: private static final BigInteger MSG2_MESSAGE_NUMBER = BigInteger.ONE
046: .add(BigInteger.ONE);
047:
048: SequenceType s1;
049: SequenceType s2;
050: SequenceAcknowledgement ack1;
051: SequenceAcknowledgement ack2;
052: AckRequestedType ar1;
053: AckRequestedType ar2;
054: SOAPBindingImpl sb = new SOAPBindingImpl(false);
055:
056: public void testGetHeaders() throws Exception {
057:
058: RMSoapHandler codec = new RMSoapHandler();
059: codec.init(null);
060: Set<QName> headers = codec.getHeaders();
061: assertTrue("expected Sequence header", headers
062: .contains(Names.WSRM_SEQUENCE_QNAME));
063: assertTrue("expected SequenceAcknowledgment header", headers
064: .contains(Names.WSRM_SEQUENCE_ACK_QNAME));
065: assertTrue("expected AckRequested header", headers
066: .contains(Names.WSRM_ACK_REQUESTED_QNAME));
067: }
068:
069: public void testOutbound() throws Exception {
070:
071: setUpOutbound();
072: SOAPMessageContext context = null;
073: RMSoapHandler codec = new RMSoapHandler();
074:
075: // no RM headers
076:
077: context = setupOutboundContext();
078: assertTrue("expected dispatch to proceed", codec
079: .handleMessage(context));
080: verifyHeaders(context, new String[] {});
081: codec.close(context);
082:
083: // one sequence header
084:
085: context = setupOutboundContext();
086:
087: RMProperties rmps = RMContextUtils.retrieveRMProperties(
088: context, true);
089: rmps.setSequence(s1);
090:
091: assertTrue("expected dispatch to proceed", codec
092: .handleMessage(context));
093: verifyHeaders(context,
094: new String[] { Names.WSRM_SEQUENCE_NAME });
095: codec.close(context);
096:
097: // one acknowledgment header
098:
099: context = setupOutboundContext();
100: rmps = RMContextUtils.retrieveRMProperties(context, true);
101:
102: Collection<SequenceAcknowledgement> acks = new ArrayList<SequenceAcknowledgement>();
103: acks.add(ack1);
104: rmps.setAcks(acks);
105: assertTrue("expected dispatch to proceed", codec
106: .handleMessage(context));
107: verifyHeaders(context,
108: new String[] { Names.WSRM_SEQUENCE_ACK_NAME });
109:
110: // two acknowledgment headers
111:
112: context = setupOutboundContext();
113: rmps = RMContextUtils.retrieveRMProperties(context, true);
114:
115: acks.add(ack2);
116: rmps.setAcks(acks);
117: assertTrue("expected dispatch to proceed", codec
118: .handleMessage(context));
119: verifyHeaders(context, new String[] {
120: Names.WSRM_SEQUENCE_ACK_NAME,
121: Names.WSRM_SEQUENCE_ACK_NAME });
122:
123: // one ack requested header
124:
125: context = setupOutboundContext();
126: rmps = RMContextUtils.retrieveRMProperties(context, true);
127:
128: Collection<AckRequestedType> requested = new ArrayList<AckRequestedType>();
129: requested.add(ar1);
130: rmps.setAcksRequested(requested);
131: assertTrue("expected dispatch to proceed", codec
132: .handleMessage(context));
133: verifyHeaders(context,
134: new String[] { Names.WSRM_ACK_REQUESTED_NAME });
135:
136: // two ack requested headers
137:
138: context = setupOutboundContext();
139: rmps = RMContextUtils.retrieveRMProperties(context, true);
140:
141: requested.add(ar2);
142: rmps.setAcksRequested(requested);
143: assertTrue("expected dispatch to proceed", codec
144: .handleMessage(context));
145: verifyHeaders(context, new String[] {
146: Names.WSRM_ACK_REQUESTED_NAME,
147: Names.WSRM_ACK_REQUESTED_NAME });
148: }
149:
150: public void testInboundSequence() throws IOException {
151: RMSoapHandler codec = new RMSoapHandler();
152: TestInputStreamContext istreamCtx = new TestInputStreamContext();
153: istreamCtx.setInputStream(RMEndpointTest.class
154: .getResourceAsStream("resources/spec/Message1.xml"));
155: sb = new SOAPBindingImpl(false);
156: ObjectMessageContext objectCtx = new ObjectMessageContextImpl();
157: SOAPMessageContext context = (SOAPMessageContext) sb
158: .createBindingMessageContext(objectCtx);
159: sb.read(istreamCtx, context);
160: assertTrue(codec.handleMessage(context));
161: RMProperties rmps = RMContextUtils.retrieveRMProperties(
162: context, false);
163: SequenceType st = rmps.getSequence();
164: assertNotNull(st);
165: assertEquals(st.getIdentifier().getValue(), SEQ_IDENTIFIER);
166: assertEquals(st.getMessageNumber(), MSG1_MESSAGE_NUMBER);
167:
168: assertNull(rmps.getAcks());
169: assertNull(rmps.getAcksRequested());
170:
171: }
172:
173: public void testInboundAcknowledgements() throws IOException {
174: RMSoapHandler codec = new RMSoapHandler();
175: TestInputStreamContext istreamCtx = new TestInputStreamContext();
176: istreamCtx
177: .setInputStream(RMEndpointTest.class
178: .getResourceAsStream("resources/spec/Acknowledgment.xml"));
179: sb = new SOAPBindingImpl(false);
180: ObjectMessageContext objectCtx = new ObjectMessageContextImpl();
181: SOAPMessageContext context = (SOAPMessageContext) sb
182: .createBindingMessageContext(objectCtx);
183: sb.read(istreamCtx, context);
184:
185: assertTrue(codec.handleMessage(context));
186: RMProperties rmps = RMContextUtils.retrieveRMProperties(
187: context, false);
188: Collection<SequenceAcknowledgement> acks = rmps.getAcks();
189: assertNotNull(acks);
190: assertEquals(1, acks.size());
191: SequenceAcknowledgement ack = acks.iterator().next();
192: assertNotNull(ack);
193: assertEquals(ack.getIdentifier().getValue(), SEQ_IDENTIFIER);
194: assertEquals(2, ack.getAcknowledgementRange().size());
195: assertNull(rmps.getSequence());
196: assertNull(rmps.getAcksRequested());
197: }
198:
199: public void testInboundAcksRequested() throws IOException {
200: RMSoapHandler codec = new RMSoapHandler();
201: TestInputStreamContext istreamCtx = new TestInputStreamContext();
202: istreamCtx
203: .setInputStream(RMEndpointTest.class
204: .getResourceAsStream("resources/spec/Retransmission.xml"));
205: sb = new SOAPBindingImpl(false);
206: ObjectMessageContext objectCtx = new ObjectMessageContextImpl();
207: SOAPMessageContext context = (SOAPMessageContext) sb
208: .createBindingMessageContext(objectCtx);
209: sb.read(istreamCtx, context);
210:
211: assertTrue(codec.handleMessage(context));
212: RMProperties rmps = RMContextUtils.retrieveRMProperties(
213: context, false);
214: Collection<AckRequestedType> requested = rmps
215: .getAcksRequested();
216: assertNotNull(requested);
217: assertEquals(1, requested.size());
218: AckRequestedType ar = requested.iterator().next();
219: assertNotNull(ar);
220: assertEquals(ar.getIdentifier().getValue(), SEQ_IDENTIFIER);
221:
222: SequenceType s = rmps.getSequence();
223: assertNotNull(s);
224: assertEquals(s.getIdentifier().getValue(), SEQ_IDENTIFIER);
225: assertEquals(s.getMessageNumber(), MSG2_MESSAGE_NUMBER);
226:
227: assertNull(rmps.getAcks());
228: }
229:
230: private void setUpOutbound() {
231: org.objectweb.celtix.ws.rm.ObjectFactory factory = RMUtils
232: .getWSRMFactory();
233: s1 = factory.createSequenceType();
234: Identifier sid = factory.createIdentifier();
235: sid.setValue("sequence1");
236: s1.setIdentifier(sid);
237: s1.setMessageNumber(BigInteger.ONE);
238: s2 = factory.createSequenceType();
239: sid = factory.createIdentifier();
240: sid.setValue("sequence2");
241: s2.setIdentifier(sid);
242: s2.setMessageNumber(BigInteger.TEN);
243:
244: ack1 = factory.createSequenceAcknowledgement();
245: AcknowledgementRange r = factory
246: .createSequenceAcknowledgementAcknowledgementRange();
247: r.setLower(BigInteger.ONE);
248: r.setUpper(BigInteger.ONE);
249: ack1.getAcknowledgementRange().add(r);
250: ack1.setIdentifier(s1.getIdentifier());
251:
252: ack2 = factory.createSequenceAcknowledgement();
253: r = factory.createSequenceAcknowledgementAcknowledgementRange();
254: r.setLower(BigInteger.ONE);
255: r.setUpper(BigInteger.TEN);
256: ack2.getAcknowledgementRange().add(r);
257: ack2.setIdentifier(s2.getIdentifier());
258:
259: ar1 = factory.createAckRequestedType();
260: ar1.setIdentifier(s1.getIdentifier());
261:
262: ar2 = factory.createAckRequestedType();
263: ar2.setIdentifier(s2.getIdentifier());
264:
265: sb = new SOAPBindingImpl(false);
266: }
267:
268: private SOAPMessageContext setupOutboundContext() throws Exception {
269: ObjectMessageContext objectCtx = new ObjectMessageContextImpl();
270: objectCtx
271: .put(ObjectMessageContext.MESSAGE_INPUT, Boolean.FALSE);
272: objectCtx.put(MESSAGE_OUTBOUND_PROPERTY, Boolean.TRUE);
273: SOAPMessageContext context = (SOAPMessageContext) sb
274: .createBindingMessageContext(objectCtx);
275:
276: MessageFactory msgFactory = MessageFactory.newInstance();
277: SOAPMessage msg = msgFactory.createMessage();
278: msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
279: msg.getSOAPPart().getEnvelope().addNamespaceDeclaration(
280: W3CConstants.NP_SCHEMA_XSD, W3CConstants.NU_SCHEMA_XSD);
281: msg.getSOAPPart().getEnvelope().addNamespaceDeclaration(
282: W3CConstants.NP_SCHEMA_XSI, W3CConstants.NU_SCHEMA_XSI);
283: context.setMessage(msg);
284:
285: RMPropertiesImpl rmps = new RMPropertiesImpl();
286: RMContextUtils.storeRMProperties(context, rmps, true);
287: return context;
288: }
289:
290: private void verifyHeaders(SOAPMessageContext context,
291: String... names) throws SOAPException {
292: SOAPMessage message = context.getMessage();
293: SOAPEnvelope env = message.getSOAPPart().getEnvelope();
294: SOAPHeader header = env.getHeader();
295:
296: // check all expected headers are present
297:
298: for (String name : names) {
299: boolean found = false;
300: Iterator headerElements = header.examineAllHeaderElements();
301: while (headerElements.hasNext()) {
302: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
303: .next();
304: Name headerName = headerElement.getElementName();
305: String localName = headerName.getLocalName();
306: if (headerName.getURI().equals(
307: RMUtils.getRMConstants().getNamespaceURI())
308: && localName.equals(name)) {
309: found = true;
310: break;
311: } else if (headerName
312: .getURI()
313: .equals(
314: org.objectweb.celtix.bus.ws.addressing.Names.WSA_NAMESPACE_NAME)
315: && localName.equals(name)) {
316: found = true;
317: break;
318: }
319: }
320: assertTrue("Could not find header element " + name, found);
321: }
322:
323: // no other headers should be present
324:
325: Iterator headerElements = header.examineAllHeaderElements();
326: while (headerElements.hasNext()) {
327: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
328: .next();
329: Name headerName = headerElement.getElementName();
330: String localName = headerName.getLocalName();
331: assertTrue(RMUtils.getRMConstants().getNamespaceURI()
332: .equals(headerName.getURI())
333: || org.objectweb.celtix.bus.ws.addressing.Names.WSA_NAMESPACE_NAME
334: .equals(headerName.getURI()));
335: boolean found = false;
336: for (String name : names) {
337: if (localName.equals(name)) {
338: found = true;
339: break;
340: }
341: }
342: assertTrue("Unexpected header element " + localName, found);
343: }
344: }
345: }
|