@Test public void updateNodeByMapParameter() { db.execute( "CREATE (n:Reference) SET n = {data} RETURN n" , map( "data", map("key1", "value1", "key2", 1234) ) ); Node node1 = getNodeByIdInTx( 0 ); assertThat( node1, inTxS( hasProperty( "key1" ).withValue( "value1" ) ) ); assertThat( node1, inTxS( hasProperty( "key2" ).withValue( 1234 ) ) ); db.execute( "MATCH (n:Reference) SET n = {data} RETURN n", map( "data", map("key1", null, "key3", 5678) ) ); Node node2 = getNodeByIdInTx( 0 ); assertThat( node2, inTxS( not( hasProperty( "key1" ) ) ) ); assertThat( node2, inTxS( not( hasProperty( "key2" ) ) ) ); assertThat( node2, inTxS( hasProperty( "key3" ).withValue(5678) ) ); }
@Test public void canAddMultipleShortStringsToTheSameNode() { Node node = graphdb.getGraphDatabaseAPI().createNode(); node.setProperty( "key", "value" ); node.setProperty( "reverse", "esrever" ); commit(); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) ); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "reverse" ).withValue( "esrever" ) ) ); }
@Documented( "Set property on node.\n" + "\n" + "Setting different properties will retain the existing ones for this node.\n" + "Note that a single value are submitted not as a map but just as a value\n" + "(which is valid JSON) like in the example\n" + "below." ) @Graph( nodes = {@NODE( name = "jim", properties = {@PROP( key = "foo2", value = "bar2" )} )} ) @Test public void shouldReturn204WhenPropertyIsSet() throws Exception { Node jim = data.get().get( "jim" ); gen.get().payload( JsonHelper.createJsonFrom( "bar" ) ).expectedStatus( 204 ).put( getPropertyUri( jim, "foo" ).toString() ); assertThat( jim, inTx(graphdb(), hasProperty( "foo" ) ) ); assertThat( jim, inTx(graphdb(), hasProperty( "foo2" ) ) ); }
@Test public void setBigArrayGraphProperty() { GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase(); long[] array = new long[1000]; for ( int i = 0; i < 10; i++ ) { array[array.length / 10 * i] = i; } String key = "big long array"; Transaction tx = db.beginTx(); properties( db ).setProperty( key, array ); assertThat( properties( db ), hasProperty( key ).withValue( array ) ); tx.success(); tx.close(); assertThat( properties( db ), inTx( db, hasProperty( key ).withValue( array ) ) ); assertThat( properties( db ), inTx( db, hasProperty( key ).withValue( array ) ) ); db.shutdown(); }
@Test public void canRemoveShortStringProperty() { GraphDatabaseService db = graphdb.getGraphDatabaseAPI(); Node node = db.createNode(); node.setProperty( "key", "value" ); newTx(); assertEquals( "value", node.getProperty( "key" ) ); node.removeProperty( "key" ); commit(); assertThat( node, inTx( db, not( hasProperty( "key" ) ) ) ); } }
@Graph( "jim knows joe" ) @Documented( "Update node properties.\n" + "\n" + "This will replace all existing properties on the node with the new set\n" + "of attributes." ) @Test public void shouldReturn204WhenPropertiesAreUpdated() { Node jim = data.get().get( "jim" ); assertThat( jim, inTx(graphdb(), not( hasProperty( "age" ) ) ) ); gen.get().payload( JsonHelper.createJsonFrom( MapUtil.map( "age", "18" ) ) ).expectedStatus( 204 ).put( getPropertiesUri( jim ) ); assertThat( jim, inTx(graphdb(), hasProperty( "age" ).withValue( "18" ) ) ); }
@Graph( "jim knows joe" ) @Test public void set_node_properties_in_Unicode() { Node jim = data.get().get( "jim" ); gen.get().payload( JsonHelper.createJsonFrom( MapUtil.map( "name", "\u4f8b\u5b50" ) ) ).expectedStatus( 204 ).put( getPropertiesUri( jim ) ); assertThat( jim, inTx( graphdb(), hasProperty( "name" ).withValue( "\u4f8b\u5b50" ) ) ); }
@Test public void setSingleProperty() throws Exception { BatchInserter inserter = newBatchInserter(); long node = inserter.createNode( null ); String value = "Something"; String key = "name"; inserter.setNodeProperty( node, key, value ); GraphDatabaseService db = switchToEmbeddedGraphDatabaseService( inserter ); assertThat( getNodeInTx( node, db ), inTx( db, hasProperty( key ).withValue( value ) ) ); db.shutdown(); }
@Test public void get_or_create_node_with_array_properties() throws Exception { final String index = indexes.newInstance(); String key = "name"; String value = "Tobias"; helper.createNodeIndex( index ); ResponseEntity response = gen() .expectedStatus( 201 /* created */ ) .payloadType( MediaType.APPLICATION_JSON_TYPE ) .payload( "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"array\": [1,2,3]}}" ) .post( functionalTestHelper.nodeIndexUri() + index + "?unique" ); MultivaluedMap<String, String> headers = response.response().getHeaders(); Map<String, Object> result = JsonHelper.jsonToMap( response.entity() ); String location = headers.getFirst("Location"); assertEquals( result.get( "indexed" ), location ); Map<String, Object> data = assertCast( Map.class, result.get( "data" ) ); assertEquals( value, data.get( key ) ); assertEquals(Arrays.asList( 1, 2, 3), data.get( "array" ) ); Node node; try ( Transaction tx = graphdb().beginTx() ) { node = graphdb().index().forNodes(index).get(key, value).getSingle(); } assertThat( node, inTx( graphdb(), hasProperty( key ).withValue( value ) ) ); assertThat( node, inTx( graphdb(), hasProperty( "array" ).withValue( new int[]{1, 2, 3} ) ) ); }
@Test @Graph( nodes = {@NODE( name = "Romeo", setNameProperty = true ), @NODE( name = "Juliet", setNameProperty = true )}, relationships = { @REL( start = "Romeo", end = "Juliet", type = "LOVES", properties = { @PROP( key = "cost", value = "high", type = GraphDescription.PropType.STRING )} )} ) public void set_single_property_on_a_relationship() { Relationship loves = getFirstRelationshipFromRomeoNode(); assertThat( loves, inTx( graphdb(), hasProperty( "cost" ).withValue( "high" ) ) ); gen().expectedStatus( ClientResponse.Status.NO_CONTENT ).payload( "\"deadly\"" ).put( getRelPropURI( loves, "cost" ) ).entity(); assertThat( loves, inTx( graphdb(), hasProperty( "cost" ).withValue( "deadly" ) ) ); }
@Test @Graph( nodes = {@NODE( name = "Romeo", setNameProperty = true ), @NODE( name = "Juliet", setNameProperty = true )}, relationships = { @REL( start = "Romeo", end = "Juliet", type = "LOVES", properties = { @PROP( key = "cost", value = "high", type = GraphDescription.PropType.STRING ), @PROP( key = "since", value = "1day", type = GraphDescription.PropType.STRING )} )} ) public void set_all_properties_on_a_relationship() { Relationship loves = getFirstRelationshipFromRomeoNode(); assertThat( loves, inTx( graphdb(), hasProperty( "cost" ).withValue( "high" ) ) ); gen().expectedStatus( ClientResponse.Status.NO_CONTENT ).payload( JsonHelper.createJsonFrom( MapUtil.map( "happy", false ) ) ).put( getRelPropsURI( loves ) ).entity(); assertThat( loves, inTx( graphdb(), hasProperty( "happy" ).withValue( false ) ) ); assertThat( loves, inTx( graphdb(), not( hasProperty( "cost" ) ) ) ); }
@Test public void setManyGraphProperties() { GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase(); Transaction tx = db.beginTx(); Object[] values = new Object[]{10, "A string value", new float[]{1234.567F, 7654.321F}, "A rather longer string which wouldn't fit inlined #!)(&ยค"}; int count = 200; for ( int i = 0; i < count; i++ ) { properties( db ).setProperty( "key" + i, values[i % values.length] ); } tx.success(); tx.close(); for ( int i = 0; i < count; i++ ) { assertThat( properties( db ), inTx( db, hasProperty( "key" + i ).withValue( values[i % values.length] ) ) ); } for ( int i = 0; i < count; i++ ) { assertThat( properties( db ), inTx( db, hasProperty( "key" + i ).withValue( values[i % values.length] ) ) ); } db.shutdown(); }
@Test public void canAddShortStringToRelationship() { GraphDatabaseService db = graphdb.getGraphDatabaseAPI(); Relationship rel = db.createNode().createRelationshipTo( db.createNode(), withName( "REL_TYPE" ) ); rel.setProperty( "type", "dimsedut" ); commit(); assertThat( rel, inTx( db, hasProperty( "type" ).withValue( "dimsedut" ) ) ); }
@Test public void canUpdateShortStringInplace() { Node node = graphdb.getGraphDatabaseAPI().createNode(); node.setProperty( "key", "value" ); newTx(); assertEquals( "value", node.getProperty( "key" ) ); node.setProperty( "key", "other" ); commit(); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "other" ) ) ); }
@Test public void canReplaceLongStringWithShortString() { Node node = graphdb.getGraphDatabaseAPI().createNode(); node.setProperty( "key", LONG_STRING ); newTx(); assertEquals( LONG_STRING, node.getProperty( "key" ) ); node.setProperty( "key", "value" ); commit(); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( "value" ) ) ); }
@Test public void canReplaceShortStringWithLongString() { Node node = graphdb.getGraphDatabaseAPI().createNode(); node.setProperty( "key", "value" ); newTx(); assertEquals( "value", node.getProperty( "key" ) ); node.setProperty( "key", LONG_STRING ); commit(); assertThat( node, inTx( graphdb.getGraphDatabaseAPI(), hasProperty( "key" ).withValue( LONG_STRING ) ) ); }
@Test public void twoUncleanInARow() throws Exception { File databaseDir = testDirectory.databaseDir(); try ( EphemeralFileSystemAbstraction snapshot = produceUncleanStore( fs.get(), databaseDir ) ) { try ( EphemeralFileSystemAbstraction snapshot2 = produceUncleanStore( snapshot, databaseDir ) ) { GraphDatabaseAPI db = (GraphDatabaseAPI) new TestGraphDatabaseFactory() .setFileSystem( produceUncleanStore( snapshot2, databaseDir ) ) .newImpermanentDatabase( databaseDir ); assertThat( properties( db ), inTx( db, hasProperty( "prop" ).withValue( "Some value" ) ) ); db.shutdown(); } } }
@Test public void canAvoidDeadlockThatWouldHappenIfTheRelationshipTypeCreationTransactionModifiedData() { GraphDatabaseService graphdb = database.getGraphDatabaseAPI(); Node node = null; try ( Transaction tx = graphdb.beginTx() ) { node = graphdb.createNode(); node.setProperty( "counter", 0L ); tx.success(); } graphdb.registerTransactionEventHandler( new RelationshipCounterTransactionEventHandler( node ) ); try ( Transaction tx = graphdb.beginTx() ) { node.setProperty( "state", "not broken yet" ); node.createRelationshipTo( graphdb.createNode(), RelationshipType.withName( "TEST" ) ); node.removeProperty( "state" ); tx.success(); } assertThat( node, inTx( graphdb, hasProperty( "counter" ).withValue( 1L ) ) ); }
@Test public void shouldCorrectlyUpdateIndexesWhenChangingLabelsAndPropertyAtTheSameTime() { // Given GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Node myNode = createNode( beansAPI, map( "name", "Hawking" ), LABEL1, LABEL2 ); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL2, "name" ); Neo4jMatchers.createIndex( beansAPI, LABEL3, "name" ); // When try ( Transaction tx = beansAPI.beginTx() ) { myNode.removeLabel( LABEL1 ); myNode.addLabel( LABEL3 ); myNode.setProperty( "name", "Einstein" ); tx.success(); } // Then assertThat( myNode, inTx( beansAPI, hasProperty("name").withValue( "Einstein" ) ) ); assertThat( labels( myNode ), containsOnly( LABEL2, LABEL3 ) ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL1, "name", "Einstein", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL2, "name", "Einstein", beansAPI ), containsOnly( myNode ) ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Hawking", beansAPI ), isEmpty() ); assertThat( findNodesByLabelAndProperty( LABEL3, "name", "Einstein", beansAPI ), containsOnly( myNode ) ); }
assertThat( myNode, inTx( beansAPI, hasProperty("name").withValue( "Feynman" ) ) ); assertThat( labels( myNode ), containsOnly( LABEL2, LABEL3 ) );