Skip to content

Commit

Permalink
introducing DnsAction as an init group
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuri82 committed Oct 18, 2023
1 parent e867b94 commit 370bf9f
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class ApiWsIndividual (
*/
children: MutableList<out ActionComponent>,
childTypeVerifier: (Class<*>) -> Boolean,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children, children.size, 0)
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children, children.size, 0, 0, 0)
): EnterpriseIndividual(sampleType, trackOperator, index, children, childTypeVerifier, groups){


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.evomaster.core.sql.SqlActionUtils
import org.evomaster.core.mongo.MongoDbAction
import org.evomaster.core.problem.api.ApiWsIndividual
import org.evomaster.core.problem.externalservice.ApiExternalServiceAction
import org.evomaster.core.problem.externalservice.DnsAction
import org.evomaster.core.search.*
import org.evomaster.core.search.gene.utils.GeneUtils
import org.evomaster.core.search.service.Randomness
Expand Down Expand Up @@ -46,7 +47,10 @@ abstract class EnterpriseIndividual(
*/
children: MutableList<out ActionComponent>,
childTypeVerifier: (Class<*>) -> Boolean,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children,children.size,0)
/**
* if no group definition is specified, then it is assumed that all action are for the MAIN group
*/
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children,children.size,0, 0, 0)
) : Individual(
trackOperator,
index,
Expand All @@ -59,52 +63,70 @@ abstract class EnterpriseIndividual(

/**
* Return group definition for the given children.
* The first [sizeDb] are assumed to be database actions, followed by [sizeMain] main actions
* The first [sizeSQL] are assumed to be database actions, followed by [sizeMain] main actions
*/
fun getEnterpriseTopGroups(
children: List<ActionComponent>,
sizeMain: Int,
sizeDb: Int
sizeSQL: Int,
sizeMongo: Int,
sizeDNS: Int
) : GroupsOfChildren<StructuralElement>{

if(children.size != sizeDb + sizeMain){
if(children.size != sizeSQL +sizeMongo + sizeDNS + sizeMain){
throw IllegalArgumentException("Group size mismatch. Expected a total of ${children.size}, but" +
" got main=$sizeMain and db=$sizeDb")
" got main=$sizeMain, sql=$sizeSQL, mongo=$sizeMongo, dns=$sizeDNS")
}
if(sizeSQL < 0){
throw IllegalArgumentException("Negative size for sizeSQL: $sizeSQL")
}
if(sizeMongo < 0){
throw IllegalArgumentException("Negative size for sizeMongo: $sizeMain")
}
if(sizeDb < 0){
throw IllegalArgumentException("Negative size for sizeDb: $sizeDb")
if(sizeDNS < 0){
throw IllegalArgumentException("Negative size for sizeDNS: $sizeMain")
}
if(sizeMain < 0){
throw IllegalArgumentException("Negative size for sizeMain: $sizeMain")
}

//TODO in future ll need to refactor to handle multiple databases and NoSQL ones
//CHANGE: This is momentary. Needs refactor to handle multiple databases
/*
TODO in future ll need to refactor to handle multiple databases, possibly handled with
one action group per database...
but is it really common that a SUT directly access several databases of same kind?
*/

val startIndexSQL = children.indexOfFirst { a -> a is SqlAction }
val endIndexSQL = children.indexOfLast { a -> a is SqlAction }
val startIndexMongo = children.indexOfFirst { a -> a is MongoDbAction }
val endIndexMongo = children.indexOfLast { a -> a is MongoDbAction }

val db = ChildGroup<StructuralElement>(GroupsOfChildren.INITIALIZATION_SQL,{e -> e is ActionComponent && e.flatten().all { a -> a is SqlAction }},
if(sizeDb==0) -1 else startIndexSQL , if(sizeDb==0) -1 else endIndexSQL
if(sizeSQL==0) -1 else startIndexSQL , if(sizeSQL==0) -1 else endIndexSQL
)

val startIndexMongo = children.indexOfFirst { a -> a is MongoDbAction }
val endIndexMongo = children.indexOfLast { a -> a is MongoDbAction }
val mongodb = ChildGroup<StructuralElement>(GroupsOfChildren.INITIALIZATION_MONGO,{e -> e is ActionComponent && e.flatten().all { a -> a is MongoDbAction }},
if(sizeDb==0) -1 else startIndexMongo , if(sizeDb==0) -1 else endIndexMongo
if(sizeMongo==0) -1 else startIndexMongo , if(sizeMongo==0) -1 else endIndexMongo
)

val startIndexDns = children.indexOfFirst { a -> a is DnsAction }
val endIndexDns = children.indexOfLast { a -> a is DnsAction }
val dns = ChildGroup<StructuralElement>(GroupsOfChildren.INITIALIZATION_DNS,{e -> e is ActionComponent && e.flatten().all { a -> a is DnsAction }},
if(sizeDNS==0) -1 else startIndexDns , if(sizeDNS==0) -1 else endIndexDns
)

val main = ChildGroup<StructuralElement>(GroupsOfChildren.MAIN, {e -> e !is SqlAction && e !is ApiExternalServiceAction },
if(sizeMain == 0) -1 else sizeDb, if(sizeMain == 0) -1 else sizeDb + sizeMain - 1)
val initSize = sizeSQL+sizeMongo+sizeDNS

val main = ChildGroup<StructuralElement>(GroupsOfChildren.MAIN, {e -> e !is EnvironmentAction },
if(sizeMain == 0) -1 else initSize, if(sizeMain == 0) -1 else initSize + sizeMain - 1)

return GroupsOfChildren(children, listOf(db, mongodb, main))
return GroupsOfChildren(children, listOf(db, mongodb, dns, main))
}
}

/**
* a list of db actions for its Initialization
*/
private val dbInitialization: List<SqlAction>
private val sqlInitialization: List<SqlAction>
get() {
return groupsView()!!.getAllInGroup(GroupsOfChildren.INITIALIZATION_SQL)
.flatMap { (it as ActionComponent).flatten() }
Expand All @@ -117,9 +139,10 @@ abstract class EnterpriseIndividual(
ActionFilter.MAIN_EXECUTABLE -> groupsView()!!.getAllInGroup(GroupsOfChildren.MAIN)
.flatMap { (it as ActionComponent).flatten() }
.filter { it !is SqlAction && it !is ApiExternalServiceAction }
ActionFilter.INIT -> groupsView()!!.getAllInGroup(GroupsOfChildren.INITIALIZATION_SQL)
.flatMap { (it as ActionComponent).flatten() } + groupsView()!!.getAllInGroup(GroupsOfChildren.INITIALIZATION_MONGO)
.flatMap { (it as ActionComponent).flatten() }
ActionFilter.INIT ->
groupsView()!!
.getAllInGroup(GroupsOfChildren.INITIALIZATION_SQL).flatMap { (it as ActionComponent).flatten() } + groupsView()!!
.getAllInGroup(GroupsOfChildren.INITIALIZATION_MONGO).flatMap { (it as ActionComponent).flatten()}
// WARNING: this can still return DbAction, MongoDbAction and External ones...
ActionFilter.NO_INIT -> groupsView()!!.getAllInGroup(GroupsOfChildren.MAIN).flatMap { (it as ActionComponent).flatten() }
ActionFilter.ONLY_SQL -> seeAllActions().filterIsInstance<SqlAction>()
Expand Down Expand Up @@ -214,15 +237,15 @@ abstract class EnterpriseIndividual(
if (!verifyInitializationActions()) {
if (log.isTraceEnabled)
log.trace("invoke GeneUtils.repairBrokenDbActionsList")
val previous = dbInitialization.toMutableList()
val previous = sqlInitialization.toMutableList()
SqlActionUtils.repairBrokenDbActionsList(previous, randomness)
resetInitializingActions(previous)
Lazy.assert{verifyInitializationActions()}
}
}

override fun hasAnyAction(): Boolean {
return super.hasAnyAction() || dbInitialization.isNotEmpty()
return super.hasAnyAction() || sqlInitialization.isNotEmpty()
}

override fun size() = seeMainExecutableActions().size
Expand Down Expand Up @@ -266,7 +289,7 @@ abstract class EnterpriseIndividual(
}

/**
* remove specified dbactions i.e., [actions] from [dbInitialization]
* remove specified dbactions i.e., [actions] from [sqlInitialization]
*/
fun removeInitDbActions(actions: List<SqlAction>) {
killChildren { it is SqlAction && actions.contains(it)}
Expand All @@ -276,6 +299,6 @@ abstract class EnterpriseIndividual(
* @return a list table names which are used to insert data directly
*/
open fun getInsertTableNames(): List<String>{
return dbInitialization.filterNot { it.representExistingData }.map { it.table.name }
return sqlInitialization.filterNot { it.representExistingData }.map { it.table.name }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.evomaster.core.problem.externalservice

import org.evomaster.core.search.EnvironmentAction
import org.evomaster.core.search.StructuralElement
import org.evomaster.core.search.gene.Gene


//TODO
class DnsAction(children: List<StructuralElement>) : EnvironmentAction(children) {
override fun getName(): String {
TODO("Not yet implemented")
}

override fun seeTopGenes(): List<out Gene> {
TODO("Not yet implemented")
}

override fun copyContent(): StructuralElement {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class GraphQLIndividual(
sampleType: SampleType,
allActions : MutableList<out ActionComponent>,
mainSize : Int = allActions.size,
dbSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,dbSize)
sqlSize: Int = 0,
mongoSize: Int = 0,
dnsSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,sqlSize,mongoSize,dnsSize)
) : ApiWsIndividual(
sampleType = sampleType,
children = allActions,
Expand All @@ -35,7 +37,9 @@ class GraphQLIndividual(
sampleType,
children.map { it.copy() }.toMutableList() as MutableList<ActionComponent>,
mainSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.MAIN),
dbSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL)
sqlSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL),
mongoSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_MONGO),
dnsSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_DNS)
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ abstract class GuiIndividual (
*/
children: MutableList<out ActionComponent>,
childTypeVerifier: (Class<*>) -> Boolean,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children, children.size, 0)
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children, children.size, 0, 0 ,0)
): EnterpriseIndividual(sampleType, trackOperator, index, children, childTypeVerifier, groups)
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class RestIndividual(
index : Int = -1,
allActions : MutableList<out ActionComponent>,
mainSize : Int = allActions.size,
dbSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,dbSize)
sqlSize: Int = 0,
mongoSize: Int = 0,
dnsSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions,mainSize,sqlSize,mongoSize,dnsSize)
): ApiWsIndividual(sampleType, trackOperator, index, allActions,
childTypeVerifier = {
RestResourceCalls::class.java.isAssignableFrom(it)
Expand Down Expand Up @@ -81,8 +83,9 @@ class RestIndividual(
index,
children.map { it.copy() }.toMutableList() as MutableList<out ActionComponent>,
mainSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.MAIN),
//CHANGE: This is momentary for testing. Needs refactor to handle multiple databases
dbSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_MONGO ) + groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL )
sqlSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL),
mongoSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_MONGO),
dnsSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_DNS)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class RPCIndividual(
index: Int = -1,
allActions: MutableList<ActionComponent>,
mainSize: Int = allActions.size,
dbSize: Int = 0,
groups: GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions, mainSize, dbSize)
sqlSize: Int = 0,
mongoSize: Int = 0,
dnsSize: Int = 0,
groups: GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(allActions, mainSize, sqlSize,mongoSize,dnsSize)
) : ApiWsIndividual(
sampleType,
trackOperator, index, allActions,
Expand Down Expand Up @@ -65,7 +67,7 @@ class RPCIndividual(
)
}})
},
mainSize = actions.size, dbSize = dbInitialization.size)
mainSize = actions.size, sqlSize = dbInitialization.size)

