Test extension with configuration
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.aitrainer.api.test
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
import java.sql.*
|
||||
import kotlin.test.assertFails
|
||||
|
||||
import java.util.*
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
|
||||
class AitrainerDBTest(configuration: ApplicationConfiguration) {
|
||||
|
||||
|
||||
@Autowired
|
||||
private lateinit var username: String;
|
||||
private lateinit var password: String;
|
||||
private lateinit var datasourceUrl: String;
|
||||
private lateinit var conn: Connection
|
||||
private val conf: ApplicationConfiguration = configuration
|
||||
|
||||
fun initDB() {
|
||||
this.username = this.conf.username.orEmpty()
|
||||
this.password = this.conf.password.orEmpty()
|
||||
this.datasourceUrl = this.conf.url.orEmpty()
|
||||
assertTrue ("username should not be empty", { this.username.isNotEmpty() })
|
||||
assertTrue ("password should not be empty", { this.password.isNotEmpty() })
|
||||
assertTrue ("url should not be empty", { this.datasourceUrl.isNotEmpty() })
|
||||
|
||||
this.getConnection()
|
||||
|
||||
assertNotNull({this.conn}, "MySQL connection should not be null")
|
||||
}
|
||||
|
||||
|
||||
private fun executeDBQueries() {
|
||||
var stmt: Statement? = null
|
||||
var resultset: ResultSet? = null
|
||||
try {
|
||||
stmt = conn.createStatement()
|
||||
val sql: String = "CREATE DATABASE aitrainer_test;"
|
||||
if ( stmt.execute( sql ) ) {
|
||||
|
||||
}
|
||||
//while (resultset!!.next()) {
|
||||
// println(resultset.getString("Database"))
|
||||
//}
|
||||
} catch (ex: SQLException) {
|
||||
throw Exception(ex)
|
||||
} finally {
|
||||
// release resources
|
||||
if (resultset != null) {
|
||||
try {
|
||||
resultset.close()
|
||||
} catch (sqlEx: SQLException) {
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close()
|
||||
} catch (sqlEx: SQLException) {
|
||||
throw Exception(sqlEx)
|
||||
}
|
||||
}
|
||||
conn.close()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This method makes a connection to MySQL Server
|
||||
* In this example, MySQL Server is running in the local host (so 127.0.0.1)
|
||||
* at the standard port 3306
|
||||
*/
|
||||
private fun getConnection() {
|
||||
val connectionProps = Properties()
|
||||
connectionProps["user"] = this.username
|
||||
connectionProps["password"] = this.password
|
||||
//try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance()
|
||||
this.conn = DriverManager.getConnection(this.datasourceUrl, connectionProps)
|
||||
//} catch (ex: SQLException) {
|
||||
// handle any errors
|
||||
// ex.printStackTrace()
|
||||
//} catch (ex: Exception) {
|
||||
// handle any errors
|
||||
// ex.printStackTrace()
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.aitrainer.api.test
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "spring.datasource")
|
||||
class ApplicationConfiguration {
|
||||
open var username: String? = null
|
||||
open var password: String? = null
|
||||
open var url: String? = null
|
||||
}
|
||||
Reference in New Issue
Block a user