Search in sources :

Example 1 with AdapterStubResponse

use of com.centurylink.mdw.model.value.event.AdapterStubResponse in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseRun method getStubResponse.

/**
 * Callback method invoked from stub server when notified from server.
 * Default implementation to substitute values from request as super.getStubResponse();
 * Users can provide their own implementation as a Groovy closure.
 */
@Override
public String getStubResponse(String masterRequestId, String request, int run) throws JSONException, TestException {
    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) {
        // activity stubbing
        if (testCaseProcess != null && testCaseProcess.getActivityStubs() != null) {
            for (TestCaseActivityStub activityStub : testCaseProcess.getActivityStubs()) {
                if (activityStub.getMatcher().call(activityRuntimeContext)) {
                    Closure<String> completer = activityStub.getCompleter();
                    completer.setResolveStrategy(Closure.DELEGATE_FIRST);
                    completer.setDelegate(activityStub);
                    String resultCode = null;
                    try {
                        resultCode = completer.call(request);
                    } catch (Throwable th) {
                        th.printStackTrace(log);
                        throw new TestException(th.getMessage(), th);
                    }
                    ActivityStubResponse activityStubResponse = new ActivityStubResponse();
                    activityStubResponse.setResultCode(resultCode);
                    if (activityStub.getSleep() > 0)
                        activityStubResponse.setSleep(activityStub.getSleep());
                    else if (activityStub.getDelay() > 0)
                        activityStubResponse.setSleep(activityStub.getDelay());
                    if (activityStub.getVariables() != null) {
                        Map<String, String> responseVariables = new HashMap<String, String>();
                        Map<String, Object> variables = activityStub.getVariables();
                        for (String name : variables.keySet()) {
                            Object value = variables.get(name);
                            // TODO: handle non-string objects
                            responseVariables.put(name, value.toString());
                        }
                        activityStubResponse.setVariables(responseVariables);
                    }
                    if (verbose)
                        log.println("Stubbing activity " + activityRuntimeContext.getProcess().getProcessName() + ":" + activityRuntimeContext.getActivityLogicalId() + " with result code: " + resultCode);
                    if (activityStubRequest != null) {
                        // mdw6+
                        return activityStubResponse.getJson().toString(2);
                    } else {
                        return "RESPONSE~" + activityStubResponse.getSleep() + "~" + activityStubResponse.getJson();
                    }
                }
            }
        }
        if (activityStubRequest != null) {
            // mdw6+
            ActivityStubResponse activityStubResponse = new ActivityStubResponse();
            activityStubResponse.setPassthrough(true);
            return activityStubResponse.getJson().toString(2);
        } else {
            return "(EXECUTE_ACTIVITY)";
        }
    } else {
        // adapter stubbing
        for (TestCaseAdapterStub adapterStub : adapterStubs) {
            String requestContent = adapterStubRequest == null ? request : adapterStubRequest.getContent();
            boolean match = false;
            try {
                if (adapterStub.isEndpoint() && adapterStubRequest != null) {
                    match = adapterStub.getMatcher().call(adapterStubRequest);
                } else {
                    match = adapterStub.getMatcher().call(requestContent);
                }
            } catch (Throwable th) {
                th.printStackTrace(log);
                throw new TestException(th.getMessage(), th);
            }
            if (match) {
                try {
                    String stubbedResponseContent = adapterStub.getResponder().call(requestContent);
                    int delay = 0;
                    if (adapterStub.getDelay() > 0)
                        delay = adapterStub.getDelay();
                    else if (adapterStub.getSleep() > 0)
                        delay = adapterStub.getSleep();
                    if (adapterStubRequest != null) {
                        // mdw6+
                        AdapterStubResponse stubResponse = new AdapterStubResponse(stubbedResponseContent);
                        stubResponse.setDelay(delay);
                        stubResponse.setStatusCode(adapterStub.getStatusCode());
                        stubResponse.setStatusMessage(adapterStub.getStatusMessage());
                        if (verbose)
                            log.println("Stubbing endpoint " + adapterStubRequest.getUrl() + " with:\n" + stubbedResponseContent);
                        return stubResponse.getJson().toString(2);
                    } else {
                        if (verbose)
                            log.println("Stubbing response with: " + stubbedResponseContent);
                        return "RESPONSE~" + delay + "~" + stubbedResponseContent;
                    }
                } catch (Throwable th) {
                    th.printStackTrace(log);
                    throw new TestException(th.getMessage(), th);
                }
            }
        }
        if (verbose)
            log.println("Stubbing response with: " + AdapterActivity.MAKE_ACTUAL_CALL);
        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;
        }
    }
}
Also used : HashMap(java.util.HashMap) ActivityStubResponse(com.centurylink.mdw.model.value.activity.ActivityStubResponse) JSONException(org.json.JSONException) ActivityStubRequest(com.centurylink.mdw.model.value.activity.ActivityStubRequest) ActivityRuntimeContext(com.centurylink.mdw.model.value.activity.ActivityRuntimeContext) AdapterStubRequest(com.centurylink.mdw.model.value.event.AdapterStubRequest) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AdapterStubResponse(com.centurylink.mdw.model.value.event.AdapterStubResponse)

Example 2 with AdapterStubResponse

use of com.centurylink.mdw.model.value.event.AdapterStubResponse 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

ActivityRuntimeContext (com.centurylink.mdw.model.value.activity.ActivityRuntimeContext)2 ActivityStubRequest (com.centurylink.mdw.model.value.activity.ActivityStubRequest)2 ActivityStubResponse (com.centurylink.mdw.model.value.activity.ActivityStubResponse)2 AdapterStubRequest (com.centurylink.mdw.model.value.event.AdapterStubRequest)2 AdapterStubResponse (com.centurylink.mdw.model.value.event.AdapterStubResponse)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 VersionControl (com.centurylink.mdw.dataaccess.VersionControl)1 VersionControlGit (com.centurylink.mdw.dataaccess.file.VersionControlGit)1 DesignerDataAccess (com.centurylink.mdw.designer.DesignerDataAccess)1 Stubber (com.centurylink.mdw.designer.testing.StubServer.Stubber)1 RestfulServer (com.centurylink.mdw.designer.utils.RestfulServer)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 BuildException (org.apache.tools.ant.BuildException)1