-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LabelsAcceptanceTest.java
713 lines (630 loc) · 23.4 KB
/
LabelsAcceptanceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.graphdb;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.neo4j.cursor.Cursor;
import org.neo4j.function.Consumer;
import org.neo4j.function.Function;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.CommunityIdTypeConfigurationProvider;
import org.neo4j.kernel.GraphDatabaseDependencies;
import org.neo4j.kernel.IdGeneratorFactory;
import org.neo4j.kernel.IdType;
import org.neo4j.kernel.IdTypeConfiguration;
import org.neo4j.kernel.IdTypeConfigurationProvider;
import org.neo4j.kernel.TopLevelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.cursor.LabelItem;
import org.neo4j.kernel.api.cursor.NodeItem;
import org.neo4j.kernel.api.cursor.PropertyItem;
import org.neo4j.kernel.impl.factory.CommunityEditionModule;
import org.neo4j.kernel.impl.factory.CommunityFacadeFactory;
import org.neo4j.kernel.impl.factory.EditionModule;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory;
import org.neo4j.kernel.impl.factory.PlatformModule;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.impl.store.id.IdGenerator;
import org.neo4j.test.ImpermanentDatabaseRule;
import org.neo4j.test.ImpermanentGraphDatabase;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.TestGraphDatabaseFactoryState;
import org.neo4j.test.impl.EphemeralIdGenerator;
import org.neo4j.tooling.GlobalGraphOperations;
import org.neo4j.udc.UsageDataKeys.OperationalMode;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.neo4j.graphdb.DynamicLabel.label;
import static org.neo4j.graphdb.Neo4jMatchers.hasLabel;
import static org.neo4j.graphdb.Neo4jMatchers.hasLabels;
import static org.neo4j.graphdb.Neo4jMatchers.hasNoLabels;
import static org.neo4j.graphdb.Neo4jMatchers.hasNoNodes;
import static org.neo4j.graphdb.Neo4jMatchers.hasNodes;
import static org.neo4j.graphdb.Neo4jMatchers.inTx;
import static org.neo4j.helpers.collection.Iterables.map;
import static org.neo4j.helpers.collection.Iterables.toList;
import static org.neo4j.helpers.collection.IteratorUtil.asEnumNameSet;
import static org.neo4j.helpers.collection.IteratorUtil.asSet;
import static org.neo4j.helpers.collection.IteratorUtil.count;
public class LabelsAcceptanceTest
{
public @Rule ImpermanentDatabaseRule dbRule = new ImpermanentDatabaseRule();
private enum Labels implements Label
{
MY_LABEL,
MY_OTHER_LABEL
}
/** https://github.com/neo4j/neo4j/issues/1279 */
@Test
public void shouldInsertLabelsWithoutDuplicatingThem() throws Exception
{
final Node node = dbRule.executeAndCommit( new Function<GraphDatabaseService, Node>()
{
@Override
public Node apply( GraphDatabaseService db )
{
return db.createNode();
}
} );
// POST "FOOBAR"
dbRule.executeAndCommit( new Consumer<GraphDatabaseService>()
{
@Override
public void accept( GraphDatabaseService db )
{
node.addLabel( label( "FOOBAR" ) );
}
} );
// POST ["BAZQUX"]
dbRule.executeAndCommit( new Consumer<GraphDatabaseService>()
{
@Override
public void accept( GraphDatabaseService db )
{
node.addLabel( label( "BAZQUX" ) );
}
} );
// PUT ["BAZQUX"]
dbRule.executeAndCommit( new Consumer<GraphDatabaseService>()
{
@Override
public void accept( GraphDatabaseService db )
{
for ( Label label : node.getLabels() )
{
node.removeLabel( label );
}
node.addLabel( label( "BAZQUX" ) );
}
} );
// GET
List<Label> labels = dbRule.executeAndCommit( new Function<GraphDatabaseService, List<Label>>()
{
@Override
public List<Label> apply( GraphDatabaseService db )
{
List<Label> labels = new ArrayList<>();
for ( Label label : node.getLabels() )
{
labels.add( label );
}
return labels;
}
} );
assertEquals( labels.toString(), 1, labels.size() );
assertEquals( "BAZQUX", labels.get( 0 ).name() );
}
@Test
public void addingALabelUsingAValidIdentifierShouldSucceed() throws Exception
{
// Given
GraphDatabaseService graphDatabase = dbRule.getGraphDatabaseService();
Node myNode = null;
// When
try ( Transaction tx = graphDatabase.beginTx() )
{
myNode = graphDatabase.createNode();
myNode.addLabel( Labels.MY_LABEL );
tx.success();
}
// Then
assertThat( "Label should have been added to node", myNode, inTx( graphDatabase, hasLabel( Labels.MY_LABEL ) ) );
}
@Test
public void addingALabelUsingAnInvalidIdentifierShouldFail() throws Exception
{
// Given
GraphDatabaseService graphDatabase = dbRule.getGraphDatabaseService();
// When I set an empty label
try ( Transaction tx = graphDatabase.beginTx() )
{
graphDatabase.createNode().addLabel( label( "" ) );
fail( "Should have thrown exception" );
}
catch ( ConstraintViolationException ex )
{ // Happy
}
// And When I set a null label
try ( Transaction tx2 = graphDatabase.beginTx() )
{
graphDatabase.createNode().addLabel( label( null ) );
fail( "Should have thrown exception" );
}
catch ( ConstraintViolationException ex )
{ // Happy
}
}
@Test
public void addingALabelThatAlreadyExistsBehavesAsNoOp() throws Exception
{
// Given
GraphDatabaseService graphDatabase = dbRule.getGraphDatabaseService();
Node myNode = null;
// When
try (Transaction tx = graphDatabase.beginTx())
{
myNode = graphDatabase.createNode();
myNode.addLabel( Labels.MY_LABEL );
myNode.addLabel( Labels.MY_LABEL );
tx.success();
}
// Then
assertThat( "Label should have been added to node", myNode, inTx( graphDatabase, hasLabel( Labels.MY_LABEL ) ) );
}
@Test
public void oversteppingMaxNumberOfLabelsShouldFailGracefully() throws Exception
{
// Given
GraphDatabaseService graphDatabase = beansAPIWithNoMoreLabelIds();
// When
try (Transaction tx = graphDatabase.beginTx())
{
graphDatabase.createNode().addLabel( Labels.MY_LABEL );
fail( "Should have thrown exception" );
}
catch ( ConstraintViolationException ex )
{ // Happy
}
graphDatabase.shutdown();
}
@Test
public void removingCommittedLabel() throws Exception
{
// Given
GraphDatabaseService graphDatabase = dbRule.getGraphDatabaseService();
Label label = Labels.MY_LABEL;
Node myNode = createNode( graphDatabase, label );
// When
try (Transaction tx = graphDatabase.beginTx())
{
myNode.removeLabel( label );
tx.success();
}
// Then
assertThat( myNode, not( inTx( graphDatabase, hasLabel( label ) ) ) );
}
@Test
public void createNodeWithLabels() throws Exception
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseService();
// WHEN
Node node = null;
try (Transaction tx = db.beginTx())
{
node = db.createNode( Labels.values() );
tx.success();
}
// THEN
assertThat( node, inTx( db, hasLabels( asEnumNameSet( Labels.class ) ) ));
}
@Test
public void removingNonExistentLabel() throws Exception
{
// Given
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Label label = Labels.MY_LABEL;
// When
Node myNode;
try (Transaction tx = beansAPI.beginTx())
{
myNode = beansAPI.createNode();
myNode.removeLabel( label );
tx.success();
}
// THEN
assertThat( myNode, not( inTx( beansAPI, hasLabel( label ) ) ) );
}
@Test
public void removingExistingLabelFromUnlabeledNode() throws Exception
{
// Given
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Label label = Labels.MY_LABEL;
createNode( beansAPI, label );
Node myNode = createNode( beansAPI );
// When
try (Transaction tx = beansAPI.beginTx())
{
myNode.removeLabel( label );
tx.success();
}
// THEN
assertThat( myNode, not( inTx( beansAPI, hasLabel( label ) ) ) ) ;
}
@Test
public void removingUncommittedLabel() throws Exception
{
// Given
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Label label = Labels.MY_LABEL;
// When
Node myNode;
try (Transaction tx = beansAPI.beginTx())
{
myNode = beansAPI.createNode();
myNode.addLabel( label );
myNode.removeLabel( label );
// THEN
assertFalse( myNode.hasLabel( label ) );
tx.success();
}
}
@Test
public void shouldBeAbleToListLabelsForANode() throws Exception
{
// GIVEN
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Node node = null;
Set<String> expected = asSet( Labels.MY_LABEL.name(), Labels.MY_OTHER_LABEL.name() );
try (Transaction tx = beansAPI.beginTx())
{
node = beansAPI.createNode();
for ( String label : expected )
{
node.addLabel( label( label ) );
}
tx.success();
}
assertThat(node, inTx( beansAPI, hasLabels( expected ) ));
}
@Test
public void shouldReturnEmptyListIfNoLabels() throws Exception
{
// GIVEN
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Node node = createNode( beansAPI );
// WHEN THEN
assertThat(node, inTx( beansAPI, hasNoLabels() ));
}
@Test
public void getNodesWithLabelCommitted() throws Exception
{
// Given
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
// When
Node node = null;
try ( Transaction tx = beansAPI.beginTx() )
{
node = beansAPI.createNode();
node.addLabel( Labels.MY_LABEL );
tx.success();
}
// THEN
assertThat( beansAPI, inTx( beansAPI, hasNodes( Labels.MY_LABEL, node ) ) );
assertThat( beansAPI, inTx( beansAPI, hasNoNodes( Labels.MY_OTHER_LABEL ) ) );
}
@Test
public void getNodesWithLabelsWithTxAddsAndRemoves() throws Exception
{
// GIVEN
GraphDatabaseService beansAPI = dbRule.getGraphDatabaseService();
Node node1 = createNode( beansAPI, Labels.MY_LABEL, Labels.MY_OTHER_LABEL );
Node node2 = createNode( beansAPI, Labels.MY_LABEL, Labels.MY_OTHER_LABEL );
// WHEN
Node node3 = null;
Set<Node> nodesWithMyLabel = null, nodesWithMyOtherLabel = null;
try (Transaction tx = beansAPI.beginTx())
{
node3 = beansAPI.createNode( Labels.MY_LABEL );
node2.removeLabel( Labels.MY_LABEL );
// extracted here to be asserted below
nodesWithMyLabel = asSet( beansAPI.findNodes( Labels.MY_LABEL ) );
nodesWithMyOtherLabel = asSet( beansAPI.findNodes( Labels.MY_OTHER_LABEL ) );
tx.success();
}
// THEN
assertEquals( asSet( node1, node3 ), nodesWithMyLabel );
assertEquals( asSet( node1, node2 ), nodesWithMyOtherLabel );
}
@Test
public void shouldListLabels() throws Exception
{
// Given
GraphDatabaseService db = dbRule.getGraphDatabaseService();
GlobalGraphOperations globalOps = GlobalGraphOperations.at( db );
createNode( db, Labels.MY_LABEL, Labels.MY_OTHER_LABEL );
List<Label> labels = null;
// When
try (Transaction tx = db.beginTx())
{
labels = toList( globalOps.getAllLabels() );
}
// Then
assertEquals( 2, labels.size() );
assertThat( map( new Function<Label,String>()
{
@Override
public String apply( Label label )
{
return label.name();
}
}, labels ), hasItems( Labels.MY_LABEL.name(), Labels.MY_OTHER_LABEL.name() ) );
}
@Test
public void deleteAllNodesAndTheirLabels() throws Exception
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseService();
final Label label = DynamicLabel.label( "A" );
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode();
node.addLabel( label );
node.setProperty( "name", "bla" );
tx.success();
}
// WHEN
try ( Transaction tx = db.beginTx() )
{
for ( final Node node : GlobalGraphOperations.at( db ).getAllNodes() )
{
node.removeLabel( label ); // remove Label ...
node.delete(); // ... and afterwards the node
}
tx.success();
} // tx.close(); - here comes the exception
// THEN
try (Transaction transaction = db.beginTx())
{
assertEquals( 0, count( GlobalGraphOperations.at( db ).getAllNodes() ) );
}
}
@Test
public void removingLabelDoesNotBreakPreviouslyCreatedLabelsIterator()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseService();
Label label1 = DynamicLabel.label( "A" );
Label label2 = DynamicLabel.label( "B" );
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( label1, label2 );
for ( Label next : node.getLabels() )
{
node.removeLabel( next );
}
tx.success();
}
}
@Test
public void removingPropertyDoesNotBreakPreviouslyCreatedNodePropertyKeysIterator()
{
// GIVEN
GraphDatabaseService db = dbRule.getGraphDatabaseService();
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode();
node.setProperty( "name", "Horst" );
node.setProperty( "age", "72" );
Iterator<String> iterator = node.getPropertyKeys().iterator();
while ( iterator.hasNext() )
{
node.removeProperty( iterator.next() );
}
tx.success();
}
}
@Test
public void shouldCreateNodeWithLotsOfLabelsAndThenRemoveMostOfThem() throws Exception
{
// given
final int TOTAL_NUMBER_OF_LABELS = 200, NUMBER_OF_PRESERVED_LABELS = 20;
GraphDatabaseService db = dbRule.getGraphDatabaseService();
Node node;
try ( Transaction tx = db.beginTx() )
{
node = db.createNode();
for ( int i = 0; i < TOTAL_NUMBER_OF_LABELS; i++ )
{
node.addLabel( DynamicLabel.label( "label:" + i ) );
}
tx.success();
}
// when
try ( Transaction tx = db.beginTx() )
{
for ( int i = NUMBER_OF_PRESERVED_LABELS; i < TOTAL_NUMBER_OF_LABELS; i++ )
{
node.removeLabel( DynamicLabel.label( "label:" + i ) );
}
tx.success();
}
// then
try ( Transaction transaction = db.beginTx() )
{
List<String> labels = new ArrayList<>();
for ( Label label : node.getLabels() )
{
labels.add( label.name() );
}
assertEquals( "labels on node: " + labels, NUMBER_OF_PRESERVED_LABELS, labels.size() );
}
}
@Ignore("Fix this properly later")
@Test
public void shouldAllowManyLabelsAndPropertyCursor()
{
// given
GraphDatabaseService db = dbRule.getGraphDatabaseService();
Node node;
try ( Transaction tx = db.beginTx() )
{
node = db.createNode();
node.setProperty( "foo", "bar" );
for ( int i = 0; i < 20; i++ )
{
node.addLabel( DynamicLabel.label( "label:" + i ) );
}
tx.success();
}
// when
try ( Transaction tx = db.beginTx() )
{
try (Statement statement = ((TopLevelTransaction)tx).getTransaction().acquireStatement())
{
try ( Cursor<NodeItem> nodeCursor = statement.readOperations().nodeCursor( node.getId() ) )
{
if (nodeCursor.next())
{
try ( Cursor<PropertyItem> properties = nodeCursor.get().properties() )
{
while (properties.next())
{
try ( Cursor<LabelItem> labels = nodeCursor.get().labels() )
{
while (labels.next())
{
}
}
}
}
}
}
}
tx.success();
}
// then
// No exceptions from the above
}
@SuppressWarnings("deprecation")
private GraphDatabaseService beansAPIWithNoMoreLabelIds()
{
final EphemeralIdGenerator.Factory idFactory = new EphemeralIdGenerator.Factory()
{
private IdTypeConfigurationProvider
idTypeConfigurationProvider = new CommunityIdTypeConfigurationProvider();
@Override
public IdGenerator open( File fileName, int grabSize, IdType idType, long highId )
{
if ( idType == IdType.LABEL_TOKEN )
{
IdGenerator generator = generators.get( idType );
if ( generator == null )
{
IdTypeConfiguration idTypeConfiguration = idTypeConfigurationProvider.getIdTypeConfiguration( idType );
generator = new EphemeralIdGenerator( idType, idTypeConfiguration )
{
@Override
public long nextId()
{
// Same exception as the one thrown by IdGeneratorImpl
throw new UnderlyingStorageException( "Id capacity exceeded" );
}
};
generators.put( idType, generator );
}
return generator;
}
return super.open( fileName, grabSize, idType, Long.MAX_VALUE );
}
};
TestGraphDatabaseFactory dbFactory = new TestGraphDatabaseFactory()
{
@Override
protected GraphDatabaseBuilder.DatabaseCreator createImpermanentDatabaseCreator(
final File storeDir, final TestGraphDatabaseFactoryState state )
{
return new GraphDatabaseBuilder.DatabaseCreator()
{
@Override
public GraphDatabaseService newDatabase( Map<String,String> config )
{
return new ImpermanentGraphDatabase( storeDir, config, GraphDatabaseDependencies.newDependencies(state.databaseDependencies() ))
{
@Override
protected void create( File storeDir, Map<String, String> params, GraphDatabaseFacadeFactory
.Dependencies dependencies )
{
new CommunityFacadeFactory()
{
@Override
protected EditionModule createEdition( PlatformModule platformModule )
{
return new CommunityEditionModule( platformModule )
{
@Override
protected IdGeneratorFactory createIdGeneratorFactory(
FileSystemAbstraction fs,
IdTypeConfigurationProvider idTypeConfigurationProvider )
{
return idFactory;
}
};
}
@Override
protected PlatformModule createPlatform( File storeDir, Map<String, String> params,
Dependencies dependencies, GraphDatabaseFacade graphDatabaseFacade,
OperationalMode operationalMode )
{
return new ImpermanentPlatformModule( storeDir, params, dependencies, graphDatabaseFacade );
}
}.newFacade( storeDir, params, dependencies, this );
}
};
}
};
}
};
return dbFactory.newImpermanentDatabase();
}
private Node createNode( GraphDatabaseService db, Label... labels )
{
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( labels );
tx.success();
return node;
}
}
}