Skip to content
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 @@ -46,7 +46,7 @@ public static void initClass() {
connection.execute("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE +
" WITH replication = {'class':'SimpleStrategy','replication_factor':1}");
connection.execute("CREATE TABLE IF NOT EXISTS " + KEYSPACE + "." + TABLE +
" (id int PRIMARY KEY, name text)");
" (id int PRIMARY KEY, name text, elapsed duration)");
}

@AfterAll
Expand Down Expand Up @@ -77,6 +77,26 @@ public void testInsert() {
assertTrue(connection.execute("SELECT * FROM " + KEYSPACE + "." + TABLE).iterator().hasNext());
}

/**
* A duration is written as a bare literal, ie not enclosed in quotes the way a text is, and it
* carries at most one leading sign, applying to the whole value. This is what
* CassandraLiteralRenderer in the core module relies on when rendering the value of a
* CqlDurationGene, so it is checked here against a real Cassandra.
*/
@Test
public void testInsertDuration() {
List<CassandraInsertionDto> insertions = CassandraDsl.cassandra()
.insertInto(KEYSPACE, TABLE).d("id", "1").d("elapsed", "1mo2d3ns")
.and().insertInto(KEYSPACE, TABLE).d("id", "2").d("elapsed", "-1mo2d3ns")
.dtos();

CassandraInsertionResultsDto resultsDto = CassandraScriptRunner.executeInsert(connection, insertions);

assertTrue(resultsDto.executionResults.get(0));
assertTrue(resultsDto.executionResults.get(1));
assertEquals(2, connection.execute("SELECT * FROM " + KEYSPACE + "." + TABLE).all().size());
}

@Test
public void testInsertionFailureDoesNotStopFollowingInsertions() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.evomaster.core.database.cassandra

/**
* A single column of a Cassandra table, as recovered from the schema description string carried by
* a failed CQL query reported by the SUT driver.
*/
data class CassandraColumn(

val name: String,

/**
* The CQL type of the column, as named in the CQL schema, eg "text", "int", "map<text, int>".
*/
val cqlType: String,

/**
* Whether this column is part of the table's partition key.
*/
val isPartitionKey: Boolean = false,

/**
* Whether this column is one of the table's clustering columns.
*/
val isClusteringColumn: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.evomaster.core.database.cassandra

import org.evomaster.core.search.gene.BooleanGene
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.UUIDGene
import org.evomaster.core.search.gene.cassandra.CqlDurationGene
import org.evomaster.core.search.gene.datetime.DateGene
import org.evomaster.core.search.gene.datetime.DateTimeGene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.numeric.*
import org.evomaster.core.search.gene.string.StringGene

/**
* Builds the gene used to generate the value of a Cassandra column, based on its CQL type.
*
* Two different reasons keep a CQL type out of the ones handled here:
* - the value of a column of that type cannot be generated at all, ie a counter, which is only
* writable with an UPDATE, and a timeuuid, which requires a version 1 UUID, whereas [UUIDGene]
* generates a random one;
* - no gene generating a value of that type has been written yet, ie blob, inet, the collections
* and the user defined types.
*/
object CassandraColumnGeneBuilder {

/**
* How the gene generating the value of a column is built, for each of the CQL types handled
* here, keyed by the normalized name of the type. Being the single place where such types are
* enumerated, it is also what [isSupported] answers from, so that the two cannot disagree.
*/
private val GENE_BUILDERS: Map<String, (String) -> Gene> = mapOf(
"ascii" to { name -> StringGene(name) },
"text" to { name -> StringGene(name) },
"varchar" to { name -> StringGene(name) },
"tinyint" to { name -> IntegerGene(name, min = Byte.MIN_VALUE.toInt(), max = Byte.MAX_VALUE.toInt()) },
"smallint" to { name -> IntegerGene(name, min = Short.MIN_VALUE.toInt(), max = Short.MAX_VALUE.toInt()) },
"int" to { name -> IntegerGene(name) },
"bigint" to { name -> LongGene(name) },
"varint" to { name -> BigIntegerGene(name) },
"decimal" to { name -> BigDecimalGene(name) },
"float" to { name -> FloatGene(name) },
"double" to { name -> DoubleGene(name) },
"boolean" to { name -> BooleanGene(name) },
"uuid" to { name -> UUIDGene(name) },
/*
Only valid values are generated, as these genes are used to set up the state of the
database, and Cassandra would just reject an insertion carrying an invalid one.
*/
"timestamp" to { name -> DateTimeGene(name, onlyValid = true) },
"date" to { name -> DateGene(name, onlyValidDates = true) },
"time" to { name -> TimeGene(name, onlyValidTimes = true) },
"duration" to { name -> CqlDurationGene(name) }
)

/**
* @return whether a gene can be built for [column], ie whether its CQL type is one of the
* scalar types handled here
*/
fun isSupported(column: CassandraColumn) = normalize(column.cqlType) in GENE_BUILDERS

/**
* @throws IllegalArgumentException if the CQL type of [column] is not handled, as verifiable
* beforehand with [isSupported]
*/
fun buildGene(column: CassandraColumn): Gene {

val builder = GENE_BUILDERS[normalize(column.cqlType)]
?: throw IllegalArgumentException("Cannot handle the CQL type of column $column")

return builder(column.name)
}

private fun normalize(cqlType: String) = cqlType.trim().lowercase()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.evomaster.core.database.cassandra

import org.evomaster.core.search.action.Action
import org.evomaster.core.search.action.EnvironmentAction
import org.evomaster.core.search.gene.Gene

/**
* An action inserting a single row into a Cassandra table, used to set up the state of the database
* before the main actions of a test are executed.
*/
class CassandraDbAction(
/**
* The keyspace containing the table to insert the row into
*/
val keyspace: String,
/**
* The table to insert the row into
*/
val table: String,
/**
* The columns the row is composed of, ie the ones a value is generated for.
* There is exactly one gene per column, in the same order.
*/
val columns: List<CassandraColumn>,
/**
* The genes generating the value of each of the [columns], in the same order.
* Only meant to be given when copying an existing action, so that its genes are carried over
* instead of being built anew: when not given, one gene is built per column.
*/
computedGenes: List<Gene>? = null
) : EnvironmentAction(listOf()) {

private val genes: List<Gene> = (computedGenes ?: computeGenes()).also { addChildren(it) }

init {
if (genes.size != columns.size) {
throw IllegalArgumentException("Mismatch between the ${columns.size} columns and the ${genes.size} genes")
}
}

private fun computeGenes(): List<Gene> {
return columns.map { CassandraColumnGeneBuilder.buildGene(it) }
}

override fun getName(): String {
return "CASSANDRA_Insert_${keyspace}_${table}"
}

override fun seeTopGenes(): List<Gene> {
return genes
}

override fun copyContent(): Action {
return CassandraDbAction(keyspace, table, columns, genes.map(Gene::copy))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.evomaster.core.database.cassandra

import org.evomaster.core.search.action.Action
import org.evomaster.core.search.action.ActionResult

/**
* Cassandra insert action execution result
*/
class CassandraDbActionResult : ActionResult {

constructor(sourceLocalId: String, stopping: Boolean = false) : super(sourceLocalId, stopping)
constructor(other: CassandraDbActionResult) : super(other)

companion object {
const val INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY = "INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY"
}

override fun copy(): CassandraDbActionResult {
return CassandraDbActionResult(this)
}

/**
* @param success specifies whether the INSERT CASSANDRA executed successfully
*/
fun setInsertExecutionResult(success: Boolean) =
addResultValue(INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY, success.toString())

/**
* @return whether the Cassandra action executed successfully
*/
fun getInsertExecutionResult() = getResultValue(INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY)?.toBoolean() ?: false

override fun matchedType(action: Action): Boolean {
return action is CassandraDbAction
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.evomaster.core.database.cassandra

import org.evomaster.client.java.controller.api.dto.database.operations.CassandraDatabaseCommandDto
import org.evomaster.client.java.controller.api.dto.database.operations.CassandraInsertionDto
import org.evomaster.client.java.controller.api.dto.database.operations.CassandraInsertionEntryDto

/**
* Transforms the Cassandra insert actions of an individual into the commands to be executed on the
* SUT side.
*/
object CassandraDbActionTransformer {

fun transform(actions: List<CassandraDbAction>): CassandraDatabaseCommandDto {

val insertionDtos = mutableListOf<CassandraInsertionDto>()

for (action in actions) {

val insertionDto = CassandraInsertionDto().apply {
keyspaceName = action.keyspace
tableName = action.table
}

action.seeTopGenes()
.filter { it.isPrintable() }
.forEach { gene ->
val entry = CassandraInsertionEntryDto().apply {
columnName = gene.name
printableValue = CassandraLiteralRenderer.toCqlLiteral(gene)
}
insertionDto.data.add(entry)
}

insertionDtos.add(insertionDto)
}

return CassandraDatabaseCommandDto().apply { this.insertions = insertionDtos }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.evomaster.core.database.cassandra

import org.evomaster.core.logging.LoggingUtil
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* Builds the action inserting a row into a Cassandra table, based on the description of the
* columns of that table reported by the SUT driver.
*/
class CassandraInsertBuilder {

companion object {
private val log: Logger = LoggerFactory.getLogger(CassandraInsertBuilder::class.java)
}

/**
* @param tableSchema the description of the columns of a table, as reported by the SUT driver
* @return whether an insertion that could be executed can be built for such a table, ie whether
* a value can be generated for at least one of its columns and for all of the ones composing
* its primary key
*/
fun canBuildInsertionFor(tableSchema: String): Boolean {

val (supported, unsupported) = partitionBySupport(CassandraTableSchemaParser.parse(tableSchema))

return supported.isNotEmpty() && unsupported.none { isPartOfPrimaryKey(it) }
}

/**
* The columns whose CQL type is not handled are left out of the insertion, as no value can be
* generated for them. The resulting insertion is still worth executing, since the remaining
* columns might be all that is needed, and a rejected insertion is already recorded as a failed
* one instead of stopping the search.
*
* That argument does not hold when no value can be generated for any column, nor when one of
* the skipped columns is part of the primary key, as Cassandra requires a full primary key in
* an INSERT: in both cases the insertion could only be rejected, so none is built.
*
* Note that the genes of the returned action are not initialized yet, which is left to the
* caller, as it is done for the other types of database action.
*
* @throws IllegalArgumentException if no insertion that could be executed can be built for the
* table, as verifiable beforehand with [canBuildInsertionFor]
*/
fun createCassandraInsertionAction(keyspace: String, table: String, tableSchema: String): CassandraDbAction {

val (supported, unsupported) = partitionBySupport(CassandraTableSchemaParser.parse(tableSchema))

val qualifiedTableName = "$keyspace.$table"

if (supported.isEmpty()) {
throw IllegalArgumentException("No value can be generated for any column of" +
" $qualifiedTableName: ${describe(unsupported)}")
}

val unsupportedKeyColumns = unsupported.filter { isPartOfPrimaryKey(it) }
if (unsupportedKeyColumns.isNotEmpty()) {
throw IllegalArgumentException("No value can be generated for some of the columns composing" +
" the primary key of $qualifiedTableName: ${describe(unsupportedKeyColumns)}")
}

if (unsupported.isNotEmpty()) {
LoggingUtil.uniqueWarn(
log,
"Cannot generate data for some columns of a Cassandra table, as their CQL type is not handled: {}",
"$qualifiedTableName: ${describe(unsupported)}"
)
}

return CassandraDbAction(keyspace, table, supported).apply { forceNewTaints() }
}

/**
* @return the columns a value can be generated for (first), and the ones it cannot (second)
*/
private fun partitionBySupport(columns: List<CassandraColumn>) =
columns.partition { CassandraColumnGeneBuilder.isSupported(it) }

private fun isPartOfPrimaryKey(column: CassandraColumn) = column.isPartitionKey || column.isClusteringColumn

private fun describe(columns: List<CassandraColumn>) = columns.joinToString(", ") { "${it.name} ${it.cqlType}" }
}
Loading
Loading