<commit_before>/*
*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.dss.integration.test.fileservice;

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.axis2.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.test.utils.axis2client.AxisServiceClient;
import org.wso2.carbon.automation.test.utils.common.FileManager;
import org.wso2.carbon.automation.test.utils.concurrency.test.ConcurrencyTest;
import org.wso2.carbon.automation.test.utils.concurrency.test.exception.ConcurrencyTestFailedError;
import org.wso2.carbon.registry.resource.stub.common.xsd.ResourceData;
import org.wso2.dss.integration.common.clients.ResourceAdminServiceClient;
import org.wso2.carbon.registry.resource.stub.ResourceAdminServiceExceptionException;
import org.wso2.dss.integration.common.utils.DSSTestCaseUtils;
import org.wso2.dss.integration.test.DSSIntegrationTest;

import javax.activation.DataHandler;
import javax.xml.xpath.XPathExpressionException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import static org.testng.Assert.assertTrue;

public class ExcelDataServiceTestCase extends DSSIntegrationTest {
    private static final Log log = LogFactory.getLog(ExcelDataServiceTestCase.class);
    private final String serviceName = "ExcelDataService";

    @BeforeClass(alwaysRun = true)
    public void serviceDeployment() throws Exception {

        super.init();
        addResource();
        try {
            checkResourceExist("/_system/config/automation/resources/excel/Products.xls");
            checkResourceExist("/_system/config/automation/resources/excel/transform.xslt");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("ExcelDataServiceTestCase::serviceDeployment Failed, Required resources does deployed correctly to registry");
        }
        deployService(serviceName,
                      AXIOMUtil.stringToOM(FileManager.readFile(getResourceLocation()
                                                                + File.separator + "dbs" + File.separator
                                                                + "excel" + File.separator
                                                                + "ExcelDataService.dbs")));

    }

    @Test(groups = "wso2.dss", description = "Check whether fault service deployed or not")
    public void testServiceDeployment() throws RemoteException, XPathExpressionException {
        DSSTestCaseUtils dssTestCaseUtils = new DSSTestCaseUtils();
        assertTrue(dssTestCaseUtils.isServiceDeployed(dssContext.getContextUrls().getBackEndUrl(),
                                                      sessionCookie, serviceName));
        log.info(serviceName + " is deployed");
    }

    @AfterClass(alwaysRun = true)
    public void destroy() throws Exception {
        deleteService(serviceName);
        deleteResource();
        cleanup();
    }

    @Test(groups = {"wso2.dss"}, invocationCount = 5, dependsOnMethods = "testServiceDeployment")
    public void selectOperation() throws AxisFault, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProducts", omNs);
        OMElement result = new AxisServiceClient().sendReceive(payload, getServiceUrlHttp(serviceName), "getProducts");
        log.info("Response :" + result);
        Assert.assertTrue((result.toString().indexOf("Products") == 1), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<ID>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Name>"), "Expected Result Not found");

        log.info("Service invocation success");
    }

    @Test(groups = {"wso2.dss"}, invocationCount = 5, dependsOnMethods = "testServiceDeployment")
    public void xsltTransformation() throws AxisFault, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProductClassifications", omNs);
        OMElement result = new AxisServiceClient().sendReceive(payload, getServiceUrlHttp(serviceName), "getProductClassifications");
        if (log.isDebugEnabled()) {
            log.debug("Response :" + result);
        }
        Assert.assertTrue((result.toString().indexOf("Products") == 1), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product-Name>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product-Classification>"), "Expected Result Not found");

        log.info("XSLT Transformation Success");
    }

    @Test(groups = {"wso2.dss"}, dependsOnMethods = {"xsltTransformation"}, timeOut = 1000 * 60 )
    public void concurrencyTest()
            throws ConcurrencyTestFailedError, InterruptedException, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProducts", omNs);
        ConcurrencyTest concurrencyTest = new ConcurrencyTest(5, 5);
        concurrencyTest.run(getServiceUrlHttp(serviceName), payload, "getProducts");
    }

    private void addResource()<commit_msg>Fixing test failure in Excel Data Service TestCase<commit_after>/*
*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.dss.integration.test.fileservice;

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.axis2.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.test.utils.axis2client.AxisServiceClient;
import org.wso2.carbon.automation.test.utils.common.FileManager;
import org.wso2.carbon.automation.test.utils.concurrency.test.ConcurrencyTest;
import org.wso2.carbon.automation.test.utils.concurrency.test.exception.ConcurrencyTestFailedError;
import org.wso2.carbon.registry.resource.stub.ResourceAdminServiceExceptionException;
import org.wso2.carbon.registry.resource.stub.common.xsd.ResourceData;
import org.wso2.dss.integration.common.clients.ResourceAdminServiceClient;
import org.wso2.dss.integration.common.utils.DSSTestCaseUtils;
import org.wso2.dss.integration.test.DSSIntegrationTest;

import javax.activation.DataHandler;
import javax.xml.xpath.XPathExpressionException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import static org.testng.Assert.assertTrue;

public class ExcelDataServiceTestCase extends DSSIntegrationTest {
    private static final Log log = LogFactory.getLog(ExcelDataServiceTestCase.class);
    private final String serviceName = "ExcelDataService";

    @BeforeClass(alwaysRun = true)
    public void serviceDeployment() throws Exception {

        super.init();
        addResource();
        try {
            checkResourceExist("/_system/config/automation/resources/excel/Products.xls");
            checkResourceExist("/_system/config/automation/resources/excel/transform.xslt");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("ExcelDataServiceTestCase::serviceDeployment Failed, Required resources does deployed correctly to registry");
        }
        deployService(serviceName,
                      AXIOMUtil.stringToOM(FileManager.readFile(getResourceLocation()
                                                                + File.separator + "dbs" + File.separator
                                                                + "excel" + File.separator
                                                                + "ExcelDataService.dbs")));

    }

    @Test(groups = "wso2.dss", description = "Check whether fault service deployed or not")
    public void testServiceDeployment() throws RemoteException, XPathExpressionException {
        DSSTestCaseUtils dssTestCaseUtils = new DSSTestCaseUtils();
        assertTrue(dssTestCaseUtils.isServiceDeployed(dssContext.getContextUrls().getBackEndUrl(),
                                                      sessionCookie, serviceName));
        log.info(serviceName + " is deployed");
    }

    @AfterClass(alwaysRun = true)
    public void destroy() throws Exception {
        deleteService(serviceName);
        deleteResource();
        cleanup();
    }

    @Test(groups = {"wso2.dss"}, invocationCount = 5, dependsOnMethods = "testServiceDeployment")
    public void selectOperation() throws AxisFault, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProducts", omNs);
        OMElement result = new AxisServiceClient().sendReceive(payload, getServiceUrlHttp(serviceName), "getProducts");
        log.info("Response :" + result);
        Assert.assertTrue((result.toString().indexOf("Products") == 1), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<ID>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Name>"), "Expected Result Not found");

        log.info("Service invocation success");
    }

    @Test(groups = {"wso2.dss"}, invocationCount = 5, dependsOnMethods = "testServiceDeployment")
    public void xsltTransformation() throws AxisFault, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProductClassifications", omNs);
        OMElement result = new AxisServiceClient().sendReceive(payload, getServiceUrlHttp(serviceName), "getProductClassifications");
        if (log.isDebugEnabled()) {
            log.debug("Response :" + result);
        }
        Assert.assertTrue((result.toString().indexOf("Products") == 1), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product-Name>"), "Expected Result Not found");
        Assert.assertTrue(result.toString().contains("<Product-Classification>"), "Expected Result Not found");

        log.info("XSLT Transformation Success");
    }

    @Test(groups = {"wso2.dss"}, dependsOnMethods = {"xsltTransformation"}, timeOut = 1000 * 60 )
    public void concurrencyTest()
            throws ConcurrencyTestFailedError, InterruptedException, XPathExpressionException {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://ws.wso2.org/dataservice", "ns1");
        OMElement payload = fac.createOMElement("getProducts", omNs);
        ConcurrencyTest concurrencyTest = new ConcurrencyTest(5, 5);
        concurrencyTest.run(getServiceUrlHttp(serviceName), payload, "getProducts");
        Thread.sleep(6000);
        log.info("Concurrency Test Success");
    }

    private void addResource()
{'fraction_non_alphanumeric': 0.11051033256795793, 'fraction_numerical': 0.007554995926227687, 'mean_word_length': 3.6558620689655172, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6278724', 'n_tokens_mistral': 3715, 'n_tokens_neox': 3560, 'n_words': 782}