@@ -24,6 +24,8 @@ import streams.service.sink.strategy.CUDNodeRel
2424import streams.service.sink.strategy.CUDOperations
2525import streams.service.sink.strategy.CUDRelationship
2626import java.util.*
27+ import java.util.stream.Collectors
28+ import java.util.stream.StreamSupport
2729import kotlin.test.assertEquals
2830import kotlin.test.assertFalse
2931import kotlin.test.assertTrue
@@ -313,6 +315,90 @@ class Neo4jSinkTaskTest {
313315 }
314316 }
315317
318+ @Test
319+ fun `should insert data into Neo4j from CDC events with schema strategy` () {
320+ val firstTopic = " neotopic"
321+ val props = mapOf (Neo4jSinkConnectorConfig .SERVER_URI to db.boltURI().toString(),
322+ Neo4jSinkConnectorConfig .TOPIC_CDC_SCHEMA to firstTopic,
323+ Neo4jSinkConnectorConfig .AUTHENTICATION_TYPE to AuthenticationType .NONE .toString(),
324+ SinkTask .TOPICS_CONFIG to firstTopic)
325+
326+ val constraints = listOf (Constraint (label = " User" , type = StreamsConstraintType .UNIQUE , properties = setOf (" name" , " surname" )))
327+ val relSchema = Schema (properties = mapOf (" since" to " Long" ), constraints = constraints)
328+ val nodeSchema = Schema (properties = mapOf (" name" to " String" , " surname" to " String" , " comp@ny" to " String" ),
329+ constraints = constraints)
330+ val cdcDataStart = StreamsTransactionEvent (
331+ meta = Meta (timestamp = System .currentTimeMillis(),
332+ username = " user" ,
333+ txId = 1 ,
334+ txEventId = 0 ,
335+ txEventsCount = 3 ,
336+ operation = OperationType .created
337+ ),
338+ payload = NodePayload (id = " 0" ,
339+ before = null ,
340+ after = NodeChange (properties = mapOf (" name" to " Andrea" , " surname" to " Santurbano" , " comp@ny" to " LARUS-BA" ), labels = listOf (" User" ))
341+ ),
342+ schema = nodeSchema
343+ )
344+ val cdcDataEnd = StreamsTransactionEvent (
345+ meta = Meta (timestamp = System .currentTimeMillis(),
346+ username = " user" ,
347+ txId = 1 ,
348+ txEventId = 1 ,
349+ txEventsCount = 3 ,
350+ operation = OperationType .created
351+ ),
352+ payload = NodePayload (id = " 1" ,
353+ before = null ,
354+ after = NodeChange (properties = mapOf (" name" to " Michael" , " surname" to " Hunger" , " comp@ny" to " Neo4j" ), labels = listOf (" User" ))
355+ ),
356+ schema = nodeSchema
357+ )
358+ val cdcDataRelationship = StreamsTransactionEvent (
359+ meta = Meta (timestamp = System .currentTimeMillis(),
360+ username = " user" ,
361+ txId = 1 ,
362+ txEventId = 2 ,
363+ txEventsCount = 3 ,
364+ operation = OperationType .created
365+ ),
366+ payload = RelationshipPayload (
367+ id = " 2" ,
368+ start = RelationshipNodeChange (id = " 0" , labels = listOf (" User" ), ids = mapOf (" name" to " Andrea" , " surname" to " Santurbano" )),
369+ end = RelationshipNodeChange (id = " 1" , labels = listOf (" User" ), ids = mapOf (" name" to " Michael" , " surname" to " Hunger" )),
370+ after = RelationshipChange (properties = mapOf (" since" to 2014 )),
371+ before = null ,
372+ label = " KNOWS WHO"
373+ ),
374+ schema = relSchema
375+ )
376+
377+ val task = Neo4jSinkTask ()
378+ task.initialize(mock(SinkTaskContext ::class .java))
379+ task.start(props)
380+ val input = listOf (SinkRecord (firstTopic, 1 , null , null , null , cdcDataStart, 42 ),
381+ SinkRecord (firstTopic, 1 , null , null , null , cdcDataEnd, 43 ),
382+ SinkRecord (firstTopic, 1 , null , null , null , cdcDataRelationship, 44 ))
383+ task.put(input)
384+ db.graph().beginTx().use {
385+ val query = """
386+ |MATCH p = (s:User{name:'Andrea', surname:'Santurbano', `comp@ny`:'LARUS-BA'})-[r:`KNOWS WHO`{since:2014}]->(e:User{name:'Michael', surname:'Hunger', `comp@ny`:'Neo4j'})
387+ |RETURN count(p) AS count
388+ |""" .trimMargin()
389+ db.graph().execute(query)
390+ .columnAs<Long >(" count" ).use {
391+ assertTrue { it.hasNext() }
392+ val count = it.next()
393+ assertEquals(1 , count)
394+ assertFalse { it.hasNext() }
395+ }
396+
397+ val labels = db.graph().allLabels.stream().map { it.name() }.collect(Collectors .toSet())
398+ assertEquals(setOf (" User" ), labels)
399+ }
400+ }
401+
316402 @Test
317403 fun `should delete data into Neo4j from CDC events` () {
318404 db.graph().beginTx().use {
0 commit comments