Search in sources :

Example 1 with Stubber

use of com.centurylink.mdw.designer.testing.StubServer.Stubber in project mdw-designer by CenturyLinkCloud.

the class AutomatedTestView method runTests.

public void runTests() {
    // TODO: currently mixed execution of Gherkin/Groovy scripts
    // fails to start LogMessageMonitor in Designer VM (otherwise will
    // conflict with Cucumber-JVM)
    boolean hasGherkin = false;
    for (AutomatedTestCase rtc : testSuite.getTestCases()) {
        if (rtc.isGherkin())
            hasGherkin = true;
    }
    try {
        prepForRun();
        counterData = new CounterData(testSuite.getTestCases().size());
        updateCounterPanel(0, 0, 0, true);
        monitor = null;
        if (!hasGherkin && !testSuite.isDebug())
            monitor = new LogMessageMonitor(testSuite.getProject().getDesignerProxy().getDesignerDataAccess(), testSuite.getProject().isOldNamespaces());
        String msg = designerProxy.checkForServerDbMismatch();
        if (msg != null) {
            MessageDialog.openError(getSite().getShell(), "Server DB Mismatch", msg);
            handleStop();
            return;
        }
        if (StubServer.isRunning())
            StubServer.stop();
        if (testSuite.isStubbing() && !hasGherkin && !testSuite.isDebug()) {
            StubServer.Stubber stubber = new TestStubber();
            int port = testSuite.getProject().getServerSettings().getStubServerPort();
            StubServer.start(testSuite.getProject().getDesignerProxy().getRestfulServer(), port, stubber, testSuite.getProject().isOldNamespaces());
        }
    } catch (Exception ex) {
        PluginMessages.uiError(ex, "Automated Test", testSuite.getProject());
        handleStop();
        return;
    }
    Thread background = new Thread(new Runnable() {

        public void run() {
            testCaseStatuses = new HashMap<>();
            updateProgressBar(5, false, false);
            threadPool = new ThreadPool(testSuite.getThreadCount());
            PrintStream printStream = null;
            updateProgressBar(10, false, false);
            if (monitor != null)
                monitor.start(true);
            for (AutomatedTestCase testCase : testSuite.getTestCases()) {
                if (!testSuite.isRunning())
                    continue;
                File resultDir = testCase.getResultsDir();
                try {
                    File executeLog = testCase.getOutputFile();
                    deleteFile(executeLog);
                    if (testCase.isPostman())
                        deleteFile(testCase.getItemOutputFile());
                    if (!executeLog.getParentFile().exists() && !executeLog.getParentFile().mkdirs())
                        throw new IOException("Unable to create test run directory: " + executeLog.getParentFile());
                    PrintStream log = new PrintStream(executeLog);
                    ImageDescriptor icon = MdwPlugin.getImageDescriptor("icons/auto_test.gif");
                    MessageConsole console = MessageConsole.findConsole("Tests", icon, display);
                    printStream = new PrintStream(console.newFileConsoleOutputStream(executeLog));
                    TestCaseRun testCaseRun = designerProxy.prepareTestCase(testCase, 0, resultDir, testSuite.isCreateReplaceResults(), testSuite.isVerbose(), log, monitor, testSuite.isSingleServer(), testSuite.isStubbing(), testSuite.isDebug());
                    masterRequestRunMap.put(testCaseRun.getMasterRequestId(), testCaseRun);
                    testCaseRun.setMasterRequestListener(AutomatedTestView.this);
                    threadPool.execute(testCaseRun);
                    updateTreeAndProgressBar(false);
                    try {
                        Thread.sleep(testSuite.getThreadInterval() * 1000);
                    } catch (InterruptedException ex) {
                    }
                } catch (Exception ex) {
                    PluginMessages.log(ex);
                    testCase.setErrored();
                    updateTreeAndProgressBar(true);
                    testSuite.setRunning(false);
                    if (printStream != null) {
                        ex.printStackTrace(printStream);
                        printStream.close();
                    }
                    handleStop();
                    return;
                }
            }
            while (!threadPool.isTerminated()) {
                // update the tree and the progress bar
                updateTreeAndProgressBar(false);
                if (testSuite.isFinished())
                    threadPool.shutdown();
                try {
                    Thread.sleep(2500);
                } catch (InterruptedException e) {
                }
            }
            updateTreeAndProgressBar(true);
            testSuite.setRunning(false);
            actionGroup.enableStopAction(false);
            actionGroup.enableRerunAction(true);
            File resultsFile = testSuite.getProject().getFunctionTestResultsFile();
            actionGroup.enableFormatFunctionTestResults(resultsFile != null && resultsFile.exists());
            locked = false;
            if (monitor != null && monitor.isBound())
                monitor.shutdown();
            if (printStream != null)
                printStream.close();
        }
    });
    background.start();
}
Also used : PrintStream(java.io.PrintStream) MessageConsole(com.centurylink.mdw.plugin.MessageConsole) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ThreadPool(com.centurylink.mdw.designer.testing.ThreadPool) Stubber(com.centurylink.mdw.designer.testing.StubServer.Stubber) IOException(java.io.IOException) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) StubServer(com.centurylink.mdw.designer.testing.StubServer) LogMessageMonitor(com.centurylink.mdw.designer.testing.LogMessageMonitor) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) AutomatedTestCase(com.centurylink.mdw.plugin.designer.model.AutomatedTestCase) TestCaseRun(com.centurylink.mdw.designer.testing.TestCaseRun) File(java.io.File)

Example 2 with Stubber

use of com.centurylink.mdw.designer.testing.StubServer.Stubber in project mdw-designer by CenturyLinkCloud.

the class AutomatedTestView method runLoadTests.

public void runLoadTests() {
    int totalRunCount = 0;
    for (AutomatedTestCase testCase : testSuite.getTestCases()) totalRunCount += testCase.getRunCount();
    testSuite.setRunCount(totalRunCount);
    try {
        prepForRun();
        counterData = new CounterData(testSuite.getRunCount());
        updateCounterPanel(0, 0, 0, true);
        monitorObj = new LoadTestLogMonitor(testSuite.getProject().getDesignerProxy().getDesignerDataAccess(), masterRequestRunMap, null);
        String msg = designerProxy.checkForServerDbMismatch();
        if (msg != null) {
            MessageDialog.openError(getSite().getShell(), "Server DB Mismatch", msg);
            handleStop();
            return;
        }
        if (testSuite.isStubbing()) {
            StubServer.Stubber stubber = new TestStubber();
            if (StubServer.isRunning())
                StubServer.stop();
            int port = testSuite.getProject().getServerSettings().getStubServerPort();
            StubServer.start(testSuite.getProject().getDesignerProxy().getRestfulServer(), port, stubber, testSuite.getProject().isOldNamespaces());
        }
    } catch (Exception ex) {
        PluginMessages.uiError(ex, "Load Test", testSuite.getProject());
        handleStop();
        return;
    }
    Thread background = new Thread(new Runnable() {

        public void run() {
            testCaseStatuses = new HashMap<>();
            updateProgressBar(5, false, false);
            threadPool = new ThreadPool(testSuite.getThreadCount());
            long startTime = System.currentTimeMillis();
            int totalPrepared = 0;
            int totalCompleted = 0;
            try {
                startTime = System.currentTimeMillis();
                monitorObj.start(true);
                for (AutomatedTestCase testCase : testSuite.getTestCases()) {
                    File resultDir = testCase.getResultsDir();
                    File executeLog = new File(resultDir.getPath() + "/execute.log");
                    int ct = testCase.getRunCount();
                    for (int k = 0; k < ct; k++) {
                        deleteFile(executeLog);
                        if (!executeLog.getParentFile().exists() && !executeLog.getParentFile().mkdirs())
                            throw new IOException("Unable to create test run directory: " + executeLog.getParentFile());
                        PrintStream log = new PrintStream(executeLog);
                        TestCaseRun testCaseRun = designerProxy.prepareTestCase(testCase, k, resultDir, false, testSuite.isVerbose(), log, monitorObj, testSuite.isSingleServer(), testSuite.isStubbing(), false);
                        totalPrepared++;
                        testCase.getTestCase().setNumberPrepared(totalPrepared);
                        masterRequestRunMap.put(testCaseRun.getMasterRequestId(), testCaseRun);
                        threadPool.execute(testCaseRun);
                        updateTreeAndProgressBar(false);
                        try {
                            Thread.sleep(testSuite.getThreadInterval() * 1000);
                        } catch (InterruptedException ex) {
                        }
                    }
                }
                updateProgressBar(10, false, false);
                // server's commonThreadPool
                while (!threadPool.isTerminated()) {
                    // update the tree and the progress bar
                    updateTreeAndProgressBar(false);
                    if (testSuite.isFinished())
                        threadPool.shutdown();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
                testSuite.setRunning(false);
                actionGroup.enableStopAction(false);
                actionGroup.enableRerunAction(true);
                File resultsFile = testSuite.getProject().getLoadTestResultsFile();
                actionGroup.enableFormatLoadTestResults(resultsFile != null && resultsFile.exists());
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (monitorObj != null)
                    monitorObj.shutdown();
                locked = false;
            }
            try {
                generateLoadTestReport(totalPrepared, totalCompleted, monitorObj, startTime, testSuite.getResultsDir().toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
    background.start();
}
Also used : PrintStream(java.io.PrintStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ThreadPool(com.centurylink.mdw.designer.testing.ThreadPool) FileNotFoundException(java.io.FileNotFoundException) Stubber(com.centurylink.mdw.designer.testing.StubServer.Stubber) IOException(java.io.IOException) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) StubServer(com.centurylink.mdw.designer.testing.StubServer) AutomatedTestCase(com.centurylink.mdw.plugin.designer.model.AutomatedTestCase) TestCaseRun(com.centurylink.mdw.designer.testing.TestCaseRun) LoadTestLogMonitor(com.centurylink.mdw.designer.testing.LoadTestLogMonitor) File(java.io.File)

Example 3 with Stubber

use of com.centurylink.mdw.designer.testing.StubServer.Stubber in project mdw-designer by CenturyLinkCloud.

the class AutoTestAntTask method execute.

@Override
public void execute() throws BuildException {
    try {
        if (sslTrustStore != null)
            System.setProperty("javax.net.ssl.trustStore", sslTrustStore.getAbsolutePath());
        DesignerDataAccess.getAuthenticator().authenticate(user, password);
        RestfulServer restfulServer = new RestfulServer(jdbcUrl == null ? "dummy" : jdbcUrl, user, serverUrl.toString());
        if (workflowDir != null) {
            // vcs-based assets
            VersionControl versionControl = new VersionControlGit();
            versionControl.connect(null, null, null, workflowDir);
            restfulServer.setVersionControl(versionControl);
            restfulServer.setRootDirectory(workflowDir);
            restfulServer.setDatabaseUrl("jdbc://dummy");
            designerDataAccess = new DesignerDataAccess(restfulServer, null, user, false);
            designerDataAccess.setCurrentServer(restfulServer);
        } else {
            designerDataAccess = new DesignerDataAccess(restfulServer, null, user, oldNamespaces);
            if (designerDataAccess.getUser(user) == null)
                throw new BuildException("User '" + user + "' not found to run automated tests for this environment.");
            if (!designerDataAccess.getUser(user).hasRole(UserGroupVO.COMMON_GROUP, UserRoleVO.PROCESS_EXECUTION) && !designerDataAccess.getUser(user).hasRole(UserGroupVO.SITE_ADMIN_GROUP, UserRoleVO.PROCESS_EXECUTION))
                throw new BuildException("User '" + user + "' not authorized to run automated tests for this environment.");
        }
        VariableTypeCache.loadCache(designerDataAccess.getVariableTypes());
        log("Setup completed for test suite: '" + suiteName + "'", Project.MSG_ERR);
        try {
            if (stubbing) {
                Stubber stubber = new Stubber() {

                    public String processMessage(String masterRequestId, String request) {
                        try {
                            TestCaseRun run = masterRequestRunMap.get(masterRequestId);
                            if (run == null) {
                                JSONObject requestJson = null;
                                // mdw6
                                ActivityStubRequest activityStubRequest = null;
                                ActivityRuntimeContext activityRuntimeContext = null;
                                // mdw6
                                AdapterStubRequest adapterStubRequest = null;
                                if (request != null && request.trim().startsWith("{")) {
                                    try {
                                        requestJson = new JSONObject(request);
                                    } catch (JSONException ex) {
                                    // unparseable -- handle old way for
                                    // adapter stubbing
                                    }
                                    if (requestJson != null) {
                                        if (requestJson.has(ActivityStubRequest.JSON_NAME)) {
                                            activityStubRequest = new ActivityStubRequest(requestJson);
                                            activityRuntimeContext = activityStubRequest.getRuntimeContext();
                                        } else if (requestJson.has("ActivityRuntimeContext")) {
                                            activityRuntimeContext = new ActivityRuntimeContext(requestJson);
                                        } else if (requestJson.has(AdapterStubRequest.JSON_NAME)) {
                                            adapterStubRequest = new AdapterStubRequest(requestJson);
                                        }
                                    }
                                }
                                if (activityRuntimeContext != null) {
                                    if (activityStubRequest != null) {
                                        // mdw6+
                                        ActivityStubResponse activityStubResponse = new ActivityStubResponse();
                                        activityStubResponse.setPassthrough(true);
                                        return activityStubResponse.getJson().toString(2);
                                    } else {
                                        return "(EXECUTE_ACTIVITY)";
                                    }
                                } else {
                                    if (adapterStubRequest != null) {
                                        // mdw6+
                                        AdapterStubResponse stubResponse = new AdapterStubResponse(AdapterActivity.MAKE_ACTUAL_CALL);
                                        stubResponse.setPassthrough(true);
                                        return stubResponse.getJson().toString(2);
                                    } else {
                                        return AdapterActivity.MAKE_ACTUAL_CALL;
                                    }
                                }
                            }
                            return run.getStubResponse(masterRequestId, request, run.getRunNumber());
                        } catch (Exception ex) {
                            log(ex.getMessage(), ex, Project.MSG_ERR);
                            return null;
                        }
                    }
                };
                if (StubServer.isRunning())
                    StubServer.stop();
                StubServer.start(restfulServer, stubPort, stubber, this.oldNamespaces);
            }
            runTests();
        } finally {
            // try not to leave the socket connection open
            StubServer.stop();
        }
    } catch (BuildException ex) {
        throw ex;
    } catch (Exception ex) {
        try {
            updateResults(true);
        } catch (BuildException bex) {
            throw bex;
        } catch (IOException ioex) {
            log(ioex.getMessage(), ioex, Project.MSG_ERR);
        }
        log(ex.getMessage(), ex, Project.MSG_ERR);
        throw new BuildException(ex);
    }
}
Also used : VersionControlGit(com.centurylink.mdw.dataaccess.file.VersionControlGit) ActivityStubResponse(com.centurylink.mdw.model.value.activity.ActivityStubResponse) JSONException(org.json.JSONException) RestfulServer(com.centurylink.mdw.designer.utils.RestfulServer) ActivityStubRequest(com.centurylink.mdw.model.value.activity.ActivityStubRequest) IOException(java.io.IOException) VersionControl(com.centurylink.mdw.dataaccess.VersionControl) JSONException(org.json.JSONException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ActivityRuntimeContext(com.centurylink.mdw.model.value.activity.ActivityRuntimeContext) AdapterStubRequest(com.centurylink.mdw.model.value.event.AdapterStubRequest) JSONObject(org.json.JSONObject) Stubber(com.centurylink.mdw.designer.testing.StubServer.Stubber) DesignerDataAccess(com.centurylink.mdw.designer.DesignerDataAccess) BuildException(org.apache.tools.ant.BuildException) AdapterStubResponse(com.centurylink.mdw.model.value.event.AdapterStubResponse)

Aggregations

Stubber (com.centurylink.mdw.designer.testing.StubServer.Stubber)3 IOException (java.io.IOException)3 JSONException (org.json.JSONException)3 StubServer (com.centurylink.mdw.designer.testing.StubServer)2 TestCaseRun (com.centurylink.mdw.designer.testing.TestCaseRun)2 ThreadPool (com.centurylink.mdw.designer.testing.ThreadPool)2 AutomatedTestCase (com.centurylink.mdw.plugin.designer.model.AutomatedTestCase)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 PrintStream (java.io.PrintStream)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 VersionControl (com.centurylink.mdw.dataaccess.VersionControl)1 VersionControlGit (com.centurylink.mdw.dataaccess.file.VersionControlGit)1 DesignerDataAccess (com.centurylink.mdw.designer.DesignerDataAccess)1 LoadTestLogMonitor (com.centurylink.mdw.designer.testing.LoadTestLogMonitor)1 LogMessageMonitor (com.centurylink.mdw.designer.testing.LogMessageMonitor)1 RestfulServer (com.centurylink.mdw.designer.utils.RestfulServer)1 ActivityRuntimeContext (com.centurylink.mdw.model.value.activity.ActivityRuntimeContext)1 ActivityStubRequest (com.centurylink.mdw.model.value.activity.ActivityStubRequest)1