Skip to content
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

Add support for @CookieValue annotation #2519

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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 @@ -125,6 +125,7 @@ object SpringModelUtils {
private val requestMappingClassId = ClassId("org.springframework.web.bind.annotation.RequestMapping")
private val pathVariableClassId = ClassId("org.springframework.web.bind.annotation.PathVariable")
private val requestHeaderClassId = ClassId("org.springframework.web.bind.annotation.RequestHeader")
private val cookieValueClassId = ClassId("org.springframework.web.bind.annotation.CookieValue")
private val requestBodyClassId = ClassId("org.springframework.web.bind.annotation.RequestBody")
private val requestParamClassId = ClassId("org.springframework.web.bind.annotation.RequestParam")
private val uriComponentsBuilderClassId = ClassId("org.springframework.web.util.UriComponentsBuilder")
Expand All @@ -148,6 +149,7 @@ object SpringModelUtils {
private val httpHeaderClassId = ClassId("org.springframework.http.HttpHeaders")

private val objectMapperClassId = ClassId("com.fasterxml.jackson.databind.ObjectMapper")
private val cookieClassId = ClassId("javax.servlet.http.Cookie")

val mockMvcPerformMethodId = MethodId(
classId = mockMvcClassId,
Expand Down Expand Up @@ -323,10 +325,45 @@ object SpringModelUtils {

requestBuilderModel = addHeadersToRequestBuilderModel(headersContentModel, requestBuilderModel, idGenerator)

// TODO #2462 (support @CookieValue, @RequestAttribute, ...)
val cookieClass = utContext.classLoader.loadClass(cookieClassId.name)
val cookieArrayClassId = java.lang.reflect.Array.newInstance(cookieClass,0)::class.java.id
val cookieValuesModel = createCookieValuesModel(cookieArrayClassId, methodId, arguments, idGenerator)

requestBuilderModel =
addCookiesToRequestBuilderModel(cookieValuesModel, cookieArrayClassId, requestBuilderModel, idGenerator)

return addContentToRequestBuilderModel(methodId, arguments, requestBuilderModel, idGenerator)
}

private fun addCookiesToRequestBuilderModel(
cookieValuesModel: UtArrayModel,
cookieArrayClassId: ClassId,
requestBuilderModel: UtAssembleModel,
idGenerator: () -> Int
): UtAssembleModel {
@Suppress("NAME_SHADOWING")
var requestBuilderModel = requestBuilderModel

if(cookieValuesModel.length > 0) {
requestBuilderModel = UtAssembleModel(
id = idGenerator(),
classId = mockHttpServletRequestBuilderClassId,
modelName = "requestBuilder",
instantiationCall = UtExecutableCallModel(
instance = requestBuilderModel,
executable = MethodId(
classId = mockHttpServletRequestBuilderClassId,
name = "cookie",
returnType = mockHttpServletRequestBuilderClassId,
parameters = listOf(cookieArrayClassId)
),
params = listOf(cookieValuesModel)
)
)
}
return requestBuilderModel
}

private fun addHeadersToRequestBuilderModel(
headersContentModel: UtAssembleModel,
requestBuilderModel: UtAssembleModel,
Expand Down Expand Up @@ -467,6 +504,39 @@ object SpringModelUtils {
return requestBuilderModel
}

private fun createCookieValuesModel(
cookieArrayClassId: ClassId,
methodId: MethodId,
arguments: List<UtModel>,
idGenerator: () -> Int,
): UtArrayModel {
val cookieValues = collectArgumentsWithAnnotationModels(methodId, cookieValueClassId, arguments)
.mapValues { (_, model) -> convertModelValueToString(model) }.toList()

// Creating an indexed Map for `UtArrayModel.stores`
val indexedCookieValues = HashMap<Int, UtModel>()
cookieValues.indices.forEach { ind ->
indexedCookieValues[ind] = UtAssembleModel(
id = idGenerator(),
classId = cookieClassId,
modelName = "cookie",
instantiationCall = UtExecutableCallModel(
instance = null,
executable = constructorId(cookieClassId, stringClassId, stringClassId),
params = listOf(UtPrimitiveModel(cookieValues[ind].first), cookieValues[ind].second),
)
)
}

return UtArrayModel(
id = idGenerator(),
classId = cookieArrayClassId,
length = cookieValues.size,
constModel = UtNullModel(cookieClassId),
stores = indexedCookieValues,
)
}

private fun createHeadersContentModel(
methodId: MethodId,
arguments: List<UtModel>,
Expand Down