/**
* EasySOA Samples - Smart Travel
* Copyright 2011 Open Wide
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact : easysoa-dev@googlegroups.com
*/
package org.easysoa.galaxydemotest;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
import org.apache.log4j.Logger;
import org.eclipse.jdt.core.dom.ThisExpression;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.objectweb.fractal.api.Component;
import org.ow2.frascati.FraSCAti;
import org.ow2.frascati.assembly.factory.processor.ProcessingContextImpl;
import org.ow2.frascati.util.FrascatiException;
public class GalaxyDemoTestStarter {
private static String serviceUrl = "http://localhost:9000/GalaxyTrip?wsdl";
private static String TNS = "http://scenario1.j1.galaxy.inria.fr/";
private static QName serviceName;
private static QName portName;
/** The FraSCAti platform */
private static FraSCAti frascati;
/**
* Logger
*/
private static Logger logger = Logger.getLogger(getInvokingClassName());
private static Component galaxyCpt;
private static Collection<Component> componentList; // TODO move in abstract class
/**
*
* @return
*/
public static String getInvokingClassName() {
return Thread.currentThread().getStackTrace()[1].getClassName();
}
//http://localhost:9000/GalaxyTrip?wsdl
static {
serviceName = new QName(TNS, "Trip");
portName = new QName(TNS, "TripPort");
System.setProperty("org.ow2.frascati.bootstrap", "org.ow2.frascati.bootstrap.FraSCAti");
////System.setProperty("cxf.config.file", "src/test/resources/configurationCXF.xml");
}
/**
* Init the remote systems for the test
* Nuxeo, Frascati, Galaxy demo and HTTP Proxy ...
* Instantiate FraSCAti and retrieve services.
* @throws InterruptedException
*/
@BeforeClass
public static final void setUp() throws FrascatiException, InterruptedException {
logger.debug("user.dir : " + System.getProperty("user.dir"));
// Start fraSCAti
startFraSCAti();
// Start Galaxy Demo
startGalaxyDemo();
}
/**
* Start FraSCAti
* @throws FrascatiException
*/
private static void startFraSCAti() throws FrascatiException{
frascati = FraSCAti.newFraSCAti();
componentList = new ArrayList<Component>();
}
/**
* Remove the Jetty deployed apps to avoid blocking tests
* @param port The port where the Jetty application is deployed
*/
protected static void cleanJetty(int port){
JettyHTTPServerEngineFactory jettyFactory = BusFactory.getDefaultBus().getExtension(JettyHTTPServerEngineFactory.class);
JettyHTTPServerEngine jettyServer = jettyFactory.retrieveJettyHTTPServerEngine(port);
try {
Collection<Object> beans = jettyServer.getServer().getBeans(); // jetty server null ?!
if(beans != null){
for(Object bean : beans){
logger.debug("Removing Jetty bean for port " + port);
jettyServer.getServer().removeBean(bean);
}
}
jettyFactory.destroyForPort(port);
}
catch(Exception ex){
logger.warn("No beans found for app deployed on Jetty port " + port);
}
}
/**
* Stop FraSCAti components and cleans jett (?)
* @throws FrascatiException
*/
@AfterClass
public static void tearDown() throws Exception{
logger.info("Stopping FraSCAti...");
stopFraSCAti();
cleanJetty(9000); // GalaxyTrip
//cleanJetty(9002); GalaxyTrip
cleanJetty(9080); // CreateSummary
}
/**
*
* @throws FrascatiException
*/
protected static void stopFraSCAti() throws Exception {
if(frascati != null) {
logger.info("FraSCATI Stopping");
if (componentList != null) {
for(Component component : componentList) {
logger.debug("Closing component : " + component);
frascati.close(component);
}
}
frascati = null;
componentList = null;
}
}
/**
* Start The Galaxy Demo
* @throws FrascatiException
*/
private static void startGalaxyDemo() throws FrascatiException{
URL compositeUrl = ClassLoader.getSystemResource("smart-travel-mock-services.composite");
galaxyCpt = frascati.processComposite(compositeUrl.toString(), new ProcessingContextImpl());
componentList.add(galaxyCpt);
}
/**
* Works including proxy (notifs are sent to nuxeo)
* @throws Exception
*/
@Test
public final void testWithScaLocalClient() throws Exception {
Runnable simpleClient = frascati.getService(galaxyCpt, "r", Runnable.class);
simpleClient.run();
}
/**
* Fails to load SCA, why ??
* @throws Exception
*/
@Test
@Ignore
public final void testWithScaWsClient() throws Exception {
URL wsClientCompositeUrl = ClassLoader.getSystemResource("smart-travel-wsclient.composite");
Component wsClientCpt = frascati.processComposite(wsClientCompositeUrl.toString(), new ProcessingContextImpl());
Runnable wsClient = frascati.getService(wsClientCpt, "r", Runnable.class);
wsClient.run();
}
/**
* Send a request to trigger the Galaxy demo test
* PROXY DOES NOT WORK (runs but sends no notification to Nuxeo)
* TODO test with mocked Nuxeo
*
* @throws ClientException
* @throws SOAPException
* @throws IOException
* @throws InterruptedException
*
*/
@Test
public final void testGalaxyDemo() throws IOException, SOAPException, InterruptedException {
logger.debug("Sending Demo request !");
Service jaxwsService = Service.create(new URL(serviceUrl), serviceName);
Dispatch<SOAPMessage> disp = jaxwsService.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
FileInputStream requestMessage = new FileInputStream(new File(System.getProperty("user.dir") + "/src/test/resources/galaxyDemoTestRequest.xml"));
SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(null, requestMessage);
assertNotNull(reqMsg);
SOAPMessage response = disp.invoke(reqMsg);
FileInputStream fis = new FileInputStream(new File(System.getProperty("user.dir") + "/src/test/resources/galaxyDemoTestResponse.xml"));
StringBuffer responseMessage = new StringBuffer("");
int ch;
while((ch = fis.read()) != -1) {
responseMessage.append((char)ch);
}
logger.debug("Response : " + response.getSOAPBody().getTextContent().trim());
System.in.read();
// Warn : Sometimes, the returned response doesn't match the contents of galaxyDemoTestResponse.xml file.
// but the test works anyway
//assertEquals(response.getSOAPBody().getTextContent(), responseMessage.toString());
assertTrue(response.getSOAPBody().getTextContent().contains("City:"));
logger.debug("Demo request sent !");
requestMessage.close();
fis.close();
}
}