#!/usr/bin/env bash

test_description="Test config parsing"

. ./lib/sharness.sh
fake_install

test_expect_success "should default port and address if none are provided" "
  clear_config &&
  test_expect_stdout_matching 'It is available at http://localhost:7474/' run_daemon
"

test_expect_success "http: should read port and address from config" "
  clear_config &&
  set_config 'dbms.connector.http.address' '1.2.3.4:1234' ongdb.conf &&
  set_config 'dbms.connector.http.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at http://1.2.3.4:1234/' run_daemon
"

test_expect_success "http: should read only port from config" "
  clear_config &&
  set_config 'dbms.connector.http.address' ':1234' ongdb.conf &&
  set_config 'dbms.connector.http.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at http://localhost:1234/' run_daemon
"

test_expect_success "http: should fallback to default listening address if defined" "
  clear_config &&
  set_config 'dbms.connectors.default_listen_address' '100.200.300.400' ongdb.conf &&
  set_config 'dbms.connector.http.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at http://100.200.300.400:7474/' run_daemon
"

test_expect_success "http: should read port and address from config and prioritize non-deprecated" "
  clear_config &&
  set_config 'dbms.connector.http.address' '1.2.3.4:1234' ongdb.conf &&
  set_config 'dbms.connector.http.listen_address' 'a.b.c.d:333' ongdb.conf &&
  set_config 'dbms.connector.http.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at http://a.b.c.d:333/' run_daemon
"

test_expect_success "https: should display default https if http disabled and no other options" "
  clear_config &&
  set_config 'dbms.connector.http.enabled' 'false' ongdb.conf &&
  test_expect_stdout_matching 'It is available at https://localhost:7473/' run_daemon
"

test_expect_success "https: should read port and address from config" "
  clear_config &&
  set_config 'dbms.connector.http.enabled' 'false' ongdb.conf &&
  set_config 'dbms.connector.https.address' '1.2.3.4:1234' ongdb.conf &&
  set_config 'dbms.connector.https.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at https://1.2.3.4:1234/' run_daemon
"

test_expect_success "https: should read only port from config" "
  clear_config &&
  set_config 'dbms.connector.http.enabled' 'false' ongdb.conf &&
  set_config 'dbms.connector.https.address' ':1234' ongdb.conf &&
  set_config 'dbms.connector.https.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at https://localhost:1234/' run_daemon
"

test_expect_success "https: should fallback to default listening address if defined" "
  clear_config &&
  set_config 'dbms.connector.http.enabled' 'false' ongdb.conf &&
  set_config 'dbms.connectors.default_listen_address' '100.200.300.400' ongdb.conf &&
  set_config 'dbms.connector.https.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at https://100.200.300.400:7473/' run_daemon
"

test_expect_success "https: should read port and address from config and prioritize non-deprecated" "
  clear_config &&
  set_config 'dbms.connector.http.enabled' 'false' ongdb.conf &&
  set_config 'dbms.connector.https.address' '1.2.3.4:1234' ongdb.conf &&
  set_config 'dbms.connector.https.listen_address' 'a.b.c.d:333' ongdb.conf &&
  set_config 'dbms.connector.https.enabled' 'true' ongdb.conf &&
  test_expect_stdout_matching 'It is available at https://a.b.c.d:333/' run_daemon
"

test_expect_success "should write a specific message in HA mode" "
  clear_config &&
  set_config 'dbms.mode' 'HA' ongdb.conf &&
  test_expect_stdout_matching 'This HA instance will be operational once it has joined the cluster' run_daemon
"

test_expect_success "should respect log directory configuration" "
  clear_config &&
  mkdir -p '$(ongdb_home)/other-log-dir' &&
  set_config 'dbms.directories.logs' 'other-log-dir' ongdb.conf &&
  run_daemon &&
  test_expect_file_matching 'stdout from java' '$(ongdb_home)/other-log-dir/ongdb.log'
"

test_expect_success "can configure log directory outside ongdb-root" "
  clear_config &&
  mkdir -p other-log-dir &&
  set_config 'dbms.directories.logs' '$(pwd)/other-log-dir' ongdb.conf &&
  run_daemon &&
  test_expect_file_matching 'stdout from java' '$(pwd)/other-log-dir/ongdb.log'
"

test_expect_success "should write paths in use" "
  clear_config &&
  mkdir '$(ongdb_home)/loogs' &&
  mkdir '$(ongdb_home)/ruun' &&
  set_config 'dbms.directories.data' '/data/bob' ongdb.conf &&
  set_config 'dbms.directories.import' '/import/bob' ongdb.conf &&
  set_config 'dbms.directories.plugins' '/plugins/bob' ongdb.conf &&
  set_config 'dbms.directories.certificates' '/certs/bob' ongdb.conf &&
  set_config 'dbms.directories.run' 'ruun' ongdb.conf &&
  set_config 'dbms.directories.logs' 'loogs' ongdb.conf &&
  test_expect_stdout_matching '  home:         $(ongdb_home)' run_daemon &&
  test_expect_stdout_matching '  config:       $(ongdb_home)/conf' run_daemon &&
  test_expect_stdout_matching '  data:         /data/bob' run_daemon &&
  test_expect_stdout_matching '  import:       /import/bob' run_daemon &&
  test_expect_stdout_matching '  plugins:      /plugins/bob' run_daemon &&
  test_expect_stdout_matching '  certificates: /certs/bob' run_daemon &&
  test_expect_stdout_matching '  logs:         $(ongdb_home)/loogs' run_daemon &&
  test_expect_stdout_matching '  run:          $(ongdb_home)/ruun' run_daemon
"

test_expect_success "should write active database" "
  clear_config &&
  set_config 'dbms.active_database' 'bigdata' ongdb.conf &&
  test_expect_stdout_matching 'Active database: bigdata' run_daemon
"

test_done
