├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main ├── kotlin │ └── eu │ │ └── wojciechzurek │ │ └── example │ │ └── ExampleApplication.kt └── resources │ └── application.properties └── test └── kotlin └── eu └── wojciechzurek └── example └── ExampleApplicationTests.kt /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | /build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | /out/ 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | anguage: java 2 | jdk: 3 | - oraclejdk8 4 | services: 5 | - docker 6 | before_install: 7 | - docker pull apacheignite/ignite 8 | - docker run -d -p 47100:47100 apacheignite/ignite 9 | - docker ps -a 10 | script: sh gradlew cleanTest check -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Wojciech Żurek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kotlin-spring-boot-apache-ignite-example 2 | kotlin-spring-boot-apache-ignite-example 3 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.springframework.boot' version '2.1.3.RELEASE' 3 | id 'org.jetbrains.kotlin.jvm' version '1.2.71' 4 | id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71' 5 | } 6 | 7 | ext{ 8 | igniteVersion = "2.7.0" 9 | reactorTestVersion = "3.2.6.RELEASE" 10 | } 11 | 12 | apply plugin: 'io.spring.dependency-management' 13 | 14 | group = 'eu.wojciechzurek' 15 | version = '0.0.1-SNAPSHOT' 16 | sourceCompatibility = '1.8' 17 | 18 | repositories { 19 | mavenCentral() 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.boot:spring-boot-starter-webflux' 24 | implementation "org.apache.ignite:ignite-spring-data:$igniteVersion" 25 | implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' 26 | implementation 'org.jetbrains.kotlin:kotlin-reflect' 27 | implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' 28 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 29 | testImplementation "io.projectreactor:reactor-test:$reactorTestVersion" 30 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0' 31 | } 32 | 33 | compileKotlin { 34 | kotlinOptions { 35 | freeCompilerArgs = ['-Xjsr305=strict'] 36 | jvmTarget = '1.8' 37 | } 38 | } 39 | 40 | compileTestKotlin { 41 | kotlinOptions { 42 | freeCompilerArgs = ['-Xjsr305=strict'] 43 | jvmTarget = '1.8' 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wojciech-zurek/kotlin-spring-boot-apache-ignite-example/510ed9edd4a74de75e68aedb8f1139a1056204e4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Mar 08 18:22:03 CET 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS='"-Xmx64m"' 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | } 5 | } 6 | rootProject.name = 'kotlin-spring-boot-apache-ignite-example' 7 | -------------------------------------------------------------------------------- /src/main/kotlin/eu/wojciechzurek/example/ExampleApplication.kt: -------------------------------------------------------------------------------- 1 | package eu.wojciechzurek.example 2 | 3 | import org.apache.ignite.Ignite 4 | import org.apache.ignite.Ignition 5 | import org.apache.ignite.cache.query.annotations.QuerySqlField 6 | import org.apache.ignite.configuration.CacheConfiguration 7 | import org.apache.ignite.configuration.IgniteConfiguration 8 | import org.slf4j.LoggerFactory 9 | import org.springframework.boot.CommandLineRunner 10 | import org.springframework.boot.autoconfigure.SpringBootApplication 11 | import org.springframework.boot.runApplication 12 | import org.springframework.context.support.beans 13 | import org.springframework.web.reactive.function.server.ServerRequest 14 | import org.springframework.web.reactive.function.server.ServerResponse.* 15 | import org.springframework.web.reactive.function.server.router 16 | import reactor.core.publisher.Flux 17 | import reactor.core.publisher.Mono 18 | import reactor.core.publisher.MonoSink 19 | import java.net.URI 20 | import java.util.* 21 | import javax.cache.Cache 22 | 23 | private val log = LoggerFactory.getLogger(ExampleApplication::class.java) 24 | const val CACHE_NAME = "exampleCache" 25 | 26 | @SpringBootApplication 27 | class ExampleApplication 28 | 29 | fun main(args: Array) { 30 | runApplication(*args) { 31 | addInitializers(beans) 32 | } 33 | } 34 | 35 | val beans = beans { 36 | bean("igniteInstance") { ignite() } 37 | bean { UserRepository.get(ref()) } 38 | bean() 39 | bean { routes(ref()) } 40 | bean { runner(ref()) } 41 | } 42 | 43 | fun ignite(): Ignite { 44 | val config = IgniteConfiguration() 45 | 46 | val cache = CacheConfiguration(CACHE_NAME) 47 | cache.setIndexedTypes(String::class.java, User::class.java) 48 | 49 | config.setCacheConfiguration(cache) 50 | return Ignition.start(config) 51 | } 52 | 53 | fun runner(userRepository: UserRepository) = CommandLineRunner { userRepository.init() } 54 | 55 | fun routes(userHandler: UserHandler) = router { 56 | "/api".nest { 57 | GET("/users", userHandler::findAll) 58 | POST("/users", userHandler::new) 59 | GET("/users/{id}", userHandler::findById) 60 | PUT("/users/{id}", userHandler::update) 61 | DELETE("/users/{id}", userHandler::delete) 62 | } 63 | } 64 | 65 | class UserHandler(private val userRepository: UserRepository) { 66 | 67 | fun findAll(request: ServerRequest) = ok().body(userRepository.findAll(), User::class.java) 68 | 69 | fun findById(request: ServerRequest) = userRepository 70 | .findById(request.pathVariable("id")) 71 | .flatMap { ok().syncBody(it) } 72 | .switchIfEmpty(notFound().build()) 73 | 74 | fun new(request: ServerRequest) = request 75 | .bodyToMono(UserRequest::class.java) 76 | .map { User(login = it.login, age = it.age) } 77 | .flatMap { userRepository.save(it) } 78 | .flatMap { created(URI.create("/api/users/${it.id}")).syncBody(it) } 79 | 80 | fun update(request: ServerRequest) = request 81 | .bodyToMono(UserRequest::class.java) 82 | .zipWith(userRepository.findById(request.pathVariable("id"))) 83 | .map { User(it.t2.id, it.t1.login, it.t1.age) } 84 | .flatMap { userRepository.save(it) } 85 | .flatMap { ok().syncBody(it) } 86 | .switchIfEmpty(notFound().build()) 87 | 88 | fun delete(request: ServerRequest) = userRepository 89 | .findById(request.pathVariable("id")) 90 | .flatMap { userRepository.delete(it).then(noContent().build()) } 91 | .switchIfEmpty(notFound().build()) 92 | } 93 | 94 | data class UserRequest( 95 | val login: String, 96 | val age: Int 97 | ) 98 | 99 | data class User( 100 | @QuerySqlField(index = true) 101 | val id: String = UUID.randomUUID().toString(), 102 | val login: String, 103 | val age: Int 104 | ) 105 | 106 | abstract class RepositoryProvider { 107 | var instance: T? = null 108 | var mock: T? = null 109 | abstract fun create(ignite: Ignite): T 110 | fun get(ignite: Ignite): T = mock ?: instance ?: create(ignite) 111 | .also { instance = it } 112 | } 113 | 114 | interface UserRepository { 115 | 116 | companion object : RepositoryProvider() { 117 | override fun create(ignite: Ignite) = UserRepositoryImpl(ignite) 118 | } 119 | 120 | fun init() 121 | fun findById(id: String): Mono 122 | fun save(user: User): Mono 123 | fun delete(user: User): Mono 124 | fun findAll(): Flux 125 | } 126 | 127 | class UserRepositoryImpl(ignite: Ignite) : UserRepository { 128 | 129 | private val cache = ignite.cache(CACHE_NAME) 130 | 131 | override fun init() { 132 | cache.clear() 133 | Flux.just( 134 | User(id = "e2ac4fba-ce48-42fe-a0b9-c7555b65154f", login = "test", age = 10), 135 | User(id = "10b86e02-109d-488a-8e25-8bb63a7c4f1c", login = "wojtek", age = 18), 136 | User(id = "4ca315c0-e214-4b62-9c2a-71d78e29412e", login = "admin", age = 60) 137 | ).map { 138 | cache.put(it.id, it) 139 | it.id 140 | }.map { 141 | cache.get(it) 142 | }.subscribe { 143 | log.info(it.toString()) 144 | } 145 | } 146 | 147 | override fun findAll() = Flux.create { sink -> 148 | cache.asSequence().forEach { 149 | sink.next(it.value) 150 | } 151 | sink.complete() 152 | } 153 | 154 | override fun findById(id: String) = Mono.create { sink -> 155 | cache.getAsync(id).listen { future -> 156 | future.get().let { 157 | when (it) { 158 | null -> sink.success() 159 | else -> sink.success(it) 160 | } 161 | } 162 | } 163 | } 164 | 165 | override fun save(user: User) = Mono.create { sink -> 166 | cache.putAsync(user.id, user).listen { future -> 167 | future.get() ?: sink.success(user) ?: sink.error(RuntimeException("Unknown error")) 168 | } 169 | } 170 | 171 | override fun delete(user: User) = Mono.create { sink -> 172 | cache.remove(user.id) 173 | sink.success() 174 | // cache.removeAsync(user.id).listen { future -> 175 | // future.get() ?: sink.success() ?: sink.error(RuntimeException("Unknown error")) 176 | // } 177 | } 178 | } -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/test/kotlin/eu/wojciechzurek/example/ExampleApplicationTests.kt: -------------------------------------------------------------------------------- 1 | package eu.wojciechzurek.example 2 | 3 | import org.junit.Test 4 | import org.junit.runner.RunWith 5 | import org.springframework.beans.factory.annotation.Autowired 6 | import org.springframework.boot.test.context.SpringBootTest 7 | import org.springframework.http.HttpHeaders 8 | import org.springframework.http.MediaType 9 | import org.springframework.test.context.junit4.SpringRunner 10 | import org.springframework.test.web.reactive.server.WebTestClient 11 | import reactor.test.StepVerifier 12 | 13 | @RunWith(SpringRunner::class) 14 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = [ExampleApplication::class]) 15 | class ExampleApplicationTests { 16 | 17 | //private val client = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build() 18 | @Autowired 19 | private lateinit var client: WebTestClient 20 | 21 | @Test 22 | fun contextLoads() { 23 | } 24 | 25 | 26 | @Test 27 | fun `Get all users endpoint`() { 28 | client 29 | .get() 30 | .uri("/api/users") 31 | .exchange() 32 | .expectStatus().is2xxSuccessful 33 | .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) 34 | .expectBodyList(User::class.java) 35 | } 36 | 37 | @Test 38 | fun `Get one user endpoint`() { 39 | val result = client 40 | .get() 41 | .uri("/api/users/e2ac4fba-ce48-42fe-a0b9-c7555b65154f") 42 | .exchange() 43 | .expectStatus().is2xxSuccessful 44 | .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) 45 | .returnResult(User::class.java) 46 | 47 | StepVerifier 48 | .create(result.responseBody) 49 | .expectNextMatches { it.login == "test" && it.age == 10 } 50 | .thenCancel() 51 | .verify() 52 | } 53 | 54 | @Test 55 | fun `Update one user endpoint`() { 56 | 57 | val userRequest = UserRequest(login = "update-login", age = 40) 58 | 59 | val result = client 60 | .put() 61 | .uri("/api/users/10b86e02-109d-488a-8e25-8bb63a7c4f1c") 62 | .syncBody(userRequest) 63 | .exchange() 64 | .expectStatus().is2xxSuccessful 65 | .returnResult(User::class.java) 66 | 67 | StepVerifier 68 | .create(result.responseBody) 69 | .expectNextMatches { it.login == userRequest.login && it.age == userRequest.age } 70 | .thenCancel() 71 | .verify() 72 | } 73 | 74 | @Test 75 | fun `Post new user endpoint`() { 76 | 77 | val user = UserRequest(login = "super-test", age = 99) 78 | 79 | val result = client 80 | .post() 81 | .uri("/api/users") 82 | .syncBody(user) 83 | .exchange() 84 | .expectStatus().is2xxSuccessful 85 | .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) 86 | .expectHeader().exists(HttpHeaders.LOCATION) 87 | .returnResult(User::class.java) 88 | 89 | StepVerifier 90 | .create(result.responseBody) 91 | .expectNextMatches { it.login == user.login && it.age == user.age } 92 | .thenCancel() 93 | .verify() 94 | } 95 | 96 | @Test 97 | fun `Deleter one user endpoint`() { 98 | client 99 | .delete() 100 | .uri("/api/users/4ca315c0-e214-4b62-9c2a-71d78e29412e") 101 | .exchange() 102 | .expectStatus().isNoContent 103 | .expectBody().isEmpty 104 | 105 | } 106 | 107 | } 108 | --------------------------------------------------------------------------------