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

com.ats.script.actions.ActionChannelStart Maven / Gradle / Ivy

The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF 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 com.ats.script.actions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Predicate;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ats.AtsSingleton;
import com.ats.executor.ActionTestScript;
import com.ats.executor.channels.Channel;
import com.ats.generator.variables.CalculatedValue;
import com.ats.script.Script;
import com.ats.script.ScriptHeader;
import com.ats.script.actions.neoload.ActionNeoload;
import com.ats.script.actions.performance.ActionPerformance;
import com.google.common.base.Strings;
import com.google.gson.JsonObject;

public class ActionChannelStart extends ActionChannel {

	private static final String UNKNOWN_ICON = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAABhWlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw1AUhU9TRdGqgx1EHCJUJ7tUEcdSxSJYKG2FVh1MXvoHTRqSFBdHwbXg4M9i1cHFWVcHV0EQ/AFxdnBSdJES70sKLWK88Hgf591zeO8+QGhUmGp2RQFVs4xUPCZmc6tizyt86McgxhGRmKkn0osZeNbXPXVT3YV5lnffnzWg5E0G+ETiKNMNi3iDeHbT0jnvEwdZSVKIz4mnDLog8SPXZZffOBcdFnhm0Mik5omDxGKxg+UOZiVDJZ4hDimqRvlC1mWF8xZntVJjrXvyFwby2kqa67TGEMcSEkhChIwayqjAQph2jRQTKT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G7SJUZMwQuP2oyEQAPXbrm892OV2xXd8ZwENroJEO0RHIrUqypUNO9erJJpo8892JDeVOwIV8ODMQuBQgi4lld8ta66yWtJIsRiN55Wg0yym4pB0VVnFyVap72R6wtDwJUbNW4WBP9sar77wyEk/5d8HUibINZAzwMv4itnuF52ef6HKsGhCreLJmbnClxgHsjJXGq8BJzz5rkvDoUkXx+3ST9tMz1moDqL5w5wLpsMFyVALvVIBkh587wFSkMiEmCgVzEUkhzeb1DxJ98EUjvb7f47MMxZlDuE1m/v3QlSwJWNGhdWLaJFh/TV+kv3If8AmG27MhPv/96rXecEcLrdyuwLGFj9MQr4rsyUtzLLnZf+6V8ngZPeyO1m6c97Pc909auof1t+C6H1GDANHPX5tiwCaSCXTYaL7Xj+A+GqRtjLA+pTAAAAAElFTkSuQmCC";

	private static final String SCRIPT_START_LABEL = SCRIPT_LABEL + "start";
	public static final Predicate PREDICATE = g -> SCRIPT_START_LABEL.equals(g);

	public static final int PERF = 1;
	public static final int NEOLOAD = 2;

	public static final String ATTACH = "attach";
	public static final String USE_COOKIE = "use-cookie";

	private CalculatedValue application;
	private ArrayList arguments = new ArrayList<>();

	private int performance = 0;

	private boolean useCookie = false;

	private boolean attach = false;

	private String authentication = "";
	private String authenticationValue = "";

	public ActionChannelStart() {
		super();
	}

	public ActionChannelStart(Script script, String name, List options, CalculatedValue value) {
		this(script, name, options, value, null);
	}

	public ActionChannelStart(Script script, String name, List options, CalculatedValue value, List dataArray) {
		super(script, name);
		setApplication(value);

		if(dataArray != null) {
			options.addAll(dataArray);
		}
		options.forEach(o -> parseOptions(script, o.trim()));
	}

	private void parseOptions(Script script, String value){
		if(ActionNeoload.SCRIPT_NEOLOAD_LABEL.equalsIgnoreCase(value)) {
			setPerformance(NEOLOAD);
		}else if(ATTACH.equalsIgnoreCase(value)) {
			setAttach(true);
		}else if(USE_COOKIE.equalsIgnoreCase(value)) {
			setUseCookie(true);
		}else if(ActionPerformance.SCRIPT_PERFORMANCE_LABEL.equalsIgnoreCase(value)) {
			setPerformance(PERF);
		}else if(Channel.BASIC_AUTHENTICATION.equalsIgnoreCase(value) || Channel.BEARER_AUTHENTICATION.equalsIgnoreCase(value)){
			setAuthentication(value);
		}else if(!Strings.isNullOrEmpty(getAuthentication())) {
			setAuthenticationValue(value);
		}else {
			arguments.add(new CalculatedValue(script, value));
		}
	}

	public ActionChannelStart(Script script, boolean attach, boolean useCookie, String name, CalculatedValue value, String[] options) {
		this(script, name, Arrays.asList(options), value, null);
		this.setAttach(attach);
		this.setUseCookie(useCookie);
	}

	public ActionChannelStart(Script script, boolean attach, boolean useCookie, String name, CalculatedValue value, String[] options, CalculatedValue ...calculatedValues) {
		this(script, name, Arrays.asList(options), value, null);
		this.setArguments(new ArrayList<>(Arrays.asList(calculatedValues)));
		this.setAttach(attach);
		this.setUseCookie(useCookie);
	}

