lundi 28 mai 2018

Neo4j procedure testing and test server

I'm creating a Neo4J procedure to import RDF data. The RDF data has a few complex structure information and I write the tests to cover every case (some triples creates labels, some properties, some relations...etc..).

The procedures are written in Kotlin.

It works fine, and actually each test when executed individually succeeds. but when I run the whole test case at one, I get one success then all other tests fails with the exception:

org.neo4j.kernel.impl.core.ThreadToStatementContextBridge$BridgeDatabaseShutdownException: This database is shutdown.

I'm new to Neo4J and I struggle to find good examples, here is the structure of a test case:

package mypackage

import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.neo4j.driver.internal.value.NullValue

import org.neo4j.driver.v1.Config
import org.neo4j.driver.v1.GraphDatabase
import org.neo4j.harness.junit.Neo4jRule

import java.io.File
import java.net.URI

class PropertyParserTest {

    // this rule starts a Neo4j instance
    @Rule
    @JvmField
    var neo4j: Neo4jRule = Neo4jRule()
            // This is the procedure/function to test
            .withProcedure(Mypackage::class.java)

    @Test
    @Throws(Throwable::class)
    fun shouldSetTheNameCorrectly() {

        GraphDatabase.driver(neo4j.boltURI(), Config.build().withoutEncryption().toConfig()).use({ driver ->
            driver.session().use({ session ->
                // Given
                val path: String = File("src/test/resources/test_rdf__1.ttl").getAbsolutePath()
                val testFile = File(path)
                val urlTestFile: URI = testFile.toURI()
                session.run("CALL mypackage.import('${urlTestFile}')")

                // When
                val result = session.run("MATCH (n) WHERE n:Person RETURN n.name as name")

                // Then
                var rec = result.next()
                Assert.assertEquals("Manuel, Niklaus (Niclaus)", rec.get("name").asString())
                rec = result?.next()
                Assert.assertEquals("Fischli / Weiss", rec.get("name").asString())
                rec = result?.next()
                Assert.assertEquals("Hodler, Ferdinand", rec.get("name").asString())
            })
        })
    }

    @Test
    @Throws(Throwable::class)
    fun shouldSetTheAlternateNameCorrectly() {

        GraphDatabase.driver(neo4j?.boltURI(), Config.build().withoutEncryption().toConfig()).use({ driver ->
            driver.session().use({ session ->
                // Given
                val path: String = File("src/test/resources/test_rdf_2.ttl").absolutePath
                val testFile = File(path)
                val urlTestFile: URI = testFile.toURI()
                session.run("CALL mypackage.import('${urlTestFile}')")

                // When
                val result = session.run("MATCH (n) WHERE n:Person RETURN n.name as name, n.alternate_names as alternate_names")

                // Then
                var rec = result.next()
                Assert.assertEquals("Holbein, Hans", rec.get("name").asString())
                var alternateNames = rec.get("alternate_names").asList()
                Assert.assertEquals(9, alternateNames.size)
                Assert.assertEquals("Holpenius, Joannes", alternateNames[0])
                Assert.assertEquals("Olpenius, Hans", alternateNames[8])

                rec = result.next()
                Assert.assertEquals("Manuel, Niklaus (Niclaus)", rec.get("name").asString())
                alternateNames = rec.get("alternate_names").asList()
                Assert.assertEquals(8, alternateNames.size)
                rec = result.next()
                Assert.assertEquals("Fischli / Weiss", rec.get("name").asString())
                Assert.assertTrue(rec.get("alternate_names") is NullValue)

                rec = result.next()
                Assert.assertEquals("Hodler, Ferdinand", rec.get("name").asString())
                Assert.assertTrue(rec.get("alternate_names") is NullValue)

                rec = result.next()
                Assert.assertEquals("Holbein", rec.get("name").asString())
                alternateNames = rec.get("alternate_names").asList()
                Assert.assertEquals(3, alternateNames.size)

            })
        })    
    }    
}

Any idea ? I'm using this code base as starting point : https://github.com/jbarrasa/neosemantics/blob/3.3/src/test/java/semantics/RDFImportTest.java

Aucun commentaire:

Enregistrer un commentaire