EncodedBodyBuilder extends AbstractBodyBuilder { @Override public SOAPElement build(Map inputMap) throws WSDLException, ParserConfigurationException, SOAPException, IOException, SAXException, UnknownOperationException { SOAPElement result = super.build(inputMap); for (Iterator iterator = namespaceMappings.keySet().iterator(); iterator.hasNext();) { String namespaceURI = (String) iterator.next(); String ns = namespaceMappings.get(namespaceURI); result.addNamespaceDeclaration(ns, namespaceURI); } result.setAttributeNS(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE, "soapenv:encodingStyle", SOAPConstants.URI_NS_SOAP_ENCODING); return result; } EncodedBodyBuilder(String style, WSDLParser parser, String operationName, List<TypeDescriptor> inputDescriptors); @Override SOAPElement build(Map inputMap);  }
@Test public void testSimpleCase() throws Exception { Map<String,Object> inputMap = new HashMap<String, Object>(); BodyBuilder builder = createBuilder(wsdlResourcePath("TestServices-rpcencoded.wsdl"), "countString"); assertTrue("Wrong type of builder created",builder instanceof EncodedBodyBuilder); inputMap.put("str", "Roger Ramjet"); SOAPElement body = builder.build(inputMap); Iterator<SOAPElement> children = body.getChildElements(); assertTrue("empty body content", children.hasNext()); SOAPElement child = children.next(); assertTrue("wrong body content (must be '{}str')", "str".equals(child.getLocalName()) && child.getNamespaceURI() == null); String type = child.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); assertNotNull("missing xsi:type", type); assertTrue("wrong xsi:type", "xsd:string".equals(type)); assertTrue("wrong text value", "Roger Ramjet".equals(child.getTextContent())); }
@Test public void testStringArray() throws Exception { Map<String,Object> inputMap = new HashMap<String, Object>(); BodyBuilder builder = createBuilder(wsdlResourcePath("TestServices-rpcencoded.wsdl"), "countStringArray"); assertTrue("Wrong type of builder created",builder instanceof EncodedBodyBuilder); List<String> array = new ArrayList<String>(); array.add("one"); array.add("two"); array.add("three"); inputMap.put("array", array); SOAPElement body = builder.build(inputMap); Iterator<SOAPElement> children = body.getChildElements(); assertTrue("missing body element", children.hasNext()); SOAPElement child = children.next(); assertTrue("wrong body element (must be '{}array')", "array".equals(child.getLocalName()) && child.getNamespaceURI() == null); String type = child.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); assertNotNull("missing xsi:type", type); assertTrue("wrong xsi:type", "soapenc:Array".equals(type)); Iterator<Element> elements = child.getChildElements(); for (int i = 0, n = array.size(); i < n; i++) { assertTrue("missing array element", elements.hasNext()); Element element = elements.next(); assertTrue("wrong array element (must be '{}string')", "string".equals(element.getLocalName()) && element.getNamespaceURI() == null); assertTrue("wrong array text content", array.get(i).equals(element.getTextContent())); } }
@Test public void testComplexType() throws Exception { BodyBuilder builder = createBuilder(wsdlResourcePath("TestServices-rpcencoded.wsdl"), "personToString"); assertTrue("Wrong type of builder created",builder instanceof EncodedBodyBuilder); String p = "<Person xmlns:xsi=\"http: Map<String,Object> inputMap = new HashMap<String, Object>(); inputMap.put("p",p); SOAPElement body = builder.build(inputMap); Iterator<SOAPElement> persons = body.getChildElements(new QName("", "p")); assertTrue("'Person' tag is missing", persons.hasNext()); SOAPElement person = persons.next(); assertFalse("more than one 'Person' tag found", persons.hasNext()); String personType = person.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); assertNotNull("type definition of Person is missing", personType); assertTrue("wrong xsi:type for the 'Person' tag", "ns1:Person".equals(personType)); assertTrue("wrong type definition for Person", "ns1:Person".equals(personType)); Iterator<SOAPElement> names = person.getChildElements(new QName("", "name")); assertTrue("'name' tag is missing", names.hasNext()); SOAPElement name = names.next(); assertFalse("More than one 'name' tag found", names.hasNext()); String nameType = name.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); assertNotNull("type definition of name is missing", nameType); assertTrue("wrong name's xsi:type", "xsd:string".equals(nameType)); assertTrue("wrong 'name' tag value (must be 'bob')", "bob".equals(name.getTextContent())); Iterator<SOAPElement> ages = person.getChildElements(new QName("", "age")); assertTrue("'age' tag is missing", ages.hasNext()); SOAPElement age = ages.next(); assertFalse("more than one 'age' tag found", ages.hasNext()); String ageType = age.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); assertNotNull("missing type definition for the 'age' tag", ageType); assertTrue("wrong xsi:type for 'age' tag", "xsd:int".equals(ageType)); assertTrue("wrong 'age' tag value (must be 12)", "12".equals(age.getTextContent())); }