diff options
| author | J08nY | 2017-11-13 18:03:00 +0100 |
|---|---|---|
| committer | J08nY | 2017-11-13 18:03:00 +0100 |
| commit | cccf2c9c382fa63c68a6c3821d587bc2caa72b05 (patch) | |
| tree | c97b1ce7c3a81b0b2c23d6c36a2c22bef8056c2d /src/cz/crcs/ectester/reader/test/CommandTest.java | |
| parent | 9e615b101398bd4c8e2678bf86337e2756a8ee7a (diff) | |
| download | ECTester-cccf2c9c382fa63c68a6c3821d587bc2caa72b05.tar.gz ECTester-cccf2c9c382fa63c68a6c3821d587bc2caa72b05.tar.zst ECTester-cccf2c9c382fa63c68a6c3821d587bc2caa72b05.zip | |
Diffstat (limited to 'src/cz/crcs/ectester/reader/test/CommandTest.java')
| -rw-r--r-- | src/cz/crcs/ectester/reader/test/CommandTest.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/reader/test/CommandTest.java b/src/cz/crcs/ectester/reader/test/CommandTest.java new file mode 100644 index 0000000..b7728b6 --- /dev/null +++ b/src/cz/crcs/ectester/reader/test/CommandTest.java @@ -0,0 +1,71 @@ +package cz.crcs.ectester.reader.test; + +import cz.crcs.ectester.common.test.Result; +import cz.crcs.ectester.common.test.Test; +import cz.crcs.ectester.common.test.TestException; +import cz.crcs.ectester.reader.command.Command; +import cz.crcs.ectester.reader.response.Response; + +import javax.smartcardio.CardException; +import java.util.function.BiFunction; + +/** + * A simple test that runs one Command to get and evaluate one Response + * to get a Result and compare it with the expected one. + */ +public class CommandTest extends Test { + private BiFunction<Command, Response, Result> callback; + private Command command; + private Response response; + + public CommandTest(Command command, BiFunction<Command, Response, Result> callback) { + this.command = command; + this.callback = callback; + } + + public CommandTest(Command command, Result.ExpectedValue expected, String ok, String nok) { + this(command, (cmd, resp) -> { + Result.Value resultValue = Result.Value.fromExpected(expected, resp.successful(), resp.error()); + return new Result(resultValue, resultValue.ok() ? ok : nok); + }); + } + + public CommandTest(Command command, Result.ExpectedValue expected) { + this(command, expected, null, null); + } + + public Command getCommand() { + return command; + } + + public Response getResponse() { + return response; + } + + @Override + public void run() throws TestException { + if (hasRun) + return; + + try { + response = command.send(); + } catch (CardException e) { + throw new TestException(e); + } + if (callback != null) { + result = callback.apply(command, response); + } else { + if (response.successful()) { + result = new Result(Result.Value.SUCCESS); + } else { + result = new Result(Result.Value.FAILURE); + } + } + hasRun = true; + } + + @Override + public String getDescription() { + return response.getDescription(); + } +} |
