From 920b5acfec47e92d5a3c04f672873bec37648968 Mon Sep 17 00:00:00 2001 From: Markiyan Mykush <95693607+marki1an@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:19:29 +0300 Subject: [PATCH] Test: `Storage-module` (#139) --- .../{ModuleStorageSpec.kt => StorageSpec.kt} | 95 ++++++++++++++----- .../functional/service/PrebidCacheApi.kt | 10 +- .../PrebidCacheContainerConfig.kt | 11 ++- 3 files changed, 81 insertions(+), 35 deletions(-) rename src/test/kotlin/org/prebid/cache/functional/{ModuleStorageSpec.kt => StorageSpec.kt} (73%) diff --git a/src/test/kotlin/org/prebid/cache/functional/ModuleStorageSpec.kt b/src/test/kotlin/org/prebid/cache/functional/StorageSpec.kt similarity index 73% rename from src/test/kotlin/org/prebid/cache/functional/ModuleStorageSpec.kt rename to src/test/kotlin/org/prebid/cache/functional/StorageSpec.kt index e9009a2..09600ca 100644 --- a/src/test/kotlin/org/prebid/cache/functional/ModuleStorageSpec.kt +++ b/src/test/kotlin/org/prebid/cache/functional/StorageSpec.kt @@ -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 @@ -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 @@ -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 @@ -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 @@ -103,12 +104,12 @@ class ModuleStorageSpec : ShouldSpec({ // when: POST module-storage endpoint is called val exception = shouldThrowExactly { - 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}\"" } } @@ -122,12 +123,12 @@ class ModuleStorageSpec : ShouldSpec({ } // when: POST module-storage endpoint is called - val exception = shouldThrowExactly { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) } + val exception = shouldThrowExactly { 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" } } @@ -141,12 +142,12 @@ class ModuleStorageSpec : ShouldSpec({ } // when: POST module-storage endpoint is called - val exception = shouldThrowExactly { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) } + val exception = shouldThrowExactly { 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" } } @@ -159,12 +160,12 @@ class ModuleStorageSpec : ShouldSpec({ } // when: POST module-storage endpoint is called - val exception = shouldThrowExactly { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) } + val exception = shouldThrowExactly { 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" } } @@ -177,12 +178,12 @@ class ModuleStorageSpec : ShouldSpec({ } // when: POST module-storage endpoint is called - val exception = shouldThrowExactly { cacheApi.postModuleStorageCache(payloadTransfer, apiKey) } + val exception = shouldThrowExactly { 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" } } @@ -197,7 +198,7 @@ class ModuleStorageSpec : ShouldSpec({ // when: POST module-storage endpoint is called val exception = - shouldThrowExactly { cacheApi.postModuleStorageCache(payloadTransfer, getRandomString()) } + shouldThrowExactly { cacheApi.postStorageCache(payloadTransfer, getRandomString()) } // then: Not found exception is thrown assertSoftly { @@ -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 { - 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" } } @@ -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 { - 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}\"" } } @@ -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 { - cacheApi.getModuleStorageCache(payloadKey, applicationName, getRandomString()) + cacheApi.getStorageCache(payloadKey, applicationName, getRandomString()) } // then: Not found exception is thrown @@ -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 { + 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") { + //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()) + } }) diff --git a/src/test/kotlin/org/prebid/cache/functional/service/PrebidCacheApi.kt b/src/test/kotlin/org/prebid/cache/functional/service/PrebidCacheApi.kt index 67c21f0..59c6390 100644 --- a/src/test/kotlin/org/prebid/cache/functional/service/PrebidCacheApi.kt +++ b/src/test/kotlin/org/prebid/cache/functional/service/PrebidCacheApi.kt @@ -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 @@ -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" diff --git a/src/test/kotlin/org/prebid/cache/functional/testcontainers/PrebidCacheContainerConfig.kt b/src/test/kotlin/org/prebid/cache/functional/testcontainers/PrebidCacheContainerConfig.kt index 9fb389f..c54346b 100644 --- a/src/test/kotlin/org/prebid/cache/functional/testcontainers/PrebidCacheContainerConfig.kt +++ b/src/test/kotlin/org/prebid/cache/functional/testcontainers/PrebidCacheContainerConfig.kt @@ -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 = 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 =