summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java
blob: 3b2b72d87e99101887911c82a48bc1fc31761004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cz.crcs.ectester.reader.output;

import cz.crcs.ectester.reader.Util;
import cz.crcs.ectester.reader.command.Command;
import cz.crcs.ectester.reader.response.Response;
import cz.crcs.ectester.reader.test.Test;
import cz.crcs.ectester.reader.test.TestSuite;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class YAMLTestWriter implements TestWriter {
    private PrintStream output;
    private Map<String, Object> testRun;
    private Map<String, String> testSuite;
    private List<Object> tests;

    public YAMLTestWriter(PrintStream output) {
        this.output = output;
    }

    @Override
    public void begin(TestSuite suite) {
        output.println("---");
        testRun = new HashMap<>();
        testSuite = new HashMap<>();
        tests = new LinkedList<>();
        testSuite.put("name", suite.getName());
        testSuite.put("desc", suite.getDescription());

        testRun.put("suite", testSuite);
        testRun.put("tests", tests);
    }

    private Map<String, Object> commandObject(Command c) {
        Map<String, Object> commandObj = new HashMap<>();
        commandObj.put("apdu", Util.bytesToHex(c.getAPDU().getBytes()));
        return commandObj;
    }

    private Map<String, Object> responseObject(Response r) {
        Map<String, Object> responseObj = new HashMap<>();
        responseObj.put("successful", r.successful());
        responseObj.put("apdu", Util.bytesToHex(r.getAPDU().getBytes()));
        responseObj.put("natural_sw", Short.toUnsignedInt(r.getNaturalSW()));
        List<Integer> sws = new LinkedList<>();
        for (int i = 0; i < r.getNumSW(); ++i) {
            sws.add(Short.toUnsignedInt(r.getSW(i)));
        }
        responseObj.put("sws", sws);
        responseObj.put("duration", r.getDuration());
        responseObj.put("desc", r.getDescription());
        return responseObj;
    }

    private Map<String, Object> testObject(Test t) {
        Map<String, Object> testObj = new HashMap<>();

        if (t instanceof Test.Simple) {
            Test.Simple test = (Test.Simple) t;
            testObj.put("type", "simple");
            testObj.put("command", commandObject(test.getCommand()));
            testObj.put("response", responseObject(test.getResponse()));
        } else if (t instanceof Test.Compound) {
            Test.Compound test = (Test.Compound) t;
            testObj.put("type", "compound");
            List<Map<String, Object>> tests = new LinkedList<>();
            for (Test innerTest : test.getTests()) {
                tests.add(testObject(innerTest));
            }
            testObj.put("tests", tests);
        }

        testObj.put("desc", t.getDescription());
        Map<String, Object> result = new HashMap<>();
        result.put("ok", t.ok());
        result.put("value", t.getResultValue().name());
        result.put("cause", t.getResultCause());
        testObj.put("result", result);

        return testObj;
    }

    @Override
    public void outputTest(Test t) {
        if (!t.hasRun())
            return;
        tests.add(testObject(t));
    }

    @Override
    public void end() {
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        Yaml yaml = new Yaml(options);

        Map<String, Object> result = new HashMap<>();
        result.put("testRun", testRun);
        String out = yaml.dump(result);

        output.println(out);
        output.println("---");
    }
}