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

com.exactpro.sf.scriptrunner.ScriptContext Maven / Gradle / Ivy

There is a newer version: 3.4.260
Show newest version
/******************************************************************************
 * Copyright 2009-2018 Exactpro (Exactpro Systems Limited)
 *
 * Licensed 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 com.exactpro.sf.scriptrunner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.exactpro.sf.aml.script.AMLHashMap;
import com.exactpro.sf.aml.script.CheckPoint;
import com.exactpro.sf.center.ISFContext;
import com.exactpro.sf.common.services.ServiceName;
import com.exactpro.sf.configuration.IDataManager;
import com.exactpro.sf.configuration.IDictionaryManager;
import com.exactpro.sf.configuration.IEnvironmentManager;
import com.exactpro.sf.configuration.netdumper.NetDumperService;
import com.exactpro.sf.configuration.workspace.IWorkspaceDispatcher;
import com.exactpro.sf.scriptrunner.Outcome.Status;
import com.exactpro.sf.scriptrunner.actionmanager.IActionManager;
import com.exactpro.sf.scriptrunner.utilitymanager.IUtilityManager;
import com.exactpro.sf.services.ServiceMarshalManager;
import com.exactpro.sf.storage.ScriptRun;
import com.exactpro.sf.util.BugDescription;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

public class ScriptContext
{
	private final Map staticMap;
	private final Map serviceNames;
	private final List serviceList;
	private final Set receivedMessages;

	private final IEnvironmentManager environmentManager;
    private final OutcomeCollector outcomes;
    private final IScriptProgress scriptProgress;
    private final DebugController debugController;
    private final NetDumperService netDumper;

    private final long scriptDescriptionId;

    private final String userName;

    private boolean conditionallyPassed;
    private final Set knownBugs = new HashSet<>();
	private String actionName;
    private Exception exception;
    private boolean interrupt;
	private ScriptRun scriptRun;
    private final Set unexpectedMessage;
	private CheckPoint tCStartCheckPoint;
	private long scriptStartTime;
	private String testCaseName;
	private final String environmentName;
    private final String environmentVariableSet;
	private IScriptConfig scriptConfig;
	private final IActionManager actionManager;
	private final IUtilityManager utilityManager;
	private final IDictionaryManager dictionaryManager;
	private final IScriptReport report;
	private final IWorkspaceDispatcher workspaceDispatcher;
    private final IDataManager dataManager;
    private final Map pluginClassLoaders;
    private final SetMultimap executedActions = HashMultimap.create();
    private final ServiceMarshalManager serviceMarshalManager;

    public ScriptContext(ISFContext context, IScriptProgress scriptProgress, IScriptReport report, DebugController debugController, String userName, long scriptDescriptionId) {
        this(context, scriptProgress, report, debugController, userName, scriptDescriptionId, ServiceName.DEFAULT_ENVIRONMENT);
    }

	public ScriptContext(ISFContext context, IScriptProgress scriptProgress, IScriptReport report, DebugController debugController, String userName, long scriptDescriptionId, String environmentName) {
		Objects.requireNonNull(context, "context cannot be null");

		this.scriptProgress = scriptProgress;
		this.debugController = debugController;
		this.staticMap = new AMLHashMap<>();
		this.serviceNames = new HashMap<>();
		this.serviceList = Collections.synchronizedList(new ArrayList());
		this.environmentManager = context.getEnvironmentManager();
		this.outcomes = new OutcomeCollector();
		this.receivedMessages = new LinkedHashSet<>(); // preserve insert order
		this.unexpectedMessage = new LinkedHashSet<>(); // preserve insert order
		this.userName = userName;
		this.report = report;
		this.actionManager = context.getActionManager();
		this.utilityManager = context.getUtilityManager();
		this.dictionaryManager = context.getDictionaryManager();
		this.workspaceDispatcher = context.getWorkspaceDispatcher();
		this.netDumper = context.getNetDumperService();
		this.scriptDescriptionId = scriptDescriptionId;
        this.dataManager = context.getDataManager();
        this.pluginClassLoaders = context.getPluginClassLoaders();
        this.environmentName = environmentName;
        this.serviceMarshalManager = context.getServiceMarshalManager();

        IConnectionManager connectionManager = context.getConnectionManager();

        /*
         HOTFIX: for the case when we try to load variable set for environment that doesn't exist anymore (e.g. bb__automation).
         This could happen, for example, when we're loading test script description from file. Ideally, environment's variable set
         should be serialized alongside environment name and passed as a parameter too, because even if environment exists it
         could have a different variable set than it had when test script description was serialized.
        */
        if (connectionManager.getEnvironmentList().contains(environmentName)) {
            this.environmentVariableSet = connectionManager.getEnvironmentVariableSet(environmentName);
        } else {
            this.environmentVariableSet = null;
        }

		reset();
	}

	public void reset() {
		this.actionName = null;
		this.exception = null;
        outcomes.clear();
        receivedMessages.clear();
        unexpectedMessage.clear();
		this.tCStartCheckPoint = null;
        this.conditionallyPassed = false;
        knownBugs.clear();
	}

    public String getUserName() {
        return userName;
    }

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
        if(this.exception == null) {
            this.exception = exception;
        }
	}

	public boolean isInterrupt() {
		return interrupt;
	}

	public void setInterrupt(boolean interrupt) {
		this.interrupt = interrupt;
	}

	public Map getStaticMap() {
		return staticMap;
	}

	public Map getServiceNames() {
		return serviceNames;
	}

	public List getServiceList() {
		return serviceList;
	}

	public IEnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}

	public OutcomeCollector getOutcomeCollector() {
        return outcomes;
	}

	public void onOutcomeComplete(String group, String name) {
        outcomes.onOutcomeComplete(group, name);
	}

	public void onGroupComplete(String group) {
        outcomes.onGroupComplete(group);
	}

	public void storeOutcome(Outcome outcome) {
        outcomes.storeOutcome(outcome);
	}

    public Status getOutcomeStatus(String group, String name) {
        return outcomes.getOutcomeStatus(group, name);
	}

    public Status getOutcomeGroupStatus(String group) {
        return outcomes.getGroupStatus(group);
	}

	public IScriptProgress getScriptProgress()
	{
        return scriptProgress;
	}

	public ScriptRun getScriptRun() {
		return scriptRun;
	}

	public void setScriptRun(ScriptRun scriptRun) {
		this.scriptRun = scriptRun;
	}

	// generated code adds received (wait action) message to this list
	public Set getReceivedMessages() {
		return receivedMessages;
	}

    public Set getUnexpectedMessage() {
        return unexpectedMessage;
    }

    public void addUnexpectedMessages(Set setUnexpectedMessages){
        unexpectedMessage.addAll(setUnexpectedMessages);
    }

	public CheckPoint getTCStartCheckPoint() {
		return tCStartCheckPoint;
	}

	public void setTCStartCheckPoint(CheckPoint tCStartCheckPoint) {
		this.tCStartCheckPoint = tCStartCheckPoint;
	}

	public long getScriptStartTime() {
        return scriptStartTime;
	}

	public void setScriptStartTime(long time) {
		this.scriptStartTime = time;
	}

	public String getTestCaseName() {
        return testCaseName;
	}

	public void setTestCaseName(String testCaseName) {
		this.testCaseName = testCaseName;
	}

	public String getEnvironmentName() {
		return environmentName;
	}

    public String getEnvironmentVariableSet() {
        return environmentVariableSet;
    }

    public IScriptReport getReport() {
		return report;
	}

    public void setScriptConfig(IScriptConfig scriptConfig) {
        this.scriptConfig = scriptConfig;
    }

    public IScriptConfig getScriptConfig() {
        return scriptConfig;
    }

    public DebugController getDebugController() {
        return debugController;
    }

    public IActionManager getActionManager() {
        return actionManager;
    }

    public IUtilityManager getUtilityManager() {
        return utilityManager;
    }

	public IDictionaryManager getDictionaryManager() {
		return dictionaryManager;
	}

    public IWorkspaceDispatcher getWorkspaceDispatcher() {
        return workspaceDispatcher;
    }

	public NetDumperService getNetDumperService() {
		return netDumper;
	}

	public long getScriptDescriptionId() {
		return scriptDescriptionId;
	}

    public boolean isConditionallyPassed() {
        return conditionallyPassed;
    }

    public void setConditionallyPassed(boolean conditionallyPassed) {
        this.conditionallyPassed = conditionallyPassed;
    }

    /**
     * @return the dataManager
     */
    public IDataManager getDataManager() {
        return dataManager;
    }

    public Set getKnownBugs() {
        return knownBugs;
    }

    public Map getPluginClassLoaders() {
        return pluginClassLoaders;
    }

    public void addExecutedAction(String reference) {
        executedActions.put(getTestCaseName(), reference);
    }

    public boolean checkExecutedActions(List references) {
        return executedActions.get(getTestCaseName()).containsAll(references);
    }

    public ServiceMarshalManager getServiceMarshalManager() {
        return serviceMarshalManager;
    }
}