|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.neo4j.connectors.kafka.sink |
| 18 | + |
| 19 | +import io.confluent.connect.avro.AvroData |
| 20 | +import kotlin.time.Duration.Companion.seconds |
| 21 | +import kotlin.time.toJavaDuration |
| 22 | +import org.apache.avro.generic.GenericData |
| 23 | +import org.apache.avro.generic.GenericRecord |
| 24 | +import org.apache.kafka.clients.producer.KafkaProducer |
| 25 | +import org.apache.kafka.clients.producer.ProducerRecord |
| 26 | +import org.apache.kafka.connect.data.Schema |
| 27 | +import org.apache.kafka.connect.data.SchemaBuilder |
| 28 | +import org.awaitility.Awaitility.await |
| 29 | +import org.junit.jupiter.api.Test |
| 30 | +import org.junit.jupiter.api.TestInfo |
| 31 | +import org.neo4j.connectors.kafka.testing.sink.Neo4jSink |
| 32 | +import org.neo4j.connectors.kafka.testing.sink.TopicProducer |
| 33 | +import org.neo4j.driver.Session |
| 34 | + |
| 35 | +class Neo4jSinkIT { |
| 36 | + |
| 37 | + companion object { |
| 38 | + const val TOPIC = "persons" |
| 39 | + } |
| 40 | + |
| 41 | + @Neo4jSink( |
| 42 | + topics = [TOPIC], |
| 43 | + queries = |
| 44 | + [ |
| 45 | + "MERGE (p:Person {name: event.name, surname: event.surname, executionId: event.executionId})"]) |
| 46 | + @Test |
| 47 | + fun `writes messages to Neo4j`( |
| 48 | + @TopicProducer producer: KafkaProducer<String, GenericRecord>, |
| 49 | + session: Session, |
| 50 | + testInfo: TestInfo |
| 51 | + ) { |
| 52 | + val executionId = testInfo.displayName + System.currentTimeMillis() |
| 53 | + val avroRecord = |
| 54 | + GenericData.Record( |
| 55 | + AvroData(20) |
| 56 | + .fromConnectSchema( |
| 57 | + SchemaBuilder.struct() |
| 58 | + .field("name", Schema.STRING_SCHEMA) |
| 59 | + .field("surname", Schema.STRING_SCHEMA) |
| 60 | + .field("executionId", Schema.STRING_SCHEMA) |
| 61 | + .build())) |
| 62 | + avroRecord.put("name", "Jane") |
| 63 | + avroRecord.put("surname", "Doe") |
| 64 | + avroRecord.put("executionId", executionId) |
| 65 | + val record = ProducerRecord<String, GenericRecord>(TOPIC, avroRecord) |
| 66 | + |
| 67 | + producer.send(record) |
| 68 | + |
| 69 | + await().atMost(30.seconds.toJavaDuration()).until { |
| 70 | + session |
| 71 | + .run( |
| 72 | + "MATCH (p:Person {name: \$name, surname: \$surname, executionId: \$executionId}) RETURN count(p) = 1 AS result", |
| 73 | + mapOf("name" to "Jane", "surname" to "Doe", "executionId" to executionId)) |
| 74 | + .single()["result"] |
| 75 | + .asBoolean() |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments