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.jaxws.support;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.activation.DataHandler;
028: import javax.activation.DataSource;
029: import javax.mail.util.ByteArrayDataSource;
030: import javax.xml.ws.BindingProvider;
031: import javax.xml.ws.handler.MessageContext;
032:
033: import org.apache.cxf.attachment.AttachmentImpl;
034: import org.apache.cxf.headers.Header;
035: import org.apache.cxf.message.Attachment;
036: import org.apache.cxf.message.Exchange;
037: import org.apache.cxf.message.ExchangeImpl;
038: import org.apache.cxf.message.Message;
039: import org.apache.cxf.message.MessageImpl;
040: import org.easymock.EasyMock;
041: import org.junit.Assert;
042: import org.junit.Before;
043: import org.junit.Test;
044:
045: public class ContextPropertiesMappingTest extends Assert {
046: private static final String ADDRESS = "test address";
047: private static final String REQUEST_METHOD = "GET";
048: private static final String HEADER = "header";
049: private static final Integer RESPONSE_CODE = 401;
050:
051: private Map<String, Object> message = new HashMap<String, Object>();
052: private Map<String, Object> requestContext = new HashMap<String, Object>();
053: private Map<String, Object> responseContext = new HashMap<String, Object>();
054:
055: @Before
056: public void setUp() throws Exception {
057: message.clear();
058: message.put(Message.ENDPOINT_ADDRESS, ADDRESS);
059: message.put(Message.HTTP_REQUEST_METHOD, REQUEST_METHOD);
060: message.put(Message.PROTOCOL_HEADERS, HEADER);
061:
062: requestContext.clear();
063: requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
064: ADDRESS + "jaxws");
065: requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, HEADER
066: + "jaxws");
067: responseContext.clear();
068: }
069:
070: @Test
071: public void testMapRequestfromJaxws2Cxf() {
072: Object address = requestContext.get(Message.ENDPOINT_ADDRESS);
073: assertNull("address should be null", address);
074: ContextPropertiesMapping
075: .mapRequestfromJaxws2Cxf(requestContext);
076: address = requestContext.get(Message.ENDPOINT_ADDRESS);
077: assertNotNull("address should not be null", address);
078: assertEquals("address should get from requestContext", address,
079: ADDRESS + "jaxws");
080: message.putAll(requestContext);
081: address = message.get(Message.ENDPOINT_ADDRESS);
082: assertNotNull("address should not be null", address);
083: assertEquals("address should get from requestContext", address,
084: ADDRESS + "jaxws");
085: Object header = message.get(Message.PROTOCOL_HEADERS);
086: assertEquals("the message PROTOCOL_HEADERS should be updated",
087: header, HEADER + "jaxws");
088: }
089:
090: @Test
091: public void testMapResponseCxf2Jaxws() {
092: responseContext.putAll(message);
093: Object requestMethod = responseContext
094: .get(MessageContext.HTTP_REQUEST_METHOD);
095: assertNull("requestMethod should be null", requestMethod);
096: ContextPropertiesMapping
097: .mapResponsefromCxf2Jaxws(responseContext);
098: requestMethod = responseContext
099: .get(MessageContext.HTTP_REQUEST_METHOD);
100: assertNotNull("requestMethod should not be null", requestMethod);
101: assertEquals(requestMethod, REQUEST_METHOD);
102: Object header = responseContext
103: .get(MessageContext.HTTP_RESPONSE_HEADERS);
104: assertNotNull("the HTTP_RESPONSE_HEADERS should not be null ",
105: header);
106: assertEquals("the HTTP_RESPONSE_HEADERS should be updated",
107: header, HEADER);
108: }
109:
110: @Test
111: public void testCreateWebServiceContext() {
112: Exchange exchange = new ExchangeImpl();
113: Message inMessage = new MessageImpl();
114: Message outMessage = new MessageImpl();
115:
116: inMessage.putAll(message);
117:
118: exchange.setInMessage(inMessage);
119: exchange.setOutMessage(outMessage);
120:
121: MessageContext ctx = ContextPropertiesMapping
122: .createWebServiceContext(exchange);
123:
124: Object requestHeader = ctx
125: .get(MessageContext.HTTP_REQUEST_HEADERS);
126: assertNotNull("the request header should not be null",
127: requestHeader);
128: assertEquals("we should get the request header", requestHeader,
129: HEADER);
130: Object responseHeader = ctx
131: .get(MessageContext.HTTP_RESPONSE_HEADERS);
132: assertNull("the response header should be null", responseHeader);
133: Object outMessageHeader = outMessage
134: .get(Message.PROTOCOL_HEADERS);
135: assertEquals(
136: "the outMessage PROTOCOL_HEADERS should be update",
137: responseHeader, outMessageHeader);
138:
139: Object inAttachments = ctx
140: .get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
141: assertNotNull("inbound attachments object must be initialized",
142: inAttachments);
143: assertTrue("inbound attachments must be in a Map",
144: inAttachments instanceof Map);
145: assertTrue("no inbound attachments expected",
146: ((Map) inAttachments).isEmpty());
147: }
148:
149: @Test
150: public void testUpdateWebServiceContext() {
151: Exchange xchng = new ExchangeImpl();
152: Message outMsg = new MessageImpl();
153: List<Header> hdrList = new ArrayList<Header>();
154: xchng.setOutMessage(outMsg);
155:
156: responseContext.put(MessageContext.HTTP_RESPONSE_CODE,
157: RESPONSE_CODE);
158:
159: MessageContext ctx = EasyMock.createMock(MessageContext.class);
160: ctx.containsKey(MessageContext.HTTP_RESPONSE_CODE);
161: EasyMock.expectLastCall().andReturn(true);
162: ctx.get(MessageContext.HTTP_RESPONSE_CODE);
163: EasyMock.expectLastCall().andReturn(RESPONSE_CODE);
164:
165: ctx.containsKey(Header.HEADER_LIST);
166: EasyMock.expectLastCall().andReturn(true);
167: ctx.get(Header.HEADER_LIST);
168: EasyMock.expectLastCall().andReturn(hdrList);
169: ctx.get(Header.HEADER_LIST);
170: EasyMock.expectLastCall().andReturn(hdrList);
171:
172: ctx.containsKey(MessageContext.HTTP_RESPONSE_HEADERS);
173: EasyMock.expectLastCall().andReturn(false);
174:
175: EasyMock.replay(ctx);
176:
177: ContextPropertiesMapping.updateWebServiceContext(xchng, ctx);
178: Integer respCode = (Integer) outMsg.get(Message.RESPONSE_CODE);
179: assertNotNull("no response code set on out message", respCode);
180: assertEquals("incorrect response code returned", RESPONSE_CODE,
181: respCode);
182: }
183:
184: @Test
185: @SuppressWarnings("unchecked")
186: public void testCreateWebServiceContextWithInAttachments() {
187: Exchange exchange = new ExchangeImpl();
188: Message inMessage = new MessageImpl();
189:
190: Collection<Attachment> attachments = new LinkedList<Attachment>();
191:
192: DataSource source = new ByteArrayDataSource(new byte[0],
193: "text/xml");
194:
195: DataHandler handler1 = new DataHandler(source);
196: attachments.add(new AttachmentImpl("part1", handler1));
197: DataHandler handler2 = new DataHandler(source);
198: attachments.add(new AttachmentImpl("part2", handler2));
199: inMessage.setAttachments(attachments);
200:
201: inMessage.putAll(message);
202: exchange.setInMessage(inMessage);
203: exchange.setOutMessage(new MessageImpl());
204:
205: MessageContext ctx = ContextPropertiesMapping
206: .createWebServiceContext(exchange);
207:
208: Object inAttachments = ctx
209: .get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
210: assertNotNull("inbound attachments object must be initialized",
211: inAttachments);
212: assertTrue("inbound attachments must be in a Map",
213: inAttachments instanceof Map);
214: Map<String, DataHandler> dataHandlers = (Map) inAttachments;
215: assertEquals("two inbound attachments expected", 2,
216: dataHandlers.size());
217:
218: assertTrue("part1 attachment is missing", dataHandlers
219: .containsKey("part1"));
220: // should do as it's the same instance
221: assertTrue("part1 handler is missing", dataHandlers
222: .get("part1") == handler1);
223: assertTrue("part2 attachment is missing", dataHandlers
224: .containsKey("part2"));
225: assertTrue("part2 handler is missing", dataHandlers
226: .get("part2") == handler2);
227: }
228: }
|