-
Notifications
You must be signed in to change notification settings - Fork 12
fix: use incoming object if schema is not defined #184
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
sink-connector/src/test/kotlin/org/neo4j/connectors/kafka/sink/Neo4jSinkRawJsonIT.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /* | ||
| * Copyright (c) "Neo4j" | ||
| * Neo4j Sweden AB [https://neo4j.com] | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.neo4j.connectors.kafka.sink | ||
|
|
||
| import io.kotest.matchers.shouldBe | ||
| import kotlin.time.Duration.Companion.seconds | ||
| import kotlin.time.toJavaDuration | ||
| import org.apache.kafka.connect.data.Schema | ||
| import org.apache.kafka.connect.data.SchemaBuilder | ||
| import org.awaitility.Awaitility.await | ||
| import org.junit.jupiter.api.Test | ||
| import org.neo4j.connectors.kafka.testing.format.KafkaConverter.JSON_RAW | ||
| import org.neo4j.connectors.kafka.testing.format.KeyValueConverter | ||
| import org.neo4j.connectors.kafka.testing.kafka.ConvertingKafkaProducer | ||
| import org.neo4j.connectors.kafka.testing.sink.CypherStrategy | ||
| import org.neo4j.connectors.kafka.testing.sink.Neo4jSink | ||
| import org.neo4j.connectors.kafka.testing.sink.TopicProducer | ||
| import org.neo4j.driver.Session | ||
|
|
||
| @KeyValueConverter(key = JSON_RAW, value = JSON_RAW) | ||
| class Neo4jSinkRawJsonIT { | ||
| companion object { | ||
| private const val TOPIC = "persons" | ||
| } | ||
|
|
||
| @Neo4jSink( | ||
| cypher = | ||
| [ | ||
| CypherStrategy( | ||
| TOPIC, | ||
| "WITH __value AS person MERGE (p:Person {name: person.name, surname: person.surname})", | ||
| ), | ||
| ], | ||
| ) | ||
| @Test | ||
| fun `should support json map`( | ||
| @TopicProducer(TOPIC) producer: ConvertingKafkaProducer, | ||
| session: Session, | ||
| ) { | ||
| producer.publish( | ||
| value = mapOf("name" to "Jane", "surname" to "Doe"), | ||
| valueSchema = SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA).build(), | ||
| ) | ||
|
|
||
| await().atMost(30.seconds.toJavaDuration()).until { | ||
| session | ||
| .run( | ||
| "MATCH (p:Person {name: \$name, surname: \$surname}) RETURN count(p) = 1 AS result", | ||
| mapOf("name" to "Jane", "surname" to "Doe"), | ||
| ) | ||
| .single()["result"] | ||
| .asBoolean() | ||
| } | ||
| } | ||
|
|
||
| @Neo4jSink( | ||
| cypher = | ||
| [ | ||
| CypherStrategy( | ||
| TOPIC, | ||
| "WITH __value AS persons UNWIND persons AS person MERGE (p:Person {name: person.name, surname: person.surname})", | ||
| ), | ||
| ], | ||
| ) | ||
| @Test | ||
| fun `should support json list`( | ||
| @TopicProducer(TOPIC) producer: ConvertingKafkaProducer, | ||
| session: Session, | ||
| ) { | ||
| producer.publish( | ||
| value = | ||
| listOf( | ||
| mapOf("name" to "Jane", "surname" to "Doe"), | ||
| mapOf("name" to "John", "surname" to "Doe"), | ||
| ), | ||
| valueSchema = | ||
| SchemaBuilder.array( | ||
| SchemaBuilder.map( | ||
| Schema.STRING_SCHEMA, | ||
| Schema.STRING_SCHEMA, | ||
| ) | ||
| .build(), | ||
| ) | ||
| .build(), | ||
| ) | ||
|
|
||
| await().atMost(30.seconds.toJavaDuration()).untilAsserted { | ||
| session | ||
| .run( | ||
| "MATCH (p:Person) WHERE [p.name, p.surname] IN ${'$'}names RETURN count(p) as result", | ||
| mapOf("names" to listOf(listOf("Jane", "Doe"), listOf("John", "Doe"))), | ||
| ) | ||
| .single()["result"] | ||
| .asLong() shouldBe 2L | ||
| } | ||
| } | ||
|
|
||
| @Neo4jSink( | ||
| cypher = | ||
| [ | ||
| CypherStrategy( | ||
| TOPIC, | ||
| "WITH __value AS name MERGE (p:Person {name: name})", | ||
| ), | ||
| ], | ||
| ) | ||
| @Test | ||
| fun `should support raw string value`( | ||
| @TopicProducer(TOPIC) producer: ConvertingKafkaProducer, | ||
| session: Session, | ||
| ) { | ||
| producer.publish( | ||
| value = "John", | ||
| valueSchema = Schema.STRING_SCHEMA, | ||
| ) | ||
|
|
||
| await().atMost(30.seconds.toJavaDuration()).untilAsserted { | ||
| session | ||
| .run( | ||
| "MATCH (p:Person {name: ${'$'}name}) RETURN count(p) as result", | ||
| mapOf("name" to "John"), | ||
| ) | ||
| .single()["result"] | ||
| .asLong() shouldBe 1L | ||
| } | ||
| } | ||
|
|
||
| @Neo4jSink( | ||
| cypher = | ||
| [ | ||
| CypherStrategy( | ||
| TOPIC, | ||
| "WITH __value AS age MERGE (p:Person {age: age})", | ||
| ), | ||
| ], | ||
| ) | ||
| @Test | ||
| fun `should support raw numeric value`( | ||
| @TopicProducer(TOPIC) producer: ConvertingKafkaProducer, | ||
| session: Session, | ||
| ) { | ||
| producer.publish( | ||
| value = 25L, | ||
| valueSchema = Schema.INT64_SCHEMA, | ||
| ) | ||
|
|
||
| await().atMost(30.seconds.toJavaDuration()).untilAsserted { | ||
| session | ||
| .run( | ||
| "MATCH (p:Person {age: ${'$'}age}) RETURN count(p) as result", | ||
| mapOf("age" to 25L), | ||
| ) | ||
| .single()["result"] | ||
| .asLong() shouldBe 1L | ||
| } | ||
| } | ||
|
|
||
| @Neo4jSink( | ||
| cypher = | ||
| [ | ||
| CypherStrategy( | ||
| TOPIC, | ||
| "WITH __value AS status MERGE (p:Person {single: status})", | ||
| ), | ||
| ], | ||
| ) | ||
| @Test | ||
| fun `should support raw boolean value`( | ||
| @TopicProducer(TOPIC) producer: ConvertingKafkaProducer, | ||
| session: Session, | ||
| ) { | ||
| producer.publish( | ||
| value = true, | ||
| valueSchema = Schema.BOOLEAN_SCHEMA, | ||
| ) | ||
|
|
||
| await().atMost(30.seconds.toJavaDuration()).untilAsserted { | ||
| session | ||
| .run( | ||
| "MATCH (p:Person {single: ${'$'}single}) RETURN count(p) as result", | ||
| mapOf("single" to true), | ||
| ) | ||
| .single()["result"] | ||
| .asLong() shouldBe 1L | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
testing/src/main/kotlin/org/neo4j/connectors/kafka/testing/format/json/JsonRawSerializer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright (c) "Neo4j" | ||
| * Neo4j Sweden AB [https://neo4j.com] | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.neo4j.connectors.kafka.testing.format.json | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import org.apache.kafka.connect.data.Schema | ||
| import org.apache.kafka.connect.json.JsonConverter | ||
| import org.neo4j.connectors.kafka.testing.format.KafkaRecordSerializer | ||
|
|
||
| object JsonRawSerializer : KafkaRecordSerializer { | ||
|
|
||
| private val converter = JsonConverter() | ||
| private val objectMapper = ObjectMapper() | ||
|
|
||
| override fun serialize(value: Any, schema: Schema, isKey: Boolean): Any { | ||
| converter.configure(mapOf("schemas.enable" to false), isKey) | ||
| val data = converter.fromConnectData(null, schema, value) | ||
| return objectMapper.readTree(data) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.