
com.exactpro.sf.aml.generator.OldImpl Maven / Gradle / Ivy
/******************************************************************************
* Copyright 2009-2024 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.aml.generator;
import static com.exactpro.sf.common.util.StringUtil.enclose;
import static com.exactpro.sf.common.util.StringUtil.toJavaString;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.exactpro.sf.aml.AMLAction;
import com.exactpro.sf.aml.AMLException;
import com.exactpro.sf.aml.AMLLangConst;
import com.exactpro.sf.aml.AMLLangUtil;
import com.exactpro.sf.aml.AMLTestCase;
import com.exactpro.sf.aml.generator.matrix.Column;
import com.exactpro.sf.aml.generator.matrix.JavaStatement;
import com.exactpro.sf.aml.generator.matrix.RefParameter;
import com.exactpro.sf.aml.generator.matrix.Value;
import com.exactpro.sf.aml.generator.matrix.Variable;
import com.exactpro.sf.aml.script.ActionContext;
import com.exactpro.sf.common.adapting.IAdapterManager;
import com.exactpro.sf.common.impl.messages.xml.configuration.JavaType;
import com.exactpro.sf.common.messages.IMessage;
import com.exactpro.sf.common.messages.MessageUtil;
import com.exactpro.sf.common.messages.structures.IFieldStructure;
import com.exactpro.sf.common.messages.structures.impl.FieldStructure;
import com.exactpro.sf.common.util.Pair;
import com.exactpro.sf.common.util.StringUtil;
import com.exactpro.sf.configuration.IDictionaryManager;
import com.exactpro.sf.configuration.suri.SailfishURI;
import com.exactpro.sf.configuration.suri.SailfishURIException;
import com.exactpro.sf.scriptrunner.actionmanager.ActionInfo;
import com.exactpro.sf.scriptrunner.actionmanager.IActionManager;
import com.exactpro.sf.scriptrunner.utilitymanager.IUtilityManager;
import com.exactpro.sf.services.IService;
public class OldImpl {
private static final Logger logger = LoggerFactory.getLogger(OldImpl.class);
protected static final String EOL = System.getProperty("line.separator");
public static final String TAB1 = "\t";
public static final String TAB2 = "\t\t";
public static final String TAB3 = "\t\t\t";
public static final String TAB4 = "\t\t\t\t";
private static final String MESSAGE_PREFIX = "m";
private static final String LIST_PREFIX = "l";
public static final String MAP_NAME = "messages";
public static final String CONTEXT_NAME = "context";
public static final String REPORT_NAME = "report";
public static final String STATIC_MAP_NAME = CONTEXT_NAME+".getStaticMap()";
public static final String SERVICE_MAP_NAME = CONTEXT_NAME+".getServiceNames()";
private static final String LOGGER_NAME = "logger";
private boolean continueOnFailed;
private List variables;
private final AlertCollector alertCollector;
private final IAdapterManager adapterManager;
private final IDictionaryManager dictionaryManager;
private final IActionManager actionManager;
private final IUtilityManager utilityManager;
private final CodeGenerator_new codeGenerator;
private static final int CAPACITY_4K = 4096;
private static final int CAPACITY_128K = 131072;
private final boolean allElementsReferenced;
public OldImpl(AlertCollector alertCollector, IAdapterManager adapterManager, IDictionaryManager dictionaryManager, IActionManager actionManager, IUtilityManager utilityManager, CodeGenerator_new codeGenerator, boolean allElementsReferenced) {
this.alertCollector = alertCollector;
this.adapterManager = adapterManager;
this.dictionaryManager = dictionaryManager;
this.actionManager = actionManager;
this.utilityManager = utilityManager;
this.codeGenerator = codeGenerator;
this.allElementsReferenced = allElementsReferenced;
}
protected void setContinueOnFailed(boolean b) {
this.continueOnFailed = b;
}
String writeFillMessage(AMLTestCase tc, AMLAction action, List variables2) throws AMLException {
this.variables = variables2;
if (action.getGenerateStatus() == AMLGenerateStatus.GENERATED) {
return null;
}
if (action.getGenerateStatus() == AMLGenerateStatus.GENERATING) {
alertCollector.add(new Alert(action.getLine(), action.getUID(), action.getReference(), "Recursion detected"));
return null;
}
action.setGenerateStatus(AMLGenerateStatus.GENERATING);
StringBuilder sb = new StringBuilder(CAPACITY_128K);
Variable inputVariable = null;
Variable outputVariable = null;
Class> type = null;
Value serviceName = action.getServiceName();
if (serviceName != null && !serviceName.isReference()) {
IService service = codeGenerator.resolveService(serviceName.getValue(), action.getLine(), action.getUID(), Column.ServiceName.getName());
if (service != null) {
codeGenerator.resolveDictionary(action, service);
}
}
if (action.hasActionURI()) {
openTryClause(sb);
}
if (action.getActionInfo() != null) {
type = action.getActionInfo().getMessageType();
if (type != null) {
inputVariable = getVariable(type, MESSAGE_PREFIX + type.getSimpleName());
sb.append(createMessageDefinition(tc, action, inputVariable));
addReferenceToFilter(sb, action, inputVariable);
}
Class> returnType = action.getActionInfo().getReturnType();
if (allElementsReferenced && action.hasReference() && void.class.equals(returnType)) {
alertCollector.add(new Alert(action.getLine(), action.getUID(), action.getReference(), "Cannot refer to void method: " + action.getActionURI() + ". "
+ "Please remove reference label and all reference occurrences to this row.", AlertType.WARNING));
} else if (!void.class.equals(returnType)) {
outputVariable = getVariable(returnType, MESSAGE_PREFIX + returnType.getSimpleName());
}
}
if (action.hasActionURI())
{
codeGenerator.writeMetaContainers(sb, tc, action, action.getActionInfo(), alertCollector, null);
JavaStatement statement = JavaStatement.value(action.getActionURI());
if (statement != null)
{
action.setGenerateStatus(AMLGenerateStatus.GENERATED);
return sb.toString();
}
Variable settings = getVariable(ActionContext.class, "actionContext");
String s = codeGenerator.createFillSettings(tc, action, null, settings, alertCollector);
sb.append(s);
sb.append(TAB2+LOGGER_NAME+".debug(\"start action: "+action.getActionURI()+", line:"+action.getLine()+"\");"+EOL);
writeActionCall(tc, action, inputVariable, outputVariable, settings, sb);
Variable variable = new Variable("containedMessage", Object.class);
sb.append(TAB2 + "} catch (KnownBugException e) {" + EOL);
sb.append(TAB3 + LOGGER_NAME + ".warn(e.getMessage(), e);" + EOL);
sb.append(TAB3 + variable.getType().getSimpleName() + " ");
sb.append(variable.getName() + " = null;" + EOL);
sb.append(TAB3 + "if(e instanceof MessageKnownBugException) {" + EOL);
sb.append(TAB4 + variable.getName() + " = ((MessageKnownBugException)e).getContainedMessage();" + EOL);
sb.append(TAB4 + CONTEXT_NAME + ".getReceivedMessages().add(");
sb.append(variable.getName());
sb.append(");" + EOL);
addReference(sb, action, inputVariable, variable, TAB4);
sb.append(TAB3 + "}" + EOL);
CodeGenerator_new.addExecutedActionReferences(sb, action, TAB3);
if(action.hasOutcome()) {
sb.append(TAB3 + CONTEXT_NAME + ".getOutcomeCollector().storeOutcome(settings.getOutcome());" + EOL);
}
NewImpl.writeCreateTestCase(tc, sb);
writeCreateAction(action, sb);
NewImpl.addActionToReport(action, sb, true, variable, TAB3);
closeTryClause(tc, action, sb, action.getContinueOnFailed() || continueOnFailed);
}
else
{
// add reference for submessage
addReference(sb, action, inputVariable, outputVariable, TAB2);
CodeGenerator_new.addExecutedActionReferences(sb, action, TAB2);
}
if (sb.length() != 0) {
sb.append(EOL);
}
action.setGenerateStatus(AMLGenerateStatus.GENERATED);
return sb.toString();
}
private Variable getVariable(Class> type, String varNameOrig)
{
String varName = varNameOrig;
Variable var = new Variable(varName, type);
// check if variable with same name and type already exists
if(variables.contains(var))
{
return var;
}
// check if variable with same name and different type already exists
int i=1;
boolean found = true;
while (found)
{
found = false;
for(Variable v : variables)
{
if (v.getName().equals(varName))
{
found = true;
varName = varNameOrig + i++;
break;
}
}
if (found == false)
{
var = new Variable(varName, type);
variables.add(var);
return var;
}
}
// should not happen
logger.error("This should not happen");
return null;
}
private String createMessageDefinition(AMLTestCase tc, AMLAction action, Variable variable)
{
List subMessages = new ArrayList<>();
StringBuilder sb = new StringBuilder(CAPACITY_128K);
sb.append(TAB2+variable.getName()+" = new "+variable.getType().getCanonicalName()+"();"+EOL);
for (Entry e : action.getParameters().entrySet())
{
String column = e.getKey();
Value v = e.getValue();
try {
if (AMLLangUtil.isSubmessage(v.getValue()))
{
expandSubmessage(tc, action, variable, column, v, sb, subMessages);
}
else if (AMLLangUtil.isArray(v.getValue()))
{
OldImplHelper.substituteReference(tc, action, alertCollector, column, v, codeGenerator.getDefinedReferences(), dictionaryManager, actionManager, utilityManager);
String[] values = AMLLangUtil.getValues(v.getValue());
for (String val : values)
{
expandSetter(action, sb, column, v, val, variable.getName());
}
}
else
{
OldImplHelper.substituteReference(tc, action, alertCollector, column, v, codeGenerator.getDefinedReferences(), dictionaryManager, actionManager, utilityManager);
expandSetter(action, sb, column, v, v.getValue(), variable.getName());
}
} catch (AMLException | SailfishURIException ex) {
alertCollector.add(new Alert(v.getLineNumber(), action.getUID(), action.getReference(), column, "Column '" + column + "': " + ex.getMessage()));
continue;
}
}
StringBuilder sb1 = new StringBuilder(CAPACITY_128K);
for (String subMessage : subMessages)
{
sb1.append(subMessage);
}
sb1.append(sb);
return sb1.toString();
}
private void expandSetter(AMLAction action, StringBuilder sb, String column, Value v, String val, String variable) throws AMLException {
Value tempValue = new Value(val, v.getLineNumber());
for (RefParameter p : v.getParameters()) {
tempValue.addParameter(p);
}
if (v.isReference()) {
val = NewImpl.generateFilter(tempValue.getLineNumber(), column, tempValue, TAB3);
} else if (NewImpl.isNotASimpleFilter(val)) {
IFieldStructure structure = new FieldStructure(column, null, null, null, null, null, JavaType.JAVA_LANG_STRING, false, false, false, null);
val = NewImpl.createFilterExpression(structure, tempValue.getOrigValue(), tempValue.getLineNumber(), action.getUID(), column, alertCollector);
} else if (!v.isJava()) {
val = enclose(val);
}
String setter = ".put(\"" + column + "\", " + val + ")";
if (v.isCheck())
{
String code = "new "+action.getActionInfo().getMessageType().getCanonicalName()+"()"+setter;
action.getSetters().add(new Pair<>(column, code));
}
sb.append(TAB2+variable+setter+";"+EOL);
}
private void expandSubmessage(AMLTestCase tc, AMLAction action, Variable variable, String column, Value v, StringBuilder sb, List subMessages)
throws AMLException
{
String[] references = AMLLangUtil.getReferences(v.getValue());
StringBuilder setter = new StringBuilder(CAPACITY_4K);
Variable subListVariable = getVariable(List.class, LIST_PREFIX + column);
for (String ref : references)
{
if (setter.length() == 0) {
setter.append(TAB2 + subListVariable.getName() +" = new ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy