Skip to content

Commit

Permalink
Add test for basic authentication configuration of the schemaretriever
Browse files Browse the repository at this point in the history
  • Loading branch information
Bdegraaf1234 committed Jun 12, 2024
1 parent 1b5a23a commit 22220d9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
7 changes: 4 additions & 3 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
@Suppress("ConstPropertyName")
object Versions {
const val project = "0.7.2"
const val project = "0.7.2-SNAPSHOT"

const val java = 17
const val kotlin = "1.9.22"
const val dockerCompose = "0.17.6"

const val ktor = "2.3.10"
const val radarJersey = "0.11.1"
const val radarCommons = "1.1.3"
const val radarSchemas = "0.8.10"
const val radarCommons = "1.1.2"
const val radarSchemas = "0.8.9"
const val jackson = "2.15.3"
const val slf4j = "2.0.13"
const val log4j2 = "2.23.1"
Expand All @@ -22,4 +22,5 @@ object Versions {
const val mockitoKotlin = "5.3.1"
const val grizzly = "4.0.2"
const val hamcrest = "2.2"
const val okHttp = "4.12.0"
}
2 changes: 1 addition & 1 deletion radar-gateway/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ dependencies {

testImplementation("org.mockito.kotlin:mockito-kotlin:${Versions.mockitoKotlin}")
testImplementation("org.hamcrest:hamcrest:${Versions.hamcrest}")
testImplementation("com.squareup.okhttp3:mockwebserver:${Versions.okHttp}")

integrationTestImplementation(platform("io.ktor:ktor-bom:${Versions.ktor}"))
integrationTestImplementation("io.ktor:ktor-client-content-negotiation")
integrationTestImplementation("io.ktor:ktor-client-content-negotiation")
integrationTestImplementation("io.ktor:ktor-serialization-kotlinx-json")

testImplementation("org.radarbase:radar-schemas-commons:${Versions.radarSchemas}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_CONFIG
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.radarbase.gateway.config.GatewayConfig
import org.radarbase.gateway.config.KafkaConfig
import org.radarbase.gateway.inject.SchemaRetrieverFactory
import org.radarbase.topic.AvroTopic
import org.radarcns.kafka.ObservationKey
import org.radarcns.passive.phone.PhoneAcceleration

class SchemaRetrieverFactoryTest {
@Test
fun `test basic auth credentials are correctly passed`() {
val mockConfig: GatewayConfig = mock()
// Create a new MockWebServer
val server = MockWebServer()

val topic = AvroTopic(
"test",
ObservationKey.getClassSchema(),
PhoneAcceleration.getClassSchema(),
ObservationKey::class.java,
PhoneAcceleration::class.java,
)

// Schedule a response

server.enqueue(
MockResponse()
.setHeader(HttpHeaders.ContentType, "application/json")
.setBody(
"""
{
"id": 1
}
""".trimIndent(),
),
)

// Start the server
server.start()

// Update the SCHEMA_REGISTRY_URL_CONFIG to use the mock server's URL
val serializationConfig = mapOf(
SCHEMA_REGISTRY_URL_CONFIG to server.url("/").toString(),
SCHEMA_REGISTRY_USER_INFO_CONFIG to "username:password",
)

val mockKafkaConfig: KafkaConfig = mock()
whenever(mockConfig.kafka).doReturn(mockKafkaConfig)
whenever(mockKafkaConfig.serialization).doReturn(serializationConfig)

val factory = SchemaRetrieverFactory(mockConfig)
val retriever = factory.get()
runBlocking {
retriever.addSchema(topic.name, false, topic.keySchema)
}

// Get the request that was received by the MockWebServer
val request = server.takeRequest()

// Verify the Basic Auth credentials
val authHeader = request.getHeader("Authorization")
assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", authHeader)

// Shut down the server. Instances cannot be reused.
server.shutdown()
}
}

0 comments on commit 22220d9

Please sign in to comment.