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 a test to see if basic-authentication is really passed to the sch… #380

Merged
merged 1 commit into from
Jun 12, 2024
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
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)
}
}
}
Loading