diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..625dd55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Compiled classes +*.class +# Gradle files +.gralde +# IntelliJ IDEA files +.idea +# Build files +build +# Log files +*.log +logs diff --git a/README.md b/README.md index 3e9c1a7..fd8d4f8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,39 @@ -# spring-telemetry-receiver -A RESTful microservice using spring boot, to receive telemetry data and publish to kafka using the spring-kafka library +# Spring Kafka based Telemetry Data Receiver +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 +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..9e1b8eb --- /dev/null +++ b/build.gradle @@ -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+') + } +} diff --git a/config/dev.properties b/config/dev.properties new file mode 100644 index 0000000..3345dbe --- /dev/null +++ b/config/dev.properties @@ -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 diff --git a/config/prod.properties b/config/prod.properties new file mode 100644 index 0000000..8a26359 --- /dev/null +++ b/config/prod.properties @@ -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 diff --git a/config/qa.properties b/config/qa.properties new file mode 100644 index 0000000..8a26359 --- /dev/null +++ b/config/qa.properties @@ -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 diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..94210e7 --- /dev/null +++ b/gradle.properties @@ -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 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..4d9ca16 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..4f906e0 --- /dev/null +++ b/gradlew @@ -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" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -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 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..a3f387d --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'spring-telemetry-receiver' diff --git a/src/main/java/com/barrelsofdata/springexamples/Application.java b/src/main/java/com/barrelsofdata/springexamples/Application.java new file mode 100644 index 0000000..b608a0d --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/Application.java @@ -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); + } +} diff --git a/src/main/java/com/barrelsofdata/springexamples/constants/EventType.java b/src/main/java/com/barrelsofdata/springexamples/constants/EventType.java new file mode 100644 index 0000000..82d5c41 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/constants/EventType.java @@ -0,0 +1,6 @@ +package com.barrelsofdata.springexamples.constants; + +public enum EventType { + LEFT_MOUSE_BUTTON_CLICK, + RIGHT_MOUSE_BUTTON_CLICK +} diff --git a/src/main/java/com/barrelsofdata/springexamples/controller/ApplicationErrorController.java b/src/main/java/com/barrelsofdata/springexamples/controller/ApplicationErrorController.java new file mode 100644 index 0000000..a8ee0b5 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/controller/ApplicationErrorController.java @@ -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 handleError(WebRequest request) { + Map 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"; + } +} diff --git a/src/main/java/com/barrelsofdata/springexamples/controller/TelemetryController.java b/src/main/java/com/barrelsofdata/springexamples/controller/TelemetryController.java new file mode 100644 index 0000000..ab5a491 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/controller/TelemetryController.java @@ -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 receiveTelemetry(@RequestBody @Valid EventRequestDto eventRequest) { + telemetryService.receiveTelemetry(eventRequest); + return new ResponseEntity<>(HttpStatus.CREATED.getReasonPhrase(), HttpStatus.CREATED); + } + +} diff --git a/src/main/java/com/barrelsofdata/springexamples/dto/EventDetailsDto.java b/src/main/java/com/barrelsofdata/springexamples/dto/EventDetailsDto.java new file mode 100644 index 0000000..f9187f3 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/dto/EventDetailsDto.java @@ -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; +} diff --git a/src/main/java/com/barrelsofdata/springexamples/dto/EventRequestDto.java b/src/main/java/com/barrelsofdata/springexamples/dto/EventRequestDto.java new file mode 100644 index 0000000..c6e689f --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/dto/EventRequestDto.java @@ -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; +} diff --git a/src/main/java/com/barrelsofdata/springexamples/dto/ExceptionDto.java b/src/main/java/com/barrelsofdata/springexamples/dto/ExceptionDto.java new file mode 100644 index 0000000..b6f2dfd --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/dto/ExceptionDto.java @@ -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; +} diff --git a/src/main/java/com/barrelsofdata/springexamples/exception/JsonConversionException.java b/src/main/java/com/barrelsofdata/springexamples/exception/JsonConversionException.java new file mode 100644 index 0000000..aad6e86 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/exception/JsonConversionException.java @@ -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); + } +} diff --git a/src/main/java/com/barrelsofdata/springexamples/producer/Kafka.java b/src/main/java/com/barrelsofdata/springexamples/producer/Kafka.java new file mode 100644 index 0000000..302f376 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/producer/Kafka.java @@ -0,0 +1,7 @@ +package com.barrelsofdata.springexamples.producer; + +import org.springframework.kafka.KafkaException; + +public interface Kafka { + void publish(String eventRequest) throws KafkaException; +} diff --git a/src/main/java/com/barrelsofdata/springexamples/producer/KafkaImpl.java b/src/main/java/com/barrelsofdata/springexamples/producer/KafkaImpl.java new file mode 100644 index 0000000..189f2ef --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/producer/KafkaImpl.java @@ -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 kafkaTemplate; + + @Value("${spring.kafka.producer.topic}") + private String topic; + + public void publish(String payload) throws KafkaException { + ListenableFuture> future = kafkaTemplate.send(topic, payload); // Blocks call if kafka broker isn't available/responding + future.addCallback(new ListenableFutureCallback>() { + @Override + public void onSuccess(SendResult 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); + } + }); + } +} diff --git a/src/main/java/com/barrelsofdata/springexamples/service/TelemetryService.java b/src/main/java/com/barrelsofdata/springexamples/service/TelemetryService.java new file mode 100644 index 0000000..0f011cf --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/service/TelemetryService.java @@ -0,0 +1,7 @@ +package com.barrelsofdata.springexamples.service; + +import com.barrelsofdata.springexamples.dto.EventRequestDto; + +public interface TelemetryService { + void receiveTelemetry(EventRequestDto eventRequest); +} diff --git a/src/main/java/com/barrelsofdata/springexamples/service/TelemetryServiceImpl.java b/src/main/java/com/barrelsofdata/springexamples/service/TelemetryServiceImpl.java new file mode 100644 index 0000000..1319872 --- /dev/null +++ b/src/main/java/com/barrelsofdata/springexamples/service/TelemetryServiceImpl.java @@ -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 + } + } +} diff --git a/src/test/java/com/barrelsofdata/springexamples/ApplicationIntegrationTest.java b/src/test/java/com/barrelsofdata/springexamples/ApplicationIntegrationTest.java new file mode 100644 index 0000000..2258e55 --- /dev/null +++ b/src/test/java/com/barrelsofdata/springexamples/ApplicationIntegrationTest.java @@ -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> records; + private KafkaMessageListenerContainer container; + + @BeforeEach + void setUp() { + Map consumerConfigs = new HashMap<>(KafkaTestUtils.consumerProps("consumer", "false", embeddedKafkaBroker)); + DefaultKafkaConsumerFactory consumerFactory = new DefaultKafkaConsumerFactory<>(consumerConfigs, new StringDeserializer(), new StringDeserializer()); + ContainerProperties containerProperties = new ContainerProperties(TOPIC); + container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); + records = new LinkedBlockingQueue<>(); + container.setupMessageListener((MessageListener) 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 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 singleRecord = records.poll(100, TimeUnit.MILLISECONDS); + Assertions.assertThat(singleRecord).isNotNull(); + Assertions.assertThat(singleRecord.key()).isNull(); + Assertions.assertThat(singleRecord.value()).isEqualTo(kafkaJson); + } + +} diff --git a/src/test/java/com/barrelsofdata/springexamples/controller/TelemetryControllerTest.java b/src/test/java/com/barrelsofdata/springexamples/controller/TelemetryControllerTest.java new file mode 100644 index 0000000..38882ad --- /dev/null +++ b/src/test/java/com/barrelsofdata/springexamples/controller/TelemetryControllerTest.java @@ -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() + ); + } +} diff --git a/src/test/java/com/barrelsofdata/springexamples/producer/KafkaTest.java b/src/test/java/com/barrelsofdata/springexamples/producer/KafkaTest.java new file mode 100644 index 0000000..0e823a7 --- /dev/null +++ b/src/test/java/com/barrelsofdata/springexamples/producer/KafkaTest.java @@ -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 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)); + } + +} diff --git a/src/test/java/com/barrelsofdata/springexamples/service/TelemetryServiceTest.java b/src/test/java/com/barrelsofdata/springexamples/service/TelemetryServiceTest.java new file mode 100644 index 0000000..cea08e2 --- /dev/null +++ b/src/test/java/com/barrelsofdata/springexamples/service/TelemetryServiceTest.java @@ -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()); + } + +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..293c951 --- /dev/null +++ b/src/test/resources/application.properties @@ -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