@@ -39,10 +39,11 @@ class GeneToDto(
3939 /* *
4040 * @param leafGene to obtain the refType if the component is defined with a name
4141 * @param fallback to provide a fallback on the DTO named with the action if the component is defined inline
42+ * @param capitalize to determine if the DTO string name must be capitalized for test case writing
4243 *
4344 * @return the DTO name that will be used to instantiate the first variable
4445 */
45- fun getDtoName (leafGene : Gene , fallback : String ): String {
46+ fun getDtoName (leafGene : Gene , fallback : String , capitalize : Boolean ): String {
4647 return when (leafGene) {
4748 is ObjectGene -> TestWriterUtils .safeVariableName(leafGene.refType? : fallback)
4849 is ArrayGene <* > -> {
@@ -51,7 +52,7 @@ class GeneToDto(
5152 TestWriterUtils .safeVariableName(template.refType? : fallback)
5253 } else {
5354 // TODO handle arrays of basic data types
54- return getListType(fallback, template)
55+ return getListType(fallback, template, capitalize )
5556 }
5657 }
5758 else -> throw IllegalStateException (" Gene $leafGene is not supported for DTO payloads for action: $fallback " )
@@ -61,20 +62,21 @@ class GeneToDto(
6162 /* *
6263 * @param gene from which to extract the setter calls
6364 * @param dtoName that will be instantiated for payload
64- * @param counter to provide uniqueness under the same DTO being used in a single test case
65+ * @param counter list to provide uniqueness under the same DTO being used in a single test case
66+ * @param capitalize to determine if the DTO string name must be capitalized for test case writing
6567 *
6668 * @return a [DtoCall] object that can be written to the test case
6769 */
68- fun getDtoCall (gene : Gene , dtoName : String , counter : Int ): DtoCall {
70+ fun getDtoCall (gene : Gene , dtoName : String , counter : MutableList < Int >, capitalize : Boolean ): DtoCall {
6971 return when (gene) {
70- is ObjectGene -> getObjectDtoCall(gene, dtoName, counter)
71- is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counter, null )
72+ is ObjectGene -> getObjectDtoCall(gene, dtoName, counter, capitalize )
73+ is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counter, null , capitalize )
7274 else -> throw RuntimeException (" BUG: Gene $gene (with type ${this ::class .java.simpleName} ) should not be creating DTOs" )
7375 }
7476 }
7577
76- private fun getObjectDtoCall (gene : ObjectGene , dtoName : String , counter : Int ): DtoCall {
77- val dtoVarName = " dto_${dtoName} _${counter} "
78+ private fun getObjectDtoCall (gene : ObjectGene , dtoName : String , counter : MutableList < Int >, capitalize : Boolean ): DtoCall {
79+ val dtoVarName = " dto_${dtoName} _${counter.joinToString( " _ " ) } "
7880
7981 val result = mutableListOf<String >()
8082 result.add(dtoOutput.getNewObjectStatement(dtoName, dtoVarName))
@@ -88,13 +90,13 @@ class GeneToDto(
8890 val attributeName = it.name
8991 when (leafGene) {
9092 is ObjectGene -> {
91- val childDtoCall = getDtoCall(leafGene, getDtoName(leafGene, attributeName), counter)
93+ val childDtoCall = getDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counter, true )
9294
9395 result.addAll(childDtoCall.objectCalls)
9496 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
9597 }
9698 is ArrayGene <* > -> {
97- val childDtoCall = getArrayDtoCall(leafGene, getDtoName(leafGene, attributeName), counter, attributeName)
99+ val childDtoCall = getArrayDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counter, attributeName, true )
98100
99101 result.addAll(childDtoCall.objectCalls)
100102 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
@@ -108,19 +110,22 @@ class GeneToDto(
108110 return DtoCall (dtoVarName, result)
109111 }
110112
111- private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counter : Int , targetAttribute : String? ): DtoCall {
113+ private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counter : MutableList < Int > , targetAttribute : String? , capitalize : Boolean ): DtoCall {
112114 val result = mutableListOf<String >()
113115 val template = gene.template
114116
115- val listType = getListType(dtoName,template)
116- val listVarName = " list_${targetAttribute? : dtoName} _${counter} "
117+ val listType = getListType(dtoName,template, capitalize )
118+ val listVarName = " list_${targetAttribute? : dtoName} _${counter.joinToString( " _ " ) } "
117119 result.add(dtoOutput.getNewListStatement(listType, listVarName))
118120
119121 if (template is ObjectGene ) {
120- val childDtoName = template.refType? : StringUtils .capitalization(gene.name)
122+ val childDtoName = template.refType? : if (capitalize) StringUtils .capitalization(dtoName) else dtoName
121123 var listCounter = 1
122124 gene.getViewOfElements().forEach {
123- val childDtoCall = getDtoCall(it,childDtoName, listCounter++ )
125+ val childCounter = mutableListOf<Int >()
126+ childCounter.addAll(counter)
127+ childCounter.add(listCounter++ )
128+ val childDtoCall = getDtoCall(it,childDtoName, childCounter, true )
124129 result.addAll(childDtoCall.objectCalls)
125130 result.add(dtoOutput.getAddElementToListStatement(listVarName, childDtoCall.varName))
126131 }
@@ -134,7 +139,7 @@ class GeneToDto(
134139 return DtoCall (listVarName, result)
135140 }
136141
137- private fun getListType (fieldName : String , gene : Gene ): String {
142+ private fun getListType (fieldName : String , gene : Gene , capitalize : Boolean ): String {
138143 return when (gene) {
139144 is StringGene -> " String"
140145 is IntegerGene -> if (outputFormat.isJava()) " Integer" else " Int"
@@ -148,8 +153,8 @@ class GeneToDto(
148153 is DateTimeGene -> " String"
149154 is RegexGene -> " String"
150155 is BooleanGene -> " Boolean"
151- is ObjectGene -> gene.refType? : StringUtils .capitalization(fieldName)
152- is ArrayGene <* > -> " List<${getListType(gene.name, gene.template)} >"
156+ is ObjectGene -> gene.refType? : if (capitalize) StringUtils .capitalization(fieldName) else fieldName
157+ is ArrayGene <* > -> if (outputFormat.isJava()) " List<${getListType(gene.name, gene.template, capitalize)} > " else " MutableList< ${getListType(gene.name, gene.template, capitalize )} >"
153158 else -> throw Exception (" Not supported gene at the moment: ${gene?.javaClass?.simpleName} for field $fieldName " )
154159 }
155160 }
0 commit comments