1
1
package org.utbot.framework.context.spring
2
2
3
3
import mu.KotlinLogging
4
- import org.utbot.common.tryLoadClass
5
4
import org.utbot.framework.context.ConcreteExecutionContext
5
+ import org.utbot.framework.context.JavaFuzzingContext
6
6
import org.utbot.framework.plugin.api.ClassId
7
7
import org.utbot.framework.plugin.api.ConcreteContextLoadingResult
8
- import org.utbot.framework.plugin.api.ConstructorId
9
- import org.utbot.framework.plugin.api.EnvironmentModels
10
- import org.utbot.framework.plugin.api.ExecutableId
11
- import org.utbot.framework.plugin.api.FieldId
12
- import org.utbot.framework.plugin.api.MethodId
13
- import org.utbot.framework.plugin.api.SpringRepositoryId
14
8
import org.utbot.framework.plugin.api.SpringSettings
15
9
import org.utbot.framework.plugin.api.UtExecution
16
- import org.utbot.framework.plugin.api.UtModel
17
- import org.utbot.framework.plugin.api.util.SpringModelUtils
18
- import org.utbot.framework.plugin.api.util.SpringModelUtils.createMockMvcModel
19
- import org.utbot.framework.plugin.api.util.SpringModelUtils.createRequestBuilderModelOrNull
20
- import org.utbot.framework.plugin.api.util.SpringModelUtils.mockMvcPerformMethodId
21
- import org.utbot.framework.plugin.api.util.allDeclaredFieldIds
22
- import org.utbot.framework.plugin.api.util.jField
23
- import org.utbot.framework.plugin.api.util.utContext
24
- import org.utbot.fuzzer.IdGenerator
25
10
import org.utbot.fuzzer.IdentityPreservingIdGenerator
26
- import org.utbot.fuzzing.JavaValueProvider
27
- import org.utbot.fuzzing.ValueProvider
28
- import org.utbot.fuzzing.providers.AnyDepthNullValueProvider
29
- import org.utbot.fuzzing.spring.GeneratedFieldValueProvider
30
- import org.utbot.fuzzing.spring.SpringBeanValueProvider
31
- import org.utbot.fuzzing.spring.preserveProperties
32
- import org.utbot.fuzzing.spring.valid.EmailValueProvider
33
- import org.utbot.fuzzing.spring.valid.NotBlankStringValueProvider
34
- import org.utbot.fuzzing.spring.valid.NotEmptyStringValueProvider
35
- import org.utbot.fuzzing.spring.valid.ValidEntityValueProvider
36
11
import org.utbot.instrumentation.ConcreteExecutor
37
12
import org.utbot.instrumentation.getRelevantSpringRepositories
38
13
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
39
14
import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrumentation
40
15
import org.utbot.instrumentation.instrumentation.spring.SpringUtExecutionInstrumentation
41
16
import org.utbot.instrumentation.tryLoadingSpringContext
42
17
import java.io.File
43
- import org.utbot.fuzzing.providers.ObjectValueProvider
44
- import org.utbot.fuzzing.providers.anyObjectValueProvider
45
18
46
19
class SpringIntegrationTestConcreteExecutionContext (
47
20
private val delegateContext : ConcreteExecutionContext ,
@@ -79,11 +52,11 @@ class SpringIntegrationTestConcreteExecutionContext(
79
52
classUnderTestId : ClassId
80
53
): List <UtExecution > = delegateContext.transformExecutionsBeforeMinimization(executions, classUnderTestId)
81
54
82
- override fun tryCreateValueProvider (
55
+ override fun tryCreateFuzzingContext (
83
56
concreteExecutor : ConcreteExecutor <UtConcreteExecutionResult , UtExecutionInstrumentation >,
84
57
classUnderTest : ClassId ,
85
58
idGenerator : IdentityPreservingIdGenerator <Int >
86
- ): JavaValueProvider {
59
+ ): JavaFuzzingContext {
87
60
if (springApplicationContext.getBeansAssignableTo(classUnderTest).isEmpty())
88
61
error(
89
62
" No beans of type ${classUnderTest.name} are found. " +
@@ -94,77 +67,10 @@ class SpringIntegrationTestConcreteExecutionContext(
94
67
val relevantRepositories = concreteExecutor.getRelevantSpringRepositories(classUnderTest)
95
68
logger.info { " Detected relevant repositories for class $classUnderTest : $relevantRepositories " }
96
69
97
- val springBeanValueProvider = SpringBeanValueProvider (
98
- idGenerator,
99
- beanNameProvider = { classId ->
100
- springApplicationContext.getBeansAssignableTo(classId).map { it.beanName }
101
- },
102
- relevantRepositories = relevantRepositories
70
+ return SpringIntegrationTestJavaFuzzingContext (
71
+ delegateContext = delegateContext.tryCreateFuzzingContext(concreteExecutor, classUnderTest, idGenerator),
72
+ relevantRepositories = relevantRepositories,
73
+ springApplicationContext = springApplicationContext,
103
74
)
104
-
105
- return springBeanValueProvider
106
- .withFallback(ValidEntityValueProvider (idGenerator, onlyAcceptWhenValidIsRequired = true ))
107
- .withFallback(EmailValueProvider ())
108
- .withFallback(NotBlankStringValueProvider ())
109
- .withFallback(NotEmptyStringValueProvider ())
110
- .withFallback(
111
- delegateContext.tryCreateValueProvider(concreteExecutor, classUnderTest, idGenerator)
112
- .except { p -> p is ObjectValueProvider }
113
- .with (anyObjectValueProvider(idGenerator, shouldMutateWithMethods = true ))
114
- .with (ValidEntityValueProvider (idGenerator, onlyAcceptWhenValidIsRequired = false ))
115
- .with (createGeneratedFieldValueProviders(relevantRepositories, idGenerator))
116
- .withFallback(AnyDepthNullValueProvider )
117
- )
118
- .preserveProperties()
119
- }
120
-
121
- private fun createGeneratedFieldValueProviders (
122
- relevantRepositories : Set <SpringRepositoryId >,
123
- idGenerator : IdentityPreservingIdGenerator <Int >
124
- ): JavaValueProvider {
125
- val generatedValueAnnotationClasses = SpringModelUtils .generatedValueClassIds.mapNotNull {
126
- @Suppress(" UNCHECKED_CAST" ) // type system fails to understand that @GeneratedValue is indeed an annotation
127
- utContext.classLoader.tryLoadClass(it.name) as Class <out Annotation >?
128
- }
129
-
130
- val generatedValueFields =
131
- relevantRepositories
132
- .flatMap { springRepositoryId ->
133
- val entityClassId = springRepositoryId.entityClassId
134
- entityClassId.allDeclaredFieldIds
135
- .filter { fieldId -> generatedValueAnnotationClasses.any { fieldId.jField.isAnnotationPresent(it) } }
136
- .map { entityClassId to it }
137
- }
138
-
139
- logger.info { " Detected @GeneratedValue fields: $generatedValueFields " }
140
-
141
- return ValueProvider .of(generatedValueFields.map { (entityClassId, fieldId) ->
142
- GeneratedFieldValueProvider (idGenerator, entityClassId, fieldId)
143
- })
144
- }
145
-
146
- override fun createStateBefore (
147
- thisInstance : UtModel ? ,
148
- parameters : List <UtModel >,
149
- statics : Map <FieldId , UtModel >,
150
- executableToCall : ExecutableId ,
151
- idGenerator : IdGenerator <Int >
152
- ): EnvironmentModels {
153
- val delegateStateBefore = delegateContext.createStateBefore(thisInstance, parameters, statics, executableToCall, idGenerator)
154
- return when (executableToCall) {
155
- is ConstructorId -> delegateStateBefore
156
- is MethodId -> {
157
- val requestBuilderModel = createRequestBuilderModelOrNull(
158
- methodId = executableToCall,
159
- arguments = parameters,
160
- idGenerator = { idGenerator.createId() }
161
- ) ? : return delegateStateBefore
162
- delegateStateBefore.copy(
163
- thisInstance = createMockMvcModel { idGenerator.createId() },
164
- parameters = listOf (requestBuilderModel),
165
- executableToCall = mockMvcPerformMethodId,
166
- )
167
- }
168
- }
169
75
}
170
76
}
0 commit comments