diff --git a/.gitignore b/.gitignore index 6c01878..1a54428 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ out/ ### VS Code ### .vscode/ + +### Java Target ### +target/ diff --git a/build.gradle b/build.gradle deleted file mode 100644 index db74c71..0000000 --- a/build.gradle +++ /dev/null @@ -1,113 +0,0 @@ -plugins { - // Apply the Kotlin JVM plugin to add support for Kotlin on the JVM. - id 'org.jetbrains.kotlin.jvm' version '1.3.50' - id 'org.jetbrains.kotlin.plugin.noarg' version '1.3.50' - id 'org.jetbrains.kotlin.plugin.jpa' version '1.3.50' - id 'org.jetbrains.kotlin.plugin.allopen' version '1.3.50' - id 'org.jetbrains.kotlin.plugin.spring' version '1.3.50' - id 'org.springframework.boot' version '2.2.1.RELEASE' - id 'org.jetbrains.kotlin.kapt' version '1.3.50' - id 'io.spring.dependency-management' version '1.0.8.RELEASE' - id 'com.github.ben-manes.versions' version '0.27.0' -} - -group = "com.example" -version = "0.0.1-SNAPSHOT" -sourceCompatibility = 11 -ext['junit-jupiter.version'] = '5.5.2' -ext['mockito.version'] = '2.27.0' - -repositories { - mavenCentral() -} - -dependencies { - implementation("org.springframework.boot:spring-boot-starter-data-jdbc") - implementation("org.springframework.boot:spring-boot-starter-data-jpa") - implementation("org.springframework.boot:spring-boot-starter-web") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("org.springframework.boot:spring-boot-devtools") - runtimeOnly("org.postgresql:postgresql") -} - -repositories { - mavenCentral() - jcenter() - maven { url 'https://repo.spring.io/milestone' } -} - -allOpen { - annotation("javax.persistence.Entity") - annotation("javax.persistence.MappedSuperclass") - annotation("javax.persistence.Embeddable") -} - -dependencies { - // Use the Kotlin JDK 8 standard library. - implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' - - // Use the Kotlin test library. - testImplementation 'org.jetbrains.kotlin:kotlin-test' - - // Use the Kotlin JUnit integration. - testImplementation 'org.jetbrains.kotlin:kotlin-test-junit' - - // Java 9+ compatibility dependencies - implementation 'javax.xml.bind:jaxb-api:2.3.1' - implementation 'javax.inject:javax.inject:1' - - // Spring boot - implementation 'org.springframework.boot:spring-boot-starter-actuator' - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-jdbc' - implementation 'org.springframework.boot:spring-boot-starter-web' - implementation 'org.springframework.boot:spring-boot-starter-validation' - runtimeOnly 'org.springframework.boot:spring-boot-devtools' - annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' - - implementation 'commons-codec:commons-codec:1.13' - - // Kotlin - implementation 'org.jetbrains.kotlin:kotlin-reflect' - - // Utils - implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.10.0' - implementation 'io.github.microutils:kotlin-logging:1.6.26' - implementation 'com.vladmihalcea:hibernate-types-52:2.5.0' - implementation 'org.postgresql:postgresql:42.2.8' - - implementation 'com.google.guava:guava:28.1-jre' - - // Testing - testImplementation('org.springframework.boot:spring-boot-starter-test') { - exclude module: 'junit' - } - testImplementation 'io.rest-assured:rest-assured:3.3.0' - testImplementation 'org.junit.jupiter:junit-jupiter-params:5.5.0' - - // Mockito - testImplementation 'com.nhaarman:mockito-kotlin:1.6.0' - testImplementation 'org.mockito:mockito-inline:2.27.0' - testImplementation 'org.mockito:mockito-junit-jupiter' - - // JUnit 5 (Jupiter) - testImplementation 'org.junit.jupiter:junit-jupiter-api' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' - -} - -compileKotlin { - kotlinOptions { - freeCompilerArgs = ['-Xjsr305=strict'] - jvmTarget = '11' - } -} -compileTestKotlin { - kotlinOptions { - freeCompilerArgs = ['-Xjsr305=strict'] - jvmTarget = '11' - } -} - diff --git a/gradlew b/gradlew deleted file mode 100644 index 2fe81a7..0000000 --- a/gradlew +++ /dev/null @@ -1,183 +0,0 @@ -#!/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 deleted file mode 100644 index 62bd9b9..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,103 +0,0 @@ -@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 init - -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 init - -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 - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -: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 %CMD_LINE_ARGS% - -: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/pom.xml b/pom.xml new file mode 100644 index 0000000..55415b8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,162 @@ + + + 4.0.0 + com.example.demo + demo + 1.0.0-SNAPSHOT + + + 1.11 + 1.3.72 + 1.12.1 + 1.3.72 + 2.2.6.RELEASE + 42.2.12 + 2.9.8 + 2.11.0 + 11 + + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-actuator + ${spring-boot.version} + + + org.springframework.boot + spring-boot-configuration-processor + ${spring-boot.version} + + + + + com.vladmihalcea + hibernate-types-52 + ${hibernate-types.version} + + + + + com.fasterxml.jackson.module + jackson-module-kotlin + ${jackson.version} + + + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring-boot.version} + + + org.postgresql + postgresql + ${postgresql.version} + + + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + org.testcontainers + postgresql + ${testcontainers.version} + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + + + src/main/kotlin + src/test/kotlin + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + -Xjsr305=strict + + + spring + jpa + + 1.8 + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + + + + + + + diff --git a/settings.gradle.kts b/settings.gradle.kts deleted file mode 100644 index dae155f..0000000 --- a/settings.gradle.kts +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = "demo" diff --git a/src/main/kotlin/com/example/demo/entity/SampleJsonNodeEntity.kt b/src/main/kotlin/com/example/demo/entity/SampleJsonNodeEntity.kt new file mode 100644 index 0000000..44dca66 --- /dev/null +++ b/src/main/kotlin/com/example/demo/entity/SampleJsonNodeEntity.kt @@ -0,0 +1,32 @@ +package com.example.demo.entity + +import com.fasterxml.jackson.databind.JsonNode +import com.vladmihalcea.hibernate.type.json.JsonBinaryType +import com.vladmihalcea.hibernate.type.json.JsonStringType +import org.hibernate.annotations.Type +import org.hibernate.annotations.TypeDef +import org.hibernate.annotations.TypeDefs +import javax.persistence.* + +@Entity +@Table(name = "tests") +@TypeDefs( + TypeDef(name = "json", typeClass = JsonStringType::class), + TypeDef(name = "jsonb", typeClass = JsonBinaryType::class) +) +data class SampleJsonNodeEntity ( + @Id @GeneratedValue + val id: Long?, + val name: String?, + + @Type(type = "jsonb") + @Column(columnDefinition = "jsonb") + var data: JsonNode? +) { + + /** + * Dependently on use-case this can be done differently: + * https://stackoverflow.com/questions/37873995/how-to-create-empty-constructor-for-data-class-in-kotlin-android + */ + constructor(): this(null, null, null) +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/entity/SampleEntity.kt b/src/main/kotlin/com/example/demo/entity/SampleMapEntity.kt similarity index 60% rename from src/main/kotlin/com/example/demo/entity/SampleEntity.kt rename to src/main/kotlin/com/example/demo/entity/SampleMapEntity.kt index 2306535..c547e3f 100644 --- a/src/main/kotlin/com/example/demo/entity/SampleEntity.kt +++ b/src/main/kotlin/com/example/demo/entity/SampleMapEntity.kt @@ -1,6 +1,7 @@ package com.example.demo.entity import com.example.demo.pojo.SamplePojo +import com.fasterxml.jackson.databind.JsonNode import com.vladmihalcea.hibernate.type.json.JsonBinaryType import com.vladmihalcea.hibernate.type.json.JsonStringType import org.hibernate.annotations.Type @@ -14,14 +15,19 @@ import javax.persistence.* TypeDef(name = "json", typeClass = JsonStringType::class), TypeDef(name = "jsonb", typeClass = JsonBinaryType::class) ) - -data class SampleEntity ( +data class SampleMapEntity ( @Id @GeneratedValue - val id: Long, - val name: String, + val id: Long?, + val name: String?, + @Type(type = "jsonb") @Column(columnDefinition = "jsonb") - var data: SamplePojo - ) : AbstractJpaPersistable() { + var data: Map? +) { + /** + * Dependently on use-case this can be done differently: + * https://stackoverflow.com/questions/37873995/how-to-create-empty-constructor-for-data-class-in-kotlin-android + */ + constructor(): this(null, null, null) } \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/pojo/SamplePojo.kt b/src/main/kotlin/com/example/demo/pojo/SamplePojo.kt index 3666d75..fa3c447 100644 --- a/src/main/kotlin/com/example/demo/pojo/SamplePojo.kt +++ b/src/main/kotlin/com/example/demo/pojo/SamplePojo.kt @@ -1,29 +1,15 @@ package com.example.demo.pojo -import java.io.Serializable +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +@JsonIgnoreProperties(ignoreUnknown = true) data class SamplePojo( val payload: String, val flag: Boolean -) : Serializable { +) { + constructor(map: Map) : this(map["payload"] as String, map["flag"] as Boolean) - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is SamplePojo) return false - - if (payload != other.payload) return false - if (flag != other.flag) return false - - return true - } - - override fun hashCode(): Int { - var result = payload.hashCode() - result = 31 * result + payload.hashCode() - return result - } - - override fun toString(): String { - return "SamplePojo(payload='$payload', flag=$flag)" + fun toMap() : Map { + return mapOf("payload" to payload, "flag" to flag) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/example/demo/repository/SampleJsonNodeRepository.kt b/src/main/kotlin/com/example/demo/repository/SampleJsonNodeRepository.kt new file mode 100644 index 0000000..e4392f3 --- /dev/null +++ b/src/main/kotlin/com/example/demo/repository/SampleJsonNodeRepository.kt @@ -0,0 +1,7 @@ +package com.example.demo.repository + +import com.example.demo.entity.SampleJsonNodeEntity +import org.springframework.data.jpa.repository.JpaRepository + +interface SampleJsonNodeRepository: JpaRepository { +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/repository/SampleMapRepository.kt b/src/main/kotlin/com/example/demo/repository/SampleMapRepository.kt new file mode 100644 index 0000000..4960913 --- /dev/null +++ b/src/main/kotlin/com/example/demo/repository/SampleMapRepository.kt @@ -0,0 +1,7 @@ +package com.example.demo.repository + +import com.example.demo.entity.SampleMapEntity +import org.springframework.data.jpa.repository.JpaRepository; + +interface SampleMapRepository : JpaRepository{ +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/repository/SampleRepository.kt b/src/main/kotlin/com/example/demo/repository/SampleRepository.kt deleted file mode 100644 index 4205590..0000000 --- a/src/main/kotlin/com/example/demo/repository/SampleRepository.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.demo.repository - -import com.example.demo.entity.SampleEntity -import org.springframework.data.jpa.repository.JpaRepository; -import java.util.* - -interface SampleRepository : JpaRepository{ - //TODO -} \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/DbTestInitializer.kt b/src/test/kotlin/com/example/demo/DbTestInitializer.kt new file mode 100644 index 0000000..958439d --- /dev/null +++ b/src/test/kotlin/com/example/demo/DbTestInitializer.kt @@ -0,0 +1,31 @@ +package com.example.demo + +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.boot.test.util.TestPropertyValues +import org.springframework.context.ApplicationContextInitializer +import org.springframework.context.ConfigurableApplicationContext + +import org.testcontainers.containers.PostgreSQLContainer + +class DbTestInitializer: ApplicationContextInitializer { + private val log: Logger = LoggerFactory.getLogger(this::class.qualifiedName) + + companion object { + lateinit var postgresContainer: PostgreSQLContainer + } + + override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) { + + postgresContainer = KPostgresContainer("postgres:11") + postgresContainer.start() + + TestPropertyValues.of( + "spring.datasource.url=" + postgresContainer.jdbcUrl, + "spring.datasource.username=" + postgresContainer.username, + "spring.datasource.password=" + postgresContainer.password + ).applyTo(configurableApplicationContext.environment) + } +} + +class KPostgresContainer(imageName: String) : PostgreSQLContainer(imageName) diff --git a/src/test/kotlin/com/example/demo/DemoApplicationTests.kt b/src/test/kotlin/com/example/demo/DemoApplicationTests.kt deleted file mode 100644 index 2388354..0000000 --- a/src/test/kotlin/com/example/demo/DemoApplicationTests.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.demo - -import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest - -@SpringBootTest -class DemoApplicationTests { - - @Test - fun contextLoads() { - } - -} diff --git a/src/test/kotlin/com/example/demo/pojo/SamplePojoTest.kt b/src/test/kotlin/com/example/demo/pojo/SamplePojoTest.kt new file mode 100644 index 0000000..06edfba --- /dev/null +++ b/src/test/kotlin/com/example/demo/pojo/SamplePojoTest.kt @@ -0,0 +1,20 @@ +package com.example.demo.pojo + +import junit.framework.Assert.assertEquals +import org.junit.Test + +class SamplePojoTest { + + private val pojo = SamplePojo("Test", true) + private var map = mapOf("payload" to "Test", "flag" to true) + + @Test + fun toPojo() { + assertEquals(pojo, SamplePojo(map)) + } + + @Test + fun toMap() { + assertEquals(map, pojo.toMap()) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/repository/SampleRepositoryTest.kt b/src/test/kotlin/com/example/demo/repository/SampleRepositoryTest.kt new file mode 100644 index 0000000..08e0549 --- /dev/null +++ b/src/test/kotlin/com/example/demo/repository/SampleRepositoryTest.kt @@ -0,0 +1,64 @@ +package com.example.demo.repository + +import com.example.demo.DbTestInitializer +import com.example.demo.entity.SampleJsonNodeEntity +import com.example.demo.entity.SampleMapEntity +import com.example.demo.pojo.SamplePojo +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import junit.framework.Assert.assertEquals +import junit.framework.Assert.assertNotNull +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.junit4.SpringRunner + + +@RunWith(SpringRunner::class) +@SpringBootTest +@ContextConfiguration(initializers = [DbTestInitializer::class]) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +class SampleRepositoryTest { + + @Autowired + lateinit var sampleMapRepository: SampleMapRepository + + @Autowired + lateinit var sampleJsonNodeRepository: SampleJsonNodeRepository + + lateinit var dto: SamplePojo + lateinit var mapEntity: SampleMapEntity + lateinit var jsonNodeEntity: SampleJsonNodeEntity + + @Before + fun setUp() { + dto = SamplePojo("Test", true) + mapEntity = SampleMapEntity(null, + "POJO1", + dto.toMap() + ) + + jsonNodeEntity = SampleJsonNodeEntity(null, + "POJO2", + jacksonObjectMapper().valueToTree(dto) + ) + } + + @Test + fun createMapPojo() { + val id = sampleMapRepository.save(mapEntity).id!! + assertNotNull(sampleMapRepository.getOne(id)) + assertEquals(sampleMapRepository.getOne(id).data?.let { SamplePojo(it) }, dto) + } + + @Test + fun createJsonNodePojo() { + val id = sampleJsonNodeRepository.save(jsonNodeEntity).id!! + assertNotNull(sampleJsonNodeRepository.getOne(id)) + assertEquals(jacksonObjectMapper().treeToValue(sampleJsonNodeRepository.getOne(id).data, SamplePojo::class.java), dto) + } + +} \ No newline at end of file