Initial commit
This commit is contained in:
parent
ba76ff2463
commit
82669ccb12
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Compiled classes
|
||||||
|
*.class
|
||||||
|
# Gradle files
|
||||||
|
.gralde
|
||||||
|
# IntelliJ IDEA files
|
||||||
|
.idea
|
||||||
|
# Build files
|
||||||
|
build
|
||||||
|
# Log files
|
||||||
|
*.log
|
||||||
|
logs
|
41
README.md
41
README.md
@ -1,2 +1,39 @@
|
|||||||
# spring-telemetry-receiver
|
# Spring Kafka based Telemetry Data Receiver
|
||||||
A RESTful microservice using spring boot, to receive telemetry data and publish to kafka using the spring-kafka library
|
The project is built on spring boot using spring kafka, to receive telemetry data on a REST endpoint and publish the same to a Kafka topic. The related blog post can be found at [https://www.barrelsofdata.com/spring-boot-based-telemetry-data-receiver-api-spring-kafka-producer/](https://www.barrelsofdata.com/spring-boot-based-telemetry-data-receiver-api-spring-kafka-producer/)
|
||||||
|
|
||||||
|
## Build instructions
|
||||||
|
From the root of the project execute the below commands
|
||||||
|
- To clear all compiled classes, build and log directories
|
||||||
|
```shell script
|
||||||
|
./gradlew clean
|
||||||
|
```
|
||||||
|
- To run tests
|
||||||
|
```shell script
|
||||||
|
./gradlew test
|
||||||
|
```
|
||||||
|
- To build jar
|
||||||
|
```shell script
|
||||||
|
./gradlew bootJar
|
||||||
|
```
|
||||||
|
- Build OCI compliant image
|
||||||
|
```shell script
|
||||||
|
./gradlew bootBuildImage
|
||||||
|
```
|
||||||
|
- All combined
|
||||||
|
```shell script
|
||||||
|
./gradlew clean test bootBuildImage
|
||||||
|
```
|
||||||
|
- Run from IDE
|
||||||
|
```shell script
|
||||||
|
./gradlew bootRun -PjvmArgs="-D--spring.config.location=config/dev.properties"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run native
|
||||||
|
```shell script
|
||||||
|
java -jar build/libs/spring-telemetry-receiver-1.0.jar --spring.config.location=config/dev.properties
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run as docker container
|
||||||
|
```shell script
|
||||||
|
docker run -itd --rm --network host --mount type=bind,source=$(pwd)/config/dev.properties,target=/application.properties,readonly -e JAVA_OPTS=-D--spring.config.location=/application.properties --name spring-telemetry-server spring-telemetry-receiver:1.0
|
||||||
|
```
|
47
build.gradle
Normal file
47
build.gradle
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
plugins {
|
||||||
|
id "java"
|
||||||
|
id "org.springframework.boot" version "${springBootVersion}"
|
||||||
|
}
|
||||||
|
|
||||||
|
group "${projectGroup}"
|
||||||
|
version "${projectVersion}"
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation group: "org.springframework.boot", name:"spring-boot-starter-web", version:"${springBootVersion}"
|
||||||
|
implementation group: "org.springframework.boot", name:"spring-boot-starter-validation", version:"${springBootVersion}"
|
||||||
|
implementation group: "org.springframework.kafka", name: "spring-kafka", version: "${springKafkaVersion}"
|
||||||
|
|
||||||
|
implementation group: "org.projectlombok", name: "lombok", version: "${lombokVersion}"
|
||||||
|
annotationProcessor group: "org.projectlombok", name: "lombok", version: "${lombokVersion}"
|
||||||
|
|
||||||
|
testImplementation group: "org.springframework.boot", name: "spring-boot-starter-test", version:"${springBootVersion}"
|
||||||
|
testImplementation group: "org.springframework.kafka", name: "spring-kafka-test", version:"${springKafkaVersion}"
|
||||||
|
}
|
||||||
|
|
||||||
|
springBoot {
|
||||||
|
mainClass.set("${applicationClass}")
|
||||||
|
}
|
||||||
|
|
||||||
|
compileJava {
|
||||||
|
options.encoding = "UTF-8"
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
|
bootBuildImage {
|
||||||
|
imageName = "${rootProject.name}:${projectVersion}"
|
||||||
|
}
|
||||||
|
|
||||||
|
bootRun {
|
||||||
|
if ( project.hasProperty('jvmArgs') ) {
|
||||||
|
jvmArgs project.jvmArgs.split('\\s+')
|
||||||
|
}
|
||||||
|
}
|
13
config/dev.properties
Normal file
13
config/dev.properties
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
server.port=9080
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
debug=false
|
||||||
|
|
||||||
|
spring.kafka.bootstrap-servers=localhost:9092
|
||||||
|
spring.kafka.producer.topic=telemetry
|
||||||
|
spring.kafka.properties.retries=1
|
||||||
|
spring.kafka.properties.max.block.ms=500
|
||||||
|
|
||||||
|
logging.level.org.springframework=WARN
|
||||||
|
logging.level.org.apache.catalina=WARN
|
||||||
|
logging.level.org.apache.kafka=INFO
|
||||||
|
logging.level.com.barrelsofdata=DEBUG
|
12
config/prod.properties
Normal file
12
config/prod.properties
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
server.port=9080
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
debug=false
|
||||||
|
|
||||||
|
spring.kafka.bootstrap-servers=localhost:9092
|
||||||
|
spring.kafka.producer.topic=telemetry
|
||||||
|
spring.kafka.properties.retries=1
|
||||||
|
spring.kafka.properties.max.block.ms=500
|
||||||
|
|
||||||
|
logging.level.org.springframework=WARN
|
||||||
|
logging.level.org.apache.catalina=WARN
|
||||||
|
logging.level.org.apache.kafka=WARN
|
12
config/qa.properties
Normal file
12
config/qa.properties
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
server.port=9080
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
debug=false
|
||||||
|
|
||||||
|
spring.kafka.bootstrap-servers=localhost:9092
|
||||||
|
spring.kafka.producer.topic=telemetry
|
||||||
|
spring.kafka.properties.retries=1
|
||||||
|
spring.kafka.properties.max.block.ms=500
|
||||||
|
|
||||||
|
logging.level.org.springframework=WARN
|
||||||
|
logging.level.org.apache.catalina=WARN
|
||||||
|
logging.level.org.apache.kafka=WARN
|
9
gradle.properties
Normal file
9
gradle.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
springBootVersion=2.4.0
|
||||||
|
springKafkaVersion=2.6.3
|
||||||
|
lombokVersion=1.18.16
|
||||||
|
|
||||||
|
applicationClass=com.barrelsofdata.springexamples.Application
|
||||||
|
projectGroup=com.barrelsofdata.springexamples
|
||||||
|
projectVersion=1.0
|
||||||
|
|
||||||
|
org.gradle.daemon=false
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
185
gradlew
vendored
Executable file
185
gradlew
vendored
Executable file
@ -0,0 +1,185 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright 2015 the original author or authors.
|
||||||
|
#
|
||||||
|
# Licensed 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
|
||||||
|
#
|
||||||
|
# https://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.
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
##
|
||||||
|
## Gradle start up script for UN*X
|
||||||
|
##
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
PRG="$0"
|
||||||
|
# Need this for relative symlinks.
|
||||||
|
while [ -h "$PRG" ] ; do
|
||||||
|
ls=`ls -ld "$PRG"`
|
||||||
|
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||||
|
if expr "$link" : '/.*' > /dev/null; then
|
||||||
|
PRG="$link"
|
||||||
|
else
|
||||||
|
PRG=`dirname "$PRG"`"/$link"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
SAVED="`pwd`"
|
||||||
|
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||||
|
APP_HOME="`pwd -P`"
|
||||||
|
cd "$SAVED" >/dev/null
|
||||||
|
|
||||||
|
APP_NAME="Gradle"
|
||||||
|
APP_BASE_NAME=`basename "$0"`
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD="maximum"
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN* )
|
||||||
|
cygwin=true
|
||||||
|
;;
|
||||||
|
Darwin* )
|
||||||
|
darwin=true
|
||||||
|
;;
|
||||||
|
MINGW* )
|
||||||
|
msys=true
|
||||||
|
;;
|
||||||
|
NONSTOP* )
|
||||||
|
nonstop=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
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"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="java"
|
||||||
|
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||||
|
MAX_FD_LIMIT=`ulimit -H -n`
|
||||||
|
if [ $? -eq 0 ] ; then
|
||||||
|
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||||
|
MAX_FD="$MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
ulimit -n $MAX_FD
|
||||||
|
if [ $? -ne 0 ] ; then
|
||||||
|
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Darwin, add options to specify how the application appears in the dock
|
||||||
|
if $darwin; then
|
||||||
|
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||||
|
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||||
|
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||||
|
|
||||||
|
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||||
|
|
||||||
|
# We build the pattern for arguments to be converted via cygpath
|
||||||
|
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||||
|
SEP=""
|
||||||
|
for dir in $ROOTDIRSRAW ; do
|
||||||
|
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||||
|
SEP="|"
|
||||||
|
done
|
||||||
|
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||||
|
# Add a user-defined pattern to the cygpath arguments
|
||||||
|
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||||
|
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||||
|
fi
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
i=0
|
||||||
|
for arg in "$@" ; do
|
||||||
|
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||||
|
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||||
|
|
||||||
|
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||||
|
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||||
|
else
|
||||||
|
eval `echo args$i`="\"$arg\""
|
||||||
|
fi
|
||||||
|
i=`expr $i + 1`
|
||||||
|
done
|
||||||
|
case $i in
|
||||||
|
0) set -- ;;
|
||||||
|
1) set -- "$args0" ;;
|
||||||
|
2) set -- "$args0" "$args1" ;;
|
||||||
|
3) set -- "$args0" "$args1" "$args2" ;;
|
||||||
|
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||||
|
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||||
|
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||||
|
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||||
|
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||||
|
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Escape application args
|
||||||
|
save () {
|
||||||
|
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||||
|
echo " "
|
||||||
|
}
|
||||||
|
APP_ARGS=`save "$@"`
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||||
|
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
89
gradlew.bat
vendored
Normal file
89
gradlew.bat
vendored
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%" == "" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if "%ERRORLEVEL%" == "0" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'spring-telemetry-receiver'
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.barrelsofdata.springexamples;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@ComponentScan(basePackages = "com.barrelsofdata.springexamples")
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.barrelsofdata.springexamples.constants;
|
||||||
|
|
||||||
|
public enum EventType {
|
||||||
|
LEFT_MOUSE_BUTTON_CLICK,
|
||||||
|
RIGHT_MOUSE_BUTTON_CLICK
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.barrelsofdata.springexamples.controller;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.ExceptionDto;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.web.error.ErrorAttributeOptions;
|
||||||
|
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||||
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
|
import javax.servlet.RequestDispatcher;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class ApplicationErrorController implements ErrorController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ErrorAttributes errorAttributes;
|
||||||
|
|
||||||
|
@RequestMapping("/error")
|
||||||
|
public ResponseEntity<ExceptionDto> handleError(WebRequest request) {
|
||||||
|
Map<String, Object> requestErrors = errorAttributes.getErrorAttributes(request, ErrorAttributeOptions.defaults());
|
||||||
|
Object statusObject = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE, RequestAttributes.SCOPE_REQUEST);
|
||||||
|
StringBuilder errorMessage = new StringBuilder();
|
||||||
|
ExceptionDto.ExceptionDtoBuilder exceptionBuilder = ExceptionDto.builder();
|
||||||
|
if(requestErrors.containsKey("timestamp")) exceptionBuilder.timestamp((Date) requestErrors.get("timestamp"));
|
||||||
|
if(requestErrors.containsKey("error")) errorMessage.append(requestErrors.get("error"));
|
||||||
|
if(requestErrors.containsKey("message")) errorMessage.append((String) requestErrors.get("message"));
|
||||||
|
exceptionBuilder.error(errorMessage.toString());
|
||||||
|
HttpStatus status = statusObject != null ? HttpStatus.resolve(Integer.parseInt(statusObject.toString())) : HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
ExceptionDto exception = exceptionBuilder.build();
|
||||||
|
return new ResponseEntity<>(exception, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings( "deprecation" )
|
||||||
|
public String getErrorPath() {
|
||||||
|
return "/error";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.barrelsofdata.springexamples.controller;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
import com.barrelsofdata.springexamples.service.TelemetryService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class TelemetryController {
|
||||||
|
|
||||||
|
@Autowired private TelemetryService telemetryService;
|
||||||
|
|
||||||
|
@PutMapping(
|
||||||
|
value = "/telemetry",
|
||||||
|
consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||||
|
produces = MediaType.APPLICATION_JSON_VALUE
|
||||||
|
)
|
||||||
|
public ResponseEntity<String> receiveTelemetry(@RequestBody @Valid EventRequestDto eventRequest) {
|
||||||
|
telemetryService.receiveTelemetry(eventRequest);
|
||||||
|
return new ResponseEntity<>(HttpStatus.CREATED.getReasonPhrase(), HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.barrelsofdata.springexamples.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
public class EventDetailsDto {
|
||||||
|
@JsonAlias("w")
|
||||||
|
private Integer width;
|
||||||
|
@JsonAlias("h")
|
||||||
|
private Integer height;
|
||||||
|
@JsonAlias("x")
|
||||||
|
private Float x;
|
||||||
|
@JsonAlias("y")
|
||||||
|
private Float y;
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.barrelsofdata.springexamples.dto;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.constants.EventType;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
public class EventRequestDto {
|
||||||
|
@NotNull
|
||||||
|
@JsonAlias("ts")
|
||||||
|
private Timestamp timestamp;
|
||||||
|
@NotNull
|
||||||
|
@JsonAlias("id")
|
||||||
|
private Integer id;
|
||||||
|
@NotNull
|
||||||
|
@JsonAlias("ty")
|
||||||
|
private EventType type;
|
||||||
|
@JsonAlias("pl")
|
||||||
|
private EventDetailsDto payload;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.barrelsofdata.springexamples.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Builder;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
public class ExceptionDto {
|
||||||
|
@JsonProperty("timestamp")
|
||||||
|
private Date timestamp;
|
||||||
|
@JsonProperty("error")
|
||||||
|
private String error;
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.barrelsofdata.springexamples.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
|
||||||
|
public class JsonConversionException extends RuntimeException {
|
||||||
|
public JsonConversionException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
public JsonConversionException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
public JsonConversionException(String message, Exception e) {
|
||||||
|
super(message, e);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.barrelsofdata.springexamples.producer;
|
||||||
|
|
||||||
|
import org.springframework.kafka.KafkaException;
|
||||||
|
|
||||||
|
public interface Kafka {
|
||||||
|
void publish(String eventRequest) throws KafkaException;
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.barrelsofdata.springexamples.producer;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.kafka.KafkaException;
|
||||||
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
|
import org.springframework.kafka.support.SendResult;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.concurrent.ListenableFuture;
|
||||||
|
import org.springframework.util.concurrent.ListenableFutureCallback;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class KafkaImpl implements Kafka {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Kafka.class);
|
||||||
|
|
||||||
|
@Autowired private KafkaTemplate<String, String> kafkaTemplate;
|
||||||
|
|
||||||
|
@Value("${spring.kafka.producer.topic}")
|
||||||
|
private String topic;
|
||||||
|
|
||||||
|
public void publish(String payload) throws KafkaException {
|
||||||
|
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, payload); // Blocks call if kafka broker isn't available/responding
|
||||||
|
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(SendResult<String, String> result) {
|
||||||
|
logger.info("Message published to Kafka partition {} with offset {}", result.getRecordMetadata().partition(), result.getRecordMetadata().offset());
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(Throwable ex) {
|
||||||
|
logger.error("Unable to publish message {}", payload, ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.barrelsofdata.springexamples.service;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
|
||||||
|
public interface TelemetryService {
|
||||||
|
void receiveTelemetry(EventRequestDto eventRequest);
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.barrelsofdata.springexamples.service;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
import com.barrelsofdata.springexamples.exception.JsonConversionException;
|
||||||
|
import com.barrelsofdata.springexamples.producer.Kafka;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.kafka.KafkaException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TelemetryServiceImpl implements TelemetryService {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(TelemetryService.class);
|
||||||
|
|
||||||
|
@Autowired private Kafka producer;
|
||||||
|
|
||||||
|
@Autowired private ObjectMapper jsonMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveTelemetry(EventRequestDto eventRequest) {
|
||||||
|
try {
|
||||||
|
String payload = jsonMapper.writeValueAsString(eventRequest);
|
||||||
|
producer.publish(payload);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
logger.error("Unable to convert message to json {}", eventRequest);
|
||||||
|
throw new JsonConversionException("Failed json conversion");
|
||||||
|
} catch (KafkaException e) {
|
||||||
|
logger.error("Kafka exception for request {}", eventRequest);
|
||||||
|
// TODO: Handle what you want to do with the data here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,138 @@
|
|||||||
|
package com.barrelsofdata.springexamples;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.service.TelemetryService;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
||||||
|
import org.springframework.kafka.listener.ContainerProperties;
|
||||||
|
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
|
||||||
|
import org.springframework.kafka.listener.MessageListener;
|
||||||
|
import org.springframework.kafka.test.EmbeddedKafkaBroker;
|
||||||
|
import org.springframework.kafka.test.context.EmbeddedKafka;
|
||||||
|
import org.springframework.kafka.test.utils.ContainerTestUtils;
|
||||||
|
import org.springframework.kafka.test.utils.KafkaTestUtils;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@EmbeddedKafka
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class ApplicationIntegrationTest {
|
||||||
|
private int NUMBER_OF_BROKERS = 2;
|
||||||
|
private boolean CONTROLLER_SHUTDOWN = false;
|
||||||
|
private int NUMBER_OF_PARTITIONS = 2;
|
||||||
|
@Value("${spring.kafka.producer.topic}")
|
||||||
|
private String TOPIC;
|
||||||
|
|
||||||
|
@Autowired private TelemetryService telemetryService;
|
||||||
|
|
||||||
|
@Autowired private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmbeddedKafkaBroker embeddedKafkaBroker = new EmbeddedKafkaBroker(NUMBER_OF_BROKERS, CONTROLLER_SHUTDOWN, NUMBER_OF_PARTITIONS, TOPIC);
|
||||||
|
|
||||||
|
private BlockingQueue<ConsumerRecord<String, String>> records;
|
||||||
|
private KafkaMessageListenerContainer<String, String> container;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
Map<String, Object> consumerConfigs = new HashMap<>(KafkaTestUtils.consumerProps("consumer", "false", embeddedKafkaBroker));
|
||||||
|
DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerConfigs, new StringDeserializer(), new StringDeserializer());
|
||||||
|
ContainerProperties containerProperties = new ContainerProperties(TOPIC);
|
||||||
|
container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);
|
||||||
|
records = new LinkedBlockingQueue<>();
|
||||||
|
container.setupMessageListener((MessageListener<String, String>) records::add);
|
||||||
|
container.start();
|
||||||
|
ContainerTestUtils.waitForAssignment(container, embeddedKafkaBroker.getPartitionsPerTopic());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
container.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Integration: API request success")
|
||||||
|
@CsvSource(value = {
|
||||||
|
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"LEFT_MOUSE_BUTTON_CLICK\",\"payload\":{\"width\":213,\"height\":124,\"x\":1000.0,\"y\":5000.0}}",
|
||||||
|
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"RIGHT_MOUSE_BUTTON_CLICK\",\"payload\":null}"}
|
||||||
|
, delimiter = ';')
|
||||||
|
public void success(String inputJson, String kafkaJson) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(inputJson)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isCreated()
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().contentType(MediaType.APPLICATION_JSON)
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().string(HttpStatus.CREATED.getReasonPhrase())
|
||||||
|
);
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
ConsumerRecord<String, String> singleRecord = records.poll(100, TimeUnit.MILLISECONDS);
|
||||||
|
Assertions.assertThat(singleRecord).isNotNull();
|
||||||
|
Assertions.assertThat(singleRecord.key()).isNull();
|
||||||
|
Assertions.assertThat(singleRecord.value()).isEqualTo(kafkaJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Integration: API request success")
|
||||||
|
@CsvSource(value = {
|
||||||
|
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"LEFT_MOUSE_BUTTON_CLICK\",\"payload\":{\"width\":213,\"height\":124,\"x\":1000.0,\"y\":5000.0}}",
|
||||||
|
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"RIGHT_MOUSE_BUTTON_CLICK\",\"payload\":null}"}
|
||||||
|
, delimiter = ';')
|
||||||
|
public void unsupportedMedia(String inputJson, String kafkaJson) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(inputJson)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isCreated()
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().contentType(MediaType.APPLICATION_JSON)
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().string(HttpStatus.CREATED.getReasonPhrase())
|
||||||
|
);
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
ConsumerRecord<String, String> singleRecord = records.poll(100, TimeUnit.MILLISECONDS);
|
||||||
|
Assertions.assertThat(singleRecord).isNotNull();
|
||||||
|
Assertions.assertThat(singleRecord.key()).isNull();
|
||||||
|
Assertions.assertThat(singleRecord.value()).isEqualTo(kafkaJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
package com.barrelsofdata.springexamples.controller;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
import com.barrelsofdata.springexamples.exception.JsonConversionException;
|
||||||
|
import com.barrelsofdata.springexamples.producer.Kafka;
|
||||||
|
import com.barrelsofdata.springexamples.service.TelemetryService;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
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.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class TelemetryControllerTest {
|
||||||
|
@MockBean private TelemetryService telemetryService;
|
||||||
|
@MockBean private Kafka kafka;
|
||||||
|
@Autowired private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Success API request")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void success(String json) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(json)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isCreated()
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().contentType(MediaType.APPLICATION_JSON)
|
||||||
|
)
|
||||||
|
.andExpect(
|
||||||
|
content().string(HttpStatus.CREATED.getReasonPhrase())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Json conversion fail API response")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void failBadJson(String json) throws Exception {
|
||||||
|
String expectedErrorMessage = "Failed json conversion";
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Mockito.doThrow(new JsonConversionException(expectedErrorMessage)).when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(json)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isBadRequest()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Unsupported media type")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}}"})
|
||||||
|
public void unsupportedMediaType(String json) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.TEXT_PLAIN)
|
||||||
|
.content(json)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isUnsupportedMediaType()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Missing required field or wrong value for type, bad request")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void missingRequiredField(String json) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
put("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(json)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isBadRequest()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Method not allowed for non-PUT requests")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void methodNotAllowed(String json) throws Exception {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
post("/telemetry")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(json)
|
||||||
|
.headers(headers))
|
||||||
|
.andExpect(
|
||||||
|
status().isMethodNotAllowed()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.barrelsofdata.springexamples.producer;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.kafka.common.KafkaException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
|
import org.springframework.kafka.support.SendResult;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.util.concurrent.ListenableFuture;
|
||||||
|
import org.springframework.util.concurrent.SettableListenableFuture;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
public class KafkaTest {
|
||||||
|
@MockBean private KafkaTemplate<String, String> kafkaTemplate;
|
||||||
|
|
||||||
|
@Autowired @InjectMocks private KafkaImpl producer;
|
||||||
|
|
||||||
|
@Autowired private ObjectMapper mapper;
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Check successful send")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void successSend(String json) throws JsonProcessingException {
|
||||||
|
EventRequestDto eventRequestDto = mapper.readValue(json, EventRequestDto.class);
|
||||||
|
String kafkaPayload = mapper.writeValueAsString(eventRequestDto);
|
||||||
|
Mockito.doReturn(new SettableListenableFuture<>()).when(kafkaTemplate).send(any(String.class), any(String.class));
|
||||||
|
Assertions.assertDoesNotThrow(() -> producer.publish(kafkaPayload));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Check failed send")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void failedSend(String json) throws JsonProcessingException, InterruptedException {
|
||||||
|
EventRequestDto eventRequestDto = mapper.readValue(json, EventRequestDto.class);
|
||||||
|
String kafkaPayload = mapper.writeValueAsString(eventRequestDto);
|
||||||
|
Mockito.doThrow(KafkaException.class).when(kafkaTemplate).send(any(String.class), any(String.class));
|
||||||
|
Assertions.assertThrows(KafkaException.class, () -> producer.publish(kafkaPayload));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.barrelsofdata.springexamples.service;
|
||||||
|
|
||||||
|
import com.barrelsofdata.springexamples.dto.EventRequestDto;
|
||||||
|
import com.barrelsofdata.springexamples.exception.JsonConversionException;
|
||||||
|
import com.barrelsofdata.springexamples.producer.Kafka;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
public class TelemetryServiceTest {
|
||||||
|
@MockBean private Kafka kafka;
|
||||||
|
@MockBean private ObjectMapper mapper;
|
||||||
|
@Autowired @InjectMocks private TelemetryServiceImpl telemetryService;
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Successful publish")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
|
||||||
|
public void successPublish(String json) throws JsonProcessingException {
|
||||||
|
EventRequestDto eventRequestDto = new ObjectMapper().readValue(json, EventRequestDto.class);
|
||||||
|
telemetryService.receiveTelemetry(eventRequestDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Json conversion failure")
|
||||||
|
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}"})
|
||||||
|
public void failPublish(String json) throws JsonProcessingException {
|
||||||
|
EventRequestDto eventRequestDto = new ObjectMapper().readValue(json, EventRequestDto.class);
|
||||||
|
Mockito.doThrow(JsonProcessingException.class).when(mapper).writeValueAsString(any(EventRequestDto.class));
|
||||||
|
JsonConversionException exception = Assertions.assertThrows(JsonConversionException.class, () -> telemetryService.receiveTelemetry(eventRequestDto));
|
||||||
|
Assertions.assertEquals("Failed json conversion", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
src/test/resources/application.properties
Normal file
13
src/test/resources/application.properties
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
server.port=9080
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
debug=false
|
||||||
|
|
||||||
|
spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}
|
||||||
|
spring.kafka.producer.topic=telemetryTest
|
||||||
|
spring.kafka.properties.retries=1
|
||||||
|
spring.kafka.properties.max.block.ms=500
|
||||||
|
|
||||||
|
logging.level.org.springframework=WARN
|
||||||
|
logging.level.org.apache.catalina=WARN
|
||||||
|
logging.level.org.apache.kafka=INFO
|
||||||
|
logging.level.com.barrelsofdata=DEBUG
|
Loading…
Reference in New Issue
Block a user