001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.wsrp;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.net.URL;
028:
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.xml.rpc.Service;
032: import javax.xml.soap.MessageFactory;
033: import javax.xml.soap.SOAPBody;
034: import javax.xml.soap.SOAPConnection;
035: import javax.xml.soap.SOAPConnectionFactory;
036: import javax.xml.soap.SOAPEnvelope;
037: import javax.xml.soap.SOAPFault;
038: import javax.xml.soap.SOAPHeader;
039: import javax.xml.soap.SOAPMessage;
040:
041: import junit.framework.Test;
042:
043: import org.jboss.test.webservice.WebserviceTestBase;
044: import org.jboss.test.wsrp.core.BlockingInteractionResponse;
045: import org.jboss.test.wsrp.core.Extension;
046: import org.jboss.test.wsrp.core.GetMarkup;
047: import org.jboss.test.wsrp.core.InitCookie;
048: import org.jboss.test.wsrp.core.InteractionParams;
049: import org.jboss.test.wsrp.core.MarkupParams;
050: import org.jboss.test.wsrp.core.MarkupResponse;
051: import org.jboss.test.wsrp.core.PerformBlockingInteraction;
052: import org.jboss.test.wsrp.core.PortletContext;
053: import org.jboss.test.wsrp.core.RegistrationContext;
054: import org.jboss.test.wsrp.core.ReleaseSessions;
055: import org.jboss.test.wsrp.core.ReturnAny;
056: import org.jboss.test.wsrp.core.RuntimeContext;
057: import org.jboss.test.wsrp.core.UserContext;
058: import org.jboss.test.wsrp.core.UserProfile;
059:
060: //$Id: MarkupTestCase.java 57211 2006-09-26 12:39:46Z dimitris@jboss.org $
061: /**
062: * Test Case that tests the MarkUp WSRP service
063: * @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
064: * @since Mar 27, 2006
065: * @version $Revision: 57211 $
066: */
067: public class MarkupTestCase extends WebserviceTestBase {
068: private String url = "http://" + getServerHost() + ":"
069: + Integer.getInteger("web.port", 8080)
070: + "/wsrp/MarkupService";
071:
072: public MarkupTestCase(String name) {
073: super (name);
074: }
075:
076: /**
077: * deploy the test archives
078: */
079: public static Test suite() throws Exception {
080: return getDeploySetup(MarkupTestCase.class,
081: "wsrp.war, wsrp-client.jar");
082: }
083:
084: public void testGetMarkup() throws Exception {
085: Service service = getService();
086: WSRP_v1_Markup_PortType endpoint = (WSRP_v1_Markup_PortType) service
087: .getPort(WSRP_v1_Markup_PortType.class);
088: GetMarkup gm = new GetMarkup(new RegistrationContext(),
089: new PortletContext("portletHandle", "portletState"
090: .getBytes(), new Extension[] {}),
091: new RuntimeContext(), new UserContext("userContextKey",
092: new String[] {}, new UserProfile(),
093: new Extension[] {}), new MarkupParams());
094: //Invoke the Web Service
095: MarkupResponse gmr = endpoint.getMarkup(gm);
096: assertNotNull("gmr != null", gmr);
097: }
098:
099: public void testPerformBlockingInteraction() throws Exception {
100: Service service = getService();
101: WSRP_v1_Markup_PortType endpoint = (WSRP_v1_Markup_PortType) service
102: .getPort(WSRP_v1_Markup_PortType.class);
103: PerformBlockingInteraction pbi = new PerformBlockingInteraction(
104: new RegistrationContext(), new PortletContext(
105: "portletHandle", "portletState".getBytes(),
106: new Extension[] {}), new RuntimeContext(),
107: new UserContext("userContextKey", new String[] {},
108: new UserProfile(), new Extension[] {}),
109: new MarkupParams(), new InteractionParams());
110: //Invoke the Web Service
111: BlockingInteractionResponse bir = endpoint
112: .performBlockingInteraction(pbi);
113: assertNotNull("bir != null", bir);
114: }
115:
116: public void testReleaseSessions() throws Exception {
117: Service service = getService();
118: WSRP_v1_Markup_PortType endpoint = (WSRP_v1_Markup_PortType) service
119: .getPort(WSRP_v1_Markup_PortType.class);
120: ReleaseSessions rs = new ReleaseSessions(
121: new RegistrationContext(), new String[] {});
122: //Invoke the Web Service
123: ReturnAny ra = endpoint.releaseSessions(rs);
124: assertNotNull("ra != null", ra);
125: }
126:
127: public void testInitCookie() throws Exception {
128: Service service = getService();
129: WSRP_v1_Markup_PortType endpoint = (WSRP_v1_Markup_PortType) service
130: .getPort(WSRP_v1_Markup_PortType.class);
131: InitCookie ic = new InitCookie(new RegistrationContext());
132: //Invoke the Web Service
133: ReturnAny ra = endpoint.initCookie(ic);
134: assertNotNull("ra != null", ra);
135: }
136:
137: /**
138: * Send a blank soap message to the wsrp endpoint
139: *
140: * @throws Exception
141: */
142: public void testBlankSOAPMessage() throws Exception {
143: String xmlMessage = " ";
144: //construct the SOAP message
145: MessageFactory factory = MessageFactory.newInstance();
146: SOAPMessage msg = factory.createMessage(null,
147: new ByteArrayInputStream(xmlMessage.getBytes()));
148: assertNotNull("SOAPMessage is not null", msg);
149: msg.writeTo(System.out);
150:
151: //Send the SOAPMessage
152: SOAPConnectionFactory smFactory = SOAPConnectionFactory
153: .newInstance();
154: SOAPConnection con = smFactory.createConnection();
155: SOAPMessage reply = con.call(msg, new URL(url));
156: reply.writeTo(System.out);
157: assertNotNull("Reply SOAPMessage is not null", reply);
158: assertNotNull("Reply SOAPPart is not null", reply.getSOAPPart());
159: assertNotNull("Reply SOAPEnvelope is not null", reply
160: .getSOAPPart().getEnvelope());
161: assertNotNull("Reply SOAPBody is not null", reply.getSOAPPart()
162: .getEnvelope().getBody());
163: SOAPFault soapFault = reply.getSOAPPart().getEnvelope()
164: .getBody().getFault();
165: assertNull("SOAP Fault is null", soapFault);
166: }
167:
168: /**
169: * Send a soap message to the wsrp markup service
170: * that has a jaxrpc handler fitted to strip the
171: * wsrp extensions. The jaxrpc handler checks to
172: * see if there is a SOAP header element asking for
173: * the strip.
174: *
175: * @throws Exception
176: */
177: public void testWSRPExtensionInteropWithHandler() throws Exception {
178: checkWSRPService(url, true);
179: }
180:
181: /**
182: * Send a soap message to the wsrp markup service
183: *
184: * @throws Exception
185: */
186: public void testWSRPExtensionInterop() throws Exception {
187: checkWSRPService(url, false);
188: }
189:
190: private void checkWSRPService(String endpointURL,
191: boolean needStrippingHeader) throws Exception {
192: String fileurl = "resources/wsrp/messages/extension_markup.txt";
193: File file = new File(fileurl);
194: assertNotNull(file);
195: //construct the SOAP message
196: MessageFactory factory = MessageFactory.newInstance();
197: SOAPMessage msg = factory.createMessage(null,
198: new FileInputStream(file));
199: if (needStrippingHeader)
200: addStrippingHeader(msg);
201: assertNotNull("SOAPMessage is not null", msg);
202:
203: //Send the SOAPMessage
204: SOAPConnectionFactory smFactory = SOAPConnectionFactory
205: .newInstance();
206: SOAPConnection con = smFactory.createConnection();
207: SOAPMessage reply = con.call(msg, new URL(endpointURL));
208: assertNotNull("Reply SOAPMessage is not null", reply);
209: SOAPBody soapBody = reply.getSOAPPart().getEnvelope().getBody();
210: assertNotNull("Reply SoapBody is not null", soapBody);
211: assertFalse("SOAPFault is null", soapBody.hasFault());
212: assertNull("SOAP Fault is null", soapBody.getFault());
213: }
214:
215: private Service getService() throws NamingException {
216: InitialContext iniCtx = getClientContext("wsrp-client");
217: Service service = (Service) iniCtx
218: .lookup("java:comp/env/service/MarkupService");
219: return service;
220: }
221:
222: /**
223: * Add a soap header element to the soap message
224: *
225: * @param soapMessage
226: * @throws Exception
227: */
228: private void addStrippingHeader(SOAPMessage soapMessage)
229: throws Exception {
230: SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
231: SOAPHeader header = env.getHeader();
232: if (header == null)
233: header = env.addHeader();
234: header.addChildElement("jboss_wsrp_remove_extension");
235: }
236: }
|