|
| 1 | += CardSimulationTestKit |
| 2 | + |
| 3 | +NOTE: This framework rather is meant to be used for gematik-internal development purposes (since the actual CardSimulation application can neither be open sourced nor provided as of now). We publish this code anyway for further reference/usage. |
| 4 | + |
| 5 | +CardSimulationTestKit provides an easy-to-use interface for testing against a German Health Card Simulator. |
| 6 | +It comes bundled with |
| 7 | + |
| 8 | +* `CardSimulationLoader`: loads and starts a card simulation as a Java process listening on a TCP port |
| 9 | +* `CardSimulationCardReaderProvider`: implements the `CardReaderProviderApi` interface |
| 10 | +* `CardSimulationTerminalTestCase`: provides a fully initialized and functional `HealthCard` object to send commands to and receive responses from. |
| 11 | +
|
| 12 | +
|
| 13 | +The intended usage of this project is to ease integration and use the G2-Kartensimulation with Swift projects, specifically the test cases in this project. |
| 14 | +This guide is separated in two (2) main parts. Describing |
| 15 | + |
| 16 | +. the usage of the `CardSimulation-Loader` framework (How-to) and |
| 17 | +. how to maintain the technical consideration(s) and implementation(s). |
| 18 | + |
| 19 | +== Frameworks |
| 20 | + |
| 21 | +=== CardSimulation-Loader |
| 22 | + |
| 23 | +The sole purpose of this framework is to launch and monitor a G2-Kartensimulation Java process. |
| 24 | +For detailed usage information see the inlined documentation on `SimulationManager` |
| 25 | +and `SimulationRunner`. |
| 26 | + |
| 27 | +*Starting the simulator*: |
| 28 | + |
| 29 | +Of course the best way to find out how-to use the CardSimulation-Loader is by checking the `SimulationManagerTest` and `SimulationRunnerTest` to see their intended and tested use-cases. |
| 30 | +Next to checking the test-cases you also find some (example) configuration files in the _Configuration.bundle_ file. |
| 31 | + |
| 32 | +In general, you would prepare such a *card-configuration* XML as in the Configuration.bundle and pass it to the `SimulationManager.shared` by invoking its: |
| 33 | + |
| 34 | +[source,Swift] |
| 35 | +---- |
| 36 | +func createSimulation( |
| 37 | + configFile: URL, |
| 38 | + preprocessor manipulators: [XMLPathManipulator] = [], |
| 39 | + simulatorVersion: String = "2.7.6-352", |
| 40 | + simulatorDirectory: String = "simulator", |
| 41 | +) |
| 42 | +---- |
| 43 | + |
| 44 | +Note: you can specify the G2-Kartensimulation version it needs to download/use. |
| 45 | + |
| 46 | +The returned `SimulationRunnerType` can be used to monitor the newly started G2-Kartensimulation instance. To - for instance - figure out on which TLV-port the simulator is registered, just check the `SimulationRunnerType.mode`. When `running` the TLV TCP/IP port is projected there. And for convenience reasons made available through `var tlvPort: Int?` on SimulationRunnerType(s). |
| 47 | + |
| 48 | +This SimulationRunnerType instance will need a CardTerminalControllerType to expose this G2-Kartensimulation virtual `HealthCard` to the HealthCardAccess/Control realm. |
| 49 | + |
| 50 | +*Example*: |
| 51 | + |
| 52 | +[source,Swift] |
| 53 | +---- |
| 54 | +/// Read configFile from included Resources Bundle |
| 55 | +let simulatorConfig = Bundle(for: MyClass.self) |
| 56 | + .resourceFilePath(in: "Configuration", for: "configuration_EGKG2_80276883110000017222_gema5_TCP.xml") |
| 57 | + .asURL |
| 58 | +/// Launch a G2-Kartensimulation with this configuration file |
| 59 | +let runner = try SimulationManager.shared.startSimulation( |
| 60 | + configFile: simulatorConfig, |
| 61 | + preprocessor: [ |
| 62 | + XMLPathManipulatorHolder.TLVPortManipulator(port: "0"), |
| 63 | + XMLPathManipulatorHolder.RelativeToAbsolutePathManipulator(with: XMLPathManipulatorHolder.CardConfigFileXMLPath, absolutePath: simulatorConfig.deletingLastPathComponent()), |
| 64 | + XMLPathManipulatorHolder.RelativeToAbsolutePathManipulator(with: XMLPathManipulatorHolder.ChannelConfigFileXMLPath, absolutePath: simulatorConfig.deletingLastPathComponent()) |
| 65 | + ], |
| 66 | + waitUntilLaunched: true |
| 67 | +) |
| 68 | +
|
| 69 | +// ... Do amazing things with the runner |
| 70 | +
|
| 71 | +/// Stop the runner when done |
| 72 | +runner.stop(waitUntilTerminated: true) |
| 73 | +---- |
| 74 | + |
| 75 | +==== Technical overview |
| 76 | + |
| 77 | +As described in the previous section(s) the CardSimulationLoader provides an easy-to-use API to launch and manage a G2-Kartensimulation. |
| 78 | +In order to achieve this we need to combine some various technologies/environments (read: Nexus <--> Java <--> Swift -> CardSimulationLoader API). |
| 79 | + |
| 80 | +The main components for this project to work: |
| 81 | + |
| 82 | +* Download G2-Kartensimulation Nexus artifacts |
| 83 | +* Launch and monitor Java Process |
| 84 | + |
| 85 | +These two (2) steps are taken care of when using the `SimulationManager` to launch a simulation. |
| 86 | + |
| 87 | +==== Maven step |
| 88 | + |
| 89 | +The `SimulationManager` reads the `pom.xml` and executes a shell script to run `mvn dependency:copy-dependencies`. |
| 90 | +And puts these artifacts in the same transient environment to be cleaned (manually) by calling `SimulationManager.clean` upon |
| 91 | +finishing with the simulator(s). Reason for this is to not download the artifacts for every simulator instance in case they |
| 92 | +are launch sequentially - which is reasonable to assume. |
| 93 | + |
| 94 | +==== Java process |
| 95 | + |
| 96 | +When the artifacts are in place, the `SimulationRunner` creates a JavaProcess that will be launched/forked in a separate process. |
| 97 | +And monitors this process by reading/parsing the `stdout` and `stderr` to detect the tlv-port number and successful initialization. |
| 98 | + |
| 99 | +To start developing the project follow the Project Setup section below 👇. |
| 100 | + |
| 101 | +=== CardSimulation-CardReaderProvider |
| 102 | + |
| 103 | +CardTerminalProvider for communication with G2-Kartensimulation |
| 104 | + |
| 105 | +=== CardSimulationTerminalTestCase |
| 106 | + |
| 107 | +CardSimulationTerminalTestCase provides a fully initialized and functional `HealthCard` object to send commands |
| 108 | +against and receive responses from. |
| 109 | + |
| 110 | +== Getting Started |
| 111 | + |
| 112 | +CardSimulationLoader requires Swift 5.1. |
| 113 | + |
| 114 | +=== Usage |
| 115 | + |
| 116 | +In your Test class, derive from the `CardSimulationTerminalTestCase` which itself is a `XCTestCase`. |
| 117 | +You then have a `HealthCard` and a `CardTerminal` object directly link to an up and running CardSimulation at your disposal. |
| 118 | + |
| 119 | +[source,Swift] |
| 120 | +---- |
| 121 | +final class SelectCommandIntegrationTest: CardSimulationTerminalTestCase { |
| 122 | + func testSelectRoot() { |
| 123 | + let healthCard = CardSimulationTerminalTestCase.healthCard |
| 124 | + HealthCardCommand.Select.selectRoot() |
| 125 | + .execute(on: healthCard) |
| 126 | + .run(on: Executor.trampoline) |
| 127 | + } |
| 128 | +} |
| 129 | +---- |
| 130 | + |
| 131 | +CardSimulationTestKit comes with various CardImage configuration files. |
| 132 | +You can choose between the following images |
| 133 | + |
| 134 | +* configuration_EGK_G2_1_80276883110000095711_GuD_TCP.xml (default) |
| 135 | +* configuration_EGK_G2_1_ecc.xml |
| 136 | +* configuration_EGKG2_80276883110000017222_gema5_TCP.xml |
| 137 | +* configuration_HBA_G2_1_80276883110000205690_gema5_TCP.xml |
| 138 | +* configuration_HBAG2_80276883110000017289_gema5_TCP.xml |
| 139 | +* configuration_TLK_COS_image-kontaktlos128.xml |
| 140 | + |
| 141 | +by overwriting the `class func configFile() -> URL?` like this: |
| 142 | + |
| 143 | +[source,Swift] |
| 144 | +---- |
| 145 | +final class SelectCommandIntegrationTest: CardSimulationTerminalTestCase { |
| 146 | + override class func configFile() -> URL? { |
| 147 | + let bundle = Bundle(for: CardSimulationTerminalTestCase.self) |
| 148 | + let path = bundle.resourceFilePath(in: "Resources", for: "Configuration/configuration_EGK_G2_1_ecc.xml") |
| 149 | + return path.asURL |
| 150 | + } |
| 151 | +} |
| 152 | +---- |
| 153 | +or bring your own image: |
| 154 | + |
| 155 | +[source,Swift] |
| 156 | +---- |
| 157 | +final class SelectCommandIntegrationTest: CardSimulationTerminalTestCase { |
| 158 | + override class func configFile() -> URL? { |
| 159 | + // this assumes, your use a test class and have a resource bundle called "Resources2.bundle" |
| 160 | + let bundle = Bundle(for: self) |
| 161 | + let path = bundle.testResourceFilePath(in: "Resources2", for: "Configuration/configuration_EGK_G2_1_ecc.xml") |
| 162 | + return path.asURL |
| 163 | + } |
| 164 | +} |
0 commit comments