PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/AbstractCxfWsdlFirstTest.java

https://gitlab.com/matticala/apache-camel
Java | 140 lines | 104 code | 17 blank | 19 comment | 2 complexity | ac4a30519182f82e32f161d8ee0819b0 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.cxf;
  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import javax.xml.namespace.QName;
  22. import javax.xml.ws.BindingProvider;
  23. import javax.xml.ws.Holder;
  24. import javax.xml.ws.WebServiceException;
  25. import org.apache.camel.Exchange;
  26. import org.apache.camel.Processor;
  27. import org.apache.camel.component.cxf.common.message.CxfConstants;
  28. import org.apache.camel.test.spring.CamelSpringTestSupport;
  29. import org.apache.camel.wsdl_first.JaxwsTestHandler;
  30. import org.apache.camel.wsdl_first.Person;
  31. import org.apache.camel.wsdl_first.PersonService;
  32. import org.apache.camel.wsdl_first.UnknownPersonFault;
  33. import org.junit.Test;
  34. public abstract class AbstractCxfWsdlFirstTest extends CamelSpringTestSupport {
  35. static int port1 = CXFTestSupport.getPort1();
  36. public static int getPort1() {
  37. return port1;
  38. }
  39. public static int getPort2() {
  40. return CXFTestSupport.getPort2();
  41. }
  42. @Override
  43. public boolean isCreateCamelContextPerClass() {
  44. return true;
  45. }
  46. @Test
  47. public void testInvokingServiceFromCXFClient() throws Exception {
  48. JaxwsTestHandler fromHandler = getMandatoryBean(JaxwsTestHandler.class, "fromEndpointJaxwsHandler");
  49. fromHandler.reset();
  50. JaxwsTestHandler toHandler = getMandatoryBean(JaxwsTestHandler.class, "toEndpointJaxwsHandler");
  51. toHandler.reset();
  52. URL wsdlURL = getClass().getClassLoader().getResource("person.wsdl");
  53. PersonService ss = new PersonService(wsdlURL, new QName("http://camel.apache.org/wsdl-first", "PersonService"));
  54. Person client = ss.getSoap();
  55. ((BindingProvider)client).getRequestContext()
  56. .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
  57. "http://localhost:" + getPort2() + "/" + getClass().getSimpleName()
  58. + "/PersonService/");
  59. Holder<String> personId = new Holder<>();
  60. personId.value = "hello";
  61. Holder<String> ssn = new Holder<>();
  62. Holder<String> name = new Holder<>();
  63. client.getPerson(personId, ssn, name);
  64. assertEquals("we should get the right answer from router", "Bonjour", name.value);
  65. personId.value = "";
  66. try {
  67. client.getPerson(personId, ssn, name);
  68. fail("We expect to get the UnknowPersonFault here");
  69. } catch (UnknownPersonFault fault) {
  70. // We expect to get fault here
  71. }
  72. personId.value = "Invoking getPerson with invalid length string, expecting exception...xxxxxxxxx";
  73. try {
  74. client.getPerson(personId, ssn, name);
  75. fail("We expect to get the WebSerivceException here");
  76. } catch (WebServiceException ex) {
  77. // Caught expected WebServiceException here
  78. assertTrue("Should get the xml vaildate error! " + ex.getMessage(),
  79. ex.getMessage().indexOf("MyStringType") > 0
  80. || ex.getMessage().indexOf("Could not parse the XML stream") != -1);
  81. }
  82. verifyJaxwsHandlers(fromHandler, toHandler);
  83. }
  84. protected void verifyJaxwsHandlers(JaxwsTestHandler fromHandler, JaxwsTestHandler toHandler) {
  85. assertEquals(2, fromHandler.getFaultCount());
  86. assertEquals(4, fromHandler.getMessageCount());
  87. // Changed to use noErrorhandler and now the message will not be sent again.
  88. assertEquals(3, toHandler.getMessageCount());
  89. assertEquals(1, toHandler.getFaultCount());
  90. }
  91. @Test
  92. @SuppressWarnings("unchecked")
  93. public void testInvokingServiceWithCamelProducer() throws Exception {
  94. Exchange exchange = sendJaxWsMessageWithHolders("hello");
  95. assertEquals("The request should be handled sucessfully ", exchange.isFailed(), false);
  96. org.apache.camel.Message out = exchange.getOut();
  97. List<Object> result = out.getBody(List.class);
  98. assertEquals("The result list should not be empty", result.size(), 4);
  99. Holder<String> name = (Holder<String>) result.get(3);
  100. assertEquals("we should get the right answer from router", "Bonjour", name.value);
  101. exchange = sendJaxWsMessageWithHolders("");
  102. assertEquals("We should get a fault here", exchange.isFailed(), true);
  103. Throwable ex = exchange.getException();
  104. assertTrue("We should get the UnknowPersonFault here", ex instanceof UnknownPersonFault);
  105. }
  106. protected Exchange sendJaxWsMessageWithHolders(final String personIdString) {
  107. Exchange exchange = template.send("direct:producer", new Processor() {
  108. public void process(final Exchange exchange) {
  109. final List<Object> params = new ArrayList<>();
  110. Holder<String> personId = new Holder<>();
  111. personId.value = personIdString;
  112. params.add(personId);
  113. Holder<String> ssn = new Holder<>();
  114. Holder<String> name = new Holder<>();
  115. params.add(ssn);
  116. params.add(name);
  117. exchange.getIn().setBody(params);
  118. exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "GetPerson");
  119. }
  120. });
  121. return exchange;
  122. }
  123. }