Skip to content

Commit

Permalink
Keep thisInstance modifications in controller-specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov committed Aug 28, 2023
1 parent f9c9534 commit 7d12db4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import org.utbot.framework.plugin.api.UtModel
import org.utbot.framework.plugin.api.UtNullModel
import org.utbot.framework.plugin.api.UtPrimitiveModel
import org.utbot.framework.plugin.api.UtSpringContextModel
import org.utbot.framework.plugin.api.UtStatementModel
import org.utbot.framework.plugin.api.mapper.UtModelDeepMapper
import org.utbot.framework.plugin.api.mapper.mapModels
import java.util.Optional

object SpringModelUtils {
Expand Down Expand Up @@ -108,7 +111,12 @@ object SpringModelUtils {
bypassesSandbox = true // TODO may be we can use some alternative sandbox that has more permissions
)

fun createBeanModel(beanName: String, id: Int, classId: ClassId) = UtAssembleModel(
fun createBeanModel(
beanName: String,
id: Int,
classId: ClassId,
modificationChainProvider: UtAssembleModel.() -> List<UtStatementModel> = { mutableListOf() },
) = UtAssembleModel(
id = id,
classId = classId,
modelName = "@Autowired $beanName",
Expand All @@ -117,7 +125,7 @@ object SpringModelUtils {
executable = getBeanMethodId,
params = listOf(UtPrimitiveModel(beanName))
),
modificationsChainProvider = { mutableListOf() }
modificationsChainProvider = modificationChainProvider
)

fun UtModel.isAutowiredFromContext(): Boolean =
Expand Down Expand Up @@ -341,8 +349,19 @@ object SpringModelUtils {
returnType = resultMatcherClassId
)

fun createMockMvcModel(idGenerator: () -> Int) =
createBeanModel("mockMvc", idGenerator(), mockMvcClassId)
fun createMockMvcModel(controller: UtModel?, idGenerator: () -> Int) =
createBeanModel("mockMvc", idGenerator(), mockMvcClassId, modificationChainProvider = {
// we need to keep controller modifications if there are any, so we add them to mockMvc
(controller as? UtAssembleModel)?.let { assembledController ->
val controllerModificationRemover = UtModelDeepMapper { model ->
if (model == assembledController) assembledController.copy(modificationsChain = emptyList())
else model
}
// modificationsChain may mention controller, causing controller modifications to evaluate twice:
// once for mockMvc and once for controller itself, to avoid that we remove modifications from controller
assembledController.modificationsChain.map { it.mapModels(controllerModificationRemover) }
} ?: mutableListOf()
})

fun createRequestBuilderModelOrNull(methodId: MethodId, arguments: List<UtModel>, idGenerator: () -> Int): UtModel? {
check(methodId.parameters.size == arguments.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.utbot.framework.plugin.api.util.utContext
import org.utbot.fuzzer.IdentityPreservingIdGenerator
import org.utbot.fuzzing.JavaValueProvider
import org.utbot.fuzzing.ValueProvider
import org.utbot.fuzzing.providers.AbstractsObjectValueProvider
import org.utbot.fuzzing.providers.AnyDepthNullValueProvider
import org.utbot.fuzzing.providers.ModifyingWithMethodsProviderWrapper
import org.utbot.fuzzing.providers.ObjectValueProvider
Expand Down Expand Up @@ -110,7 +109,7 @@ class SpringIntegrationTestJavaFuzzingContext(
idGenerator = { idGenerator.createId() }
) ?: return delegateStateBefore
delegateStateBefore.copy(
thisInstance = SpringModelUtils.createMockMvcModel { idGenerator.createId() },
thisInstance = SpringModelUtils.createMockMvcModel(controller = thisInstance) { idGenerator.createId() },
parameters = listOf(requestBuilderModel),
executableToCall = SpringModelUtils.mockMvcPerformMethodId,
)
Expand Down

0 comments on commit 7d12db4

Please sign in to comment.