Skip to content

Keep thisInstance modifications in controller-specific tests #2563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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