001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.jaxb;
019:
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import javax.xml.bind.JAXBContext;
026: import javax.xml.namespace.QName;
027: import javax.xml.stream.XMLInputFactory;
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.stream.XMLStreamWriter;
030:
031: import org.apache.cxf.Bus;
032: import org.apache.cxf.BusFactory;
033: import org.apache.cxf.binding.Binding;
034: import org.apache.cxf.binding.BindingFactory;
035: import org.apache.cxf.binding.BindingFactoryManager;
036: import org.apache.cxf.endpoint.Endpoint;
037: import org.apache.cxf.endpoint.EndpointImpl;
038: import org.apache.cxf.interceptor.DocLiteralInInterceptor;
039: import org.apache.cxf.interceptor.Interceptor;
040: import org.apache.cxf.message.Exchange;
041: import org.apache.cxf.message.ExchangeImpl;
042: import org.apache.cxf.message.Message;
043: import org.apache.cxf.message.MessageImpl;
044: import org.apache.cxf.phase.PhaseInterceptorChain;
045: import org.apache.cxf.service.Service;
046: import org.apache.cxf.service.model.BindingInfo;
047: import org.apache.cxf.service.model.BindingOperationInfo;
048: import org.apache.cxf.service.model.EndpointInfo;
049: import org.apache.cxf.service.model.ServiceInfo;
050: import org.apache.cxf.staxutils.StaxUtils;
051: import org.apache.cxf.wsdl11.WSDLServiceFactory;
052: import org.apache.hello_world_doc_lit_bare.types.TradePriceData;
053: import org.apache.hello_world_soap_http.types.GreetMe;
054: import org.apache.hello_world_soap_http.types.GreetMeResponse;
055: import org.easymock.classextension.IMocksControl;
056: import org.junit.Assert;
057: import org.junit.Before;
058: import org.junit.Test;
059:
060: import static org.easymock.EasyMock.expect;
061: import static org.easymock.classextension.EasyMock.createNiceControl;
062:
063: public class DocLiteralInInterceptorTest extends Assert {
064: PhaseInterceptorChain chain;
065: MessageImpl message;
066: Bus bus;
067: ServiceInfo serviceInfo;
068: BindingInfo bindingInfo;
069: Service service;
070: EndpointInfo endpointInfo;
071: EndpointImpl endpoint;
072: BindingOperationInfo operation;
073:
074: @Before
075: public void setUp() throws Exception {
076: bus = BusFactory.newInstance().createBus();
077:
078: BindingFactoryManager bfm = bus
079: .getExtension(BindingFactoryManager.class);
080:
081: IMocksControl control = createNiceControl();
082: BindingFactory bf = control.createMock(BindingFactory.class);
083: Binding binding = control.createMock(Binding.class);
084: expect(bf.createBinding(null)).andStubReturn(binding);
085: expect(binding.getInFaultInterceptors()).andStubReturn(
086: new ArrayList<Interceptor>());
087: expect(binding.getOutFaultInterceptors()).andStubReturn(
088: new ArrayList<Interceptor>());
089:
090: bfm.registerBindingFactory(
091: "http://schemas.xmlsoap.org/wsdl/soap/", bf);
092: }
093:
094: @Test
095: public void testInterceptorInboundWrapped() throws Exception {
096: setUpUsingHelloWorld();
097:
098: //WrappedInInterceptor interceptor = new WrappedInInterceptor();
099: DocLiteralInInterceptor interceptor = new DocLiteralInInterceptor();
100:
101: message.setContent(XMLStreamReader.class, XMLInputFactory
102: .newInstance().createXMLStreamReader(
103: getTestStream(getClass(),
104: "resources/GreetMeDocLiteralReq.xml")));
105: XMLStreamReader reader = (XMLStreamReader) message
106: .getContent(XMLStreamReader.class);
107: // skip the start element of soap body
108: StaxUtils.skipToStartOfElement(reader);
109:
110: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
111:
112: interceptor.handleMessage(message);
113:
114: assertNull(message.getContent(Exception.class));
115:
116: List<?> parameters = message.getContent(List.class);
117: assertEquals(1, parameters.size());
118:
119: Object obj = parameters.get(0);
120: assertTrue(obj instanceof GreetMe);
121: GreetMe greet = (GreetMe) obj;
122: assertEquals("TestSOAPInputPMessage", greet.getRequestType());
123: }
124:
125: @Test
126: public void testInterceptorInboundBare() throws Exception {
127: setUpUsingDocLit();
128:
129: DocLiteralInInterceptor interceptor = new DocLiteralInInterceptor();
130: message.setContent(XMLStreamReader.class, XMLInputFactory
131: .newInstance().createXMLStreamReader(
132: getTestStream(getClass(),
133: "resources/sayHiDocLitBareReq.xml")));
134: XMLStreamReader reader = (XMLStreamReader) message
135: .getContent(XMLStreamReader.class);
136: // skip the start element of soap body
137: StaxUtils.skipToStartOfElement(reader);
138:
139: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
140:
141: interceptor.handleMessage(message);
142:
143: assertNull(message.getContent(Exception.class));
144:
145: List<?> parameters = message.getContent(List.class);
146: assertEquals(1, parameters.size());
147:
148: Object obj = parameters.get(0);
149: assertTrue(obj instanceof TradePriceData);
150: TradePriceData greet = (TradePriceData) obj;
151: assertTrue(1.0 == greet.getTickerPrice());
152: assertEquals("CXF", greet.getTickerSymbol());
153: }
154:
155: @Test
156: public void testInterceptorInboundBareNoParameter()
157: throws Exception {
158: setUpUsingDocLit();
159:
160: DocLiteralInInterceptor interceptor = new DocLiteralInInterceptor();
161: message
162: .setContent(
163: XMLStreamReader.class,
164: XMLInputFactory
165: .newInstance()
166: .createXMLStreamReader(
167: getTestStream(getClass(),
168: "resources/bareNoParamDocLitBareReq.xml")));
169:
170: XMLStreamReader reader = (XMLStreamReader) message
171: .getContent(XMLStreamReader.class);
172: // skip the start element of soap body, so that we can serve an empty request to
173: // interceptor
174: StaxUtils.skipToStartOfElement(reader);
175: StaxUtils.nextEvent(reader);
176:
177: message.put(Message.INBOUND_MESSAGE, Message.INBOUND_MESSAGE);
178:
179: interceptor.handleMessage(message);
180:
181: assertNull(message.getContent(Exception.class));
182:
183: List<?> parameters = message.getContent(List.class);
184: assertNull(parameters);
185: }
186:
187: //TODO: remove duplicate code in setUpUsingHelloWorld and setUpUsingDocLit
188: private void setUpUsingHelloWorld() throws Exception {
189: String ns = "http://apache.org/hello_world_soap_http";
190: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
191: getClass().getResource("/wsdl/hello_world.wsdl"),
192: new QName(ns, "SOAPService"));
193:
194: service = factory.create();
195: endpointInfo = service
196: .getEndpointInfo(new QName(ns, "SoapPort"));
197: endpoint = new EndpointImpl(bus, service, endpointInfo);
198: JAXBDataBinding db = new JAXBDataBinding();
199: db.setContext(JAXBContext.newInstance(new Class[] {
200: GreetMe.class, GreetMeResponse.class }));
201: service.setDataBinding(db);
202:
203: operation = endpointInfo.getBinding().getOperation(
204: new QName(ns, "greetMe"));
205: operation.getOperationInfo().getInput()
206: .getMessagePartByIndex(0).setTypeClass(GreetMe.class);
207: operation.getOperationInfo().getOutput().getMessagePartByIndex(
208: 0).setTypeClass(GreetMeResponse.class);
209:
210: message = new MessageImpl();
211: Exchange exchange = new ExchangeImpl();
212: message.setExchange(exchange);
213:
214: exchange.put(Service.class, service);
215: exchange.put(Endpoint.class, endpoint);
216: exchange.put(Binding.class, endpoint.getBinding());
217: }
218:
219: private void setUpUsingDocLit() throws Exception {
220: String ns = "http://apache.org/hello_world_doc_lit_bare";
221: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
222: getClass().getResource("/wsdl/doc_lit_bare.wsdl"),
223: new QName(ns, "SOAPService"));
224:
225: service = factory.create();
226: endpointInfo = service
227: .getEndpointInfo(new QName(ns, "SoapPort"));
228: endpoint = new EndpointImpl(bus, service, endpointInfo);
229: JAXBDataBinding db = new JAXBDataBinding();
230: db.setContext(JAXBContext
231: .newInstance(new Class[] { TradePriceData.class }));
232: service.setDataBinding(db);
233:
234: operation = endpointInfo.getBinding().getOperation(
235: new QName(ns, "SayHi"));
236: operation.getOperationInfo().getInput()
237: .getMessagePartByIndex(0).setTypeClass(
238: TradePriceData.class);
239: operation.getOperationInfo().getOutput().getMessagePartByIndex(
240: 0).setTypeClass(TradePriceData.class);
241:
242: message = new MessageImpl();
243: Exchange exchange = new ExchangeImpl();
244: message.setExchange(exchange);
245:
246: exchange.put(Service.class, service);
247: exchange.put(Endpoint.class, endpoint);
248: exchange.put(Binding.class, endpoint.getBinding());
249: }
250:
251: public InputStream getTestStream(Class<?> clz, String file) {
252: return clz.getResourceAsStream(file);
253: }
254:
255: public XMLStreamReader getXMLStreamReader(InputStream is) {
256: return StaxUtils.createXMLStreamReader(is);
257: }
258:
259: public XMLStreamWriter getXMLStreamWriter(OutputStream os) {
260: return StaxUtils.createXMLStreamWriter(os);
261: }
262: }
|