Skip to content

Commit

Permalink
Merge pull request #380 from RADAR-base/test-basic-auth-schema-registry
Browse files Browse the repository at this point in the history
Add a test to see if basic-authentication is really passed to the sch…
  • Loading branch information
Bdegraaf1234 authored Jun 12, 2024
2 parents 267bd19 + dea9da5 commit 89e129f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
20 changes: 10 additions & 10 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
indent_size = 4

# Markdown
[*.md]
trim_trailing_whitespace = false
[{*.yml, *.yaml}]
indent_size = 2

# Gradle
[*.gradle]
indent_size = 4

# Java
[*.{java,kt,kts}]
indent_size = 4
continuation_indent_size = 8
[*.{kt,kts}]
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_allow_trailing_comma = true
# To satisfy ktlint rules, see: https://stackoverflow.com/questions/59849619/intellij-does-not-sort-kotlin-imports-according-to-ktlints-expectations
ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
2 changes: 1 addition & 1 deletion java-sdk/buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Versions {
const val java = 17
const val avroGenerator = "1.9.1"

const val radarCommons = "1.1.3-SNAPSHOT"
const val radarCommons = "1.1.2"
const val avro = "1.11.3"
const val jackson = "2.16.1"
const val argparse = "0.9.0"
Expand Down
1 change: 1 addition & 0 deletions java-sdk/radar-schemas-registration/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {

implementation("org.apache.kafka:connect-json:${Versions.kafka}")
implementation("io.ktor:ktor-client-auth:${Versions.ktor}")
testImplementation("com.squareup.okhttp3:mockwebserver:${Versions.okHttp}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import org.radarcns.kafka.ObservationKey
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.MalformedURLException
import java.net.URI
import java.time.Duration
import kotlin.streams.asSequence
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -97,7 +96,7 @@ class SchemaRegistry(
.mapNotNull {
try {
httpClient.request<List<String>> {
url(URI(baseUrl).resolve("subjects").toString())
url("subjects")
if (apiKey != null && apiSecret != null) {
basicAuth(apiKey, apiSecret)
}
Expand Down Expand Up @@ -227,7 +226,7 @@ class SchemaRegistry(
logger.info("Setting compatibility to {}", compatibility)
return try {
httpClient.requestEmpty {
url(URI(baseUrl).resolve("config").toString())
url("config")
method = HttpMethod.Put
contentType(ContentType("application", "vnd.schemaregistry.v1+json"))
setBody("{\"compatibility\": \"${compatibility.name}\"}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.radarbase.schema.registration

import io.ktor.http.HttpHeaders
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.radarbase.schema.registration.SchemaRegistry.Compatibility.FORWARD
import org.radarbase.topic.AvroTopic
import org.radarcns.kafka.ObservationKey
import org.radarcns.passive.phone.PhoneAcceleration

class SchemaRegistryTest {
private var server = MockWebServer()
private val expectedAuthHeader = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
lateinit var schemaRegistry: SchemaRegistry

@BeforeEach
fun setUpClass() {
schemaRegistry = SchemaRegistry(
server.url("/").toString(),
"username",
"password",
)

val mockresponse =
MockResponse()
.setHeader(HttpHeaders.ContentType, "application/json")
.setBody(
"""
{
"id": 1
}
""".trimIndent(),
)

server.enqueue(mockresponse)
server.enqueue(mockresponse)
}

@AfterEach
fun tearDown() {
server.shutdown()
}

@Test
fun registerSchema() {
// Create an instance of AvroTopic
val avroTopic = AvroTopic(
"test",
ObservationKey.getClassSchema(),
PhoneAcceleration.getClassSchema(),
ObservationKey::class.java,
PhoneAcceleration::class.java,
)

runBlocking {
// Register the schema
schemaRegistry.registerSchema(avroTopic)

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

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

@Test
fun putCompatibility() {
runBlocking {
// Register the schema
schemaRegistry.putCompatibility(compatibility = FORWARD)

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

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

0 comments on commit 89e129f

Please sign in to comment.