This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from JFrogDev/master
NullPointerException is thrown when cascade deleting a broken -to-many relationship
- Loading branch information
Showing
3 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...go/src/test/groovy/org/grails/datastore/gorm/mongo/BrokenManyToManyAssociationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.grails.datastore.gorm.mongo | ||
|
||
import com.mongodb.BasicDBObject | ||
import com.mongodb.DBCollection | ||
import grails.gorm.tests.GormDatastoreSpec | ||
import grails.persistence.Entity | ||
|
||
/** | ||
* @author Noam Y. Tenne | ||
*/ | ||
class BrokenManyToManyAssociationSpec extends GormDatastoreSpec { | ||
|
||
def 'Perform a cascading delete on a broken many-to-many relationship'() { | ||
given:'An owning entity with 2 owned entities' | ||
ReferencingEntity referencing = new ReferencingEntity() | ||
referencing = referencing.save(flush: true) | ||
referencing.addToReferencedEntities(new ReferencedEntity().save()) | ||
referencing.addToReferencedEntities(new ReferencedEntity().save()) | ||
|
||
referencing.save(flush: true) | ||
session.clear() | ||
|
||
when:'Low-level deleting 1 owned entity to simulate a broken relationship' | ||
((DBCollection) ReferencedEntity.collection).remove(new BasicDBObject('_id', ReferencedEntity.find{}.id)) | ||
session.clear() | ||
referencing = ReferencingEntity.find{} | ||
|
||
then:'Expect to still find 2 owned entities, but 1 of them is null (because the reference is broken)' | ||
referencing.referencedEntities.size() == 2 | ||
referencing.referencedEntities.any { it == null } | ||
|
||
and: | ||
when:'Deleting the owning entity, thus invoking a cascading delete' | ||
referencing.delete(flush: true) | ||
session.clear() | ||
|
||
then:'Expect all the entities to be removed with no error' | ||
ReferencedEntity.count == 0 | ||
ReferencingEntity.count == 0 | ||
} | ||
|
||
@Override | ||
List getDomainClasses() { | ||
[ReferencingEntity, ReferencedEntity] | ||
} | ||
} | ||
|
||
@Entity | ||
class ReferencingEntity { | ||
String id | ||
Set<ReferencedEntity> referencedEntities | ||
static hasMany = [referencedEntities: ReferencedEntity] | ||
} | ||
|
||
@Entity | ||
class ReferencedEntity { | ||
String id | ||
static belongsTo = ReferencingEntity | ||
Set<ReferencingEntity> referencingEntities | ||
static hasMany = [referencingEntities: ReferencingEntity] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters