All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.superglue.QueryShared Maven / Gradle / Ivy

There is a newer version: 1.25.a
Show newest version
package com.github.siwenyan.superglue;

import com.github.siwenyan.common.DataTableTools;
import com.github.siwenyan.common.ImmediateAbortException;
import com.github.siwenyan.common.StringTools;
import com.github.siwenyan.common.WhiteBoard;
import com.github.siwenyan.query.IQueryFactory;
import com.github.siwenyan.query.MyBatisUtils;
import com.github.siwenyan.query.QueryBase;
import com.github.siwenyan.query.QueryExpression;
import com.github.siwenyan.web.WebUtils;
import cucumber.api.DataTable;
import cucumber.api.Scenario;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;

import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

public class QueryShared extends AbstractStepSet {
    public QueryShared(Scenario scenario) {
        super(scenario, null);
        QueryExpression.putFactory("javascript", new IQueryFactory() {
            @Override
            public QueryBase createQuery(String source) {
                AbstractLogin login = Login.getLastAccessedLogin(scenario);
                WebDriver driver = login.driver();
                return new QueryBase() {
                    @Override
                    public Object query(String path) {
                        return WebUtils.js(driver, path);
                    }
                };
            }
        });
    }

    public void isExecutedInDatabase(String sqlOrFileName0OrMyBatisSqlIdWithParams0, String sqlSessionFactoryClassName0) {
        String sqlOrFileName0OrMyBatisSqlIdWithParams1 = WhiteBoard.getInstance().resolve(getOwner(), sqlOrFileName0OrMyBatisSqlIdWithParams0);
        String sqlOrMyBatisSqlIdWithParams0 = StringTools.readOrReturn(sqlOrFileName0OrMyBatisSqlIdWithParams1);
        String sqlOrMyBatisSqlIdWithParams1 = WhiteBoard.getInstance().resolve(getOwner(), sqlOrMyBatisSqlIdWithParams0);

        String sqlSessionFactoryClassName1 = WhiteBoard.getInstance().resolve(getOwner(), sqlSessionFactoryClassName0);
        SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory(sqlSessionFactoryClassName1);

        if (sqlOrMyBatisSqlIdWithParams1.startsWith("mybatis")) {
            try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
                String myBatisSqlIdWithParams = sqlOrMyBatisSqlIdWithParams1;
                String[] p = myBatisSqlIdWithParams.split("\\s+", 3);
                String sqlId = p[1];
                String paramString = (3 == p.length ? p[2] : null);
                if (null == paramString) {
                    // no parameters
                    sqlSession.update(sqlId);
                } else {
                    if (paramString.contains("=")) {
                        // hashmap parameters
                        Map params = DataTableTools.asMap(paramString);
                        sqlSession.update(sqlId, params);
                    } else {
                        // single string parameter
                        sqlSession.update(sqlId, paramString);
                    }
                }
            } catch (Exception e) {
                Assert.fail("Execution fail: " + sqlOrMyBatisSqlIdWithParams1 + "\r\n" + e.getMessage());
            }
        } else {
            String sql = sqlOrMyBatisSqlIdWithParams1;
            Reader reader = new StringReader(sql);
            MyBatisUtils.execute(sqlSessionFactory, reader, true, true);
        }

    }

    public void isExtractedFromWithQuery(String varName0, String sourceString0, String queryExpression0) {
        String varName = WhiteBoard.getInstance().resolve(getOwner(), varName0);
        String sourceString = WhiteBoard.getInstance().resolve(getOwner(), sourceString0);
        String queryExpression = WhiteBoard.getInstance().resolve(getOwner(), queryExpression0);
        scenarioLogger.warn("varName=" + varName);
        scenarioLogger.warn("sourceString=" + sourceString);
        scenarioLogger.warn("queryExpression=" + queryExpression);
        WhiteBoard.getInstance().removeAll(getOwner(), varName + ".*");

        QueryExpression qe = new QueryExpression(queryExpression);

        QueryBase q = qe.compile(sourceString);
        Object result = q.query(qe.getQuery());

        if (result instanceof List) {
            List objList = null;
            List> mapList = null;
            try {
                objList = (List) result;
            } catch (Exception e) {
                Assert.fail("Unknown type in list: " + result);
            }
            try {
                mapList = (List>) result;
            } catch (Exception e) {
                // do nothing
            }

            if (null == mapList) {
                if (objList.size() <= 0) {
                    WhiteBoard.getInstance().putString(getOwner(), varName, "[]");
                } else if (1 == objList.size()) {
                    String s = objList.get(0).toString();
                    WhiteBoard.getInstance().putString(getOwner(), varName, s);
                    scenarioLogger.warn("Value extracted:" + s);
                } else {
                    List strList = DataTableTools.asListOfStrings(objList);
                    WhiteBoard.getInstance().putList(getOwner(), varName, strList);
                    try {
                        logger.debug(varName + "_rows=" + WhiteBoard.getInstance().getString(getOwner(), varName + "_rows"));
                        scenarioLogger.warn("Value extracted:" + strList);
                    } catch (Throwable e) {
                        logger.debug("log error");
                    }
                }
            } else {
                List> stringMapList = DataTableTools.asMapsOfStrings(mapList);
                Map> indexedMaps = DataTableTools.dataTableAsMapsIndexedByKeyHeader(DataTableTools.INDEX_ROW_NUMBER, stringMapList);
                WhiteBoard.getInstance().storeMaps(getOwner(), varName, indexedMaps);
                try {
                    logger.debug(varName + "_rows=" + WhiteBoard.getInstance().getString(getOwner(), varName + "_rows"));
                    String dtString = DataTable.create(stringMapList).toString();
                    scenarioLogger.warn("Value extracted:\r" + dtString);
                } catch (Throwable e) {
                    logger.debug("log error");
                }
            }
        } else {
            String s = null == result ? "" : StringTools.peel(result.toString());
            WhiteBoard.getInstance().putString(getOwner(), varName, s);
            scenarioLogger.warn("Value extracted:" + s);
        }
    }

    public void isExtractedFromWithQueryAndIndexedBy(String varName0, String sourceString0, String queryExpression0, String indexColumnName0) {
        String varName = WhiteBoard.getInstance().resolve(getOwner(), varName0);
        String sourceString = WhiteBoard.getInstance().resolve(getOwner(), sourceString0);
        String queryExpression = WhiteBoard.getInstance().resolve(getOwner(), queryExpression0);
        String indexColumnName = WhiteBoard.getInstance().resolve(getOwner(), indexColumnName0);
        scenarioLogger.warn("varName=" + varName);
        scenarioLogger.warn("sourceString=" + sourceString);
        scenarioLogger.warn("queryExpression=" + queryExpression);
        scenarioLogger.warn("indexColumnName=" + indexColumnName);
        WhiteBoard.getInstance().removeAll(getOwner(), varName + ".*");

        QueryExpression qe = new QueryExpression(queryExpression);

        QueryBase q = qe.compile(sourceString);
        Object result = q.query(qe.getQuery());

        if (null != result && result instanceof List) {
            List objList = null;
            List> mapList = null;
            try {
                objList = (List) result;
            } catch (Exception e) {
                Assert.fail("Unknown type in list: " + result);
            }
            try {
                mapList = (List>) result;
            } catch (Exception e) {
                // do nothing
            }

            if (null == mapList) {
                throw new ImmediateAbortException("List cannot be indexed.");
            } else {
                List> stringMapList = DataTableTools.asMapsOfStrings(mapList);
                Map> indexedMaps = DataTableTools.dataTableAsMapsIndexedByKeyHeader(indexColumnName, stringMapList);
                WhiteBoard.getInstance().storeMaps(getOwner(), varName, indexedMaps);
                try {
                    logger.debug(varName + "_rows=" + WhiteBoard.getInstance().getString(getOwner(), varName + "_rows"));
                    String dtString = DataTable.create(stringMapList).toString();
                    scenarioLogger.warn("Value extracted:\r" + dtString);
                } catch (Throwable e) {
                    logger.debug("log error");
                }
            }
        } else {
            throw new ImmediateAbortException("Result cannot be indexed.");
        }
    }

    public void isExtractedAsStringFromWithQuery(String varName0, String sourceString0, String queryExpression0) {
        String varName = WhiteBoard.getInstance().resolve(getOwner(), varName0);
        String sourceString = WhiteBoard.getInstance().resolve(getOwner(), sourceString0);
        String queryExpression = WhiteBoard.getInstance().resolve(getOwner(), queryExpression0);
        scenarioLogger.warn("varName=" + varName);
        scenarioLogger.warn("sourceString=" + sourceString);
        scenarioLogger.warn("queryExpression=" + queryExpression);
        WhiteBoard.getInstance().removeAll(getOwner(), varName + ".*");

        QueryExpression qe = new QueryExpression(queryExpression);

        QueryBase q = qe.compile(sourceString);
        Object result = q.query(qe.getQuery());

        String s = null == result ? "-" : result.toString().trim();
        WhiteBoard.getInstance().putString(getOwner(), varName, s);
        scenarioLogger.warn("Value extracted:" + s);
    }
}