/**
* TODO: Verify the implementation
Expand Down Expand Up @@ -126,7 +128,9 @@ class RPCIndividual(
index,
children.map { it.copy() }.toMutableList() as MutableList<ActionComponent>,
mainSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.MAIN),
dbSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL)
sqlSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL),
mongoSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_MONGO),
dnsSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_DNS)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class WebIndividual(
sampleType: SampleType,
children: MutableList<out ActionComponent>,
mainSize : Int = children.size,
dbSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children,mainSize,dbSize)
sqlSize: Int = 0,
mongoSize: Int = 0,
dnsSize: Int = 0,
groups : GroupsOfChildren<StructuralElement> = getEnterpriseTopGroups(children,mainSize,sqlSize,mongoSize,dnsSize)
) : GuiIndividual(
sampleType = sampleType,
children = children,
Expand All @@ -31,7 +33,9 @@ class WebIndividual(
sampleType,
children.map { it.copy() }.toMutableList() as MutableList<ActionComponent>,
mainSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.MAIN),
dbSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL)
sqlSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_SQL),
mongoSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_MONGO),
dnsSize = groupsView()!!.sizeOfGroup(GroupsOfChildren.INITIALIZATION_DNS)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.evomaster.core.search.action.Action


/**
* An action used to setup the environment of a test: eg databases and external services
* An action used to set up the environment of a test: eg databases and external services
*/
abstract class EnvironmentAction(children: List<StructuralElement>) : Action(children){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class GroupsOfChildren<T>(

const val INITIALIZATION_MONGO = "INITIALIZATION_MONGO"

const val INITIALIZATION_DNS = "INITIALIZATION_DNS"

const val EXTERNAL_SERVICES = "EXTERNAL_SERVICES"

const val RESOURCE_SQL = "RESOURCE_SQL"
Expand Down

0 comments on commit 370bf9f

Please sign in to comment.