1 package org.objectweb.celtix.bus.ws.addressing; 2 3 4 import java.lang.reflect.Method ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import javax.wsdl.Binding; 9 import javax.wsdl.Port; 10 import javax.wsdl.extensions.ExtensibilityElement; 11 import javax.xml.namespace.QName ; 12 import javax.xml.ws.RequestWrapper; 13 import javax.xml.ws.ResponseWrapper; 14 import javax.xml.ws.handler.LogicalMessageContext; 15 import javax.xml.ws.handler.MessageContext; 16 import static javax.xml.ws.handler.MessageContext.MESSAGE_OUTBOUND_PROPERTY; 17 18 import junit.framework.TestCase; 19 20 import org.easymock.EasyMock; 21 import org.easymock.IArgumentMatcher; 22 import org.easymock.IMocksControl; 23 24 import org.objectweb.celtix.bindings.DataBindingCallback; 25 import org.objectweb.celtix.bindings.ServerBinding; 26 import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback; 27 import org.objectweb.celtix.context.OutputStreamMessageContext; 28 import org.objectweb.celtix.transports.ClientTransport; 29 import org.objectweb.celtix.transports.ServerTransport; 30 import org.objectweb.celtix.ws.addressing.AttributedURIType; 31 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 32 33 import static org.objectweb.celtix.bus.bindings.soap.SOAPConstants.SOAP_ENV_ENCSTYLE; 34 import static org.objectweb.celtix.context.ObjectMessageContext.METHOD_OBJ; 35 import static org.objectweb.celtix.context.ObjectMessageContext.REQUESTOR_ROLE_PROPERTY; 36 import static org.objectweb.celtix.context.OutputStreamMessageContext.ONEWAY_MESSAGE_TF; 37 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES; 38 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND; 39 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_INBOUND; 40 import static org.objectweb.celtix.ws.addressing.JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_OUTBOUND; 41 42 43 44 45 public class MAPAggregatorTest extends TestCase { 46 47 private MAPAggregator aggregator; 48 private IMocksControl control; 49 private AddressingPropertiesImpl expectedMAPs; 50 private String expectedTo; 51 private String expectedReplyTo; 52 private String expectedRelatesTo; 53 private String expectedAction; 54 55 56 57 public void setUp() { 58 aggregator = new MAPAggregator(); 59 aggregator.init(null); 60 control = EasyMock.createNiceControl(); 61 } 62 63 public void tearDown() { 64 aggregator.destroy(); 65 expectedMAPs = null; 66 expectedTo = null; 67 expectedReplyTo = null; 68 expectedRelatesTo = null; 69 expectedAction = null; 70 } 71 72 public void testRequestorOutboundUsingAddressingMAPsInContext() 73 throws Exception { 74 LogicalMessageContext context = setUpContext(true, true, false, true, true); 75 boolean proceed = aggregator.handleMessage(context); 76 assertTrue("expected dispatch to proceed", proceed); 77 control.verify(); 78 aggregator.close(context); 79 } 80 81 public void testRequestorOutboundUsingAddressingMAPsInContextZeroLengthAction() 82 throws Exception { 83 LogicalMessageContext context = setUpContext(true, true, false, true, true, true); 84 boolean proceed = aggregator.handleMessage(context); 85 assertTrue("expected dispatch to proceed", proceed); 86 control.verify(); 87 aggregator.close(context); 88 } 89 90 public void testRequestorOutboundUsingAddressingMAPsInContextFault() 91 throws Exception { 92 LogicalMessageContext context = setUpContext(true, true, false, true, true); 93 boolean proceed = aggregator.handleFault(context); 94 assertTrue("expected dispatch to proceed", proceed); 95 control.verify(); 96 aggregator.close(context); 97 } 98 99 public void testRequestorOutboundUsingAddressingNoMAPsInContext() 100 throws Exception { 101 LogicalMessageContext context = setUpContext(true, true, false, true, false); 102 boolean proceed = aggregator.handleMessage(context); 103 assertTrue("expected dispatch to proceed", proceed); 104 control.verify(); 105 aggregator.close(context); 106 } 107 108 public void testRequestorOutboundUsingAddressingNoMAPsInContextFault() 109 throws Exception { 110 LogicalMessageContext context = setUpContext(true, true, false, true, false); 111 boolean proceed = aggregator.handleFault(context); 112 assertTrue("expected dispatch to proceed", proceed); 113 control.verify(); 114 aggregator.close(context); 115 } 116 117 public void testRequestorOutboundNotUsingAddressing() throws Exception { 118 LogicalMessageContext context = setUpContext(true, true, false, false); 119 boolean proceed = aggregator.handleMessage(context); 120 assertTrue("expected dispatch to proceed", proceed); 121 control.verify(); 122 aggregator.close(context); 123 } 124 125 public void testRequestorOutboundNotUsingAddressingFault() 126 throws Exception { 127 LogicalMessageContext context = setUpContext(true, true, false, false); 128 boolean proceed = aggregator.handleFault(context); 129 assertTrue("expected dispatch to proceed", proceed); 130 control.verify(); 131 aggregator.close(context); 132 } 133 134 public void testRequestorOutboundOnewayUsingAddressingMAPsInContext() 135 throws Exception { 136 LogicalMessageContext context = setUpContext(true, true, true, true, true); 137 boolean proceed = aggregator.handleMessage(context); 138 assertTrue("expected dispatch to proceed", proceed); 139 control.verify(); 140 aggregator.close(context); 141 } 142 143 public void testRequestorOutboundOnewayUsingAddressingMAPsInContextFault() 144 throws Exception { 145 LogicalMessageContext context = setUpContext(true, true, true, true, true); 146 boolean proceed = aggregator.handleFault(context); 147 assertTrue("expected dispatch to proceed", proceed); 148 control.verify(); 149 aggregator.close(context); 150 } 151 152 public void testRequestorOutboundOnewayUsingAddressingNoMAPsInContext() 153 throws Exception { 154 LogicalMessageContext context = setUpContext(true, true, true, true, false); 155 boolean proceed = aggregator.handleMessage(context); 156 assertTrue("expected dispatch to proceed", proceed); 157 control.verify(); 158 aggregator.close(context); 159 } 160 161 public void testRequestorOutboundOnewayUsingAddressingNoMAPsInContextFault() 162 throws Exception { 163 LogicalMessageContext context = setUpContext(true, true, true, true, false); 164 boolean proceed = aggregator.handleFault(context); 165 assertTrue("expected dispatch to proceed", proceed); 166 control.verify(); 167 aggregator.close(context); 168 } 169 170 public void testRequestorOutboundOnewayNotUsingAddressing() throws Exception { 171 LogicalMessageContext context = setUpContext(true, true, true, false); 172 boolean proceed = aggregator.handleMessage(context); 173 assertTrue("expected dispatch to proceed", proceed); 174 control.verify(); 175 aggregator.close(context); 176 } 177 178 public void testRequestorOutboundOnewayNotUsingAddressingFault() 179 throws Exception { 180 LogicalMessageContext context = setUpContext(true, true, true, false); 181 boolean proceed = aggregator.handleFault(context); 182 assertTrue("expected dispatch to proceed", proceed); 183 control.verify(); 184 aggregator.close(context); 185 } 186 187 public void testResponderInboundValidMAPs() throws Exception { 188 LogicalMessageContext context = setUpContext(false, false, false); 189 boolean proceed = aggregator.handleMessage(context); 190 assertTrue("expected dispatch to proceed", proceed); 191 control.verify(); 192 aggregator.close(context); 193 } 194 195 public void testResponderInboundDecoupled() throws Exception { 196 LogicalMessageContext context = 197 setUpContext(false, false, false, true, false, true); 198 boolean proceed = aggregator.handleMessage(context); 199 assertTrue("expected dispatch to proceed", proceed); 200 control.verify(); 201 aggregator.close(context); 202 } 203 204 public void testResponderInboundOneway() throws Exception { 205 LogicalMessageContext context = 206 setUpContext(false, false, true, true, false, true); 207 boolean proceed = aggregator.handleMessage(context); 208 assertTrue("expected dispatch to proceed", proceed); 209 control.verify(); 210 aggregator.close(context); 211 } 212 213 public void testResponderInboundValidMAPsFault() throws Exception { 214 LogicalMessageContext context = setUpContext(false, false, false); 215 boolean proceed = aggregator.handleFault(context); 216 assertTrue("expected dispatch to proceed", proceed); 217 control.verify(); 218 aggregator.close(context); 219 } 220 221 public void testResponderInboundInvalidMAPs() throws Exception { 222 aggregator.messageIDs.put("urn:uuid:12345", "urn:uuid:12345"); 223 LogicalMessageContext context = setUpContext(false, false, false); 224 boolean proceed = aggregator.handleMessage(context); 225 assertFalse("expected dispatch not to proceed", proceed); 226 control.verify(); 227 aggregator.close(context); 228 } 229 230 public void testResponderInboundInvalidMAPsFault() throws Exception { 231 aggregator.messageIDs.put("urn:uuid:12345", "urn:uuid:12345"); 232 LogicalMessageContext context = setUpContext(false, false, false); 233 boolean proceed = aggregator.handleFault(context); 234 assertFalse("expected dispatch not to proceed", proceed); 235 control.verify(); 236 aggregator.close(context); 237 } 238 239 public void testResponderOutbound() throws Exception { 240 LogicalMessageContext context = setUpContext(false, true, false); 241 boolean proceed = aggregator.handleMessage(context); 242 assertTrue("expected dispatch to proceed", proceed); 243 control.verify(); 244 aggregator.close(context); 245 } 246 247 public void testResponderOutboundZeroLengthAction() throws Exception { 248 LogicalMessageContext context = 249 setUpContext(false, true, false, false, false, false, true); 250 boolean proceed = aggregator.handleMessage(context); 251 assertTrue("expected dispatch to proceed", proceed); 252 control.verify(); 253 aggregator.close(context); 254 } 255 256 public void testResponderOutboundFault() throws Exception { 257 LogicalMessageContext context = setUpContext(false, true, false); 258 boolean proceed = aggregator.handleFault(context); 259 assertTrue("expected dispatch to proceed", proceed); 260 control.verify(); 261 aggregator.close(context); 262 } 263 264 public void testRequestorInbound() throws Exception { 265 LogicalMessageContext context = setUpContext(true, true, false); 266 boolean proceed = aggregator.handleMessage(context); 267 assertTrue("expected dispatch to proceed", proceed); 268 control.verify(); 269 aggregator.close(context); 270 } 271 272 public void testRequestorInboundFault() throws Exception { 273 LogicalMessageContext context = setUpContext(true, true, false); 274 boolean proceed = aggregator.handleFault(context); 275 assertTrue("expected dispatch to proceed", proceed); 276 control.verify(); 277 aggregator.close(context); 278 } 279 280 private LogicalMessageContext setUpContext(boolean requestor, 281 boolean outbound, 282 boolean oneway) 283 throws Exception { 284 return setUpContext(requestor, outbound, oneway, false, false, false); 285 } 286 287 private LogicalMessageContext setUpContext(boolean requestor, 288 boolean outbound, 289 boolean oneway, 290 boolean usingAddressing) 291 throws Exception { 292 return setUpContext(requestor, 293 outbound, 294 oneway, 295 usingAddressing, 296 false, 297 false); 298 } 299 300 private LogicalMessageContext setUpContext(boolean requestor, 301 boolean outbound, 302 boolean oneway, 303 boolean usingAddressing, 304 boolean mapsInContext) 305 throws Exception { 306 return setUpContext(requestor, 307 outbound, 308 oneway, 309 usingAddressing, 310 mapsInContext, 311 false); 312 } 313 314 private LogicalMessageContext setUpContext(boolean requestor, 315 boolean outbound, 316 boolean oneway, 317 boolean usingAddressing, 318 boolean mapsInContext, 319 boolean decoupled) 320 throws Exception { 321 return setUpContext(requestor, 322 outbound, 323 oneway, 324 usingAddressing, 325 mapsInContext, 326 decoupled, 327 false); 328 } 329 330 private LogicalMessageContext setUpContext(boolean requestor, 331 boolean outbound, 332 boolean oneway, 333 boolean usingAddressing, 334 boolean mapsInContext, 335 boolean decoupled, 336 boolean zeroLengthAction) 337 throws Exception { 338 339 LogicalMessageContext context = 340 control.createMock(LogicalMessageContext.class); 341 context.get(MESSAGE_OUTBOUND_PROPERTY); 342 EasyMock.expectLastCall().andReturn(Boolean.valueOf(outbound)); 343 context.get(REQUESTOR_ROLE_PROPERTY); 344 EasyMock.expectLastCall().andReturn(Boolean.valueOf(requestor)); 345 if (outbound && requestor) { 346 setUpUsingAddressing(context, usingAddressing); 347 if (usingAddressing) { 348 setUpRequestor(context, 349 oneway, 350 mapsInContext, 351 decoupled, 352 zeroLengthAction); 353 } 354 } else if (!requestor) { 355 setUpResponder(context, 356 oneway, 357 outbound, 358 decoupled, 359 zeroLengthAction); 360 } 361 control.replay(); 362 return context; 363 } 364 365 private void setUpUsingAddressing(LogicalMessageContext context, 366 boolean usingAddressing) { 367 Port port = control.createMock(Port.class); 368 aggregator.clientTransport = control.createMock(ClientTransport.class); 369 aggregator.clientTransport.getPort(); 370 EasyMock.expectLastCall().andReturn(port); 371 List portExts = control.createMock(List .class); 372 port.getExtensibilityElements(); 373 EasyMock.expectLastCall().andReturn(portExts); 374 Iterator portItr = control.createMock(Iterator .class); 375 portExts.iterator(); 376 EasyMock.expectLastCall().andReturn(portItr); 377 Binding binding = control.createMock(Binding.class); 378 port.getBinding(); 379 EasyMock.expectLastCall().andReturn(binding); 380 List bindingExts = control.createMock(List .class); 381 binding.getExtensibilityElements(); 382 EasyMock.expectLastCall().andReturn(bindingExts); 383 Iterator bindingItr = control.createMock(Iterator .class); 384 bindingExts.iterator(); 385 EasyMock.expectLastCall().andReturn(bindingItr); 386 portItr.hasNext(); 387 EasyMock.expectLastCall().andReturn(Boolean.TRUE); 388 ExtensibilityElement ext = 389 control.createMock(ExtensibilityElement.class); 390 portItr.next(); 391 EasyMock.expectLastCall().andReturn(ext); 392 QName elementType = usingAddressing 393 ? Names.WSAW_USING_ADDRESSING_QNAME 394 : SOAP_ENV_ENCSTYLE; 395 ext.getElementType(); 396 EasyMock.expectLastCall().andReturn(elementType); 397 if (!usingAddressing) { 398 portItr.hasNext(); 399 EasyMock.expectLastCall().andReturn(Boolean.FALSE); 400 bindingItr.hasNext(); 401 EasyMock.expectLastCall().andReturn(Boolean.FALSE); 402 } 403 } 404 405 private void setUpRequestor(LogicalMessageContext context, 406 boolean oneway, 407 boolean mapsInContext, 408 boolean decoupled, 409 boolean zeroLengthAction) throws Exception { 410 context.get(REQUESTOR_ROLE_PROPERTY); 411 EasyMock.expectLastCall().andReturn(Boolean.TRUE); 412 AddressingPropertiesImpl maps = mapsInContext 413 ? new AddressingPropertiesImpl() 414 : null; 415 if (zeroLengthAction) { 416 maps.setAction(ContextUtils.getAttributedURI("")); 417 } 418 context.get(CLIENT_ADDRESSING_PROPERTIES); 419 EasyMock.expectLastCall().andReturn(maps); 420 Method method = SEI.class.getMethod("op", new Class [0]); 421 if (!zeroLengthAction) { 422 context.get(METHOD_OBJ); 423 EasyMock.expectLastCall().andReturn(method); 424 context.get(REQUESTOR_ROLE_PROPERTY); 425 EasyMock.expectLastCall().andReturn(Boolean.TRUE); 426 expectedAction = "http://foo/bar/SEI/opRequest"; 427 } 428 context.get(REQUESTOR_ROLE_PROPERTY); 429 EasyMock.expectLastCall().andReturn(Boolean.TRUE); 430 context.get(ONEWAY_MESSAGE_TF); 431 EasyMock.expectLastCall().andReturn(Boolean.valueOf(oneway)); 432 EasyMock.eq(CLIENT_ADDRESSING_PROPERTIES_OUTBOUND); 433 expectedMAPs = maps; 434 expectedTo = Names.WSA_NONE_ADDRESS; 435 expectedReplyTo = oneway 436 ? Names.WSA_NONE_ADDRESS 437 : Names.WSA_ANONYMOUS_ADDRESS; 438 EasyMock.reportMatcher(new MAPMatcher()); 439 context.put(CLIENT_ADDRESSING_PROPERTIES_OUTBOUND, 440 mapsInContext 441 ? maps 442 : new AddressingPropertiesImpl()); 443 EasyMock.expectLastCall().andReturn(null); 444 context.setScope(CLIENT_ADDRESSING_PROPERTIES_OUTBOUND, 445 MessageContext.Scope.HANDLER); 446 } 447 448 private void setUpResponder(LogicalMessageContext context, 449 boolean oneway, 450 boolean outbound, 451 boolean decoupled, 452 boolean zeroLengthAction) throws Exception { 453 context.get(REQUESTOR_ROLE_PROPERTY); 454 EasyMock.expectLastCall().andReturn(Boolean.FALSE); 455 AddressingPropertiesImpl maps = new AddressingPropertiesImpl(); 456 EndpointReferenceType replyTo = new EndpointReferenceType(); 457 replyTo.setAddress( 458 ContextUtils.getAttributedURI(decoupled 459 ? "http://localhost:9999/decoupled" 460 : Names.WSA_ANONYMOUS_ADDRESS)); 461 maps.setReplyTo(replyTo); 462 AttributedURIType id = 463 ContextUtils.getAttributedURI("urn:uuid:12345"); 464 maps.setMessageID(id); 465 if (zeroLengthAction) { 466 maps.setAction(ContextUtils.getAttributedURI("")); 467 } 468 context.get(SERVER_ADDRESSING_PROPERTIES_INBOUND); 469 EasyMock.expectLastCall().andReturn(maps); 470 if (oneway || decoupled) { 471 context.get(ONEWAY_MESSAGE_TF); 472 EasyMock.expectLastCall().andReturn(Boolean.valueOf(oneway)); 473 aggregator.serverBinding = control.createMock(ServerBinding.class); 474 aggregator.serverTransport = control.createMock(ServerTransport.class); 475 OutputStreamMessageContext outputContext = 476 control.createMock(OutputStreamMessageContext.class); 477 aggregator.serverTransport.rebase(context, replyTo); 478 EasyMock.expectLastCall().andReturn(outputContext); 479 DataBindingCallback callback = 480 new JAXBDataBindingCallback(null, 481 DataBindingCallback.Mode.PARTS, 482 ContextUtils.getJAXBContext()); 483 EasyMock.reportMatcher(new PartialResponseMatcher()); 484 EasyMock.reportMatcher(new PartialResponseMatcher()); 485 aggregator.serverBinding.partialResponse(outputContext, callback); 486 EasyMock.expectLastCall(); 487 } 488 if (outbound || aggregator.messageIDs.size() > 0) { 489 if (!zeroLengthAction) { 490 Method method = SEI.class.getMethod("op", new Class [0]); 491 context.get(METHOD_OBJ); 492 EasyMock.expectLastCall().andReturn(method); 493 context.get(REQUESTOR_ROLE_PROPERTY); 494 EasyMock.expectLastCall().andReturn(Boolean.FALSE); 495 expectedAction = "http://foo/bar/SEI/opResponse"; 496 } 497 context.get(REQUESTOR_ROLE_PROPERTY); 498 EasyMock.expectLastCall().andReturn(Boolean.FALSE); 499 context.get(SERVER_ADDRESSING_PROPERTIES_INBOUND); 500 EasyMock.expectLastCall().andReturn(maps); 501 EasyMock.eq(SERVER_ADDRESSING_PROPERTIES_OUTBOUND); 502 expectedTo = Names.WSA_ANONYMOUS_ADDRESS; 503 expectedRelatesTo = maps.getMessageID().getValue(); 504 EasyMock.reportMatcher(new MAPMatcher()); 505 context.put(SERVER_ADDRESSING_PROPERTIES_OUTBOUND, 506 new AddressingPropertiesImpl()); 507 EasyMock.expectLastCall().andReturn(null); 508 context.setScope(SERVER_ADDRESSING_PROPERTIES_OUTBOUND, 509 MessageContext.Scope.HANDLER); 510 } 511 } 512 513 private final class MAPMatcher implements IArgumentMatcher { 514 public boolean matches(Object obj) { 515 if (obj instanceof AddressingPropertiesImpl) { 516 AddressingPropertiesImpl other = (AddressingPropertiesImpl)obj; 517 return compareExpected(other); 518 } 519 return false; 520 } 521 522 public void appendTo(StringBuffer buffer) { 523 buffer.append("MAPs did not match"); 524 } 525 526 private boolean compareExpected(AddressingPropertiesImpl other) { 527 boolean ret = false; 528 if (expectedMAPs == null || expectedMAPs == other) { 529 boolean toOK = 530 expectedTo == null 531 || expectedTo.equals(other.getTo().getValue()); 532 boolean replyToOK = 533 expectedReplyTo == null 534 || expectedReplyTo.equals( 535 other.getReplyTo().getAddress().getValue()); 536 boolean relatesToOK = 537 expectedRelatesTo == null 538 || expectedRelatesTo.equals( 539 other.getRelatesTo().getValue()); 540 boolean actionOK = 541 expectedAction == null 542 || expectedAction.equals(other.getAction().getValue()); 543 boolean messageIdOK = other.getMessageID() != null; 544 ret = toOK 545 && replyToOK 546 && relatesToOK 547 && actionOK 548 && messageIdOK; 549 } 550 return ret; 551 } 552 } 553 554 private final class PartialResponseMatcher implements IArgumentMatcher { 555 public boolean matches(Object obj) { 556 return true; 557 } 558 559 public void appendTo(StringBuffer buffer) { 560 buffer.append("partial response args did not match"); 561 } 562 } 563 564 private static interface SEI { 565 @RequestWrapper(targetNamespace = "http://foo/bar", className = "SEI", localName = "opRequest") 566 @ResponseWrapper(targetNamespace = "http://foo/bar", className = "SEI", localName = "opResponse") 567 String op(); 568 } 569 } 570 | Popular Tags |