aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/Test.java
blob: 651274da69ead5bd66fcb65a0be641eff49a37ae (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
package cz.crcs.ectester.reader;

import javax.smartcardio.CardException;
import java.util.function.BiFunction;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class Test {
    private boolean hasRun = false;
    private BiFunction<Command, Response, Result> callback;
    private Result result;
    private Result expected;
    private Command command;
    private Response response;

    public Test(Command command, Result expected) {
        this.command = command;
        this.expected = expected;
    }

    public Test(Command command, Result expected, BiFunction<Command, Response, Result> callback) {
        this(command, expected);
        this.callback = callback;
    }

    public Command getCommand() {
        return command;
    }

    public Response getResponse() {
        return response;
    }

    public Result getResult() {
        if (!hasRun) {
            return null;
        }
        return result;
    }

    public Result getExpected() {
        return expected;
    }

    public boolean ok() {
        return result == expected || expected == Result.ANY;
    }

    public void run() throws CardException {
        response = command.send();
        if (callback != null) {
            result = callback.apply(command, response);
        } else {
            if (response.successful()) {
                result = Result.SUCCESS;
            } else {
                result = Result.FAILURE;
            }
        }
        hasRun = true;
    }

    @Override
    public String toString() {
        if (hasRun) {
            return (ok() ? "OK " : "NOK") + " " + response.toString();
        } else {
            return "";
        }
    }

    public enum Result {
        SUCCESS,
        FAILURE,
        ANY
    }
}