9090import java .time .Instant ;
9191import java .util .ArrayList ;
9292import java .util .Arrays ;
93+ import java .util .Collection ;
9394import java .util .Collections ;
9495import java .util .HashMap ;
9596import java .util .HashSet ;
@@ -2633,8 +2634,38 @@ public void testRemoveRelationshipsDuringAspectSoftDeletion() throws URISyntaxEx
26332634 public void testDeleteManyWithRelationshipRemoval () throws URISyntaxException {
26342635 FooUrn fooUrn = makeFooUrn (1 );
26352636 EbeanLocalDAO <EntityAspectUnion , FooUrn > fooDao = createDao (FooUrn .class );
2637+ // necessary flag to prevent removal of existing same-type relationships in "another aspect"
2638+ fooDao .setUseAspectColumnForRelationshipRemoval (true );
26362639
2637- setupAspectsAndRelationships (fooUrn , fooDao );
2640+ EbeanLocalDAO <EntityAspectUnion , BarUrn > barDao = createDao (BarUrn .class );
2641+
2642+ // add an aspect (AspectFooBar) which includes BelongsTo relationships and ReportsTo relationships
2643+ BarUrn barUrn1 = BarUrn .createFromString ("urn:li:bar:1" );
2644+ BelongsToV2 belongsTo1 = new BelongsToV2 ().setDestination (BelongsToV2 .Destination .create (barUrn1 .toString ()));
2645+ BarUrn barUrn2 = BarUrn .createFromString ("urn:li:bar:2" );
2646+ BelongsToV2 belongsTo2 = new BelongsToV2 ().setDestination (BelongsToV2 .Destination .create (barUrn2 .toString ()));
2647+ BarUrn barUrn3 = BarUrn .createFromString ("urn:li:bar:3" );
2648+ BelongsToV2 belongsTo3 = new BelongsToV2 ().setDestination (BelongsToV2 .Destination .create (barUrn3 .toString ()));
2649+ BelongsToV2Array belongsToArray = new BelongsToV2Array (belongsTo1 , belongsTo2 , belongsTo3 );
2650+ ReportsTo reportsTo = new ReportsTo ().setSource (fooUrn ).setDestination (barUrn1 );
2651+ ReportsToArray reportsToArray = new ReportsToArray (reportsTo );
2652+ AspectFooBar aspectFooBar = new AspectFooBar ()
2653+ .setBars (new BarUrnArray (barUrn1 , barUrn2 , barUrn3 )).setBelongsTos (belongsToArray ).setReportsTos (reportsToArray );
2654+ AuditStamp auditStamp = makeAuditStamp ("foo" , System .currentTimeMillis ());
2655+
2656+ fooDao .add (fooUrn , aspectFooBar , auditStamp );
2657+ barDao .add (barUrn1 , new AspectFoo ().setValue ("1" ), auditStamp );
2658+ barDao .add (barUrn2 , new AspectFoo ().setValue ("2" ), auditStamp );
2659+ barDao .add (barUrn3 , new AspectFoo ().setValue ("3" ), auditStamp );
2660+
2661+ // add an aspect (AspectFooBaz) which includes BelongsTo relationships
2662+ BarUrn barUrn4 = BarUrn .createFromString ("urn:li:bar:4" );
2663+ BelongsToV2 belongsTo4 = new BelongsToV2 ().setDestination (BelongsToV2 .Destination .create (barUrn4 .toString ()));
2664+ BelongsToV2Array belongsToArray2 = new BelongsToV2Array (belongsTo4 );
2665+ AspectFooBaz aspectFooBaz = new AspectFooBaz ().setBars (new BarUrnArray (barUrn4 )).setBelongsTos (belongsToArray2 );
2666+
2667+ fooDao .add (fooUrn , aspectFooBaz , auditStamp );
2668+ barDao .add (barUrn4 , new AspectFoo ().setValue ("4" ), auditStamp );
26382669
26392670 // Verify local relationships and entities are added.
26402671 EbeanLocalRelationshipQueryDAO ebeanLocalRelationshipQueryDAO = new EbeanLocalRelationshipQueryDAO (_server );
@@ -2658,7 +2689,24 @@ public void testDeleteManyWithRelationshipRemoval() throws URISyntaxException {
26582689 assertEquals (aspects .size (), 1 );
26592690
26602691 // soft delete the AspectFooBar and AspectFooBaz aspects
2661- fooDao .deleteMany (fooUrn , new HashSet <>(Arrays .asList (AspectFooBar .class , AspectFooBaz .class )), _dummyAuditStamp );
2692+ Collection <EntityAspectUnion > deletedAspects =
2693+ fooDao .deleteMany (fooUrn , new HashSet <>(Arrays .asList (AspectFooBar .class , AspectFooBaz .class )), _dummyAuditStamp );
2694+
2695+ assertEquals (deletedAspects .size (), 2 );
2696+
2697+ // check that the AspectFooBar content returned matches the pre-deletion content
2698+ Optional <EntityAspectUnion > aspectFooBarDeleted = deletedAspects .stream ()
2699+ .filter (EntityAspectUnion ::isAspectFooBar )
2700+ .findFirst ();
2701+ assertTrue (aspectFooBarDeleted .isPresent ());
2702+ assertEquals (aspectFooBarDeleted .get ().getAspectFooBar (), aspectFooBar );
2703+
2704+ // check that the AspectFooBaz content returned matches the pre-deletion content
2705+ Optional <EntityAspectUnion > aspectFooBazDeleted = deletedAspects .stream ()
2706+ .filter (EntityAspectUnion ::isAspectFooBaz )
2707+ .findFirst ();
2708+ assertTrue (aspectFooBazDeleted .isPresent ());
2709+ assertEquals (aspectFooBazDeleted .get ().getAspectFooBaz (), aspectFooBaz );
26622710
26632711 // check that the belongsTo relationships 1, 2, 3, and 4 were soft deleted
26642712 resultBelongsTos = ebeanLocalRelationshipQueryDAO .findRelationships (FooSnapshot .class , EMPTY_FILTER , BarSnapshot .class ,
@@ -2682,6 +2730,101 @@ public void testDeleteManyWithRelationshipRemoval() throws URISyntaxException {
26822730 assertFalse (optionalAspect2 .isPresent ());
26832731 }
26842732
2733+ @ Test
2734+ public void testDeleteWithReturnOnNonexistentAsset () {
2735+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2736+ FooUrn urn = makeFooUrn (1 );
2737+
2738+ AspectFoo foo = dao .deleteWithReturn (urn , AspectFoo .class , _dummyAuditStamp , 3 , null );
2739+ assertNull (foo );
2740+ }
2741+
2742+ @ Test
2743+ public void testDeleteWithReturnOnNullAspect () {
2744+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2745+ FooUrn urn = makeFooUrn (1 );
2746+
2747+ // add aspect so the row exists in the entity table, but the column for other aspects will be empty
2748+ AspectFoo v0 = new AspectFoo ().setValue ("foo" );
2749+ dao .add (urn , v0 , _dummyAuditStamp );
2750+
2751+ // attempt to delete an aspect that doesn't exist
2752+ AspectBar foo = dao .deleteWithReturn (urn , AspectBar .class , _dummyAuditStamp , 3 , null );
2753+ assertNull (foo );
2754+ }
2755+
2756+ @ Test
2757+ public void testDeleteWithReturnOnAlreadyDeletedAspect () {
2758+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2759+ FooUrn urn = makeFooUrn (1 );
2760+ AspectFoo v0 = new AspectFoo ().setValue ("foo" );
2761+ dao .add (urn , v0 , _dummyAuditStamp );
2762+ AspectFoo foo = dao .deleteWithReturn (urn , AspectFoo .class , _dummyAuditStamp , 3 , null );
2763+
2764+ // make sure that the content matches the original
2765+ assertEquals (foo , v0 );
2766+
2767+ // attempt to delete an aspect that has already been deleted
2768+ AspectFoo fooAgain = dao .deleteWithReturn (urn , AspectFoo .class , _dummyAuditStamp , 3 , null );
2769+ assertNull (fooAgain );
2770+ }
2771+
2772+ @ Test
2773+ public void testDeleteManyOnNonexistentAsset () {
2774+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2775+ FooUrn urn = makeFooUrn (1 );
2776+
2777+ Collection <EntityAspectUnion > deletionResults =
2778+ dao .deleteMany (urn , new HashSet <>(Collections .singletonList (AspectFoo .class )), _dummyAuditStamp , 3 , null );
2779+
2780+ // make sure return collection is empty
2781+ assertEquals (deletionResults .size (), 0 );
2782+ }
2783+
2784+ @ Test
2785+ public void testDeleteManyOnNullAspect () {
2786+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2787+ FooUrn urn = makeFooUrn (1 );
2788+
2789+ // add aspect so the row exists in the entity table, but the column for other aspects will be empty
2790+ AspectFoo v0 = new AspectFoo ().setValue ("foo" );
2791+ dao .add (urn , v0 , _dummyAuditStamp );
2792+
2793+ // attempt to delete an aspect that doesn't exist
2794+ Collection <EntityAspectUnion > deletionResults =
2795+ dao .deleteMany (urn , new HashSet <>(Collections .singletonList (AspectBar .class )), _dummyAuditStamp , 3 , null );
2796+
2797+ // make sure return collection is empty
2798+ assertEquals (deletionResults .size (), 0 );
2799+ }
2800+
2801+ @ Test
2802+ public void testDeleteManyOnAlreadyDeletedAspect () {
2803+ EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
2804+ FooUrn urn = makeFooUrn (1 );
2805+ AspectFoo v0 = new AspectFoo ().setValue ("foo" );
2806+ dao .add (urn , v0 , _dummyAuditStamp );
2807+
2808+ // delete the aspect
2809+ Collection <EntityAspectUnion > deletionResults =
2810+ dao .deleteMany (urn , new HashSet <>(Collections .singletonList (AspectFoo .class )), _dummyAuditStamp , 3 , null );
2811+ assertEquals (deletionResults .size (), 1 );
2812+
2813+ // make sure that the content matches the original
2814+ Optional <EntityAspectUnion > aspectFooDeleted = deletionResults .stream ()
2815+ .filter (EntityAspectUnion ::isAspectFoo )
2816+ .findFirst ();
2817+ assertTrue (aspectFooDeleted .isPresent ());
2818+ assertEquals (aspectFooDeleted .get ().getAspectFoo (), v0 );
2819+
2820+ // attempt to delete an aspect that has already been deleted
2821+ Collection <EntityAspectUnion > deletionResultsAgain =
2822+ dao .deleteMany (urn , new HashSet <>(Collections .singletonList (AspectFoo .class )), _dummyAuditStamp , 3 , null );
2823+
2824+ // make sure return collection is empty
2825+ assertEquals (deletionResultsAgain .size (), 0 );
2826+ }
2827+
26852828 @ Test
26862829 public void testGetWithExtraInfoMultipleKeys () {
26872830 EbeanLocalDAO <EntityAspectUnion , FooUrn > dao = createDao (FooUrn .class );
0 commit comments