withRealmImport method

KeycloakContainer withRealmImport(
  1. String hostPath
)

Imports the realm exported to the JSON file at hostPath when Keycloak starts (--import-realm); the container is ready when that realm answers, and realm is its name.

The file is read now, for the realm's name. A relative path starts at the working directory (the package, under dart test).

Implementation

KeycloakContainer withRealmImport(String hostPath) {
  final file = File(hostPath);
  if (!file.existsSync()) {
    throw ArgumentError.value(
      hostPath,
      'hostPath',
      'withRealmImport: no such file (a relative path starts at '
          '${Directory.current.path})',
    );
  }
  final name = (jsonDecode(file.readAsStringSync()) as Map)['realm'];
  if (name is! String || name.isEmpty) {
    throw ArgumentError.value(
      hostPath,
      'hostPath',
      'withRealmImport: the file has no "realm" name',
    );
  }
  _realm = name;
  withCopyFile(
    hostPath,
    '/opt/keycloak/data/import/${hostPath.split(RegExp(r'[/\\]')).last}',
  );
  withCommand(['start-dev', '--import-realm']);
  _waitForRealm();
  return this;
}