#!/bin/bash

### FINERACT INTEGRATION TEST RUNNER ###########

# see also: CI configs/scripts in .github/workflows/

### ASSUMPTIONS / PREREQUISITES ################
# - Bash compatible shell
# - GNU-compatible shell utilities (echo, date, etc.)
# - Zulu Java 21 JDK java/javac/javadoc set up
# - mariadb running and ready, set up with default username/password Fineract expects
#   - may also need config/docker/mysql/conf.d/server_collation.cnf
#   - example docker volume: config/docker/mysql/conf.d/server_collation.cnf:/etc/mysql/conf.d/server_collation.cnf:ro
# - current working directory is your Fineract clone (working copy)
# - Fineract server NOT already/separately running (cargo will do this)
#   - note Aleks's current work on testcontainers... fingers crossed for awesome future 🤞
# - for oauth2 tests (commented out, see below):
#   - mock oauth2 server running
#   - google chrome browser is installed
################################################

set -o errexit
set -o nounset
set -o pipefail

# Bit of magic from the github CI/Actions workflows.
# Does this need `export`? not sure if this actually has any effect.
# It might work as expected if TZ is already exported.
TZ=Asia/Kolkata

echo "🏗️ START at $(date)"

echo '🏗️ set up database'
./gradlew --quiet dropDB -PdbName=fineract_default || true
./gradlew --quiet dropDB -PdbName=fineract_tenants || true
./gradlew --quiet createDB -PdbName=fineract_default
./gradlew --quiet createDB -PdbName=fineract_tenants

# I think explicitly running the `build` task here is unnecessary.
# I think the following tasks end up doing everything this would have done.
#echo '🏗️ build'
#./gradlew build -x cucumber -x test -x doc

# FIXME: We might want to run the `clean` task here in case this script is re-run repeatedly (since we're probably not running in a short-lived VM).

echo '⚗️ cucumber tests (except e2e)'
# Can exclude some tasks to speed things up, e.g.: -x checkstyleJmh -x checkstyleMain -x checkstyleTest -x spotlessCheck -x spotlessApply -x spotbugsMain -x spotbugsTest -x javadoc -x javadocJar -x modernizer
# Or maybe gradle caching will do the smart/right thing here and not repeat them since no code should have changed since we ran the `build` task, above? Dunno how it works / is supposed to work, and too lazy to check right now. I left those out for the sake of clarity.
./gradlew cucumber -x :fineract-e2e-tests-runner:cucumber

echo '⚗️ integration tests'
# --rerun-tasks may not be necessary, especially if a `clean` is run (see above)?
# Without it the tests weren't actually running for me.
# These take a long time and I'd rather they fail fast, so I include --no-continue here.
./gradlew --rerun-tasks --no-continue test -x :twofactor-tests:test -x :oauth2-test:test -x :fineract-e2e-tests-runner:test

echo '⚗️ 2fa tests'
./gradlew :twofactor-tests:test

#echo '⚗️ oauth2 tests'
#./gradlew :oauth2-tests:test

echo "🏗️ DONE at $(date)"

# Recommended: now apply `aggregator.patch` and run `./gradlew --quiet testAggregateTestReport -x test`
