Package org.wso2.esb.integration

Source Code of org.wso2.esb.integration.ESBIntegrationTest

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.esb.integration;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.client.Stub;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.integration.core.AuthenticateStub;
import org.wso2.carbon.integration.core.FrameworkSettings;
import org.wso2.carbon.integration.core.TestTemplate;
import org.wso2.carbon.integration.core.utils.ArtifactReader;
import org.wso2.carbon.mediation.configadmin.stub.ConfigServiceAdminStub;
import org.wso2.esb.integration.axis2.SampleAxis2Server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;

public abstract class ESBIntegrationTest extends TestTemplate {

    public static final int ESB_HTTP_PORT = 8280;

    protected Log log = LogFactory.getLog(getClass());

    protected String adminServiceName = null;
    private SampleAxis2Server axis2Server = null;

    public ESBIntegrationTest() {

    }

    public ESBIntegrationTest(String adminServiceName) {
        this.adminServiceName = adminServiceName;
    }

    @Override
    public void init() {
        String msg = "Initializing " + getClass().getSimpleName();
        if (adminServiceName != null) {
            msg += " for testing " + adminServiceName;
        }
        log.info(msg);
    }

    @Override
    public void runSuccessCase() {
        try {
            successfulScenario();
        } catch (RemoteException e) {
            handleError("Error while invoking a backend service or an admin service", e);
        }
    }

    @Override
    public void runFailureCase() {

    }

    @Override
    public void cleanup() {
        if (axis2Server != null && axis2Server.isStarted()) {
            axis2Server.stop();
            axis2Server = null;
        }
    }

    public abstract void successfulScenario() throws RemoteException;

    protected void launchStockQuoteService() {
        axis2Server = new SampleAxis2Server();
        try {
            axis2Server.deployStockQuoteService();
            axis2Server.start();
        } catch (IOException e) {
            handleError("Error while launching the Axis2 server with stock quote service", e);
        }
    }

    protected void authenticate(Stub stub) {
        AuthenticateStub authenticateStub = new AuthenticateStub();
        authenticateStub.authenticateAdminStub(stub, sessionCookie);
    }

    protected void handleError(String msg, Throwable t) {
        log.error(msg, t);
        fail(msg);
    }

    protected String getAdminServiceURL() {
        if (adminServiceName != null) {
            return getAdminServiceURL(adminServiceName);
        }
        return null;
    }

    protected String getAdminServiceURL(String service) {
        return "https://" + FrameworkSettings.HOST_NAME + ":" + FrameworkSettings.HTTPS_PORT +
                "/services/" + service;
    }

    protected String getMainSequenceURL() {
        if (FrameworkSettings.STRATOS.equalsIgnoreCase("false")) {
            return "http://" + FrameworkSettings.HOST_NAME + ":" + ESB_HTTP_PORT;
        } else {
            return "http://" + FrameworkSettings.HOST_NAME + ":" + ESB_HTTP_PORT +
                    "/services/" + FrameworkSettings.TENANT_NAME + "/";
        }
    }

    protected String getProxyServiceURL(String service, boolean servletTransport) {
        int port = ESB_HTTP_PORT;
        if (servletTransport) {
            port = 9763;
        }

        if (FrameworkSettings.STRATOS.equalsIgnoreCase("false")) {
            return "http://" + FrameworkSettings.HOST_NAME + ":" + port + "/services/" +
                    service;
        } else {
            //TODO: Fix this to return a proper URL
            return "http://" + FrameworkSettings.HOST_NAME + ":" + port +
                    "/services/" + FrameworkSettings.TENANT_NAME + "/";
        }
    }


    protected void updateESBConfiguration(String filePath) throws RemoteException {
        ConfigServiceAdminStub configServiceAdminStub =
                new ConfigServiceAdminStub(getAdminServiceURL("ConfigServiceAdmin"));
        authenticate(configServiceAdminStub);
        OMElement configElement = loadResource(filePath);
        configServiceAdminStub.updateConfiguration(configElement);
        configServiceAdminStub.cleanup();
    }

    protected OMElement loadResource(String path) {
        ArtifactReader artifactReader = new ArtifactReader();
        return artifactReader.getOMElement(getClass().getResource(path).getPath());
    }

    protected void copyResourceFile(String sourcePath, String targetPath) throws IOException {
        InputStream in = getClass().getResourceAsStream(sourcePath);
        if (in == null) {
            fail("Unable to locate the specified configuration resource: " + sourcePath);
        }

        File target = new File(carbonHome + File.separator + targetPath);
        FileOutputStream fos = new FileOutputStream(target);

        byte[] data = new byte[1024];
        int i;
        while ((i = in.read(data)) != -1) {
            fos.write(data, 0, i);
        }
        fos.flush();
        fos.close();
        in.close();
    }
}
TOP

Related Classes of org.wso2.esb.integration.ESBIntegrationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.