|
| 1 | +package org.evomaster.core.output.dto |
| 2 | + |
| 3 | +import org.evomaster.core.output.OutputFormat |
| 4 | +import org.evomaster.core.output.TestWriterUtils |
| 5 | +import org.evomaster.core.problem.rest.param.BodyParam |
| 6 | +import org.evomaster.core.search.action.Action |
| 7 | +import org.evomaster.core.search.gene.BooleanGene |
| 8 | +import org.evomaster.core.search.gene.Gene |
| 9 | +import org.evomaster.core.search.gene.ObjectGene |
| 10 | +import org.evomaster.core.search.gene.datetime.DateGene |
| 11 | +import org.evomaster.core.search.gene.datetime.DateTimeGene |
| 12 | +import org.evomaster.core.search.gene.datetime.TimeGene |
| 13 | +import org.evomaster.core.search.gene.numeric.DoubleGene |
| 14 | +import org.evomaster.core.search.gene.numeric.FloatGene |
| 15 | +import org.evomaster.core.search.gene.numeric.IntegerGene |
| 16 | +import org.evomaster.core.search.gene.numeric.LongGene |
| 17 | +import org.evomaster.core.search.gene.regex.RegexGene |
| 18 | +import org.evomaster.core.search.gene.string.Base64StringGene |
| 19 | +import org.evomaster.core.search.gene.string.StringGene |
| 20 | +import org.evomaster.core.search.gene.utils.GeneUtils |
| 21 | +import org.evomaster.core.utils.StringUtils |
| 22 | +import org.slf4j.Logger |
| 23 | +import org.slf4j.LoggerFactory |
| 24 | +import java.nio.file.Path |
| 25 | + |
| 26 | +/** |
| 27 | + * When creating tests for REST APIs, enabling the [EMConfig.dtoForRequestPayload] feature causes EVoMaster to use |
| 28 | + * DTOs when handling request payloads instead of stringifying the JSON payload. |
| 29 | + * |
| 30 | + * Using DTOs provides a major advantage towards code readability and sustainability. It is more flexible and scales |
| 31 | + * better. |
| 32 | + */ |
| 33 | +object DtoWriter { |
| 34 | + |
| 35 | + private val log: Logger = LoggerFactory.getLogger(DtoWriter::class.java) |
| 36 | + |
| 37 | + /** |
| 38 | + * [MutableMap] that will collect the different DTOs to be handled by the test suite. Keys in the map are the |
| 39 | + * DTO name String which translates directly into the .kt or .java class name in filesystem. |
| 40 | + */ |
| 41 | + private val dtoCollector: MutableMap<String, DtoClass> = mutableMapOf() |
| 42 | + |
| 43 | + fun write(testSuitePath: Path, outputFormat: OutputFormat, actionDefinitions: List<Action>) { |
| 44 | + getDtos(actionDefinitions) |
| 45 | + dtoCollector.forEach { |
| 46 | + when { |
| 47 | + outputFormat.isJava() -> JavaDtoWriter.write(testSuitePath, outputFormat, it.value) |
| 48 | + else -> throw IllegalStateException("$outputFormat output format does not support DTOs as request payloads.") |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private fun getDtos(actionDefinitions: List<Action>) { |
| 54 | + actionDefinitions.forEach { action -> |
| 55 | + action.getViewOfChildren().find { it is BodyParam } |
| 56 | + .let { |
| 57 | + val primaryGene = GeneUtils.getWrappedValueGene((it as BodyParam).primaryGene()) |
| 58 | + // TODO: Payloads could also be json arrays, analyze ArrayGene |
| 59 | + if (primaryGene is ObjectGene) { |
| 60 | + getDtoFromObject(primaryGene, action.getName()) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun getDtoFromObject(gene: ObjectGene, actionName: String) { |
| 67 | + // TODO: Determine strategy for objects that are not defined as a component and do not have a name |
| 68 | + val dtoName = gene.refType?:TestWriterUtils.safeVariableName(actionName) |
| 69 | + val dtoClass = DtoClass(dtoName) |
| 70 | + // TODO: add suport for additionalFields |
| 71 | + gene.fixedFields.forEach { field -> |
| 72 | + try { |
| 73 | + val wrappedGene = GeneUtils.getWrappedValueGene(field) |
| 74 | + val dtoField = getDtoField(field.name, wrappedGene) |
| 75 | + dtoClass.addField(dtoField) |
| 76 | + if (wrappedGene is ObjectGene && !dtoCollector.contains(dtoField.type)) { |
| 77 | + getDtoFromObject(wrappedGene, dtoField.type) |
| 78 | + } |
| 79 | + } catch (ex: Exception) { |
| 80 | + log.warn("A failure has occurred when collecting DTOs. \n" |
| 81 | + + "Exception: ${ex.localizedMessage} \n" |
| 82 | + + "At ${ex.stackTrace.joinToString(separator = " \n -> ")}. " |
| 83 | + ) |
| 84 | + assert(false) |
| 85 | + } |
| 86 | + } |
| 87 | + dtoCollector.put(dtoName, dtoClass) |
| 88 | + } |
| 89 | + |
| 90 | + private fun getDtoField(fieldName: String, field: Gene?): DtoField { |
| 91 | + return DtoField(fieldName, when (field) { |
| 92 | + // TODO: handle nested arrays, objects and extend type system for dto fields |
| 93 | + is StringGene -> "String" |
| 94 | + is IntegerGene -> "Integer" |
| 95 | + is LongGene -> "Long" |
| 96 | + is DoubleGene -> "Double" |
| 97 | + is FloatGene -> "Float" |
| 98 | + is Base64StringGene -> "String" |
| 99 | + // Time, Date, DateTime and Regex genes will be handled with strings at the moment. In the future we'll evaluate if it's worth having any validation |
| 100 | + is DateGene -> "String" |
| 101 | + is TimeGene -> "String" |
| 102 | + is DateTimeGene -> "String" |
| 103 | + is RegexGene -> "String" |
| 104 | + is BooleanGene -> "Boolean" |
| 105 | + is ObjectGene -> StringUtils.capitalization(fieldName) |
| 106 | + else -> throw Exception("Not supported gene at the moment: ${field?.javaClass?.simpleName}") |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments