Skip to content

Commit 52e7c01

Browse files
committed
test case for merging two individuals with initialization actions that represent existing data
1 parent 39d2fcb commit 52e7c01

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package org.evomaster.core.problem.rest.service
2+
3+
import org.evomaster.core.problem.enterprise.SampleType
4+
import org.evomaster.core.problem.rest.data.HttpVerb
5+
import org.evomaster.core.problem.rest.data.RestCallAction
6+
import org.evomaster.core.problem.rest.data.RestIndividual
7+
import org.evomaster.core.problem.rest.data.RestPath
8+
import org.evomaster.core.search.gene.numeric.IntegerGene
9+
import org.evomaster.core.search.gene.placeholder.ImmutableDataHolderGene
10+
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
11+
import org.evomaster.core.sql.SqlAction
12+
import org.evomaster.core.sql.schema.Column
13+
import org.evomaster.core.sql.schema.ColumnDataType
14+
import org.evomaster.core.sql.schema.Table
15+
import org.evomaster.core.sql.schema.TableId
16+
import org.junit.jupiter.api.Assertions.assertEquals
17+
import org.junit.jupiter.api.Test
18+
19+
class RestIndividualBuilderTest {
20+
21+
@Test
22+
fun mergeCombinesMainActionsAndKeepsOrder() {
23+
// create two simple RestCallAction with no parameters
24+
val callA = RestCallAction("idA", HttpVerb.GET, RestPath("/a"), mutableListOf())
25+
val callB = RestCallAction("idB", HttpVerb.GET, RestPath("/b"), mutableListOf())
26+
27+
// build individuals from single actions
28+
val first = RestIndividual(actions = mutableListOf(callA), sampleType = SampleType.RANDOM)
29+
val second = RestIndividual(actions = mutableListOf(callB), sampleType = SampleType.RANDOM)
30+
31+
// merge
32+
val merged = RestIndividualBuilder.merge(first, second)
33+
34+
// main executable actions should contain both actions in order
35+
val mains = merged.seeMainExecutableActions()
36+
assertEquals(2, mains.size)
37+
assertEquals(callA.getName(), mains[0].getName())
38+
assertEquals(callB.getName(), mains[1].getName())
39+
40+
// merged total actions should equal sum of both (no initializing actions present)
41+
val before = first.seeAllActions().size + second.seeAllActions().size
42+
assertEquals(before, merged.seeAllActions().size)
43+
}
44+
45+
@Test
46+
fun mergeInitActionsAndKeepsOrder() {
47+
// create two simple RestCallAction with no parameters
48+
val callA = RestCallAction("idA", HttpVerb.GET, RestPath("/a"), mutableListOf())
49+
val callB = RestCallAction("idB", HttpVerb.GET, RestPath("/b"), mutableListOf())
50+
51+
// create a simple table/column for sql initialization
52+
val col = Column(
53+
"ID",
54+
ColumnDataType.INT,
55+
primaryKey = true,
56+
databaseType = org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType.H2
57+
)
58+
val table1 = Table(TableId("T1"), setOf(col), emptySet())
59+
val table2 = Table(TableId("T2"), setOf(col), emptySet())
60+
61+
val sql1 = SqlAction(table1, setOf(col), 1L, computedGenes = listOf(IntegerGene("ID", 1)))
62+
val sql2 = SqlAction(table2, setOf(col), 1L, computedGenes = listOf(IntegerGene("ID", 2)))
63+
64+
// build individuals from single actions with initialization SQL
65+
val first = RestIndividual(actions = mutableListOf(callA), sampleType = SampleType.RANDOM)
66+
val second = RestIndividual(actions = mutableListOf(callB), sampleType = SampleType.RANDOM)
67+
68+
first.addInitializingDbActions(0, listOf(sql1))
69+
second.addInitializingDbActions(0, listOf(sql2))
70+
71+
// merge
72+
val merged = RestIndividualBuilder.merge(first, second)
73+
74+
// main executable actions should contain both actions in order
75+
val mains = merged.seeMainExecutableActions()
76+
assertEquals(2, mains.size)
77+
assertEquals(callA.getName(), mains[0].getName())
78+
assertEquals(callB.getName(), mains[1].getName())
79+
80+
// initializing SQL actions should contain both sql1 and sql2
81+
val inits = merged.seeInitializingActions().filterIsInstance<SqlAction>()
82+
assertEquals(2, inits.size)
83+
// check their table names preserved and order: first's sql then second's sql
84+
assertEquals("T1", inits[0].table.name)
85+
assertEquals("T2", inits[1].table.name)
86+
87+
// merged total actions should be sum of both initial individuals
88+
val before = first.seeAllActions().size + second.seeAllActions().size
89+
assertEquals(before, merged.seeAllActions().size)
90+
}
91+
92+
@Test
93+
fun mergeInitActionsWithExistingDataAndKeepsOrder() {
94+
// create two simple RestCallAction with no parameters
95+
val callA = RestCallAction("idA", HttpVerb.GET, RestPath("/a"), mutableListOf())
96+
val callB = RestCallAction("idB", HttpVerb.GET, RestPath("/b"), mutableListOf())
97+
98+
// create a simple table/column for sql initialization
99+
val col = Column(
100+
"ID",
101+
ColumnDataType.INT,
102+
primaryKey = true,
103+
databaseType = org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType.H2
104+
)
105+
val table1 = Table(TableId("T1"), setOf(col), emptySet())
106+
val table2 = Table(TableId("T2"), setOf(col), emptySet())
107+
108+
109+
val sql1 = SqlAction(
110+
table1,
111+
setOf(col),
112+
1L,
113+
computedGenes = listOf(
114+
SqlPrimaryKeyGene(
115+
"ID",
116+
TableId("T1"),
117+
ImmutableDataHolderGene("ID", "1", inQuotes = false),
118+
uniqueId = 1L
119+
)
120+
),
121+
representExistingData = true
122+
)
123+
val sql2 = SqlAction(
124+
table1,
125+
setOf(col),
126+
1L,
127+
computedGenes = listOf(IntegerGene("ID", 2)),
128+
representExistingData = false
129+
)
130+
131+
val sql3 = SqlAction(
132+
table2,
133+
setOf(col),
134+
1L,
135+
computedGenes = listOf(
136+
SqlPrimaryKeyGene(
137+
"ID", TableId("T2"),
138+
ImmutableDataHolderGene("ID", "3", inQuotes = false),
139+
uniqueId = 2L
140+
)
141+
),
142+
representExistingData = true
143+
)
144+
val sql4 = SqlAction(
145+
table2,
146+
setOf(col),
147+
1L,
148+
computedGenes = listOf(IntegerGene("ID", 4)),
149+
representExistingData = false
150+
)
151+
152+
// build individuals from single actions with initialization SQL
153+
val first = RestIndividual(actions = mutableListOf(callA), sampleType = SampleType.RANDOM)
154+
val second = RestIndividual(actions = mutableListOf(callB), sampleType = SampleType.RANDOM)
155+
156+
first.addInitializingDbActions(0, listOf(sql1, sql2))
157+
second.addInitializingDbActions(0, listOf(sql3, sql4))
158+
159+
// merge
160+
val merged = RestIndividualBuilder.merge(first, second)
161+
162+
// main executable actions should contain both actions in order
163+
val mains = merged.seeMainExecutableActions()
164+
assertEquals(2, mains.size)
165+
assertEquals(callA.getName(), mains[0].getName())
166+
assertEquals(callB.getName(), mains[1].getName())
167+
168+
// initializing SQL actions should contain both sql1 and sql2
169+
val inits = merged.seeInitializingActions().filterIsInstance<SqlAction>()
170+
assertEquals(4, inits.size)
171+
// check their table names preserved and order: first's sql then second's sql
172+
assertEquals("T1", inits[0].table.name)
173+
assertEquals("T2", inits[1].table.name)
174+
175+
// merged total actions should be sum of both initial individuals
176+
val before = first.seeAllActions().size + second.seeAllActions().size
177+
assertEquals(before, merged.seeAllActions().size)
178+
}
179+
180+
181+
}

0 commit comments

Comments
 (0)