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

Test: Storage-module #139

Merged
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 @@ -16,8 +16,9 @@ import org.prebid.cache.functional.util.getRandomString
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.http.HttpStatus.UNAUTHORIZED
import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR

class ModuleStorageSpec : ShouldSpec({
class StorageSpec : ShouldSpec({

lateinit var apiKey: String
lateinit var applicationName: String
Expand All @@ -39,10 +40,10 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

// then: recorded payload should contain the same type and value
val savedPayload = cacheApi.getModuleStorageCache(payloadKey, applicationName, apiKey)
val savedPayload = cacheApi.getStorageCache(payloadKey, applicationName, apiKey)
savedPayload.type shouldBe payloadTransfer.type
savedPayload.value shouldBe payloadTransfer.value

Expand All @@ -60,10 +61,10 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

// then: recorded payload should contain the same type and value
val savedPayload = cacheApi.getModuleStorageCache(payloadKey, applicationName, apiKey)
val savedPayload = cacheApi.getStorageCache(payloadKey, applicationName, apiKey)
savedPayload.type shouldBe payloadTransfer.type
savedPayload.value shouldBe payloadTransfer.value

Expand All @@ -81,10 +82,10 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

// then: recorded payload should contain the same type and value
val savedPayload = cacheApi.getModuleStorageCache(payloadKey, applicationName, apiKey)
val savedPayload = cacheApi.getStorageCache(payloadKey, applicationName, apiKey)
savedPayload.type shouldBe payloadTransfer.type
savedPayload.value shouldBe payloadTransfer.value

Expand All @@ -103,12 +104,12 @@ class ModuleStorageSpec : ShouldSpec({

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> {
cacheApi.postModuleStorageCache(payloadTransfer, apiKey) }
cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Not found exception is thrown
assertSoftly {
exception.statusCode shouldBe NOT_FOUND.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "\"message\":\"Invalid application: ${randomApplication}\""
}
}
Expand All @@ -122,12 +123,12 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) }
val exception = shouldThrowExactly<ApiException> { cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Bad request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "application must not be empty"
}
}
Expand All @@ -141,12 +142,12 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) }
val exception = shouldThrowExactly<ApiException> { cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Bad request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "application must not be empty"
}
}
Expand All @@ -159,12 +160,12 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) }
val exception = shouldThrowExactly<ApiException> { cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Bad request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "key must not be empty"
}
}
Expand All @@ -177,12 +178,12 @@ class ModuleStorageSpec : ShouldSpec({
}

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) }
val exception = shouldThrowExactly<ApiException> { cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Bad request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "key must not be empty"
}
}
Expand All @@ -197,7 +198,7 @@ class ModuleStorageSpec : ShouldSpec({

// when: POST module-storage endpoint is called
val exception =
shouldThrowExactly<ApiException> { cacheApi.postModuleStorageCache(payloadTransfer, getRandomString()) }
shouldThrowExactly<ApiException> { cacheApi.postStorageCache(payloadTransfer, getRandomString()) }

// then: Not found exception is thrown
assertSoftly {
Expand All @@ -214,17 +215,17 @@ class ModuleStorageSpec : ShouldSpec({
}

// and: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

// when: GET module-storage endpoint is called with invalid data
val exception = shouldThrowExactly<ApiException> {
cacheApi.getModuleStorageCache(getRandomString(), applicationName, apiKey)
cacheApi.getStorageCache(getRandomString(), applicationName, apiKey)
}

// then: Not found exception is thrown
assertSoftly {
exception.statusCode shouldBe NOT_FOUND.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "Invalid application or key"
}
}
Expand All @@ -238,20 +239,20 @@ class ModuleStorageSpec : ShouldSpec({
}

// and: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

//and: random application name
val randomApplication = getRandomString()

// when: GET module-storage endpoint is called with invalid data
val exception = shouldThrowExactly<ApiException> {
cacheApi.getModuleStorageCache(payloadKey, randomApplication, apiKey)
cacheApi.getStorageCache(payloadKey, randomApplication, apiKey)
}

// then: Not found exception is thrown
assertSoftly {
exception.statusCode shouldBe NOT_FOUND.value()
exception.responseBody shouldContain "\"path\":\"/module-storage\""
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "\"message\":\"Invalid application: ${randomApplication}\""
}
}
Expand All @@ -265,11 +266,11 @@ class ModuleStorageSpec : ShouldSpec({
}

// and: POST module-storage endpoint is called
cacheApi.postModuleStorageCache(payloadTransfer, apiKey)
cacheApi.postStorageCache(payloadTransfer, apiKey)

// when: GET module-storage endpoint is called with invalid data
val exception = shouldThrowExactly<ApiException> {
cacheApi.getModuleStorageCache(payloadKey, applicationName, getRandomString())
cacheApi.getStorageCache(payloadKey, applicationName, getRandomString())
}

// then: Not found exception is thrown
Expand All @@ -278,4 +279,46 @@ class ModuleStorageSpec : ShouldSpec({
exception.responseBody should beEmpty()
}
}

should("throw an exception when ttlsecond is zero") {
//given: default json payload with application
val payloadKey = getRandomString()
val payloadTransfer = PayloadTransfer.getDefaultJsonPayloadTransfer().apply {
key = payloadKey
application = applicationName
ttlseconds = 0
}

// when: POST module-storage endpoint is called
val exception = shouldThrowExactly<ApiException> {
cacheApi.postStorageCache(payloadTransfer, apiKey) }

// then: Expire time exception is thrown
assertSoftly {
exception.statusCode shouldBe INTERNAL_SERVER_ERROR.value()
exception.responseBody shouldContain "\"path\":\"/storage\""
exception.responseBody shouldContain "\"message\":\"ERR invalid expire time in setex"
}
}

should("not throw an exception when ttlsecond is null and config ttlseconds are present") {
osulzhenko marked this conversation as resolved.
Show resolved Hide resolved
//given: default json payload with application
val payloadKey = getRandomString()
val payloadTransfer = PayloadTransfer.getDefaultJsonPayloadTransfer().apply {
key = payloadKey
application = applicationName
ttlseconds = null
}

// when: POST module-storage endpoint is called
cacheApi.postStorageCache(payloadTransfer, apiKey)

// then: recorded payload should contain the same type and value
val savedPayload = cacheApi.getStorageCache(payloadKey, applicationName, apiKey)
savedPayload.type shouldBe payloadTransfer.type
savedPayload.value shouldBe payloadTransfer.value

// and: shouldn't contain information about application
savedPayload.application?.should(beNull())
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ class PrebidCacheApi(prebidCacheHost: String, prebidCachePort: Int) {
suspend fun postCache(requestObject: RequestObject, secondaryCache: String? = null): ResponseObject =
post(CACHE_ENDPOINT, requestObject, mapOf(SECONDARY_CACHE_QUERY_PARAMETER to secondaryCache)).body()

suspend fun getModuleStorageCache(
suspend fun getStorageCache(
payloadTransferKey: String?,
application: String?,
apiKey: String?
): PayloadTransfer =
get(
MODULE_STORAGE_ENDPOINT,
STORAGE_ENDPOINT,
mapOf(KEY_PARAMETER to payloadTransferKey, APPLICATION_PARAMETER to application),
mapOf(API_KEY_PARAMETER to apiKey)
).body()

suspend fun postModuleStorageCache(requestObject: PayloadTransfer, apiKey: String? = null): Boolean =
suspend fun postStorageCache(requestObject: PayloadTransfer, apiKey: String? = null): Boolean =
post(
MODULE_STORAGE_ENDPOINT,
STORAGE_ENDPOINT,
requestObject,
headers = mapOf(API_KEY_PARAMETER to apiKey)
).status == HttpStatusCode.NoContent
Expand Down Expand Up @@ -102,7 +102,7 @@ class PrebidCacheApi(prebidCacheHost: String, prebidCachePort: Int) {
private const val PROXY_CACHE_HOST_QUERY_PARAMETER = "ch"
private const val SECONDARY_CACHE_QUERY_PARAMETER = "secondaryCache"

private const val MODULE_STORAGE_ENDPOINT = "/module-storage"
private const val STORAGE_ENDPOINT = "/storage"
private const val API_KEY_PARAMETER = "x-pbc-api-key"
private const val KEY_PARAMETER = "k"
private const val APPLICATION_PARAMETER = "a"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ class PrebidCacheContainerConfig(private val redisHost: String, private val aero
private fun getModuleStorageRedisConfig(
apiKey: String,
applicationName: String,
timeoutMs: Long = 9999L
timeoutMs: Long = 9999L,
endpoint: String = "/storage"
): Map<String, String> =
mapOf(
"api.api-key" to apiKey,
"module.storage.redis.${applicationName}.port" to RedisContainer.PORT.toString(),
"module.storage.redis.${applicationName}.host" to redisHost,
"module.storage.redis.${applicationName}.timeout" to timeoutMs.toString(),
"api.storage-path" to endpoint,
"storage.redis.${applicationName}.port" to RedisContainer.PORT.toString(),
"storage.redis.${applicationName}.host" to redisHost,
"storage.redis.${applicationName}.timeout" to timeoutMs.toString(),
"storage.default-ttl-seconds" to 1000L.toString()
)

private fun getBaseConfig(allowExternalUuid: String): Map<String, String> =
Expand Down