@Test public void shouldAcceptFolderWithCypFilesAsFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File( targetFolder, "fixture1.cyp" ), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false ); FileUtils.writeToFile( new File( targetFolder, "fixture2.cyp" ), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false ); // And given one file in a sub directory File subDir = new File( targetFolder, "subdirectory" ); subDir.mkdir(); FileUtils.writeToFile( new File( subDir, "subDirFixture.cyp" ), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false ); // When try ( ServerControls server = getServerBuilder( targetFolder ).withFixture( targetFolder ).newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.toString(), response.get( "results" ).get(0).get("data").size(), equalTo(3) ); } }
@Test public void shouldHandleFixturesWithSyntaxErrorsGracefully() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File( targetFolder, "fixture1.cyp" ), "this is not a valid cypher statement", false ); // When try ( ServerControls ignore = getServerBuilder( targetFolder ) .withFixture( targetFolder ).newServer() ) { fail("Should have thrown exception"); } catch ( RuntimeException e ) { assertThat( e.getMessage(), equalTo( "Invalid input 't': expected <init> (line 1, column 1 (offset: 0))" + lineSeparator() + "\"this is not a valid cypher statement\"" + lineSeparator() + " ^" ) ); } }
@Test public void shouldIgnoreEmptyFixtureFiles() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File( targetFolder, "fixture1.cyp" ), "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false ); FileUtils.writeToFile( new File( targetFolder, "fixture2.cyp" ), "", false ); // When try ( ServerControls server = getServerBuilder( targetFolder ) .withFixture( targetFolder ).newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get(0).get("data").size(), equalTo(1)); } }
@Test public void shouldFailIfSupplyingBothFileArgumentAndAnyOtherArgument() throws Exception { // given List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; File argumentFile = file( "args" ); String arguments = format( "--into %s%n" + "--nodes %s --relationships %s", dbRule.getDatabaseDirAbsolutePath(), nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); writeToFile( argumentFile, arguments, false ); try { // when importTool( "-f", argumentFile.getAbsolutePath(), "--into", dbRule.getDatabaseDirAbsolutePath() ); fail( "Should have failed" ); } catch ( IllegalArgumentException e ) { // then good assertThat( e.getMessage(), containsString( "in addition to" ) ); assertThat( e.getMessage(), containsString( ImportTool.Options.FILE.argument() ) ); } }
@Test public void shouldAccepSingleCypherFileAsFixture() throws Exception { // Given File targetFolder = testDir.directory(); File fixture = new File( targetFolder, "fixture.cyp" ); FileUtils.writeToFile(fixture, "CREATE (u:User)" + "CREATE (a:OtherUser)", false); // When try ( ServerControls server = getServerBuilder( targetFolder ).withFixture( fixture ).newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.status(), equalTo( 200 ) ); assertThat(response.get( "results" ).get(0).get("data").size(), equalTo(1)); } }
@Test public void shouldHandleMultipleFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); File fixture1 = new File( targetFolder, "fixture1.cyp" ); FileUtils.writeToFile( fixture1, "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false); File fixture2 = new File( targetFolder, "fixture2.cyp" ); FileUtils.writeToFile( fixture2, "CREATE (u:User)\n" + "CREATE (a:OtherUser)", false); // When try ( ServerControls server = getServerBuilder( targetFolder ) .withFixture( fixture1 ) .withFixture( fixture2 ) .newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get(0).get("data").size(), equalTo(2)); } }
@Test public void shouldSupplyArgumentsAsFile() throws Exception { // given List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; File argumentFile = file( "args" ); String nodesEscapedSpaces = escapePath( nodeData( true, config, nodeIds, TRUE ).getAbsolutePath() ); String relationshipsEscapedSpaced = escapePath( relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); String arguments = format( "--into %s%n" + "--nodes %s --relationships %s", escapePath( dbRule.getDatabaseDirAbsolutePath() ), nodesEscapedSpaces, relationshipsEscapedSpaced ); writeToFile( argumentFile, arguments, false ); // when importTool( "-f", argumentFile.getAbsolutePath() ); // then verifyData(); }