org.wso2.carbon.dss.services.test.DataServiceSqlDriverTestCase.java Source code

Java tutorial

Introduction

Here is the source code for org.wso2.carbon.dss.services.test.DataServiceSqlDriverTestCase.java

Source

/*
*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.carbon.dss.services.test;

import org.apache.axiom.attachments.ByteArrayDataSource;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.api.clients.dataservices.DataServiceFileUploaderClient;
import org.wso2.carbon.automation.core.ProductConstant;
import org.wso2.carbon.automation.core.utils.fileutils.FileManager;
import org.wso2.carbon.automation.utils.axis2client.AxisServiceClient;
import org.wso2.carbon.automation.utils.dss.DSSTestCaseUtils;
import org.wso2.carbon.dss.DSSIntegrationTest;
import org.wso2.carbon.dss.samples.test.CSVSampleTestCase;
import org.wso2.carbon.service.mgt.stub.ServiceAdminException;

import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Iterator;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class DataServiceSqlDriverTestCase extends DSSIntegrationTest {

    private static String serviceName = "sqlparsertest";
    private DSSTestCaseUtils dssTestCaseUtils;
    private String resourceFileLocation;
    private String serviceEPR;
    private long randomId;

    private static final Log log = LogFactory.getLog(CSVSampleTestCase.class);

    @BeforeClass(alwaysRun = true)
    public void initialize() throws Exception {
        super.init();
        resourceFileLocation = null;
        randomId = System.currentTimeMillis();
        dssTestCaseUtils = new DSSTestCaseUtils();
        resourceFileLocation = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" + File.separator + "DSS";
        DataServiceFileUploaderClient dataServiceAdminClient = new DataServiceFileUploaderClient(
                dssServer.getBackEndUrl(), userInfo.getUserName(), userInfo.getPassword());

        DataHandler dataHandler = modifyExcelURL(resourceFileLocation + File.separator + "dbs" + File.separator
                + "sqldriver" + File.separator + "sqlparsertest.dbs");

        dataServiceAdminClient.uploadDataServiceFile("sqlparsertest.dbs", dataHandler);
        log.info(serviceName + " uploaded");
        serviceEPR = dssServer.getBackEndUrl() + serviceName;
    }

    @Test(groups = "wso2.dss", description = "Check whether the service is deployed or not")
    public void testServiceDeployment() throws RemoteException {
        assertTrue(dssTestCaseUtils.isServiceDeployed(dssServer.getBackEndUrl(), dssServer.getSessionCookie(),
                serviceName));
        log.info(serviceName + " is deployed");
    }

    @Test(groups = "wso2.dss", description = "insert and retrieve records", dependsOnMethods = "testServiceDeployment")
    public void testInsertRecordsAndGetBack() throws RemoteException {

        OMElement result = new AxisServiceClient().sendReceive(insertNewRecord(String.valueOf(randomId)),
                serviceEPR, "insertop");
        assertTrue(result.toString().contains("SUCCESSFUL"));

        OMElement resultDetails = new AxisServiceClient().sendReceive(getDetails(), serviceEPR, "Getdetails");
        assertTrue(resultDetails.toString().contains(String.valueOf(randomId)));
    }

    @Test(groups = "wso2.dss", description = "update and delete record", dependsOnMethods = "testInsertRecordsAndGetBack")
    public void testUpdateAndDelete() throws RemoteException {

        OMElement result = new AxisServiceClient().sendReceive(updateRecord(String.valueOf(randomId)), serviceEPR,
                "Update");
        assertTrue(result.toString().contains("SUCCESSFUL"));

        OMElement resultDetails = new AxisServiceClient().sendReceive(deleteRecord(String.valueOf(randomId)),
                serviceEPR, "delete");
        assertTrue(resultDetails.toString().contains("SUCCESSFUL"));
    }

    public DataHandler modifyExcelURL(String dbsFilePath) throws XMLStreamException, IOException {
        try {
            OMElement dbsFile = AXIOMUtil.stringToOM(FileManager.readFile(dbsFilePath));
            OMElement dbsConfig = dbsFile.getFirstChildWithName(new QName("config"));
            Iterator configElement1 = dbsConfig.getChildElements();
            String productFilePath = resourceFileLocation + File.separator + "resources" + File.separator
                    + "Products-sql.xls";
            while (configElement1.hasNext()) {
                OMElement property = (OMElement) configElement1.next();
                String value = property.getAttributeValue(new QName("name"));
                if ("url".equals(value)) {
                    property.setText("jdbc:wso2:excel:filePath=" + productFilePath);
                }
            }
            log.debug(dbsFile);
            ByteArrayDataSource dbs = new ByteArrayDataSource(dbsFile.toString().getBytes());
            return new DataHandler(dbs);

        } catch (XMLStreamException e) {
            log.error("XMLStreamException when Reading Service File", e);
            throw new XMLStreamException("XMLStreamException when Reading Service File", e);
        } catch (IOException e) {
            log.error("IOException when Reading Service File", e);
            throw new IOException("IOException  when Reading Service File", e);
        }
    }

    private OMElement insertNewRecord(String idNum) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "dat");
        OMElement payload = fac.createOMElement("insertop", omNs);

        OMElement id = fac.createOMElement("id", omNs);
        OMElement mod = fac.createOMElement("mod", omNs);
        OMElement classname = fac.createOMElement("classname", omNs);
        id.setText(idNum);
        mod.setText("mod111");
        classname.setText("org.wso2.carbon.dss.test");
        payload.addChild(id);
        payload.addChild(mod);
        payload.addChild(classname);

        return payload;
    }

    private OMElement getDetails() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "dat");
        return fac.createOMElement("Getdetails", omNs);
    }

    private OMElement updateRecord(String idNum) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "dat");
        OMElement payload = fac.createOMElement("Update", omNs);

        OMElement id = fac.createOMElement("id", omNs);
        OMElement mod = fac.createOMElement("mod", omNs);
        OMElement classname = fac.createOMElement("classname", omNs);
        id.setText(idNum);
        mod.setText("mod1112" + idNum);
        classname.setText("org.wso2.carbon.dss.test2");
        payload.addChild(id);
        payload.addChild(mod);
        payload.addChild(classname);

        return payload;
    }

    private OMElement deleteRecord(String idNum) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "dat");
        OMElement payload = fac.createOMElement("delete", omNs);
        OMElement id = fac.createOMElement("id", omNs);
        id.setText(idNum);
        payload.addChild(id);

        return payload;
    }

    @AfterClass(alwaysRun = true)
    public void deleteService() throws ServiceAdminException, RemoteException {
        deleteService(serviceName);
        log.info(serviceName + " deleted");
    }

}