diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..32ef87d --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,40 @@ +name: Run Tests + +on: + pull_request: + branches: + - main + - dev + push: + branches: + - main + - dev + +jobs: + test: + runs-on: ubuntu-latest + + services: + docker: + image: docker:24.0.5-dind + options: --privileged + ports: + - 2375:2375 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 25 + + - name: Enable Docker + run: | + sudo systemctl start docker + docker version + + - name: Run tests + run: mvn clean test \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe76c68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,90 @@ +############################## +## Java +############################## +.mtj.tmp/ +*.class +*.jar +*.war +*.ear +*.nar +hs_err_pid* +replay_pid* + +############################## +## Maven +############################## +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +pom.xml.bak +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +############################## +## Gradle +############################## +bin/ +build/ +.gradle +.gradletasknamecache +gradle-app.setting +!gradle-wrapper.jar + +############################## +## IntelliJ +############################## +out/ +.idea/ +.idea_modules/ +*.iml +*.ipr +*.iws + +############################## +## Eclipse +############################## +.settings/ +bin/ +tmp/ +.metadata +.classpath +.project +*.tmp +*.bak +*.swp +*~.nib +local.properties +.loadpath +.factorypath + +############################## +## NetBeans +############################## +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +nbactions.xml +nb-configuration.xml + +############################## +## Visual Studio Code +############################## +.vscode/ +.code-workspace + +############################## +## OS X +############################## +.DS_Store + +############################## +## Miscellaneous +############################## +/logs \ No newline at end of file diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..c0bcafe --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,3 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e90a58f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# BUILD ========================= +FROM maven:3.9.11-eclipse-temurin-25-alpine AS build + +WORKDIR /app + +COPY pom.xml . + +COPY src ./src + +RUN mvn -q clean package -DskipTests + +# RUNTIME ======================= +FROM eclipse-temurin:25-jre-alpine + +WORKDIR /app + +COPY --from=build /app/target/*.jar app.jar + +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..05ad8d5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,53 @@ +services: + transaction: + build: . + depends_on: + - postgres + - kafka + environment: + SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/transaction + SPRING_DATASOURCE_USERNAME: marshmallow + SPRING_DATASOURCE_PASSWORD: tasty + SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:9092 + ports: + - "3000:3000" + + postgres: + image: postgres:17.7 + environment: + POSTGRES_DB: transaction + POSTGRES_USER: marshmallow + POSTGRES_PASSWORD: tasty + ports: + - "5432:5432" + + kafka: + image: bitnamilegacy/kafka:3.7 + container_name: kafka + ports: + - "9092:9092" + - "9094:9094" + environment: + KAFKA_ENABLE_KRAFT: "yes" # KRaft mode (Kafka manages metadata itself) + KAFKA_CFG_NODE_ID: 1 # node id + KAFKA_CFG_PROCESS_ROLES: broker,controller # node plays both roles (broker (handles topics/messages), controller (handles cluster metadata)) + KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093 # quorum voters config, controller is available at this URL + + # LISTENERS + # Kafka opens 3 sockets + # PLAINTEXT://:9092 - for other containers inside Docker (unencrypted) + # PLAINTEXT_HOST://:9094 - for the host machine to connect (unencrypted) + # CONTROLLER://:9093 - internal controller communication (KRaft metadata) + KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,PLAINTEXT_HOST://:9094,CONTROLLER://:9093 + KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:9094 # command to Kafka to advertise reachable addresses for use + KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT,CONTROLLER:PLAINTEXT # encoding protocols for the exposed ports + KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER # which listener is used for KRaft controller traffic + KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT # which listener brokers use to talk to each other + + # SINGLE NODE DEV + KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: "true" # auto-create unexisting topics + KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 # the internal topic where consumer group offsets are stored is normally replicated. For one broker it must be 1 + KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 # Kafka’s internal transaction state logs (used for idempotent/transactional). For one broker it must be 1 + KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR: 1 # Kafka’s internal transaction state logs (used for idempotent/transactional). For one broker it must be 1 + + ALLOW_PLAINTEXT_LISTENER: "yes" # bitnami images often require this explicit flag to allow plaintext listeners \ No newline at end of file diff --git a/mvnw b/mvnw new file mode 100755 index 0000000..bd8896b --- /dev/null +++ b/mvnw @@ -0,0 +1,295 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.4 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +scriptDir="$(dirname "$0")" +scriptName="$(basename "$0")" + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +actualDistributionDir="" + +# First try the expected directory name (for regular distributions) +if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then + if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then + actualDistributionDir="$distributionUrlNameMain" + fi +fi + +# If not found, search for any directory with the Maven executable (for snapshots) +if [ -z "$actualDistributionDir" ]; then + # enable globbing to iterate over items + set +f + for dir in "$TMP_DOWNLOAD_DIR"/*; do + if [ -d "$dir" ]; then + if [ -f "$dir/bin/$MVN_CMD" ]; then + actualDistributionDir="$(basename "$dir")" + break + fi + fi + done + set -f +fi + +if [ -z "$actualDistributionDir" ]; then + verbose "Contents of $TMP_DOWNLOAD_DIR:" + verbose "$(ls -la "$TMP_DOWNLOAD_DIR")" + die "Could not find Maven distribution directory in extracted archive" +fi + +verbose "Found extracted Maven distribution directory: $actualDistributionDir" +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..92450f9 --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,189 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.4 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' + +$MAVEN_M2_PATH = "$HOME/.m2" +if ($env:MAVEN_USER_HOME) { + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" +} + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..401ba08 --- /dev/null +++ b/pom.xml @@ -0,0 +1,106 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.5.8 + + + + com.neo + tx + 0.0.1-SNAPSHOT + tx + System for validating transactions + https://github.com/Hrothnar/transaction-processing-system + + + + + + + + + + + + + + + + + + 25 + + + + + org.postgresql + postgresql + + + org.liquibase + liquibase-core + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.8.14 + + + org.springframework.kafka + spring-kafka + + + org.springframework.boot + spring-boot-starter-test + test + + + org.testcontainers + testcontainers + test + + + org.testcontainers + postgresql + 1.21.3 + test + + + org.testcontainers + testcontainers-junit-jupiter + 2.0.2 + test + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..528e7a5 --- /dev/null +++ b/readme.md @@ -0,0 +1,224 @@ +# Transaction Validation System +A backend service for validating financial transactions with deterministic scoring, risk classification, and reliable event publishing. + +![Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white) ![Spring](https://img.shields.io/badge/spring-%236DB33F.svg?style=for-the-badge&logo=spring&logoColor=white) ![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white) ![Apache Maven](https://img.shields.io/badge/Apache%20Maven-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white) ![Apache Kafka](https://img.shields.io/badge/Apache%20Kafka-000?style=for-the-badge&logo=apachekafka) ![Swagger](https://img.shields.io/badge/-Swagger-%23Clojure?style=for-the-badge&logo=swagger&logoColor=white) ![Git](https://img.shields.io/badge/git-%23F05033.svg?style=for-the-badge&logo=git&logoColor=white) + +[Introduction](#introduction) | [Features](#features) | [Tech Stack](#tech-stack) | [Installation](#installation) | [Quick start](#quick-start) | [Usage](#usage) | [Known issues and limitations](#known-issues-and-limitations) | [Getting help](#getting-help) | [Contributing](#contributing) | [License](#license) + + +## Introduction +This service exposes a set of REST API endpoints, with the primary endpoint responsible for scoring financial transactions and returning a validation result that includes a risk level and a final decision. The API is stateless, uses PostgreSQL for persistence, and provides OpenAPI documentation for straightforward integration by upstream systems. + + +## Features +- Extensible rule-based scoring pipeline with initial validation rules, including amount thresholds, nighttime activity, high-risk countries, high-risk merchant categories, velocity checks, and country mismatch detection. +- Persistent storage of transactions, validation results, and outbox events for audit and history tracking. +- REST API for querying validation history with pagination and filtering. +- OpenAPI specification and Swagger UI for interactive API documentation. +- Outbox-based message publishing for validation completed events. + + +## Tech stack +- Java 25 +- Spring Boot 3.5.x + - Spring Web (Tomcat) + - Spring Data JPA (Hibernate, HikariCP) + - Jackson + - Jakarta Bean Validation +- Maven +- PostgreSQL 17.x +- Liquibase +- Kafka +- Testcontainers + + +## Installation + +### Prerequisites + +- **Option A — Container run:** Docker + Docker Compose +- **Option B — Local run:** Java 25, Maven (optional), PostgreSQL, Kafka +- **Option C — Hybrid run:** Java 25, Maven (optional), Docker + Docker Compose + +--- + +### Option A: Docker Compose (recommended) + +Runs the application together with all required infrastructure in an isolated environment, with the application, database, and message broker running in separate containers. + +```sh +docker compose up --build +``` + +### Option B: Running locally + +For this approach, PostgreSQL and Kafka must be running on the host machine, and the application connection parameters must be configured accordingly. + +After the infrastructure is ready, run the application locally: + +```sh +./mvnw spring-boot:run +``` +or, if Maven is installed globally: + +```sh +mvn spring-boot:run +``` + +### Option C: Hybrid run + +PostgreSQL and Kafka are started in containers, while the application itself runs locally and connects to the containerized infrastructure. + +Start PostgreSQL and Kafka using Docker Compose: + +```sh +docker compose up -d postgres kafka +``` + +Then run the application locally: + +```sh +./mvnw spring-boot:run +``` +or + +```sh +mvn spring-boot:run +``` + + +## Quick start + +Run the service and submit a transaction for validation: + +```sh +curl -X POST "http://localhost:3000/api/v1/transaction/validate" \ + -H "Content-Type: application/json" \ + -d '{ + "externalId": 1, + "userId": "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11", + "merchantId": "9a1d6f88-0f93-4e58-9dd7-1c9d4a7d6c22", + "deviceId": "android-13-pixel-7", + "amount": 12000.00, + "currency": "USD", + "initialized": 1735689600, + "merchantCategory": "GROCERIES", + "channel": "WEB", + "ipAddress": "203.0.113.10", + "country": "NGA", + "cardFingerprint": "fp_9d82kdk29d" + }' +``` + +Example response: + +```json +{ + "id": 843, + "transactionId": 1021, + "score": 90, + "riskLevel": "HIGH", + "decision": "BLOCK", + "validationResults": [ + { + "code": "VERY_HIGH_AMOUNT", + "description": "The transaction is above 9999 USD", + "scoreDelta": 60 + }, + { + "code": "HIGH_RISK_COUNTRY", + "description": "The transaction was made in high-risk country NGA", + "scoreDelta": 30 + } + ], + "createdAt": "2026-12-01T11:22:33Z", + "updatedAt": "2026-12-01T11:22:33Z" +} +``` + + +## Usage + +### Base URL + +All endpoints are served under the `/api` context path. + +### API endpoints + +- `POST /api/v1/transaction/validate` - validate a transaction and return score, risk level, decision, and triggered validators. +- `GET /api/v1/validation/{id}` - fetch a validation result by id. +- `GET /api/v1/validation` - list validation results with optional filters (`userId`, `riskLevel`, `decision`, `from`, `to`, `page`, `size`). +- `GET /api/v1/utility/health/liveness` - liveness probe. +- `GET /api/v1/utility/health/readiness` - readiness probe. + +OpenAPI and Swagger UI: + +- `GET /api/v1/openapi` - raw OpenAPI spec. +- `GET /api/swagger` - Swagger UI. + +### Scoring rules + +The default pipeline applies these validators and sums their score deltas: + +- High amount: amount > 4999 adds +40. +- Very high amount: amount > 9999 adds +60. +- Nighttime: 02:00-05:00 UTC adds +15. +- High-risk country: country code is in `RiskCountry` list (ISO alpha-3) adds +30. +- High-risk merchant: category in `GAMBLING`, `CRYPTO`, or `ADULT` adds +35. +- Velocity: more than 3 validations in the last 5 minutes adds +25. +- Country mismatch: user's recent history shows current country < 50% of last 30 days adds +20. + +### Risk level mapping + +Scores are mapped to risk levels and decisions using the configured thresholds: + +- `properties.risk.threshold.low` (default 30) => `LOW` / `ALLOW` +- `properties.risk.threshold.medium` (default 70) => `MEDIUM` / `REVIEW` +- Above the medium threshold => `HIGH` / `BLOCK` + +### Configuration + +Configuration lives under `src/main/resources/application*.yaml`. Key settings include: + +- Server: port `3000`, context path `/api`. +- Database: `spring.datasource.*` (overridden by Docker Compose env vars). +- Kafka: `spring.kafka.*` and `properties.kafka.*`. +- Scoring thresholds: `properties.risk.threshold.*`. + +To run with production settings: + +```sh +SPRING_PROFILES_ACTIVE=prod \ +DB_HOST=localhost \ +DB_PORT=5432 \ +DB_USER=postgres \ +DB_PASSWORD=postgres \ +./mvnw spring-boot:run +``` + +### Kafka outbox + +Each validation writes an outbox row and the scheduled publisher ships it to Kafka (topic `transaction.validation.completed`). The consumer is optional and only logs messages when `properties.kafka.consumer.enabled=true`. + + +## Known issues and limitations + +- No load or stress testing is included. +- Authentication and authorization are not implemented. +- Some rule thresholds are currently defined in code (e.g. amount limits, velocity window, country mismatch window). +- Kafka consumer logic is intentionally minimal and intended for demonstration purposes only. + + +## Getting help + +If you encounter a bug or have a question about the project, please use the repository issue tracker or reach me out directly. + + +## Contributing + +Pull requests are welcome. For substantial changes, please open an issue first to discuss the proposed approach. + + +## License + +This project is licensed under the MIT License. See the `LICENSE` file for details. \ No newline at end of file diff --git a/src/main/java/com/neo/tx/Service.java b/src/main/java/com/neo/tx/Service.java new file mode 100644 index 0000000..6cc2380 --- /dev/null +++ b/src/main/java/com/neo/tx/Service.java @@ -0,0 +1,12 @@ +package com.neo.tx; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Service { + + static void main(String[] args) { + SpringApplication.run(Service.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/config/JpaConfig.java b/src/main/java/com/neo/tx/config/JpaConfig.java new file mode 100644 index 0000000..ef3818f --- /dev/null +++ b/src/main/java/com/neo/tx/config/JpaConfig.java @@ -0,0 +1,20 @@ +package com.neo.tx.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.domain.AuditorAware; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +import java.util.Optional; + +@Configuration +@EnableJpaAuditing +public class JpaConfig { + + @Bean + public AuditorAware auditorProvider() { // called upon @CreatedBy and @LastModifiedBy + // a user's name could be retrieved through Spring Security + // return () -> Optional.of(SecurityContextHolder.getContext().getAuthentication().getName()); // TODO uncomment in case of including String Security + return () -> Optional.of("system"); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/config/OpenApiConfig.java b/src/main/java/com/neo/tx/config/OpenApiConfig.java new file mode 100644 index 0000000..65567f0 --- /dev/null +++ b/src/main/java/com/neo/tx/config/OpenApiConfig.java @@ -0,0 +1,31 @@ +package com.neo.tx.config; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.servers.Server; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@Configuration +public class OpenApiConfig { + + @Bean + public OpenAPI transactionOpenAPI() { + return new OpenAPI() + .info(new Info() + .title("Transaction Validation API") + .version("v1") + .description("System for validating financial transactions") + .contact(new Contact() + .name("Kirill Tatarenko") + .email("juniper.infusion@gmail.com")) + .license(new License() + .name("MIT") + .url("https://mit-license.org/"))) + .servers(List.of(new Server().url("/api/").description("Default server"))); + } +} diff --git a/src/main/java/com/neo/tx/config/Properties.java b/src/main/java/com/neo/tx/config/Properties.java new file mode 100644 index 0000000..b97d761 --- /dev/null +++ b/src/main/java/com/neo/tx/config/Properties.java @@ -0,0 +1,31 @@ +package com.neo.tx.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class Properties { + @Value("${properties.score.min}") + public int scoreMin; + + @Value("${properties.score.max}") + public int scoreMax; + + @Value("${properties.risk.threshold.low}") + public int riskThresholdLow; + + @Value("${properties.risk.threshold.medium}") + public int riskThresholdMedium; + + @Value("${properties.risk.threshold.high}") + public int riskThresholdHigh; + + @Value("${properties.kafka.producer.enabled}") + public boolean kafkaProducerEnabled; + + @Value("${properties.kafka.consumer.enabled}") + public boolean kafkaConsumerEnabled; + + @Value("${properties.kafka.consumer.validation.topic}") + public String kafkaConsumerValidationTopic; +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/config/SchedulingConfig.java b/src/main/java/com/neo/tx/config/SchedulingConfig.java new file mode 100644 index 0000000..4d33e1e --- /dev/null +++ b/src/main/java/com/neo/tx/config/SchedulingConfig.java @@ -0,0 +1,10 @@ +package com.neo.tx.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +public class SchedulingConfig { + +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/controller/TransactionController.java b/src/main/java/com/neo/tx/controller/TransactionController.java new file mode 100644 index 0000000..b23a83e --- /dev/null +++ b/src/main/java/com/neo/tx/controller/TransactionController.java @@ -0,0 +1,45 @@ +package com.neo.tx.controller; + +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.service.TransactionService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@Validated +@RestController +@RequestMapping("/v1/transaction") +@Tag(name = "Transaction", description = "Endpoints for interacting with 'Transactions'") +public class TransactionController { + + private final TransactionService validationService; + + public TransactionController(TransactionService validationService) { + this.validationService = validationService; + } + + @Operation(summary = "Validate a transaction", description = "Evaluates a transaction using the configured validation pipeline and returns score, risk level, decision, and triggered validators") + @ApiResponses({ + @ApiResponse(responseCode = "201", description = "Transaction validated successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ValidationResponseDto.class))), + @ApiResponse(responseCode = "400", description = "Validation error (invalid request body)", content = @Content(mediaType = "application/json")), + @ApiResponse(responseCode = "500", description = "Unexpected server error", content = @Content(mediaType = "application/json")) + }) + @PostMapping("/validate") + public ResponseEntity validateTransaction( + @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "Transaction data to be evaluated", content = @Content(schema = @Schema(implementation = TransactionRequestDto.class))) @Valid @RequestBody TransactionRequestDto transaction, + @Parameter(description = "Optional pipeline name", example = "default") @RequestParam(name = "pipeline", required = false, defaultValue = "default") String pipeline + ) { + ValidationResponseDto response = validationService.validateTransaction(transaction, pipeline); + return ResponseEntity.status(HttpStatus.CREATED).body(response); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/controller/UtilityController.java b/src/main/java/com/neo/tx/controller/UtilityController.java new file mode 100644 index 0000000..764a14c --- /dev/null +++ b/src/main/java/com/neo/tx/controller/UtilityController.java @@ -0,0 +1,31 @@ +package com.neo.tx.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Validated +@RestController +@RequestMapping("/v1/utility") +@Tag(name = "Utility", description = "Utility endpoints, for health, metrics, etc") +public class UtilityController { + + @Operation(summary = "Liveness check") + @ApiResponse(responseCode = "200", description = "Whether the service is up") + @GetMapping("/health/liveness") + public ResponseEntity probLiveness() { + return ResponseEntity.ok("{\"isItCool\":\"true\"}"); + } + + @Operation(summary = "Readiness check") + @ApiResponse(responseCode = "200", description = "Whether the service is up and ready for processing requests") + @GetMapping("/health/readiness") + public ResponseEntity probReadiness() { + return ResponseEntity.ok("{\"isItCool\":\"true\"}"); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/controller/ValidationController.java b/src/main/java/com/neo/tx/controller/ValidationController.java new file mode 100644 index 0000000..c2b3af3 --- /dev/null +++ b/src/main/java/com/neo/tx/controller/ValidationController.java @@ -0,0 +1,69 @@ +package com.neo.tx.controller; + +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.service.ValidationService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import org.springframework.data.domain.Page; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.time.Instant; +import java.util.UUID; + +@Validated +@RestController +@RequestMapping("/v1/validation") +@Tag(name = "Validation", description = "Endpoints for interacting with 'Validations'") +public class ValidationController { + + private final ValidationService validationService; + + public ValidationController(ValidationService validationService) { + this.validationService = validationService; + } + + @Operation(summary = "Get a transaction validation result by id") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Result found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ValidationResponseDto.class))), + @ApiResponse(responseCode = "404", description = "Result not found", content = @Content(mediaType = "application/json")) + }) + @GetMapping("/{id}") + public ResponseEntity getValidation( + @Parameter(description = "Validation id", example = "843") + @PathVariable("id") long id + ) { + ValidationResponseDto validationResponseDto = validationService.find(id); + return ResponseEntity.ok(validationResponseDto); + } + + @Operation(summary = "Search transaction validation results", description = "Returns paginated validation results filtered by optional criteria. All parameters are optional. If no filters are provided, all results are returned") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Results found", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Page.class))), // TODO make a dedicated page view? + @ApiResponse(responseCode = "400", description = "Invalid query parameters", content = @Content(mediaType = "application/json")) + }) + @GetMapping + public ResponseEntity> find( + @Parameter(description = "Filter by user ID", example = "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11") @RequestParam(required = false) UUID userId, + @Parameter(description = "Filter by risk level", schema = @Schema(implementation = RiskLevel.class)) @RequestParam(required = false) RiskLevel riskLevel, + @Parameter(description = "Filter by final decision", schema = @Schema(implementation = Decision.class)) @RequestParam(required = false) Decision decision, + @Parameter(description = "Transaction initialization from time (inclusive)", example = "2025-01-01T00:00:00Z") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant from, + @Parameter(description = "Transaction initialization to time (inclusive)", example = "2025-12-31T23:59:59Z") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Instant to, + @Parameter(description = "Page number (starting from 0)", example = "0") @RequestParam(defaultValue = "0") @Min(0) int page, + @Parameter(description = "Page size (max 200)", example = "20") @RequestParam(defaultValue = "20") @Min(1) @Max(200) int size + ) { + Page result = validationService.find(userId, riskLevel, decision, from, to, page, size); + return ResponseEntity.ok(result); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/dto/TransactionRequestDto.java b/src/main/java/com/neo/tx/dto/TransactionRequestDto.java new file mode 100644 index 0000000..a41404c --- /dev/null +++ b/src/main/java/com/neo/tx/dto/TransactionRequestDto.java @@ -0,0 +1,80 @@ +package com.neo.tx.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.*; + +import java.math.BigDecimal; +import java.util.UUID; + +public class TransactionRequestDto { + + @Schema(description = "External transaction identifier", example = "433") + @NotNull + public Long externalId; + + @Schema(description = "User identifier", example = "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11") + @NotNull + public UUID userId; + + @Schema(description = "Merchant identifier", example = "9a1d6f88-0f93-4e58-9dd7-1c9d4a7d6c22") + @NotNull + public UUID merchantId; + + @Schema(description = "Optional device identifier", example = "android-13-pixel-7") + @Size(max = 64) + public String deviceId; + + @Schema(description = "Transaction amount", example = "125.50") + @NotNull + @Positive + public BigDecimal amount; + + @Schema(description = "ISO 4217 currency code", example = "USD") + @NotBlank + @Pattern(regexp = "^[A-Z]{3}$") + public String currency; + + @Schema(description = "Transaction initialization time (Unix timestamp, seconds)", example = "1735689600") + @NotNull + public Long initialized; + + @Schema(description = "Merchant category code", example = "GROCERIES") + @NotBlank + public String merchantCategory; + + @Schema(description = "Transaction channel", example = "WEB") + @NotBlank + public String channel; + + @Schema(description = "Client IPv4 address", example = "192.168.1.10") + @NotBlank + @Pattern(regexp = "(\\b25[0-5]|\\b2[0-4][0-9]|\\b[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}") + public String ipAddress; + + @Schema(description = "ISO 3166-1 alpha-3 country code", example = "USA") + @NotBlank + @Pattern(regexp = "^[A-Z]{3}$") + public String country; + + @Schema(description = "Optional card fingerprint", example = "fp_9d82kdk29d") + @Size(max = 64) + public String cardFingerprint; + + @Override + public String toString() { + return "TransactionRequestDto{" + + "externalId=" + externalId + + ", userId=" + userId + + ", merchantId=" + merchantId + + ", deviceId='" + deviceId + '\'' + + ", amount=" + amount + + ", currency='" + currency + '\'' + + ", initialized=" + initialized + + ", merchantCategory='" + merchantCategory + '\'' + + ", channel='" + channel + '\'' + + ", ipAddress='" + ipAddress + '\'' + + ", country='" + country + '\'' + + ", cardFingerprint='" + cardFingerprint + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/dto/ValidationResponseDto.java b/src/main/java/com/neo/tx/dto/ValidationResponseDto.java new file mode 100644 index 0000000..878eca4 --- /dev/null +++ b/src/main/java/com/neo/tx/dto/ValidationResponseDto.java @@ -0,0 +1,43 @@ +package com.neo.tx.dto; + +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; + +import java.time.Instant; +import java.util.List; + +public class ValidationResponseDto { + public long id; + public long transactionId; + public int score; + public RiskLevel riskLevel; + public Decision decision; + public List validationResults; + public Instant createdAt; + public Instant updatedAt; + + public ValidationResponseDto() { + + } + + public ValidationResponseDto(int score, RiskLevel riskLevel, Decision decision, List validationResults) { + this.score = score; + this.riskLevel = riskLevel; + this.decision = decision; + this.validationResults = validationResults; + } + + @Override + public String toString() { + return "ValidationResponseDto{" + + "id=" + id + + ", transactionId=" + transactionId + + ", score=" + score + + ", riskLevel=" + riskLevel + + ", decision=" + decision + + ", validationResults=" + validationResults + + ", createdAt=" + createdAt + + ", updatedAt=" + updatedAt + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/dto/ValidationResultDto.java b/src/main/java/com/neo/tx/dto/ValidationResultDto.java new file mode 100644 index 0000000..7b3b4b9 --- /dev/null +++ b/src/main/java/com/neo/tx/dto/ValidationResultDto.java @@ -0,0 +1,29 @@ +package com.neo.tx.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class ValidationResultDto { + public String code; + public String description; + public int scoreDelta; + + public ValidationResultDto() { + + } + + public ValidationResultDto(String code, String description, int scoreDelta) { + this.code = code; + this.description = description; + this.scoreDelta = scoreDelta; + } + + @Override + public String toString() { + return "ValidationResultDto{" + + "code='" + code + '\'' + + ", description='" + description + '\'' + + ", scoreDelta=" + scoreDelta + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/enums/Decision.java b/src/main/java/com/neo/tx/enums/Decision.java new file mode 100644 index 0000000..8e01339 --- /dev/null +++ b/src/main/java/com/neo/tx/enums/Decision.java @@ -0,0 +1,5 @@ +package com.neo.tx.enums; + +public enum Decision { + ALLOW, REVIEW, BLOCK +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/enums/RiskCountry.java b/src/main/java/com/neo/tx/enums/RiskCountry.java new file mode 100644 index 0000000..4220ea8 --- /dev/null +++ b/src/main/java/com/neo/tx/enums/RiskCountry.java @@ -0,0 +1,53 @@ +package com.neo.tx.enums; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public enum RiskCountry { + + BURKINA_FASO("BFA"), + LAOS("LAO"), + MALI("MLI"), + NIGER("NER"), + SIERRA_LEONE("SLE"), + SOUTH_SUDAN("SSD"), + SYRIA("SYR"), + ANGOLA("AGO"), + ANTIGUA_AND_BARBUDA("ATG"), + BENIN("BEN"), + COTE_D_IVOIRE("CIV"), + DOMINICA("DMA"), + GABON("GAB"), + THE_GAMBIA("GMB"), + MALAWI("MWI"), + MAURITANIA("MRT"), + NIGERIA("NGA"), + SENEGAL("SEN"), + TANZANIA("TZA"), + TONGA("TON"), + TURKMENISTAN("TKM"), + ZAMBIA("ZMB"), + ZIMBABWE("ZWE"); + + private static final Map ALPHA3_MAP = + Arrays.stream(values()) + .collect(Collectors.toUnmodifiableMap( + RiskCountry::getAlpha3, + Function.identity() + )); + private final String alpha3; + + RiskCountry(String alpha3) { + this.alpha3 = alpha3; + } + + public static boolean contains(String code) { + return ALPHA3_MAP.containsKey(code); + } + + public String getAlpha3() { + return alpha3; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/enums/RiskLevel.java b/src/main/java/com/neo/tx/enums/RiskLevel.java new file mode 100644 index 0000000..923fda1 --- /dev/null +++ b/src/main/java/com/neo/tx/enums/RiskLevel.java @@ -0,0 +1,5 @@ +package com.neo.tx.enums; + +public enum RiskLevel { + LOW, MEDIUM, HIGH +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/enums/RiskMerchant.java b/src/main/java/com/neo/tx/enums/RiskMerchant.java new file mode 100644 index 0000000..ffd91c7 --- /dev/null +++ b/src/main/java/com/neo/tx/enums/RiskMerchant.java @@ -0,0 +1,19 @@ +package com.neo.tx.enums; + +import java.util.Arrays; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; + +public enum RiskMerchant { + GAMBLING, CRYPTO, ADULT; + + private static final Set VALUES = + Arrays.stream(values()) + .map(Enum::name) + .collect(Collectors.toUnmodifiableSet()); + + public static boolean contains(String value) { + return value != null && VALUES.contains(value.toUpperCase(Locale.ROOT)); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/exception/ApiException.java b/src/main/java/com/neo/tx/exception/ApiException.java new file mode 100644 index 0000000..e5beabb --- /dev/null +++ b/src/main/java/com/neo/tx/exception/ApiException.java @@ -0,0 +1,36 @@ +package com.neo.tx.exception; + +import org.springframework.http.HttpStatus; + +import java.util.Map; + +public class ApiException extends RuntimeException { + private final HttpStatus status; + private final String code; + private final String title; + private final Map meta; + + public ApiException(HttpStatus status, String code, String title, String message, Map meta) { + super(message); + this.status = status; + this.code = code; + this.title = title; + this.meta = meta; + } + + public HttpStatus getStatus() { + return status; + } + + public String getCode() { + return code; + } + + public String getTitle() { + return title; + } + + public Map getMeta() { + return meta; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/exception/FallbackExceptionHandler.java b/src/main/java/com/neo/tx/exception/FallbackExceptionHandler.java new file mode 100644 index 0000000..b1c219e --- /dev/null +++ b/src/main/java/com/neo/tx/exception/FallbackExceptionHandler.java @@ -0,0 +1,26 @@ +package com.neo.tx.exception; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.ProblemDetail; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.net.URI; + +@Order(Ordered.LOWEST_PRECEDENCE) +@RestControllerAdvice +public class FallbackExceptionHandler { + + @ExceptionHandler(Exception.class) + public ResponseEntity handleUnknown(Exception exception, HttpServletRequest request) { + ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR); + pd.setTitle("Internal server error"); + pd.setDetail("Unexpected error"); + pd.setInstance(URI.create(request.getRequestURI())); + return ResponseEntity.status(500).body(pd); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/exception/GlobalExceptionHandler.java b/src/main/java/com/neo/tx/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..be074c3 --- /dev/null +++ b/src/main/java/com/neo/tx/exception/GlobalExceptionHandler.java @@ -0,0 +1,96 @@ +package com.neo.tx.exception; + +import jakarta.servlet.http.HttpServletRequest; +import org.hibernate.exception.ConstraintViolationException; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ProblemDetail; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Order(Ordered.HIGHEST_PRECEDENCE) +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(ApiException.class) + public ResponseEntity handleApiException(ApiException exception, HttpServletRequest request) { + ProblemDetail pd = ProblemDetail.forStatus(exception.getStatus()); + pd.setTitle(exception.getTitle()); + pd.setDetail(exception.getMessage()); + pd.setInstance(URI.create(request.getRequestURI())); + pd.setProperty("code", exception.getCode()); + pd.setProperty("meta", exception.getMeta()); + return ResponseEntity.status(exception.getStatus()).body(pd); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidation(MethodArgumentNotValidException exception, HttpServletRequest request) { + ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); + List> errors = exception.getBindingResult().getFieldErrors().stream().map(this::fieldErrorToMap).toList(); + pd.setTitle("Validation failed"); + pd.setDetail("Request body has invalid fields"); + pd.setInstance(URI.create(request.getRequestURI())); + pd.setProperty("errors", errors); + return ResponseEntity.badRequest().body(pd); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleNotReadable(HttpMessageNotReadableException exception, HttpServletRequest request) { + ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); + pd.setTitle("Malformed JSON"); + pd.setDetail(Optional.ofNullable(exception.getMostSpecificCause()).map(Throwable::getMessage).orElse(exception.getMessage())); + pd.setInstance(URI.create(request.getRequestURI())); + return ResponseEntity.badRequest().body(pd); + } + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleDataIntegrity(DataIntegrityViolationException exception, HttpServletRequest request) { + String constraint = extractConstraintName(exception); + HttpStatus status = HttpStatus.CONFLICT; + ProblemDetail pd = ProblemDetail.forStatus(status); + pd.setTitle("Data integrity violation"); + pd.setDetail("Request conflicts with existing data"); + pd.setInstance(URI.create(request.getRequestURI())); + pd.setProperty("code", "db.constraint_violation"); + + if (constraint != null) { + pd.setProperty("constraint", constraint); + } + + return ResponseEntity.status(status).body(pd); + } + + private String extractConstraintName(Throwable throwable) { + Throwable current = throwable; + while (current != null) { + + if (current instanceof ConstraintViolationException exception) { + return exception.getConstraintName(); + } + + current = current.getCause(); + } + + return null; + } + + private Map fieldErrorToMap(FieldError error) { + Map map = new LinkedHashMap<>(); + map.put("field", error.getField()); + map.put("message", error.getDefaultMessage()); + map.put("rejectedValue", error.getRejectedValue()); + return map; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/exception/NotFoundException.java b/src/main/java/com/neo/tx/exception/NotFoundException.java new file mode 100644 index 0000000..36e87c9 --- /dev/null +++ b/src/main/java/com/neo/tx/exception/NotFoundException.java @@ -0,0 +1,18 @@ +package com.neo.tx.exception; + +import org.springframework.http.HttpStatus; + +import java.util.Map; + +public class NotFoundException extends ApiException { + + public NotFoundException(String entity, String id) { + super( + HttpStatus.NOT_FOUND, + entity.toLowerCase() + "_not_found", + entity + " not found", + entity + " with id [" + id + "] was not found", + Map.of() + ); + } +} diff --git a/src/main/java/com/neo/tx/mapper/Mapper.java b/src/main/java/com/neo/tx/mapper/Mapper.java new file mode 100644 index 0000000..c90f89d --- /dev/null +++ b/src/main/java/com/neo/tx/mapper/Mapper.java @@ -0,0 +1,64 @@ +package com.neo.tx.mapper; + +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import org.springframework.stereotype.Component; + +@Component +public class Mapper { + + public static Transaction toEntity(TransactionRequestDto dto) { + Transaction entity = new Transaction(); + entity.setAmount(dto.amount); + entity.setCardFingerprint(dto.cardFingerprint); + entity.setChannel(dto.channel); + entity.setCountry(dto.country); + entity.setCurrency(dto.currency); + entity.setDeviceId(dto.deviceId); + entity.setExternalId(dto.externalId); + entity.setInitialized(dto.initialized); + entity.setIpAddress(dto.ipAddress); + entity.setMerchantCategory(dto.merchantCategory); + entity.setMerchantId(dto.merchantId); + entity.setUserId(dto.userId); + return entity; + } + + public static Validation toEntity(ValidationResponseDto dto, Transaction transaction) { + Validation entity = new Validation(); + entity.setTransaction(transaction); + entity.setDecision(dto.decision); + entity.setScore(dto.score); + entity.setRiskLevel(dto.riskLevel); + entity.setTriggeredValidators(dto.validationResults); + return entity; + } + + public static ValidationResponseDto toDto(Validation entity, long transactionId) { + ValidationResponseDto dto = new ValidationResponseDto(); + dto.id = entity.getId(); + dto.transactionId = transactionId; + dto.score = entity.getScore(); + dto.riskLevel = entity.getRiskLevel(); + dto.decision = entity.getDecision(); + dto.validationResults = entity.getTriggeredValidators(); + dto.createdAt = entity.getCreatedAt(); + dto.updatedAt = entity.getUpdatedAt(); + return dto; + } + + public static ValidationResponseDto toDto(Validation entity) { + ValidationResponseDto dto = new ValidationResponseDto(); + dto.id = entity.getId(); + dto.transactionId = entity.getTransaction().getId(); + dto.score = entity.getScore(); + dto.riskLevel = entity.getRiskLevel(); + dto.decision = entity.getDecision(); + dto.validationResults = entity.getTriggeredValidators(); + dto.createdAt = entity.getCreatedAt(); + dto.updatedAt = entity.getUpdatedAt(); + return dto; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/message_broker/Consumer.java b/src/main/java/com/neo/tx/message_broker/Consumer.java new file mode 100644 index 0000000..0f03278 --- /dev/null +++ b/src/main/java/com/neo/tx/message_broker/Consumer.java @@ -0,0 +1,27 @@ +package com.neo.tx.message_broker; + +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Component; + +@Component +@ConditionalOnProperty(prefix = "properties.kafka.consumer", name = "enabled", havingValue = "true") +public class Consumer { + + private static final Logger log = LoggerFactory.getLogger(Consumer.class); + + @KafkaListener(topics = "${properties.kafka.consumer.validation.topic}", groupId = "${spring.kafka.consumer.group-id}") + public void onMessage(ConsumerRecord record) { + String formatted = String.format("\n\n--- Kafka message received ---\n" + + "topic={%s} partition={%d} offset={%d}\n" + + "key={%s}\n" + + "value={%s}\n" + + "------------------------------\n", + record.topic(), record.partition(), record.offset(), record.key(), record.value()); + + log.info(formatted); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/message_broker/Publisher.java b/src/main/java/com/neo/tx/message_broker/Publisher.java new file mode 100644 index 0000000..64c0e6e --- /dev/null +++ b/src/main/java/com/neo/tx/message_broker/Publisher.java @@ -0,0 +1,56 @@ +package com.neo.tx.message_broker; + +import com.neo.tx.model.OutboxEvent; +import com.neo.tx.repository.EventOutboxRepository; +import jakarta.transaction.Transactional; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.Instant; +import java.util.List; + +@Component +@ConditionalOnProperty(prefix = "properties.kafka.producer", name = "enabled", havingValue = "true") +public class Publisher { + + private static final int FIXED_DELAY = 5000; + private static final int CLAIM_BATCH_SIZE = 50; + private static final int MAX_ATTEMPTS = 10; + + private final EventOutboxRepository eventOutboxRepository; + private final KafkaTemplate kafkaTemplate; + + public Publisher(EventOutboxRepository eventOutboxRepository, KafkaTemplate kafkaTemplate) { + this.eventOutboxRepository = eventOutboxRepository; + this.kafkaTemplate = kafkaTemplate; + } + + @Scheduled(fixedDelay = FIXED_DELAY) + @Transactional + public void publish() { + List eventList = eventOutboxRepository.claimBatch(CLAIM_BATCH_SIZE); + + for (OutboxEvent outboxEvent : eventList) { + try { + kafkaTemplate.send(outboxEvent.getTopic(), outboxEvent.getEventKey(), outboxEvent.getPayload()).get(); // block = simplest correctness + outboxEvent.setStatus(OutboxEvent.Status.SENT); + outboxEvent.setLastError(null); + } catch (Exception ex) { + int attempts = outboxEvent.getAttempts() + 1; + outboxEvent.setAttempts(attempts); + outboxEvent.setLastError(ex.getMessage()); + + if (attempts >= MAX_ATTEMPTS) { + outboxEvent.setStatus(OutboxEvent.Status.FAILED); + } else { + outboxEvent.setStatus(OutboxEvent.Status.NEW); + outboxEvent.setAvailableAt(Instant.now().plusSeconds((long) Math.min(60, Math.pow(2, attempts)))); // basic backoff logic + } + } + + eventOutboxRepository.save(outboxEvent); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/model/OutboxEvent.java b/src/main/java/com/neo/tx/model/OutboxEvent.java new file mode 100644 index 0000000..b86321b --- /dev/null +++ b/src/main/java/com/neo/tx/model/OutboxEvent.java @@ -0,0 +1,184 @@ +package com.neo.tx.model; + +import jakarta.persistence.*; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +import java.time.Instant; + +@Entity +@Table( + name = "event_outbox", + indexes = { + @Index(name = "idx_outbox_status_available_at", columnList = "status,available_at") + }) +public class OutboxEvent { + + public enum Status {NEW, SENT, FAILED} + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "aggregate_type", nullable = false, length = 64) + private String aggregateType; + + @Column(name = "aggregate_id", nullable = false, length = 64) + private String aggregateId; + + @Column(name = "event_type", nullable = false, length = 128) + private String eventType; + + @Column(name = "topic", nullable = false, length = 255) + private String topic; + + @Column(name = "event_key", nullable = false, length = 128) + private String eventKey; + + @JdbcTypeCode(SqlTypes.JSON) + @Column(name = "payload", columnDefinition = "jsonb", nullable = false) + private String payload; + + @Enumerated(EnumType.STRING) + @Column(name = "status", nullable = false, length = 16) + private Status status; + + @Column(name = "attempts", nullable = false) + private Integer attempts; + + @Column(name = "last_error") + private String lastError; + + @Column(name = "available_at", nullable = false) + private Instant availableAt; + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + + @PrePersist + void prePersist() { + var now = Instant.now(); + if (availableAt == null) { + availableAt = now; + } + + if (attempts == null) { + attempts = 0; + } + + createdAt = now; + updatedAt = now; + } + + @PreUpdate + void preUpdate() { + updatedAt = Instant.now(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAggregateType() { + return aggregateType; + } + + public void setAggregateType(String aggregateType) { + this.aggregateType = aggregateType; + } + + public String getAggregateId() { + return aggregateId; + } + + public void setAggregateId(String aggregateId) { + this.aggregateId = aggregateId; + } + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + + public String getTopic() { + return topic; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public String getEventKey() { + return eventKey; + } + + public void setEventKey(String eventKey) { + this.eventKey = eventKey; + } + + public String getPayload() { + return payload; + } + + public void setPayload(String payload) { + this.payload = payload; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public Integer getAttempts() { + return attempts; + } + + public void setAttempts(Integer attempts) { + this.attempts = attempts; + } + + public String getLastError() { + return lastError; + } + + public void setLastError(String lastError) { + this.lastError = lastError; + } + + public Instant getAvailableAt() { + return availableAt; + } + + public void setAvailableAt(Instant availableAt) { + this.availableAt = availableAt; + } + + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + public Instant getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/model/Transaction.java b/src/main/java/com/neo/tx/model/Transaction.java new file mode 100644 index 0000000..809f013 --- /dev/null +++ b/src/main/java/com/neo/tx/model/Transaction.java @@ -0,0 +1,205 @@ +package com.neo.tx.model; + +import jakarta.persistence.*; +import jakarta.validation.constraints.Size; + +import java.math.BigDecimal; +import java.time.Instant; +import java.util.UUID; + +@Entity +@Table( + name = "transaction", + uniqueConstraints = { + @UniqueConstraint(name = "uq_transaction_external_id", columnNames = "external_id") + }, + indexes = { + @Index(name = "idx_transaction_user_id", columnList = "user_id"), + @Index(name = "idx_transaction_initialized", columnList = "initialized"), + @Index(name = "idx_transaction_created_at", columnList = "created_at") + }) +public class Transaction { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "external_id", nullable = false) + private Long externalId; + + @Column(name = "user_id", nullable = false) + private UUID userId; + + @Column(name = "amount", nullable = false, precision = 19, scale = 4) + private BigDecimal amount; + + @Column(name = "currency", nullable = false, length = 3) + @Size(min = 3, max = 3) + private String currency; // ISO-4217 code like "USD" + + @Column(name = "initialized", nullable = false) + private Long initialized; + + @Column(name = "merchant_category", nullable = false, length = 16) + private String merchantCategory; + + @Column(name = "merchant_id", nullable = false) + private UUID merchantId; + + @Column(name = "channel", nullable = false, length = 64) + private String channel; + + @Column(name = "ip_address", nullable = false, length = 45) + private String ipAddress; + + @Column(name = "country", nullable = false, length = 3) + @Size(min = 3, max = 3) + private String country; // 3-letter code + + @Column(name = "device_id", length = 64) + private String deviceId; + + @Column(name = "card_fingerprint", length = 64) + private String cardFingerprint; + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + + @OneToOne(mappedBy = "transaction", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + private Validation validation; + + @PrePersist + void prePersist() { + var now = Instant.now(); + this.createdAt = now; + this.updatedAt = now; + } + + @PreUpdate + void preUpdate() { + this.updatedAt = Instant.now(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getExternalId() { + return externalId; + } + + public void setExternalId(Long externalId) { + this.externalId = externalId; + } + + public UUID getUserId() { + return userId; + } + + public void setUserId(UUID userId) { + this.userId = userId; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Long getInitialized() { + return initialized; + } + + public void setInitialized(Long initialized) { + this.initialized = initialized; + } + + public String getMerchantCategory() { + return merchantCategory; + } + + public void setMerchantCategory(String merchantCategory) { + this.merchantCategory = merchantCategory; + } + + public UUID getMerchantId() { + return merchantId; + } + + public void setMerchantId(UUID merchantId) { + this.merchantId = merchantId; + } + + public String getChannel() { + return channel; + } + + public void setChannel(String channel) { + this.channel = channel; + } + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getCardFingerprint() { + return cardFingerprint; + } + + public void setCardFingerprint(String cardFingerprint) { + this.cardFingerprint = cardFingerprint; + } + + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + public Instant getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/model/Validation.java b/src/main/java/com/neo/tx/model/Validation.java new file mode 100644 index 0000000..3c0defa --- /dev/null +++ b/src/main/java/com/neo/tx/model/Validation.java @@ -0,0 +1,129 @@ +package com.neo.tx.model; + +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import jakarta.persistence.*; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Table( + name = "validation", + uniqueConstraints = { + @UniqueConstraint(name = "uq_validation_transaction_id", columnNames = "transaction_id") + }, + indexes = { + @Index(name = "idx_validation_transaction_id", columnList = "transaction_id") + }) +public class Validation { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "transaction_id", nullable = false, foreignKey = @ForeignKey(name = "fk_validation_transaction")) + private Transaction transaction; + + @Column(name = "score", nullable = false) + private Integer score; + + @Enumerated(EnumType.STRING) + @Column(name = "risk_level", nullable = false, length = 16) + private RiskLevel riskLevel; + + @Enumerated(EnumType.STRING) + @Column(name = "decision", nullable = false, length = 16) + private Decision decision; + + @JdbcTypeCode(SqlTypes.JSON) + @Column(name = "triggered_validators", columnDefinition = "jsonb", nullable = false) + private List triggeredValidators = new ArrayList<>(); + + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + + @PrePersist + void prePersist() { + var now = Instant.now(); + this.createdAt = now; + this.updatedAt = now; + } + + @PreUpdate + void preUpdate() { + this.updatedAt = Instant.now(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Transaction getTransaction() { + return transaction; + } + + public void setTransaction(Transaction transaction) { + this.transaction = transaction; + } + + public Integer getScore() { + return score; + } + + public void setScore(Integer score) { + this.score = score; + } + + public RiskLevel getRiskLevel() { + return riskLevel; + } + + public void setRiskLevel(RiskLevel riskLevel) { + this.riskLevel = riskLevel; + } + + public Decision getDecision() { + return decision; + } + + public void setDecision(Decision decision) { + this.decision = decision; + } + + public List getTriggeredValidators() { + return triggeredValidators; + } + + public void setTriggeredValidators(List triggeredRules) { + this.triggeredValidators = triggeredRules; + } + + public Instant getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + public Instant getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/repository/EventOutboxJpaRepository.java b/src/main/java/com/neo/tx/repository/EventOutboxJpaRepository.java new file mode 100644 index 0000000..94028db --- /dev/null +++ b/src/main/java/com/neo/tx/repository/EventOutboxJpaRepository.java @@ -0,0 +1,9 @@ +package com.neo.tx.repository; + +import com.neo.tx.model.OutboxEvent; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface EventOutboxJpaRepository extends JpaRepository, JpaSpecificationExecutor { + +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/repository/EventOutboxRepository.java b/src/main/java/com/neo/tx/repository/EventOutboxRepository.java new file mode 100644 index 0000000..e9c691e --- /dev/null +++ b/src/main/java/com/neo/tx/repository/EventOutboxRepository.java @@ -0,0 +1,44 @@ +package com.neo.tx.repository; + +import com.neo.tx.model.OutboxEvent; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class EventOutboxRepository { + + private final EntityManager entityManager; + + public EventOutboxRepository(EntityManager entityManager) { + this.entityManager = entityManager; + } + + @SuppressWarnings("unchecked") + @Transactional + public List claimBatch(int limit) { + return entityManager.createNativeQuery(""" + SELECT * + FROM event_outbox + WHERE status = 'NEW' AND available_at <= now() + ORDER BY id + FOR UPDATE SKIP LOCKED + LIMIT :limit + """, OutboxEvent.class) + .setParameter("limit", limit) + .getResultList(); + } + + @Transactional + public OutboxEvent save(OutboxEvent outboxEvent) { + if (outboxEvent.getId() == null) { + entityManager.persist(outboxEvent); + } else { + outboxEvent = entityManager.merge(outboxEvent); + } + + return outboxEvent; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/repository/TransactionJpaRepository.java b/src/main/java/com/neo/tx/repository/TransactionJpaRepository.java new file mode 100644 index 0000000..14794bf --- /dev/null +++ b/src/main/java/com/neo/tx/repository/TransactionJpaRepository.java @@ -0,0 +1,16 @@ +package com.neo.tx.repository; + +import com.neo.tx.model.Transaction; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.UUID; + +@Repository +public interface TransactionJpaRepository extends JpaRepository, JpaSpecificationExecutor { + + List findByUserIdAndInitializedGreaterThanEqualOrderByInitializedDesc(UUID userId, Long fromInitialized); + +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/repository/TransactionRepository.java b/src/main/java/com/neo/tx/repository/TransactionRepository.java new file mode 100644 index 0000000..5d1bbeb --- /dev/null +++ b/src/main/java/com/neo/tx/repository/TransactionRepository.java @@ -0,0 +1,32 @@ +package com.neo.tx.repository; + +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.mapper.Mapper; +import com.neo.tx.model.Transaction; +import org.springframework.stereotype.Repository; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +@Repository +public class TransactionRepository { + + private final TransactionJpaRepository repository; + + public TransactionRepository(TransactionJpaRepository repository) { + this.repository = repository; + } + + public Transaction save(Transaction transaction) { + return repository.save(transaction); + } + + public Transaction save(TransactionRequestDto transactionRequestDto) { + return save(Mapper.toEntity(transactionRequestDto)); + } + + public List find(UUID userId, Instant from) { + return repository.findByUserIdAndInitializedGreaterThanEqualOrderByInitializedDesc(userId, from.getEpochSecond()); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/repository/ValidationJpaRepository.java b/src/main/java/com/neo/tx/repository/ValidationJpaRepository.java new file mode 100644 index 0000000..42ba045 --- /dev/null +++ b/src/main/java/com/neo/tx/repository/ValidationJpaRepository.java @@ -0,0 +1,11 @@ +package com.neo.tx.repository; + +import com.neo.tx.model.Validation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface ValidationJpaRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/src/main/java/com/neo/tx/repository/ValidationRepository.java b/src/main/java/com/neo/tx/repository/ValidationRepository.java new file mode 100644 index 0000000..f97e85a --- /dev/null +++ b/src/main/java/com/neo/tx/repository/ValidationRepository.java @@ -0,0 +1,45 @@ +package com.neo.tx.repository; + +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.mapper.Mapper; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.specification.ValidationSpecification; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.stereotype.Repository; + +import java.time.Instant; +import java.util.UUID; + +@Repository +public class ValidationRepository { + + private final ValidationJpaRepository repository; + + public ValidationRepository(ValidationJpaRepository repository) { + this.repository = repository; + } + + public Validation save(Validation validation) { + return repository.save(validation); + } + + public Validation save(ValidationResponseDto validationResponseDto, Transaction transaction) { + return save(Mapper.toEntity(validationResponseDto, transaction)); + } + + public Validation find(long id) { + return repository.findById(id).orElse(null); + } + + public Page find(UUID userId, RiskLevel riskLevel, Decision decision, Instant from, Instant to, int page, int size) { + Specification specification = ValidationSpecification.buildCondition(userId, riskLevel, decision, from, to); + PageRequest pagination = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt"); + return repository.findAll(specification, pagination); + } +} diff --git a/src/main/java/com/neo/tx/repository/specification/ValidationSpecification.java b/src/main/java/com/neo/tx/repository/specification/ValidationSpecification.java new file mode 100644 index 0000000..e5ee237 --- /dev/null +++ b/src/main/java/com/neo/tx/repository/specification/ValidationSpecification.java @@ -0,0 +1,53 @@ +package com.neo.tx.repository.specification; + +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.model.Validation; +import org.springframework.data.jpa.domain.Specification; + +import java.time.Instant; +import java.util.UUID; + +public final class ValidationSpecification { + + private ValidationSpecification() { + + } + + public static Specification userId(UUID userId) { + return (root, query, builder) -> + builder.equal(root.get("transaction").get("userId"), userId); + } + + public static Specification riskLevel(RiskLevel riskLevel) { + return (root, query, builder) -> + builder.equal(root.get("riskLevel"), riskLevel); + } + + public static Specification decision(Decision decision) { + return (root, query, builder) -> + builder.equal(root.get("decision"), decision); + } + + public static Specification initializedFrom(Instant from) { + return (root, query, builder) -> + builder.greaterThanOrEqualTo(root.get("transaction").get("initialized"), from.getEpochSecond()); + } + + public static Specification initializedTo(Instant to) { + return (root, query, builder) -> + builder.lessThanOrEqualTo(root.get("transaction").get("initialized"), to.getEpochSecond()); + } + + public static Specification buildCondition(UUID userId, RiskLevel riskLevel, Decision decision, Instant from, Instant to) { + Specification specification = Specification.unrestricted(); + + if (userId != null) specification = specification.and(ValidationSpecification.userId(userId)); + if (riskLevel != null) specification = specification.and(ValidationSpecification.riskLevel(riskLevel)); + if (decision != null) specification = specification.and(ValidationSpecification.decision(decision)); + if (from != null) specification = specification.and(ValidationSpecification.initializedFrom(from)); + if (to != null) specification = specification.and(ValidationSpecification.initializedTo(to)); + + return specification; + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/service/EventOutboxService.java b/src/main/java/com/neo/tx/service/EventOutboxService.java new file mode 100644 index 0000000..1a6e559 --- /dev/null +++ b/src/main/java/com/neo/tx/service/EventOutboxService.java @@ -0,0 +1,61 @@ +package com.neo.tx.service; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.neo.tx.model.OutboxEvent; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.EventOutboxRepository; +import jakarta.transaction.Transactional; +import org.springframework.stereotype.Service; + +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; + +@Service +public class EventOutboxService { + + private final ObjectMapper objectMapper; + private final EventOutboxRepository eventOutboxRepository; + + public EventOutboxService(ObjectMapper objectMapper, EventOutboxRepository eventOutboxRepository) { + this.objectMapper = objectMapper; + this.eventOutboxRepository = eventOutboxRepository; + } + + @Transactional + public void writeValidationCompleted(Transaction transaction, Validation validation, String pipeline) { + Map payload = new HashMap<>(); + payload.put("transactionId", transaction.getId()); + payload.put("externalId", transaction.getExternalId()); + payload.put("userId", transaction.getUserId()); + payload.put("pipeline", pipeline); + payload.put("score", validation.getScore()); + payload.put("riskLevel", validation.getRiskLevel().name()); + payload.put("decision", validation.getDecision().name()); + payload.put("triggeredValidators", validation.getTriggeredValidators()); + payload.put("occurredAt", Instant.now().toString()); + + String json; + + try { + json = objectMapper.writeValueAsString(payload); + } catch (JsonProcessingException ex) { + throw new IllegalStateException("Failed to serialize outbox payload", ex); + } + + OutboxEvent outboxEvent = new OutboxEvent(); + outboxEvent.setAggregateType("Transaction"); + outboxEvent.setAggregateId(String.valueOf(transaction.getId())); + outboxEvent.setEventType("TransactionValidated"); + outboxEvent.setTopic("transaction.validation.completed"); + outboxEvent.setEventKey(String.valueOf(transaction.getExternalId())); + outboxEvent.setPayload(json); + outboxEvent.setStatus(OutboxEvent.Status.NEW); + outboxEvent.setAttempts(0); + outboxEvent.setAvailableAt(Instant.now()); + + eventOutboxRepository.save(outboxEvent); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/service/PipelineService.java b/src/main/java/com/neo/tx/service/PipelineService.java new file mode 100644 index 0000000..4773fd9 --- /dev/null +++ b/src/main/java/com/neo/tx/service/PipelineService.java @@ -0,0 +1,40 @@ +package com.neo.tx.service; + +import com.neo.tx.validator.*; +import org.springframework.stereotype.Service; + +@Service +public class PipelineService { + + private final AmountValidator amountValidator; + private final CountryMismatchValidation countryMismatchValidation; + private final HighRiskCountryValidator highRiskCountryValidator; + private final HighRiskMerchantValidator highRiskMerchantValidator; + private final NightTimeValidator nightTimeValidator; + private final VelocityValidation velocityValidation; + + public PipelineService( + AmountValidator amountValidator, + CountryMismatchValidation countryMismatchValidation, + HighRiskCountryValidator highRiskCountryValidator, + HighRiskMerchantValidator highRiskMerchantValidator, + NightTimeValidator nightTimeValidator, + VelocityValidation velocityValidation + ) { + this.amountValidator = amountValidator; + this.countryMismatchValidation = countryMismatchValidation; + this.highRiskCountryValidator = highRiskCountryValidator; + this.highRiskMerchantValidator = highRiskMerchantValidator; + this.nightTimeValidator = nightTimeValidator; + this.velocityValidation = velocityValidation; + } + + public Evaluate getDefaultChain() { + return amountValidator + .and(countryMismatchValidation) + .and(highRiskCountryValidator) + .and(highRiskMerchantValidator) + .and(nightTimeValidator) + .and(velocityValidation); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/service/TransactionService.java b/src/main/java/com/neo/tx/service/TransactionService.java new file mode 100644 index 0000000..919216d --- /dev/null +++ b/src/main/java/com/neo/tx/service/TransactionService.java @@ -0,0 +1,44 @@ +package com.neo.tx.service; + +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.mapper.Mapper; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.TransactionRepository; +import jakarta.transaction.Transactional; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class TransactionService { + + private final PipelineService pipelineService; + private final TransactionRepository transactionRepository; + private final ValidationService validationService; + private final EventOutboxService eventOutboxService; + + public TransactionService( + PipelineService pipelineService, + TransactionRepository transactionRepository, + ValidationService validationService, + EventOutboxService eventOutboxService + ) { + this.pipelineService = pipelineService; + this.transactionRepository = transactionRepository; + this.validationService = validationService; + this.eventOutboxService = eventOutboxService; + } + + @Transactional + public ValidationResponseDto validateTransaction(TransactionRequestDto transactionRequestDto, String pipeline) { // TODO include multiple pipeline support + List results = pipelineService.getDefaultChain().evaluate(transactionRequestDto); + ValidationResponseDto analyzedResults = validationService.analyzeValidationResponses(results); + Transaction transaction = transactionRepository.save(transactionRequestDto); + Validation validation = validationService.save(analyzedResults, transaction); + eventOutboxService.writeValidationCompleted(transaction, validation, pipeline); + return Mapper.toDto(validation, transaction.getId()); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/service/ValidationService.java b/src/main/java/com/neo/tx/service/ValidationService.java new file mode 100644 index 0000000..1c9ae0d --- /dev/null +++ b/src/main/java/com/neo/tx/service/ValidationService.java @@ -0,0 +1,68 @@ +package com.neo.tx.service; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.exception.NotFoundException; +import com.neo.tx.mapper.Mapper; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.ValidationRepository; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Service; + +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +@Service +public class ValidationService { + + private final Properties properties; + private final ValidationRepository validationRepository; + + public ValidationService(Properties properties, ValidationRepository validationRepository) { + this.properties = properties; + this.validationRepository = validationRepository; + } + + public ValidationResponseDto analyzeValidationResponses(List results) { + RiskLevel riskLevel = RiskLevel.HIGH; + Decision decision = Decision.BLOCK; + + int totalScore = results.stream().mapToInt((result) -> result.scoreDelta).sum(); + + if (totalScore <= properties.riskThresholdMedium) { + riskLevel = RiskLevel.MEDIUM; + decision = Decision.REVIEW; + } + + if (totalScore <= properties.riskThresholdLow) { + riskLevel = RiskLevel.LOW; + decision = Decision.ALLOW; + } + + return new ValidationResponseDto(totalScore, riskLevel, decision, results); + } + + public Validation save(ValidationResponseDto validationResponseDto, Transaction transaction) { + return validationRepository.save(validationResponseDto, transaction); + } + + public ValidationResponseDto find(long id) { + Validation validation = validationRepository.find(id); + + if (validation == null) { + throw new NotFoundException(Validation.class.getSimpleName(), String.valueOf(id)); + } + + return Mapper.toDto(validation, validation.getTransaction().getId()); + } + + public Page find(UUID userId, RiskLevel riskLevel, Decision decision, Instant from, Instant to, int page, int size) { + Page validations = validationRepository.find(userId, riskLevel, decision, from, to, page, size); + return validations.map(Mapper::toDto); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/utility/emptiness b/src/main/java/com/neo/tx/utility/emptiness new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/com/neo/tx/validator/AmountValidator.java b/src/main/java/com/neo/tx/validator/AmountValidator.java new file mode 100644 index 0000000..aa00867 --- /dev/null +++ b/src/main/java/com/neo/tx/validator/AmountValidator.java @@ -0,0 +1,32 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.util.List; + +@Component +public class AmountValidator implements Evaluate { + + private final Properties properties; + + public AmountValidator(Properties properties) { + this.properties = properties; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + if (transaction.amount.compareTo(BigDecimal.valueOf(9999)) == 1) { // TODO could include currency converter + return List.of(new ValidationResultDto("VERY_HIGH_AMOUNT", "The transaction is above 9999 USD", 60)); + } + + if (transaction.amount.compareTo(BigDecimal.valueOf(4999)) == 1) { + return List.of(new ValidationResultDto("HIGH_AMOUNT", "The transaction is above 4999 USD", 40)); + } + + return List.of(); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/validator/CountryMismatchValidation.java b/src/main/java/com/neo/tx/validator/CountryMismatchValidation.java new file mode 100644 index 0000000..46ef361 --- /dev/null +++ b/src/main/java/com/neo/tx/validator/CountryMismatchValidation.java @@ -0,0 +1,43 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.model.Transaction; +import com.neo.tx.repository.TransactionRepository; +import org.springframework.stereotype.Component; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.List; +import java.util.Objects; + +@Component +public class CountryMismatchValidation implements Evaluate { + + private final Properties properties; + private final TransactionRepository transactionRepository; + + public CountryMismatchValidation(Properties properties, TransactionRepository transactionRepository) { + this.properties = properties; + this.transactionRepository = transactionRepository; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + List transactions = transactionRepository.find(transaction.userId, Instant.now().minus(30, ChronoUnit.DAYS)); + + long totalTransactions = transactions.size(); + long countryMatch = transactions.stream().filter((e) -> Objects.equals(e.getCountry(), transaction.country)).count(); + + if (countryMatch < (double) totalTransactions / 1.50) { + return List.of(new ValidationResultDto( + "COUNTRY_MISMATCH", + "Current country + " + transaction.country + " appears in transactions less than 50% of the time", + 20 + )); + } + + return List.of(); + } +} \ No newline at end of file diff --git a/src/main/java/com/neo/tx/validator/Evaluate.java b/src/main/java/com/neo/tx/validator/Evaluate.java new file mode 100644 index 0000000..0f15497 --- /dev/null +++ b/src/main/java/com/neo/tx/validator/Evaluate.java @@ -0,0 +1,27 @@ +package com.neo.tx.validator; + +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; + +import java.util.ArrayList; +import java.util.List; + +@FunctionalInterface +public interface Evaluate { + + List evaluate(TransactionRequestDto transaction); + + default Evaluate and(Evaluate that) { + return (transaction) -> { + List thisResult = this.evaluate(transaction); + List thatResult = that.evaluate(transaction); + + List validationResults = new ArrayList<>(); + + validationResults.addAll(thisResult); + validationResults.addAll(thatResult); + + return validationResults; + }; + } +} diff --git a/src/main/java/com/neo/tx/validator/HighRiskCountryValidator.java b/src/main/java/com/neo/tx/validator/HighRiskCountryValidator.java new file mode 100644 index 0000000..de57616 --- /dev/null +++ b/src/main/java/com/neo/tx/validator/HighRiskCountryValidator.java @@ -0,0 +1,28 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.enums.RiskCountry; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class HighRiskCountryValidator implements Evaluate { + + private final Properties properties; + + public HighRiskCountryValidator(Properties properties) { + this.properties = properties; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + if (RiskCountry.contains(transaction.country)) { + return List.of(new ValidationResultDto("HIGH_RISK_COUNTRY", "The transaction was made in high-risk country " + transaction.country, 30)); + } + + return List.of(); + } +} diff --git a/src/main/java/com/neo/tx/validator/HighRiskMerchantValidator.java b/src/main/java/com/neo/tx/validator/HighRiskMerchantValidator.java new file mode 100644 index 0000000..c4cc919 --- /dev/null +++ b/src/main/java/com/neo/tx/validator/HighRiskMerchantValidator.java @@ -0,0 +1,28 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.enums.RiskMerchant; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class HighRiskMerchantValidator implements Evaluate { + + private final Properties properties; + + public HighRiskMerchantValidator(Properties properties) { + this.properties = properties; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + if (RiskMerchant.contains(transaction.merchantCategory)) { + return List.of(new ValidationResultDto("HIGH_RISK_MERCHANT", "The transaction is made in one of the risk merchant categories", 35)); + } + + return List.of(); + } +} diff --git a/src/main/java/com/neo/tx/validator/NightTimeValidator.java b/src/main/java/com/neo/tx/validator/NightTimeValidator.java new file mode 100644 index 0000000..529d8be --- /dev/null +++ b/src/main/java/com/neo/tx/validator/NightTimeValidator.java @@ -0,0 +1,31 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import org.springframework.stereotype.Component; + +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.List; + +@Component +public class NightTimeValidator implements Evaluate { + + private final Properties properties; + + public NightTimeValidator(Properties properties) { + this.properties = properties; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + int hourByUTC = Instant.ofEpochSecond(transaction.initialized).atZone(ZoneOffset.UTC).getHour(); + + if (hourByUTC >= 2 && hourByUTC < 5) { + return List.of(new ValidationResultDto("NIGH_TIME", "The transaction is made between 02:00 AM and 05:00 AM", 15)); + } + + return List.of(); + } +} diff --git a/src/main/java/com/neo/tx/validator/VelocityValidation.java b/src/main/java/com/neo/tx/validator/VelocityValidation.java new file mode 100644 index 0000000..564c20d --- /dev/null +++ b/src/main/java/com/neo/tx/validator/VelocityValidation.java @@ -0,0 +1,40 @@ +package com.neo.tx.validator; + +import com.neo.tx.config.Properties; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResultDto; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.ValidationRepository; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Component; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.List; + +@Component +public class VelocityValidation implements Evaluate { + + private final Properties properties; + private final ValidationRepository validationRepository; + + public VelocityValidation(Properties properties, ValidationRepository validationRepository) { + this.properties = properties; + this.validationRepository = validationRepository; + } + + @Override + public List evaluate(TransactionRequestDto transaction) { + int minutes = 5; + int threshold = 3; + Instant now = Instant.now(); + + Page pagedRecords = validationRepository.find(transaction.userId, null, null, now.minus(minutes, ChronoUnit.MINUTES), now, 0, 100); + + if (pagedRecords.getContent().size() > threshold) { + return List.of(new ValidationResultDto("HIGH_VELOCITY", "Transactions under user id " + transaction.userId + "are too frequent. Threshold: " + threshold, 25)); + } + + return List.of(); + } +} \ No newline at end of file diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml new file mode 100644 index 0000000..7f427eb --- /dev/null +++ b/src/main/resources/application-dev.yaml @@ -0,0 +1,35 @@ +spring: # Spring config + config: # config + activate: # activate + on-profile: dev # on which profile (service run mode) this configuration should be loaded + + datasource: + url: jdbc:postgresql://localhost:5432/transaction + username: marshmallow + password: tasty + + jpa: + hibernate: + ddl-auto: validate + show-sql: false + +logging: # logging configuration (overrides logback logging levels) + level: # levels + root: INFO # default logging level for all unspecified loggers + com.neo.tx: INFO # minimum level for the application package (log.info(), log.debug(), etc) + com.fasterxml.jackson: INFO # Jackson logs (JSON serialization/deserialization) + liquibase: INFO # Liquibase migration logs + org.hibernate.SQL: INFO # Hibernate SQL statement logging (prints SQL queries) + org.hibernate.orm.jdbc.bind: INFO # Hibernate parameter binding logger (shows bound values with TRACE) + org.springframework.web: INFO # Spring Web / MVC logs + org.springframework.web.servlet.DispatcherServlet: INFO # Specific logger for DispatcherServlet (main Spring MVC dispatcher. INFO prints request lifecycle messages) + +properties: + score.min: 0 + score.max: 100 + risk.threshold.low: 30 + risk.threshold.medium: 70 + risk.threshold.high: 100 + kafka.producer.enabled: true + kafka.consumer.enabled: true + kafka.consumer.validation.topic: transaction.validation.completed \ No newline at end of file diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml new file mode 100644 index 0000000..d934c6a --- /dev/null +++ b/src/main/resources/application-prod.yaml @@ -0,0 +1,29 @@ +spring: # Spring config + config: # config + activate: # activate + on-profile: prod # on which profile (service run mode) this configuration should be loaded + + datasource: + url: jdbc:postgresql://${DB_HOST:postgres}:${DB_PORT:5432}/transaction + username: ${DB_USER:postgres} + password: ${DB_PASSWORD:postgres} + hikari: + maximum-pool-size: ${DB_POOL_SIZE:50} + + jpa: + hibernate: + ddl-auto: validate + show-sql: false + +logging: # logging configuration (overrides logback logging levels) + level: # levels + root: INFO # default logging level for all unspecified loggers + com.neo.tx: INFO # minimum level for the application package (log.info(), log.debug(), etc) + com.fasterxml.jackson: INFO # Jackson logs (JSON serialization/deserialization) + liquibase: INFO # Liquibase migration logs + org.hibernate.SQL: INFO # Hibernate SQL statement logging (prints SQL queries) + org.hibernate.orm.jdbc.bind: TRACE # Hibernate parameter binding logger (shows bound values with TRACE) + org.springframework.web: INFO # Spring Web / MVC logs + org.springframework.web.servlet.DispatcherServlet: INFO # Specific logger for DispatcherServlet (main Spring MVC dispatcher. INFO prints request lifecycle messages) + +properties: \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..58d7907 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,109 @@ +server: # top-level Spring Boot configuration + port: 3000 # the port on which the server is going to listen on + servlet: # servlet container specific options + context-path: /api/ # prefix for all endpoints + shutdown: graceful # strategy on shut down command + compression: # GZIP HTTP response compression + enabled: true # status + mime-types: application/json,application/xml,text/html,text/plain # only responses with the specifies content types will be compressed + min-response-size: 1KB # lover bound for compressing + http2: # http2 support + enabled: true # status + +spring: # core Spring Boot configuration + application: # this application + name: transaction-processing-system # application name (used in logs, tracing, metrics) + + profiles: # chosen profile (default = dev) + default: dev # profile name (application-{profiles.default}.yaml configuration is loaded additionally. Could be overrode with: SPRING_PROFILES_ACTIVE=prod or --spring.profiles.active=prod) + + datasource: # DB connections and connection pool + url: jdbc:postgresql://localhost:5432/transaction # JDBC URL + username: marshmallow # DB credentials + password: tasty # DB credentials + driver-class-name: org.postgresql.Driver # explicitly specified JDBC driver + hikari: # chose HikariCP instead of default Tomcat JDBC pool (this one is more efficient) + maximum-pool-size: 20 # max connection pool size + minimum-idle: 5 # command to try to keep at least n connections ready + idle-timeout: 600000 # close idle connections after n milliseconds + max-lifetime: 1800000 # forcefully recycle a connection after n milliseconds (avoids issues with DB-side connection limits / NAT timeouts) + connection-timeout: 30000 # pool connection acquiring limit + + jpa: # JPA and Hibernate configurations + # true: Hibernate session is kept open for the whole web request, including view rendering With REST APIs and services, this encourages lazy-loading from controllers (bad layering) + # false: forces to fetch what is needed in service/repository layer. + open-in-view: false + generate-ddl: false # turns on or off schema auto generation + hibernate: # Hibernate configuration + ddl-auto: validate # tables validation strategy (validate - compares entities and real tables) + show-sql: false # print or not SQL to std (best controlled via logging.level.org.hibernate.SQL) + properties: # additional Hibernate properties + hibernate.format_sql: true # pretty SQL formatting + hibernate.jdbc.time_zone: UTC # forces JDBC-level timezone to UTC+0 + hibernate.default_schema: public # DB schema (default = public) + + sql: # SQL schema/data initialization + init: # initialization + mode: never # look for schema.sql or data.sql and auto-run them (never - for manual/migration tool managing) + + liquibase: + change-log: classpath:db/changelog/db.changelog-master.yaml + enabled: true # status + + jackson: # Jackson JSON serialization configuration + time-zone: UTC # Jackson will output dates by specified timezone + default-property-inclusion: non_null # do not include fields with null values + + mvc: # web layer configuration + problemdetails: # RFC 7807 ProblemDetail response format - unified error responses in Boot 3+/4 + enabled: true # status + + messages: # i18n / message sources + basename: messages # Spring will look for messages.properties, messages_en.properties, etc. Used for validation and error check. + encoding: UTF-8 # default message encoding + + kafka: # Kafka configuration + bootstrap-servers: ${SPRING_KAFKA_BOOTSTRAP_SERVERS:127.0.0.1:9094} # bootstrap server URL + producer: # producer configuration + key-serializer: org.apache.kafka.common.serialization.StringSerializer # sends message key as string + value-serializer: org.apache.kafka.common.serialization.StringSerializer # sends message value as string + acks: all # broker will acknowledge only after data is committed according to replication settings + consumer: # consumer configuration + group-id: transaction-validation-consumer # consumers with same group share partitions (load balancing). Different group IDs each get a full copy of messages + auto-offset-reset: earliest # read from the earliest offset number of the group + key-deserializer: org.apache.kafka.common.serialization.StringDeserializer # treat incoming message key as string + value-deserializer: org.apache.kafka.common.serialization.StringDeserializer # treat incoming message value as string + +management: # Spring Boot Actuator (monitoring) configuration + endpoints: # endpoints + web: # web + exposure: # exposed endpoints + include: health,info,metrics,loggers,threaddump,env # list of endpoints exposed over HTTP (/actuator/{url}) + endpoint: # specific behavior + health: # for health endpoints + show-details: when_authorized # detailed health info is accessible only to specified users (UP and DOWN for anonymous) + +springdoc: # OpenAPI/Swagger configuration + api-docs: # documents + path: /v1/openapi # path where raw OpenAPI file is served (available at /{servlet.context-path}/{api-docs.path}) + swagger-ui: # Swagger configuration + path: /swagger # path to Swagger UI web page (available at /{servlet.context-path}/{swagger-ui.path}) + operations-sorter: method # groups operations by (method - by HTTP method) + tags-sorter: alpha # sorts tags by (alpha - alphabetically) + display-request-duration: true # shows how long requests took in Swagger UI + doc-expansion: none # collapse endpoints by default + packages-to-scan: com.neo.tx # path for controller to include to OpenAPI spec + paths-to-match: /** # only endpoints start with this path are documented + +logging: # logging configuration (overrides logback logging levels) + level: # levels + root: WARN # default logging level for all unspecified loggers + com.neo.tx: WARN # minimum level for the application package (log.info(), log.debug(), etc) + com.fasterxml.jackson: WARN # Jackson logs (JSON serialization/deserialization) + liquibase: WARN # Liquibase migration logs + org.hibernate.SQL: WARN # Hibernate SQL statement logging (prints SQL queries) + org.hibernate.orm.jdbc.bind: TRACE # Hibernate parameter binding logger (shows bound values with TRACE) + org.springframework.web: WARN # Spring Web / MVC logs + org.springframework.web.servlet.DispatcherServlet: WARN # Specific logger for DispatcherServlet (main Spring MVC dispatcher. INFO prints request lifecycle messages) + +properties: \ No newline at end of file diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml new file mode 100644 index 0000000..643131e --- /dev/null +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -0,0 +1,7 @@ +databaseChangeLog: + - include: + file: "db/changes/0001-create-transaction-table.yaml" + - include: + file: "db/changes/0002-create-validation-table.yaml" + - include: + file: "db/changes/0003-create-event-outbox-table.yaml" \ No newline at end of file diff --git a/src/main/resources/db/changes/0001-create-transaction-table.yaml b/src/main/resources/db/changes/0001-create-transaction-table.yaml new file mode 100644 index 0000000..c9136a1 --- /dev/null +++ b/src/main/resources/db/changes/0001-create-transaction-table.yaml @@ -0,0 +1,127 @@ +databaseChangeLog: + - changeSet: + id: 0001-create-transaction-table + author: Kirill Tatarenko + changes: + - createTable: + tableName: transaction + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + + - column: + name: external_id + type: BIGINT + constraints: + nullable: false + + - column: + name: user_id + type: UUID + constraints: + nullable: false + + - column: + name: merchant_id + type: UUID + constraints: + nullable: false + + - column: + name: device_id + type: VARCHAR(64) + constraints: + nullable: true + + - column: + name: amount + type: NUMERIC(19, 4) + constraints: + nullable: false + + - column: + name: currency + type: VARCHAR(3) + constraints: + nullable: false + + - column: + name: initialized + type: BIGINT + constraints: + nullable: false + + - column: + name: merchant_category + type: VARCHAR(16) + constraints: + nullable: false + + - column: + name: channel + type: VARCHAR(64) + constraints: + nullable: false + + - column: + name: ip_address + type: VARCHAR(45) + constraints: + nullable: false + + - column: + name: country + type: VARCHAR(3) + constraints: + nullable: false + + - column: + name: card_fingerprint + type: VARCHAR(64) + constraints: + nullable: true + + - column: + name: created_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - column: + name: updated_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - addUniqueConstraint: + tableName: transaction + columnNames: external_id + constraintName: uq_transaction_external_id + + - createIndex: + tableName: transaction + indexName: idx_transaction_user_id + columns: + - column: + name: user_id + + - createIndex: + tableName: transaction + indexName: idx_transaction_initialized + columns: + - column: + name: initialized + + - createIndex: + tableName: transaction + indexName: idx_transaction_created_at + columns: + - column: + name: created_at diff --git a/src/main/resources/db/changes/0002-create-validation-table.yaml b/src/main/resources/db/changes/0002-create-validation-table.yaml new file mode 100644 index 0000000..cffb2c0 --- /dev/null +++ b/src/main/resources/db/changes/0002-create-validation-table.yaml @@ -0,0 +1,85 @@ +databaseChangeLog: + - changeSet: + id: 0002-create-validation-table + author: Kirill Tatarenko + changes: + - createTable: + tableName: validation + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + + - column: + name: transaction_id + type: BIGINT + constraints: + nullable: false + + - column: + name: score + type: INT + constraints: + nullable: false + + - column: + name: risk_level + type: VARCHAR(16) + constraints: + nullable: false + + - column: + name: decision + type: VARCHAR(16) + constraints: + nullable: false + + - column: + name: triggered_validators + type: JSONB + constraints: + nullable: false + + - column: + name: created_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - column: + name: updated_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - addForeignKeyConstraint: + baseTableName: validation + baseColumnNames: transaction_id + referencedTableName: transaction + referencedColumnNames: id + constraintName: fk_validation_transaction + onDelete: CASCADE + onUpdate: RESTRICT + + - createIndex: + tableName: validation + indexName: idx_validation_transaction_id + columns: + - column: + name: transaction_id + + # allow at most 1 validation row per transaction + - addUniqueConstraint: + tableName: validation + columnNames: transaction_id + constraintName: uq_validation_transaction_id + + rollback: + - dropTable: + tableName: validation diff --git a/src/main/resources/db/changes/0003-create-event-outbox-table.yaml b/src/main/resources/db/changes/0003-create-event-outbox-table.yaml new file mode 100644 index 0000000..4a6bc41 --- /dev/null +++ b/src/main/resources/db/changes/0003-create-event-outbox-table.yaml @@ -0,0 +1,100 @@ +databaseChangeLog: + - changeSet: + id: 0003-create-event-outbox-table + author: Kirill Tatarenko + changes: + - createTable: + tableName: event_outbox + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + + - column: + name: aggregate_type + type: VARCHAR(64) + constraints: + nullable: false + + - column: + name: aggregate_id + type: VARCHAR(64) + constraints: + nullable: false + + - column: + name: event_type + type: VARCHAR(128) + constraints: + nullable: false + + - column: + name: topic + type: VARCHAR(255) + constraints: + nullable: false + + - column: + name: event_key + type: VARCHAR(128) + constraints: + nullable: false + + - column: + name: payload + type: JSONB + constraints: + nullable: false + + - column: + name: status + type: VARCHAR(16) + constraints: + nullable: false + + - column: + name: attempts + type: INT + constraints: + nullable: false + + - column: + name: last_error + type: TEXT + + - column: + name: available_at + type: TIMESTAMP + constraints: + nullable: false + + - column: + name: created_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - column: + name: updated_at + type: TIMESTAMP + defaultValueComputed: CURRENT_TIMESTAMP + constraints: + nullable: false + + - createIndex: + tableName: event_outbox + indexName: idx_outbox_status_available_at + columns: + - column: + name: status + - column: + name: available_at + + rollback: + - dropTable: + tableName: event_outbox \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..db7f064 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + UTF-8 + + + + + + + + ${LOG_PATH}/current.log + + + + + + ${LOG_PATH}/%d{yyyy-MM-dd}.%i.log + + + 50MB + + + 30 + + + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/neo/tx/integration/IntegrationTestConfig.java b/src/test/java/com/neo/tx/integration/IntegrationTestConfig.java new file mode 100644 index 0000000..e4069be --- /dev/null +++ b/src/test/java/com/neo/tx/integration/IntegrationTestConfig.java @@ -0,0 +1,47 @@ +package com.neo.tx.integration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.PostgreSQLContainer; + +@SpringBootTest +@ActiveProfiles("test") +@AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public abstract class IntegrationTestConfig { + + @Autowired + JdbcTemplate jdbc; + + @BeforeEach + void clean() { + jdbc.execute("TRUNCATE TABLE validation RESTART IDENTITY CASCADE"); + jdbc.execute("TRUNCATE TABLE transaction RESTART IDENTITY CASCADE"); + jdbc.execute("TRUNCATE TABLE event_outbox RESTART IDENTITY CASCADE"); + } + + @SuppressWarnings("resource") + private static final PostgreSQLContainer POSTGRES = new PostgreSQLContainer<>("postgres:17.7-alpine") + .withDatabaseName("test_transaction") + .withUsername("test_marshmallow") + .withPassword("test_tasty"); + + static { + POSTGRES.start(); + } + + @DynamicPropertySource + static void configureDataSource(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", POSTGRES::getJdbcUrl); + registry.add("spring.datasource.username", POSTGRES::getUsername); + registry.add("spring.datasource.password", POSTGRES::getPassword); + registry.add("spring.liquibase.enabled", () -> "true"); + } +} \ No newline at end of file diff --git a/src/test/java/com/neo/tx/integration/TransactionTest.java b/src/test/java/com/neo/tx/integration/TransactionTest.java new file mode 100644 index 0000000..0e69455 --- /dev/null +++ b/src/test/java/com/neo/tx/integration/TransactionTest.java @@ -0,0 +1,174 @@ +package com.neo.tx.integration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.enums.Decision; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.model.OutboxEvent; +import com.neo.tx.model.Transaction; +import com.neo.tx.model.Validation; +import com.neo.tx.repository.TransactionJpaRepository; +import com.neo.tx.repository.TransactionRepository; +import com.neo.tx.repository.ValidationJpaRepository; +import com.neo.tx.repository.ValidationRepository; +import com.neo.tx.repository.EventOutboxJpaRepository; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TransactionTest extends IntegrationTestConfig { + + @Autowired + Utility utility; + + @Autowired + MockMvc mvc; + + @Autowired + ObjectMapper om; + + @Autowired + TransactionJpaRepository transactionJpaRepository; + + @Autowired + ValidationJpaRepository validationJpaRepository; + + @Autowired + EventOutboxJpaRepository eventOutboxJpaRepository; + + @MockitoSpyBean + ValidationRepository validationRepository; + + @MockitoSpyBean + TransactionRepository transactionRepository; + + @Test + @Transactional + public void shouldValidateTransaction() throws Exception { + String transactionInput = """ + { + "externalId": 1, + "userId": "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11", + "merchantId": "9a1d6f88-0f93-4e58-9dd7-1c9d4a7d6c22", + "deviceId": "android-13-pixel-7", + "amount": 7150.50, + "currency": "USD", + "initialized": 1735689600, + "merchantCategory": "GROCERIES", + "channel": "WEB", + "ipAddress": "192.168.1.10", + "country": "AGO", + "cardFingerprint": "fp_9d82kdk29d" + } + """; + + ArgumentCaptor transactionCaptor = ArgumentCaptor.forClass(Transaction.class); + ArgumentCaptor validationCaptor = ArgumentCaptor.forClass(Validation.class); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/v1/transaction/validate") + .contentType(MediaType.APPLICATION_JSON) + .content(transactionInput)) + .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()) + .andExpect(MockMvcResultMatchers.jsonPath("$.decision").exists()) + .andReturn(); + + Mockito.verify(transactionRepository).save(transactionCaptor.capture()); + Mockito.verify(validationRepository).save(validationCaptor.capture()); + + TransactionRequestDto transactionRequestDto = om.readValue(transactionInput, TransactionRequestDto.class); + ValidationResponseDto validationResponseDto = om.readValue(mvcResult.getResponse().getContentAsString(), ValidationResponseDto.class); + Validation savedValidation = validationCaptor.getValue(); + Transaction savedTransaction = transactionCaptor.getValue(); + List validations = validationJpaRepository.findAll(); + List transactions = transactionJpaRepository.findAll(); + Validation dbValidation = validations.get(0); + Transaction dbTransaction = transactions.get(0); + String[] expectedCodes = new String[]{"HIGH_AMOUNT", "HIGH_RISK_COUNTRY"}; + Object[] gotValidationCodes = validationResponseDto.validationResults.stream().map((e) -> e.code).toArray(); + Object[] dbValidationCodes = dbValidation.getTriggeredValidators().stream().map((e) -> e.code).toArray(); + + assertEquals(1, validations.size()); + assertEquals(1, transactions.size()); + + assertEquals(dbValidation.getDecision(), savedValidation.getDecision()); + assertEquals(dbValidation.getDecision(), validationResponseDto.decision); + + assertEquals(dbValidation.getRiskLevel(), savedValidation.getRiskLevel()); + assertEquals(dbValidation.getRiskLevel(), validationResponseDto.riskLevel); + + assertEquals(dbValidation.getScore(), savedValidation.getScore()); + assertEquals(dbValidation.getScore(), validationResponseDto.score); + + assertEquals(transactionRequestDto.externalId, savedTransaction.getExternalId()); + assertEquals(transactionRequestDto.externalId, dbTransaction.getExternalId()); + + assertEquals(validationResponseDto.transactionId, dbTransaction.getId()); + assertEquals(validationResponseDto.transactionId, dbValidation.getTransaction().getId()); + + assertEquals(transactionRequestDto.cardFingerprint, savedTransaction.getCardFingerprint()); + assertEquals(transactionRequestDto.merchantCategory, savedTransaction.getMerchantCategory()); + assertEquals(transactionRequestDto.cardFingerprint, dbTransaction.getCardFingerprint()); + assertEquals(transactionRequestDto.merchantCategory, dbTransaction.getMerchantCategory()); + + assertArrayEquals(expectedCodes, gotValidationCodes); + assertArrayEquals(expectedCodes, dbValidationCodes); + + Mockito.verify(validationRepository, Mockito.times(1)).save(Mockito.any(Validation.class)); + Mockito.verify(transactionRepository, Mockito.times(1)).save(Mockito.any(Transaction.class)); + } + + @Test + public void shouldThrowAnErrorBecauseOfInvalidInput() throws Exception { + String transactionInput = """ + { + "externalId": 1, + "userId": "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11", + "merchantId": "9a1d6f88-0f93-4e58-9dd7-1c9d4a7d6c22", + "deviceId": "android-13-pixel-7", + "amount": MANY, + "currency": "BUCKS", + "initialized": 1735689600, + "merchantCategory": "GROCERIES", + "channel": "WEB", + "ipAddress": "192.168.1.10", + "country": "CALIFORNIA", + "cardFingerprint": "fp_9d82kdk29d" + } + """; + + mvc.perform(MockMvcRequestBuilders.post("/v1/transaction/validate") + .contentType(MediaType.APPLICATION_JSON) + .content(transactionInput)) + .andExpect(MockMvcResultMatchers.status().is4xxClientError()); + } + + @Test + public void shouldInsertEventInOutboxOnTransactionValidation() throws Exception { + utility.insertTransaction(); + + List events = eventOutboxJpaRepository.findAll(); + OutboxEvent outboxEvent = events.get(0); + + assertEquals(1, events.size()); + + System.out.println(outboxEvent); + Validation validation = om.readValue(outboxEvent.getPayload(), Validation.class); + + assertEquals(Decision.REVIEW, validation.getDecision()); + assertEquals(RiskLevel.MEDIUM, validation.getRiskLevel()); + } +} \ No newline at end of file diff --git a/src/test/java/com/neo/tx/integration/Utility.java b/src/test/java/com/neo/tx/integration/Utility.java new file mode 100644 index 0000000..faa7249 --- /dev/null +++ b/src/test/java/com/neo/tx/integration/Utility.java @@ -0,0 +1,45 @@ +package com.neo.tx.integration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.neo.tx.dto.TransactionRequestDto; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.service.TransactionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.concurrent.atomic.AtomicLong; + +@Component +public class Utility { + + private static final AtomicLong EXTERNAL_ID = new AtomicLong(1); + + @Autowired + private TransactionService transactionService; + + @Autowired + private ObjectMapper om; + + public ValidationResponseDto insertTransaction() throws Exception { + String formatted = String.format(""" + { + "externalId": %d, + "userId": "c1c3a7c0-1c3b-4a2b-9e5a-7b3c1b2a9f11", + "merchantId": "9a1d6f88-0f93-4e58-9dd7-1c9d4a7d6c22", + "deviceId": "android-13-pixel-7", + "amount": 7150.50, + "currency": "USD", + "initialized": 1735689600, + "merchantCategory": "GROCERIES", + "channel": "WEB", + "ipAddress": "192.168.1.10", + "country": "AGO", + "cardFingerprint": "fp_9d82kdk29d" + } + """, EXTERNAL_ID.getAndIncrement()); + + TransactionRequestDto transactionRequestDto = om.readValue(formatted, TransactionRequestDto.class); + + return transactionService.validateTransaction(transactionRequestDto, "default"); + } +} \ No newline at end of file diff --git a/src/test/java/com/neo/tx/integration/ValidationTest.java b/src/test/java/com/neo/tx/integration/ValidationTest.java new file mode 100644 index 0000000..2a476cf --- /dev/null +++ b/src/test/java/com/neo/tx/integration/ValidationTest.java @@ -0,0 +1,105 @@ +package com.neo.tx.integration; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.neo.tx.dto.ValidationResponseDto; +import com.neo.tx.enums.RiskLevel; +import com.neo.tx.repository.TransactionJpaRepository; +import com.neo.tx.repository.TransactionRepository; +import com.neo.tx.repository.ValidationJpaRepository; +import com.neo.tx.repository.ValidationRepository; +import com.neo.tx.service.TransactionService; +import com.neo.tx.service.ValidationService; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ValidationTest extends IntegrationTestConfig { + + @Autowired + Utility utility; + + @Autowired + MockMvc mvc; + + @Autowired + ObjectMapper om; + + @Autowired + TransactionJpaRepository transactionJpaRepository; + + @Autowired + ValidationJpaRepository validationJpaRepository; + + @Autowired + TransactionService transactionService; + + @Autowired + ValidationService validationService; + + @MockitoSpyBean + ValidationRepository validationRepository; + + @MockitoSpyBean + TransactionRepository transactionRepository; + + @Test + @Transactional + public void shouldFindAndReturnValidationById() throws Exception { + + System.out.println("transactionJpaRepository.findAll().size()"); + System.out.println(transactionJpaRepository.findAll().size()); + ValidationResponseDto validationResponseDto1 = utility.insertTransaction(); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/v1/validation/{id}", validationResponseDto1.id)) + .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()) + .andReturn(); + + ValidationResponseDto validationResponseDto = om.readValue(mvcResult.getResponse().getContentAsString(), ValidationResponseDto.class); + + assertNotNull(validationResponseDto); + } + + @Test + @Transactional + public void shouldThrowNotFoundExceptionAndReturnNotFoundResponse() throws Exception { + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/v1/validation/{id}", 1)) + .andExpect(MockMvcResultMatchers.status().is4xxClientError()) + .andReturn(); + + String expectedResponse = "{\"type\":\"about:blank\",\"title\":\"Validation not found\",\"status\":404,\"detail\":\"Validation with id [1] was not found\",\"instance\":\"/v1/validation/1\",\"code\":\"validation_not_found\",\"meta\":{}}"; + assertEquals(expectedResponse, mvcResult.getResponse().getContentAsString()); + } + + @Test + @Transactional + public void shouldReturnListOfValidations() throws Exception { + utility.insertTransaction(); + utility.insertTransaction(); + utility.insertTransaction(); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get( + "/v1/validation?riskLevel={riskLevel}", + RiskLevel.MEDIUM + )) + .andExpect(MockMvcResultMatchers.status().is2xxSuccessful()) + .andReturn(); + + JsonNode contentNode = om.readTree(mvcResult.getResponse().getContentAsString()).get("content"); + ValidationResponseDto[] validationResponses = om.treeToValue(contentNode, ValidationResponseDto[].class); + + assertEquals(3, validationResponses.length); + + for (int i = 0; i < validationResponses.length; i++) { + assertEquals(RiskLevel.MEDIUM, validationResponses[i].riskLevel); + } + } +} \ No newline at end of file diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml new file mode 100644 index 0000000..25260ac --- /dev/null +++ b/src/test/resources/application-test.yaml @@ -0,0 +1,41 @@ +spring: # Spring config + config: # config + activate: # activate + on-profile: test # on which profile (service run mode) this configuration should be loaded + + jpa: + hibernate: + ddl-auto: update + properties: + hibernate.format_sql: true + show-sql: false + + datasource: # URL and credentials are overridden dynamically from Testcontainers + url: jdbc:postgresql://localhost:5432/placeholder + username: placeholder + password: placeholder + + test: + database: + replace: none # don't use in-memory DB + +logging: # logging configuration (overrides logback logging levels) + level: # levels + root: WARN # default logging level for all unspecified loggers + com.neo.tx: WARN # minimum level for the application package (log.WARN(), log.WARN(), etc) + com.fasterxml.jackson: WARN # Jackson logs (JSON serialization/deserialization) + liquibase: WARN # Liquibase migration logs + org.hibernate.SQL: WARN # Hibernate SQL statement logging (prints SQL queries) + org.hibernate.orm.jdbc.bind: WARN # Hibernate parameter binding logger (shows bound values with TRACE) + org.springframework.web: WARN # Spring Web / MVC logs + org.springframework.web.servlet.DispatcherServlet: WARN # Specific logger for DispatcherServlet (main Spring MVC dispatcher. WARN prints request lifecycle messages) + +properties: + score.min: 0 + score.max: 100 + risk.threshold.low: 30 + risk.threshold.medium: 70 + risk.threshold.high: 100 + kafka.producer.enabled: false + kafka.consumer.enabled: false + kafka.consumer.validation.topic: transaction.validation.completed \ No newline at end of file