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

com.github.siwenyan.si.SiUtils Maven / Gradle / Ivy

There is a newer version: 1.25.1.0
Show newest version
package com.github.siwenyan.si;

import com.github.siwenyan.si.model.SiModel;
import com.github.siwenyan.si.model.SiModelCommand;
import com.github.siwenyan.si.model.SiModelTest;

import java.util.*;

public class SiUtils {

    public static void extractVariablesByValues(SiModel siModel, Map varNamesByValues) {
        Set shellTests = new HashSet();

        for (SiModelTest siModelTest : siModel.getTests()) {
            Set valuesAppearedInTest = new HashSet();
            for (SiModelCommand siModelCommand : siModelTest.getCommands()) {
                String commandValue = siModelCommand.getValue();
                if (!commandValue.isEmpty()) {
                    for (String value : varNamesByValues.keySet()) {
                        if (commandValue.equals(value)) {
                            valuesAppearedInTest.add(value);
                            commandValue = commandValue.replace(value, "${" + varNamesByValues.get(value) + "}");
                            siModelCommand.setValue(commandValue);
                        }
                    }
                }
                String commandTarget = siModelCommand.getTarget();
                if (!commandTarget.isEmpty()) {
                    for (String value : varNamesByValues.keySet()) {
                        if (commandTarget.equals(value)) {
                            valuesAppearedInTest.add(value);
                            commandTarget = commandTarget.replace(value, "${" + varNamesByValues.get(value) + "}");
                            siModelCommand.setTarget(commandTarget);
                        }
                    }
                }
            }

            if (valuesAppearedInTest.size() > 0) {
                SiModelTest shellTest = new SiModelTest();
                shellTest.setName(siModelTest.getName() + "_shell");
                shellTest.setSuite(siModelTest.getSuite());
                for (String value : valuesAppearedInTest) {
                    String name = varNamesByValues.get(value);
                    SiModelCommand storeCommand = new SiModelCommand();
                    storeCommand.setCommand("store");
                    storeCommand.setTarget(value);
                    storeCommand.setValue(name);
                    shellTest.getCommands().add(storeCommand);
                }

                SiModelCommand runCommand = new SiModelCommand();
                runCommand.setCommand("run");
                runCommand.setTarget(siModelTest.getName());
                shellTest.getCommands().add(runCommand);

                shellTests.add(shellTest);
            }
        }

        for (SiModelTest shellTest : shellTests) {
            siModel.getTests().add(shellTest);
        }
    }

    public static Map namesByValues(List> pfs) {
        Map result = new HashMap();
        for (Map pf : pfs) {
            for (String name : pf.keySet()) {
                String value = pf.get(name);
                result.put(value, name);
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy