Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close cassandra session upon query execution #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import org.json.JSONException
import org.skyscreamer.jsonassert.JSONCompareMode
import java.io.InputStream
import java.sql.DriverManager
import java.util.HashMap
import java.util.Objects
import java.util.*
import javax.sql.DataSource

class DbComparatorAssertJ(
Expand Down Expand Up @@ -86,16 +85,15 @@ class DbComparatorAssertJ(
}

private fun Table.convertTableToTableModel(): TableModel {
val tableModel = TableModel()
this.rowsList.forEach {
val rowModel = RowModel()
val cells = HashMap<String, Any?>()

it.columnsNameList.forEach { columnName -> cells[columnName] = it.getColumnValue(columnName) }
rowModel.cells = cells
tableModel.addRow(rowModel)
}
return tableModel
return rowsList
.map { row ->
row.columnsNameList.associateWith {
row.getColumnValue(it)
}
}
.map(::RowModel)
.toSet()
.let(::TableModel)
}

private fun getMapperByDbType(source: Source?, dataSource: DataSource?): TypeMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import com.ekino.oss.jcv.db.cassandra.DbComparatorCassandra
import com.ekino.oss.jcv.db.config.DBValidators
import org.skyscreamer.jsonassert.JSONCompareMode

class DBComparatorBuilder {
class DBComparatorBuilder(
private var mode: JSONCompareMode,
private var validators: List<JsonValidator<*>>
) {

constructor()

constructor(
mode: JSONCompareMode,
validators: List<JsonValidator<*>>
) {
this.mode = mode
this.validators = validators
}

private lateinit var mode: JSONCompareMode
private lateinit var validators: List<JsonValidator<*>>
// todo(any): this datasource should not be nullable.
// It is however required because a builder can be built without one.
private var datasource: CassandraDataSource? = null
private var customMapper: CassandraMapper? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import com.ekino.oss.jcv.db.model.RowModel
import com.ekino.oss.jcv.db.model.TableModel
import java.net.InetSocketAddress

class QueryConverter(private val dataSource: CassandraDataSource? = null) {
class QueryConverter(private val dataSource: CassandraDataSource?) {
fun fromQueryToTableModel(query: String): TableModel {
val session = buildCqlSession(dataSource)

val resultSet = session.execute(query)
return fromResultSetToTableModel(resultSet)
return buildCqlSession().use { session ->
val resultSet = session.execute(query)
fromResultSetToTableModel(resultSet)
}
}

private fun buildCqlSession(dataSource: CassandraDataSource? = null): CqlSession {
dataSource ?: throw DbAssertException("You have to defined a valida datource")
private fun buildCqlSession(): CqlSession {
dataSource ?: throw DbAssertException("You have to defined a valida datasource")

val builder = CqlSession
.builder()
Expand All @@ -31,14 +31,16 @@ class QueryConverter(private val dataSource: CassandraDataSource? = null) {
}

private fun fromResultSetToTableModel(resultSet: ResultSet): TableModel {
val rows = mutableSetOf<RowModel>()
resultSet.forEach {
val cells = mutableMapOf<String, Any?>()
for (i in 0 until it.columnDefinitions.size()) {
cells[it.columnDefinitions[i].name.asCql(true)] = it.getObject(i)
return resultSet
.map { element ->
element.columnDefinitions
.mapIndexed { index, columnDefinition ->
columnDefinition.name.asCql(true) to element.getObject(index)
}
.toMap()
.let(::RowModel)
}
rows.add(RowModel(cells))
}
return TableModel(rows)
.toSet()
.let(::TableModel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DbComparatorAssert(private val actualJson: JSONArray, private val jsonComp

@JvmStatic
fun assertThatRowModel(rowModel: RowModel): DbComparatorAssert {
val actualJson = TableModel(mutableSetOf(rowModel)).getTableModelAsJson()
val actualJson = TableModel(setOf(rowModel)).getTableModelAsJson()
return DbComparatorAssert(
actualJson,
JsonComparator(JSONCompareMode.NON_EXTENSIBLE, Validators.defaultValidators())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ enum class DatabaseType(private val productName: String) {
MSSQL("Microsoft SQL Server");

companion object {
fun getDatabaseTypeByProductName(name: String) = values().find { it.productName == name }
fun getDatabaseTypeByProductName(name: String) = entries.find { it.productName == name }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.ekino.oss.jcv.db.mapper.TypeMapper
import org.json.JSONArray
import org.json.JSONObject

data class TableModel(val rows: MutableSet<RowModel> = mutableSetOf()) {
fun addRow(row: RowModel) = rows.add(row)

data class TableModel(val rows: Set<RowModel>) {
fun getTableModelAsJson(typeMapper: TypeMapper? = null): JSONArray {
val mapper = typeMapper ?: DefaultMapper()
return JSONArray(
Expand All @@ -24,7 +22,8 @@ data class TableModel(val rows: MutableSet<RowModel> = mutableSetOf()) {
.map { entry -> entry.key.lowercase() to getValueFromMapper(entry.value, mapper) }.toMap()
.let { map -> JSONObject(map) }

private fun getValueFromMapper(value: Any?, mapper: TypeMapper) = value?.let { mapper.getValueFromColumn(it) } ?: JSONObject.NULL
private fun getValueFromMapper(value: Any?, mapper: TypeMapper) =
value?.let { mapper.getValueFromColumn(it) } ?: JSONObject.NULL
}

data class RowModel(var cells: MutableMap<String, Any?> = mutableMapOf())
data class RowModel(val cells: Map<String, Any?>)
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,32 @@ class DbComparatorAssertTest {
]
""".trimIndent()

val tableModel = TableModel()
tableModel.addRow(
RowModel(
mutableMapOf(
"id" to "0840ad8f-db82-472d-9dff-bb6d64992222",
"column_boolean" to true,
"column_char_varying" to "char varying",
"column_varchar" to "varchar",
"column_character" to "character",
"column_char" to "char",
"column_text" to "text",
"column_unlimited_varchar" to "unlimited varcharZOEFOEZUFHHZELF",
"column_smallint" to 123,
"column_int" to 12355,
"column_serial" to 3282,
"column_float" to 123.2235,
"column_real" to 125125.13,
"column_numeric" to 312,
"column_date" to "2019-06-07",
"column_time" to "17:12:28",
"column_timestamp" to "2019-06-07 00:00:00.0",
"column_timestamptz" to "2019-06-07 00:00:00.0",
"column_interval" to "0 years 0 mons 100 days 0 hours 0 mins 0.00 secs",
"column_json" to JSONArray("[{\"key\": \"Hello World !\"}]"),
"column_jsonb" to JSONObject("{\"key\": \"Hello World !\"}")
val tableModel = TableModel(
setOf(
RowModel(
mapOf(
"id" to "0840ad8f-db82-472d-9dff-bb6d64992222",
"column_boolean" to true,
"column_char_varying" to "char varying",
"column_varchar" to "varchar",
"column_character" to "character",
"column_char" to "char",
"column_text" to "text",
"column_unlimited_varchar" to "unlimited varcharZOEFOEZUFHHZELF",
"column_smallint" to 123,
"column_int" to 12355,
"column_serial" to 3282,
"column_float" to 123.2235,
"column_real" to 125125.13,
"column_numeric" to 312,
"column_date" to "2019-06-07",
"column_time" to "17:12:28",
"column_timestamp" to "2019-06-07 00:00:00.0",
"column_timestamptz" to "2019-06-07 00:00:00.0",
"column_interval" to "0 years 0 mons 100 days 0 hours 0 mins 0.00 secs",
"column_json" to JSONArray("[{\"key\": \"Hello World !\"}]"),
"column_jsonb" to JSONObject("{\"key\": \"Hello World !\"}")
)
)
)
)
Expand All @@ -92,11 +93,12 @@ class DbComparatorAssertTest {
]
""".trimIndent()

val tableModel = TableModel()
tableModel.addRow(
RowModel(
mutableMapOf(
"content_test" to "abcde"
val tableModel = TableModel(
setOf(
RowModel(
mapOf(
"content_test" to "abcde"
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,32 @@ class JsonConverterTest {
@Test
fun `Should load file and return a json array`() {
val expectedResult = JSONArray("""[{"id":1}]""")
val inputString = this::class.java.classLoader.getResourceAsStream("test-file.json")!!.bufferedReader().use(BufferedReader::readText)
val inputString = this::class.java.classLoader.getResourceAsStream("test-file.json")!!.bufferedReader()
.use(BufferedReader::readText)
assertThat(formatInput(inputString).toString()).isEqualTo(expectedResult.toString())
}

@Test
fun `Should get an error when trying to load a json object`() {
val expectedResult = JSONArray("""[{"id":1}]""")
val inputString = this::class.java.classLoader.getResourceAsStream("test-file-object.json")!!.bufferedReader().use(BufferedReader::readText)
val inputString = this::class.java.classLoader.getResourceAsStream("test-file-object.json")!!.bufferedReader()
.use(BufferedReader::readText)
assertThat(formatInput(inputString).toString()).isEqualTo(expectedResult.toString())
}

@Test
fun `Should get an error when trying to load an invalid json`() {
val inputString = this::class.java.classLoader.getResourceAsStream("test-file-invalid.json")!!.bufferedReader().use(BufferedReader::readText)
val inputString = this::class.java.classLoader.getResourceAsStream("test-file-invalid.json")!!.bufferedReader()
.use(BufferedReader::readText)
assertThat(formatInput(inputString)).isNull()
}

@Test
fun `Should convert table model to Json Array with default mapper`() {
val table = TableModel(
mutableSetOf(
setOf(
RowModel(
mutableMapOf(
"id" to 1
)
mutableMapOf("id" to 1)
)
)
)
Expand All @@ -66,11 +67,9 @@ class JsonConverterTest {
@Test
fun `Should convert table model to Json Array with custom mapper`() {
val table = TableModel(
mutableSetOf(
setOf(
RowModel(
mutableMapOf(
"id" to 1
)
mapOf("id" to 1)
)
)
)
Expand All @@ -79,6 +78,8 @@ class JsonConverterTest {
override fun getValueFromColumn(value: Any) = "$value CUSTOM"
}

assertThat(table.getTableModelAsJson(CustomMapper()).toString()).isEqualTo(JSONArray("""[{ "id" : "1 CUSTOM" }]""").toString())
assertThat(
table.getTableModelAsJson(CustomMapper()).toString()
).isEqualTo(JSONArray("""[{ "id" : "1 CUSTOM" }]""").toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ class QueryConverter(private val connection: Connection) {
private fun buildTableModel(resultSet: ResultSet): TableModel {
val rows = mutableSetOf<RowModel>()
while (resultSet.next()) {
val numberColumn = resultSet.metaData.columnCount
val cells = mutableMapOf<String, Any?>()
for (index in 1..numberColumn) {
cells[resultSet.metaData.getColumnName(index)] = resultSet.getObject(index)
(1..resultSet.metaData.columnCount).associate {
resultSet.metaData.getColumnName(it) to resultSet.getObject(it)
}
rows.add(RowModel(cells))
.let(::RowModel)
.let(rows::add)
}
return TableModel(rows)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import org.bson.Document
class MongoConverter {

fun convertContentToTableModel(collection: List<Document>): TableModel {
return TableModel(collection.map { convertDocumentToRowModel(it) }.toMutableSet())
return TableModel(collection.map(::convertDocumentToRowModel).toSet())
}

private fun convertDocumentToRowModel(document: Document) = RowModel(document.keys.map { it to document[it]!! }.toMap().toMutableMap())
private fun convertDocumentToRowModel(document: Document) =
RowModel(document.keys.associateWith { document[it]!! }.toMap())
}
Loading