All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.bidib.wizard.common.script.node.StringGetAssertEqualsCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.node;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import org.apache.commons.collections4.CollectionUtils;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.DefaultScriptContext;
import org.bidib.wizard.common.script.ScriptExecutionException;
import org.bidib.wizard.common.script.ScriptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jgoodies.common.base.Objects;
/**
* The set string command.
*/
public class StringGetAssertEqualsCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(StringGetAssertEqualsCommand.class);
public static final String KEY = "stringGetAssertEquals";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(
" [--V:P] [--all] --namespace= --index= --expectedValue= \nstringGetAssertEquals [--V:P] [--all] --namespace= --index= --expectedValue=\"\"")
+ DIV_END + prepareHelpHtml("\nGet the string value and compare to expected value.");
private int[] vidPid;
private boolean processAllNodes;
private int namespace;
private int index;
private String expectedValue;
public StringGetAssertEqualsCommand(final ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
@Override
public void parse(String commandLine) {
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.info("Invalid command is scanned, key does not match.");
throw new IllegalArgumentException("Invalid command is scanned, key does not match.");
}
line = commandLine.trim();
String arguments = line.substring(KEY.length() + 1);
try {
vidPid = ScriptUtils.scanVidPid(scanner, true);
if (vidPid != null) {
LOGGER.info("Current vidPid: {}:{}", vidPid[0], vidPid[1]);
}
}
catch (IllegalArgumentException ex) {
LOGGER.info("Parse VID/PID failed.", ex);
throw new IllegalArgumentException("Parse VID/PID failed.\n" + getHelpHtml());
}
if (arguments.indexOf("--all") > -1) {
LOGGER.info("Process all nodes is activated.");
processAllNodes = true;
}
this.namespace = ScriptUtils.parseIntParam("--namespace=\\d+", arguments);
this.index = ScriptUtils.parseIntParam("--index=\\d+", arguments);
this.expectedValue = ScriptUtils.parseStringParam("(?<=--expectedValue=)(?s)(.*$)", arguments);
if (this.expectedValue.length() > 40) {
LOGGER.warn("Max length of expectedValue exceeded: {}", this.expectedValue.length());
throw new IllegalArgumentException(
"Max length of expectedValue exceeded: " + this.expectedValue.length());
}
}
LOGGER.info("Parsed command, namespace: {}", namespace);
}
public int getNamespace() {
return namespace;
}
public int getIndex() {
return index;
}
public String getExpectedValue() {
return this.expectedValue;
}
public int[] getVidPid() {
return vidPid;
}
public boolean isProcessAllNodes() {
return processAllNodes;
}
@Override
protected void internalExecute(final T scripting, final ApplicationContext context) {
LOGGER.info("Set the string, namespace: {}, index: {}, value: {}", namespace, index, expectedValue);
final List nodes = new LinkedList<>();
if (vidPid != null) {
List currentNodes = scripting.getNodesByVidAndPid(vidPid[0], vidPid[1]);
if (CollectionUtils.isNotEmpty(currentNodes)) {
if (processAllNodes) {
nodes.addAll(currentNodes);
}
else {
nodes.add(currentNodes.get(0));
}
}
else {
addError(context, "No matching node with provided VID/PID found.");
return;
}
}
else {
NodeInterface node = context.get(DefaultScriptContext.KEY_SELECTED_NODE, NodeInterface.class);
LOGGER.info("Fetched the registered selected node: {}", node);
nodes.add(node);
}
if (CollectionUtils.isEmpty(nodes)) {
LOGGER.warn("No node selected and registered in context. Skip set CV values.");
throw new ScriptExecutionException("No registered node in execution context.", this);
}
boolean errorDetected = false;
for (NodeInterface node : nodes) {
String stringValue = scripting.getString(node.getUniqueId(), namespace, index);
if (!Objects.equals(expectedValue, stringValue)) {
errorDetected = true;
addError(context, "The read string value does not match expected value, expectedValue: " + expectedValue
+ ", read: " + stringValue);
}
if (errorDetected) {
throw new IllegalStateException("The read string value does not match expected value!");
}
}
}
}