	//---------------------------------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------------------------------

	@Override
	public Element getXmlElement(Document document, ScriptHeader header, int index, String errorText) {
		Element e = super.getXmlElement(document, header, index, errorText);

		final Element dataJson = document.createElement(DATA_JSON);
		e.appendChild(dataJson);

		Element param = document.createElement("parameter");
		param.setAttribute("type", "name");
		param.setAttribute("value", getName());
		dataJson.appendChild(param);

		param = document.createElement("parameter");
		param.setAttribute("type", "appIcon");
		param.setAttribute("value", UNKNOWN_ICON);
		dataJson.appendChild(param);

		param = document.createElement("parameter");
		param.setAttribute("type", "app");
		param.setAttribute("value", application.getCalculated());
		dataJson.appendChild(param);

		Element channel = document.createElement("channel");
		channel.setAttribute("name", getName());

		e.appendChild(channel);

		return e;
	}

	//---------------------------------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------------------------------

	@Override
	public String execute(ActionTestScript ts, String testName, int line, int tryNum) {
		this.line = line;
		AtsSingleton.getInstance().startChannel(this, ts, testName, line);
		return null;
	}

	@Override
	public JsonObject getActionLogsData() {
		final Channel channel = status.getChannel();
		final JsonObject data = super.getActionLogsData();

		data.addProperty("app", application.getCalculated());
		data.addProperty("appVersion", channel.getApplicationVersion());

		if( arguments.size() != 0){
			ArrayList appParameters = new ArrayList<>();
			for(CalculatedValue dataCalculated : arguments) {
				appParameters.add(dataCalculated.getCalculated()) ;
			}
			data.addProperty("appParameters", String.join(", ", appParameters));
		}

		data.addProperty("os", channel.getOs());

		return data;
	}

	public JsonObject getActionData() {
		final Channel channel = status.getChannel();
		final JsonObject data = getActionLogsData();
		data.addProperty("appIcon", Base64.getEncoder().encodeToString(channel.getIcon()));

		return data;
	}

	@Override
	public ArrayList getKeywords() {
		ArrayList keywords = super.getKeywords();
		keywords.add(application.getKeywords());
		return keywords;
	}

	//---------------------------------------------------------------------------------------------------------------------------------
	// Code Generator
	//---------------------------------------------------------------------------------------------------------------------------------

	@Override
	public StringBuilder getJavaCode() {

		final StringBuilder codeBuilder =
				super.getJavaCode()
				.append(isAttach()).append(", ")
				.append(isUseCookie()).append(", ")
				.append("\"").append(getName()).append("\", ")
				.append(application.getJavaCode())
				.append(", new java.lang.String[]{");

		final ArrayList options = new ArrayList<>();
		if(performance == PERF) {
			options.add(String.format("\"%s\"", ActionPerformance.SCRIPT_PERFORMANCE_LABEL));
		}else if(performance == NEOLOAD) {
			options.add(String.format("\"%s\"", ActionNeoload.SCRIPT_NEOLOAD_LABEL));
		}

		if(authentication != null && authentication.length() > 0) {
			options.add(String.format("\"%s\"", authentication));
			options.add(String.format("\"%s\"", authenticationValue));
		}

		codeBuilder.append(String.join(", ", options)).append("}");

		if(arguments.size() > 0) {
			codeBuilder.append(", ");

			final StringJoiner argumentsJoiner = new StringJoiner(", ");
			for(CalculatedValue calc : arguments) {
				argumentsJoiner.add(calc.getJavaCode());
			}
			codeBuilder.append(argumentsJoiner.toString());
		}

		return codeBuilder.append(")");
	}

	//--------------------------------------------------------
	// getters and setters for serialization
	//--------------------------------------------------------

	public CalculatedValue getApplication() {
		return application;
	}

	public void setApplication(CalculatedValue value) {
		this.application = value;
	}

	public String getAuthentication() {
		return authentication;
	}

	public void setAuthentication(String value) {
		this.authentication = value;
	}

	public String getAuthenticationValue() {
		return authenticationValue;
	}

	public void setAuthenticationValue(String value) {
		this.authenticationValue = value;
	}

	public int getPerformance() {
		return performance;
	}

	public void setPerformance(int value) {
		this.performance = value;
	}

	public ArrayList getArguments() {
		return arguments;
	}

	public void setArguments(ArrayList arguments) {
		this.arguments = arguments;
	}

	public boolean isAttach() {
		return attach;
	}

	public void setAttach(boolean attach) {
		this.attach = attach;
	}

	public boolean isUseCookie() {
		return useCookie;
	}

	public void setUseCookie(boolean value) {
		this.useCookie = value;
	}

	public static StringBuilder getAtsCodeStr(String subFolder) {
		return new StringBuilder().append("callscript -> ").append(subFolder).append(".ChannelStart");
		// return new StringBuilder().append(SCRIPT_LABEL).append(" -> ");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy