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 @@ -17,14 +17,15 @@ import org.evomaster.core.search.gene.numeric.BigDecimalGene
import org.evomaster.core.search.gene.placeholder.ImmutableDataHolderGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.wrapper.ChoiceGene
import org.evomaster.core.search.gene.wrapper.NullableGene
import java.math.BigDecimal
import java.util.TreeSet

/**
* A typed attribute gene belonging to a DynamoDB item.
*
* @property attributeName name of the DynamoDB item attribute
* @property type native DynamoDB attribute type
* @property type native DynamoDB type of the seed attribute. A null seed can evolve into a scalar.
* @property gene evolvable value for the attribute
*/
data class DynamoDbAttributeGene(
Expand All @@ -39,7 +40,7 @@ data class DynamoDbAttributeGene(
DynamoDbValueType.STRING -> geneToPlainString(gene)
DynamoDbValueType.NUMBER -> geneToPlainNumber(gene)
DynamoDbValueType.BOOLEAN -> geneToPlainBoolean(gene)
DynamoDbValueType.NULL -> null
DynamoDbValueType.NULL -> (gene as? NullableGene)?.let(::geneToPlainValue)
DynamoDbValueType.MAP, DynamoDbValueType.LIST -> geneToPlainValue(gene)
DynamoDbValueType.STRING_SET -> geneToPlainSet(gene, ::geneToPlainString)
DynamoDbValueType.NUMBER_SET -> geneToPlainSet(gene, ::geneToPlainNumber)
Expand Down Expand Up @@ -68,7 +69,7 @@ data class DynamoDbAttributeGene(
(gene as? BooleanGene)?.value?.toString()
?: gene.getValueAsRawString()
)
DynamoDbValueType.NULL -> DynamoDbDtoUtils.fromPlainValue(attributeName, null)
DynamoDbValueType.NULL,
DynamoDbValueType.MAP,
DynamoDbValueType.LIST,
DynamoDbValueType.STRING_SET,
Expand Down Expand Up @@ -120,6 +121,7 @@ data class DynamoDbAttributeGene(
gene.getViewOfElements().map { geneToPlainValue(it) }
}
is HeterogeneousListGene -> gene.elements.map { geneToPlainValue(it) }
is NullableGene -> if (gene.isActive) geneToPlainValue(gene.gene) else null
is ChoiceGene<*> -> geneToPlainValue(gene.activeGene())
is ImmutableDataHolderGene -> when (gene.value) {
"null" -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import org.evomaster.core.search.gene.datetime.TimeOffsetGene
import org.evomaster.core.search.gene.numeric.BigDecimalGene
import org.evomaster.core.search.gene.numeric.IntegerGene
import org.evomaster.core.search.gene.numeric.LongGene
import org.evomaster.core.search.gene.placeholder.ImmutableDataHolderGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.wrapper.ChoiceGene
import org.evomaster.core.search.gene.wrapper.NullableGene
import org.evomaster.core.search.gene.wrapper.OptionalGene
import org.evomaster.core.search.service.Randomness
import java.math.BigDecimal
Expand Down Expand Up @@ -191,7 +191,8 @@ object DynamoDbInsertBuilder {
* Builds an EvoMaster gene tree from a composite, set, or null DynamoDB attribute DTO.
* Maps use fixed-key [ObjectGene]s, lists (including empty `[]`) use mutable-size [HeterogeneousListGene]s
* protecting inferred seed elements via `minSize`, string/number sets use [ProtectedUniqueArrayGene]s
* preventing empty sets and duplicates, and nulls use [ImmutableDataHolderGene].
* preventing empty sets and duplicates. Null seeds start with an inactive [NullableGene]
* that can evolve into a string, number, or boolean.
*/
fun buildDocumentGene(name: String, dto: DynamoDbAttributeValueDto): Gene {
fun build(currentName: String, currentDto: DynamoDbAttributeValueDto): Gene {
Expand All @@ -207,19 +208,11 @@ object DynamoDbInsertBuilder {
val elements = currentDto.listValue?.mapIndexed { index, childDto ->
build("${currentName}_$index", childDto)
} ?: emptyList()
val listTemplate = ChoiceGene(
"${currentName}_template",
listOf(
StringGene("${currentName}_s"),
BigDecimalGene("${currentName}_n"),
BooleanGene("${currentName}_b")
)
)
HeterogeneousListGene(
name = currentName,
elements = elements,
isFixed = false,
template = listTemplate,
template = buildScalarChoice(currentName, "${currentName}_template"),
minSize = elements.size,
maxSize = elements.size + ArrayGene.MAX_SIZE
)
Expand Down Expand Up @@ -265,7 +258,7 @@ object DynamoDbInsertBuilder {
)
}
DynamoDbValueTypeDto.NULL, null -> {
ImmutableDataHolderGene(currentName, "null", inQuotes = false)
NullableGene(currentName, buildScalarChoice(currentName), isActive = false)
}
DynamoDbValueTypeDto.STRING -> {
StringGene(currentName, currentDto.value ?: "")
Expand All @@ -283,6 +276,12 @@ object DynamoDbInsertBuilder {
return build(name, dto)
}

/** Builds the scalar alternatives used for list growth and null mutations. */
private fun buildScalarChoice(name: String, choiceName: String = name): ChoiceGene<Gene> = ChoiceGene(
choiceName,
listOf(StringGene("${name}_s"), BigDecimalGene("${name}_n"), BooleanGene("${name}_b"))
)

/**
* Builds a formatted gene or fallback [StringGene] for a DynamoDB string attribute.
*/
Expand Down
44 changes: 6 additions & 38 deletions core/src/main/kotlin/org/evomaster/core/output/DynamoDbWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.text.StringEscapeUtils
import org.evomaster.client.java.controller.api.dto.database.operations.DynamoDbAttributeValueDto
import org.evomaster.client.java.controller.api.dto.database.operations.DynamoDbValueTypeDto
import org.evomaster.core.database.dynamodb.DynamoDbValueType
import org.evomaster.core.search.action.EvaluatedDynamoDbAction
import org.evomaster.core.search.gene.BooleanGene
import org.evomaster.core.search.gene.numeric.BigDecimalGene

/**
* Generates DynamoDB initialization code for emitted tests.
Expand Down Expand Up @@ -46,42 +43,13 @@ object DynamoDbWriter {
lines.add(".insertInto(\"${escape(action.tableName, format)}\")")
action.attributes.forEach { attribute ->
val name = "\"${escape(attribute.attributeName, format)}\""
val value = when (attribute.type) {
DynamoDbValueType.MAP,
DynamoDbValueType.LIST,
DynamoDbValueType.STRING_SET,
DynamoDbValueType.NUMBER_SET,
DynamoDbValueType.NULL -> {
val ddbJson = ddbJsonMapper.writeValueAsString(toDynamoDbJsonNode(attribute.toDto()))
"\"${escape(ddbJson, format)}\""
}
DynamoDbValueType.STRING -> {
val raw = attribute.gene.getValueAsRawString()
val literal = "'${raw.replace("'", "''")}'"
"\"${escape(literal, format)}\""
}
DynamoDbValueType.NUMBER -> {
val raw = (attribute.gene as? BigDecimalGene)?.value?.toPlainString()
?: attribute.gene.getValueAsRawString()
"\"${escape(raw, format)}\""
}
DynamoDbValueType.BOOLEAN -> {
val raw = (attribute.gene as? BooleanGene)?.value?.toString()
?: attribute.gene.getValueAsRawString()
"\"$raw\""
}
val dto = attribute.toDto()
val (method, value) = when (dto.type) {
DynamoDbValueTypeDto.STRING -> "d" to "'${dto.value.replace("'", "''")}'"
DynamoDbValueTypeDto.NUMBER, DynamoDbValueTypeDto.BOOLEAN -> "d" to dto.value
else -> "ddbJson" to ddbJsonMapper.writeValueAsString(toDynamoDbJsonNode(dto))
}
val method = when (attribute.type) {
DynamoDbValueType.MAP,
DynamoDbValueType.LIST,
DynamoDbValueType.STRING_SET,
DynamoDbValueType.NUMBER_SET,
DynamoDbValueType.NULL -> "ddbJson"
DynamoDbValueType.STRING,
DynamoDbValueType.NUMBER,
DynamoDbValueType.BOOLEAN -> "d"
}
lines.add(".$method($name, $value)")
lines.add(".$method($name, \"${escape(value, format)}\")")
}
}
lines.add(".dtos()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ class NullableImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: Spe

override fun syncImpact(previous: Gene?, current: Gene) {
check(previous,current)
geneImpact.syncImpact((previous as NullableGene).gene, (current as NullableGene).gene)
geneImpact.syncImpact((previous as? NullableGene)?.gene, (current as NullableGene).gene)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package org.evomaster.core.database.dynamodb
import org.evomaster.client.java.controller.api.dto.database.operations.DynamoDbDtoUtils
import org.evomaster.client.java.controller.api.dto.database.operations.DynamoDbValueTypeDto
import org.evomaster.core.search.gene.BooleanGene
import org.evomaster.core.search.gene.collection.HeterogeneousListGene
import org.evomaster.core.search.gene.numeric.BigDecimalGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.wrapper.ChoiceGene
import org.evomaster.core.search.gene.wrapper.NullableGene
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.math.BigDecimal
Expand Down Expand Up @@ -66,6 +70,26 @@ class DynamoDbActionTransformerTest {
assertEquals(listOf("mvp", "world-cup"), attributes[2].mapValue.getValue("tags").setValue)
assertEquals(DynamoDbValueTypeDto.NULL, attributes[2].mapValue.getValue("coach").type)
assertEquals(DynamoDbValueTypeDto.NULL, attributes[3].type)

val seedKey = action.seedKey
val nullGene = action.attributes[3].gene as NullableGene
val choice = nullGene.gene as ChoiceGene<*>
nullGene.isActive = true
for ((index, value) in listOf("null", BigDecimal("10.50"), true).withIndex()) {
choice.selectActiveGene(index)
when (val selected = choice.activeGene()) {
is StringGene -> selected.value = value as String
is BigDecimalGene -> selected.value = value as BigDecimal
is BooleanGene -> selected.value = value as Boolean
}
val actual = DynamoDbActionTransformer.transform(listOf(action)).insertions.single().attributes[3]
assertEquals(DynamoDbDtoUtils.fromPlainValue("retiredAt", value), actual)
assertEquals(value, action.attributes[3].toPlainValue())
assertNotEquals(seedKey, action.insertionKey())
assertEquals(seedKey, (action.copy() as DynamoDbAction).seedKey)
}
nullGene.isActive = false
assertEquals(seedKey, action.insertionKey())
}

@Test
Expand Down Expand Up @@ -112,4 +136,19 @@ class DynamoDbActionTransformerTest {
val transported = DynamoDbActionTransformer.transform(listOf(action)).insertions.single().attributes.single()
assertEquals(profileDto, transported)
}

@Test
fun transformsAnActivatedNullListElement() {
val dto = DynamoDbDtoUtils.fromPlainValue("matchTimeline", listOf("Final", null))
val list = DynamoDbInsertBuilder.buildDocumentGene("matchTimeline", dto) as HeterogeneousListGene
val gene = list.elements[1] as NullableGene
val choice = gene.gene as ChoiceGene<*>
choice.selectActiveGene(2)
(choice.activeGene() as BooleanGene).value = true
gene.isActive = true

val attribute = DynamoDbAttributeGene("matchTimeline", DynamoDbValueType.LIST, list)
assertEquals(listOf("Final", true), attribute.toPlainValue())
assertEquals(DynamoDbDtoUtils.fromPlainValue("matchTimeline", listOf("Final", true)), attribute.toDto())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import org.evomaster.core.search.gene.datetime.DateTimeGene
import org.evomaster.core.search.gene.datetime.FormatForDatesAndTimes
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.numeric.BigDecimalGene
import org.evomaster.core.search.gene.placeholder.ImmutableDataHolderGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.wrapper.ChoiceGene
import org.evomaster.core.search.gene.wrapper.NullableGene
import org.evomaster.core.search.impact.impactinfocollection.MutatedGeneWithContext
import org.evomaster.core.search.impact.impactinfocollection.GeneImpact
import org.evomaster.core.search.impact.impactinfocollection.ImpactUtils
import org.evomaster.core.search.impact.impactinfocollection.sql.NullableImpact
import org.evomaster.core.search.impact.impactinfocollection.value.ObjectGeneImpact
import org.evomaster.core.search.impact.impactinfocollection.value.StringGeneImpact
import org.evomaster.core.search.impact.impactinfocollection.value.collection.ArrayGeneImpact
Expand Down Expand Up @@ -186,7 +187,7 @@ class DynamoDbInsertBuilderTest {
}

@Test
fun testNestedLeavesExtractedIntoImmutableGeneTree() {
fun testNestedLeavesExtractedIntoDocumentGeneTree() {
val payload = mapOf(
"profile" to mapOf(
"role" to "admin",
Expand All @@ -213,12 +214,12 @@ class DynamoDbInsertBuilderTest {
assertEquals("admin", stringMap["role"]!!.value)

val numberGenes = leaves.filterIsInstance<BigDecimalGene>()
assertEquals(3, numberGenes.size)
assertEquals(4, numberGenes.size)
assertTrue(numberGenes.any { it.value == BigDecimal("99.5") })

val boolGenes = leaves.filterIsInstance<BooleanGene>()
assertEquals(1, boolGenes.size)
assertTrue(boolGenes.first().value)
assertEquals(2, boolGenes.size)
assertTrue(boolGenes.single { it.name == "active" }.value)

val tagsGene = rootGene.fixedFields.first { it.name == "tags" } as HeterogeneousListGene
assertFalse(tagsGene.isFixed)
Expand All @@ -240,9 +241,14 @@ class DynamoDbInsertBuilderTest {
assertTrue(emptyListGene.elements.isEmpty())
assertTrue(emptyListGene.isMutable())

val nullGene = rootGene.fixedFields.first { it.name == "nothing" } as ImmutableDataHolderGene
assertEquals("null", nullGene.value)
assertFalse(nullGene.inQuotes)
val nullGene = rootGene.fixedFields.first { it.name == "nothing" } as NullableGene
assertFalse(nullGene.isActive)
assertTrue(nullGene.isMutable())
val choice = nullGene.gene as ChoiceGene<*>
assertEquals(
listOf(StringGene::class, BigDecimalGene::class, BooleanGene::class),
choice.getViewOfChildren().map { it::class }
)

assertEquals(dto, action.attributes.single().toDto())
}
Expand Down Expand Up @@ -276,7 +282,7 @@ class DynamoDbInsertBuilderTest {
assertTrue(ImpactUtils.createGeneImpact(tagsGene.elements[2], "tags_2") is HeterogeneousListGeneImpact)
assertTrue(ImpactUtils.createGeneImpact(tagsGene.elements[3], "tags_3") is ArrayGeneImpact)
assertTrue(ImpactUtils.createGeneImpact(tagsGene.elements[4], "tags_4") is ArrayGeneImpact)
assertTrue(ImpactUtils.createGeneImpact(tagsGene.elements[5], "tags_5") is GeneImpact)
assertTrue(ImpactUtils.createGeneImpact(tagsGene.elements[5], "tags_5") is NullableImpact)
}

@Test
Expand Down Expand Up @@ -357,14 +363,14 @@ class DynamoDbInsertBuilderTest {
val action = DynamoDbInsertBuilder.buildInsertActions(listOf(DynamoDbFailedQuery("Items", listOf(dto))), Randomness()).single()
val objGene = action.attributes.single().gene as ObjectGene

assertEquals(53, objGene.flatView().filterIsInstance<StringGene>().size)
assertEquals(54, objGene.flatView().filterIsInstance<StringGene>().size)
val z50 = objGene.fixedFields.first { it.name == "z50_str_null" } as StringGene
val z51 = objGene.fixedFields.first { it.name == "z51_num" } as BigDecimalGene
val z52 = objGene.fixedFields.first { it.name == "z52_bool" } as BooleanGene
val z53 = objGene.fixedFields.first { it.name == "z53_ss" } as ProtectedUniqueArrayGene<*>
val z54 = objGene.fixedFields.first { it.name == "z54_ns" } as ProtectedUniqueArrayGene<*>
val z55 = objGene.fixedFields.first { it.name == "z55_empty_list" } as HeterogeneousListGene
val z56 = objGene.fixedFields.first { it.name == "z56_null" } as ImmutableDataHolderGene
val z56 = objGene.fixedFields.first { it.name == "z56_null" } as NullableGene

assertTrue(z50.isMutable())
assertEquals("null", z50.value)
Expand All @@ -383,9 +389,8 @@ class DynamoDbInsertBuilderTest {
assertTrue(z55.isMutable())
assertTrue(z55.elements.isEmpty())

assertFalse(z56.isMutable())
assertEquals("null", z56.value)
assertFalse(z56.inQuotes)
assertTrue(z56.isMutable())
assertFalse(z56.isActive)

val roundTripped = action.attributes.single().toDto()
assertEquals(dto, roundTripped)
Expand Down
Loading
Loading