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

com.github.siwenyan.si.spy.SiSpy Maven / Gradle / Ivy

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

import com.github.siwenyan.common.Reporter;
import com.github.siwenyan.si.SiModelProvider;
import com.github.siwenyan.si.SiUtils;
import com.github.siwenyan.si.model.SiModel;
import com.github.siwenyan.si.model.SiModelCommand;
import com.github.siwenyan.si.model.SiModelTest;
import com.github.siwenyan.common.QuickJson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SiSpy {

    private SiModel recorder;
    private List> pfList;

    public SiSpy() {
        reset();
    }

    public void reset() {
        recorder = new SiModel();
        recorder.setName(Reporter.getTimestamp());
        recorder.getTests().add(new SiModelTest());
        recorder.getTests().get(0).setName("recorder");
        recorder.getTests().get(0).setCommands(new ArrayList());
        pfList = new ArrayList<>();
    }

    public void appendCommand(SiModelCommand siModelCommand) {
        recorder.getTests().get(0).getCommands().add(siModelCommand);
    }

    public void insertCommand(int index, SiModelCommand siModelCommand) {
        recorder.getTests().get(0).getCommands().add(index, siModelCommand);
    }

    public SiModel getRecordedModel() {
        return recorder;
    }

    public SiModelTest getRecordedModelTest() {
        return recorder.getTests().get(0);
    }

    @Override
    public String toString() {
        try {
            if (1 == recorder.getTests().size()) {
                Map nbv = SiUtils.namesByValues(pfList);
                SiUtils.extractVariablesByValues(recorder, nbv);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String s = SiModelProvider.toJsonString(recorder);
        s = QuickJson.prettyJson(s);
        return s;
    }

    public void addProfile(Map pf) {
        this.pfList.add(pf);
    }

    public String getProfileValue(String key) {
        for (Map pf : pfList) {
            if (pf.containsKey(key)) {
                return pf.get(key);
            }
        }
        return "";
    }

    public void clearProfiles(){
        this.pfList.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy