PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { public void removeRelation(String fieldName, PersistentResource removeResource) { Object relation = getValueUnchecked(fieldName); Object original = relation; Object modified = null; if (relation instanceof Collection) { original = copyCollection((Collection) relation); } if (relation instanceof Collection && removeResource != null) { modified = CollectionUtils.disjunction( (Collection) relation, Collections.singleton(removeResource.getObject()) ); } checkFieldAwareDeferPermissions(UpdatePermission.class, fieldName, modified, original); if (relation instanceof Collection) { if (removeResource == null || !((Collection) relation).contains(removeResource.getObject())) { return; } delFromCollection((Collection) relation, fieldName, removeResource, false); } else { if (relation == null || removeResource == null || !relation.equals(removeResource.getObject())) { return; } this.nullValue(fieldName, removeResource); } if (hasInverseRelation(fieldName)) { deleteInverseRelation(fieldName, removeResource.getObject()); removeResource.markDirty(); } if (!Objects.equals(original, modified)) { this.markDirty(); } RelationshipType type = getRelationshipType(fieldName); if (type.isToOne()) { transaction.updateToOneRelation(transaction, obj, fieldName, null, requestScope); } else { transaction.updateToManyRelation(transaction, obj, fieldName, new LinkedHashSet<>(), Sets.newHashSet(removeResource.getObject()), requestScope); } } PersistentResource(@NonNull T obj, PersistentResource parent, String id, @NonNull RequestScope scope); @Override String toString(); static PersistentResource<T> createObject(
            PersistentResource<?> parent,
            Class<T> entityClass,
            RequestScope requestScope,
            Optional<String> uuid); @Override boolean matchesId(String checkId); @SuppressWarnings("resource") @NonNull static PersistentResource<T> loadRecord(
            Class<T> loadClass, String id, RequestScope requestScope); static Set<PersistentResource> loadRecords(
            Class<?> loadClass,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination,
            RequestScope requestScope); boolean updateAttribute(String fieldName, Object newVal); boolean updateRelation(String fieldName, Set<PersistentResource> resourceIdentifiers); boolean clearRelation(String relationName); void removeRelation(String fieldName, PersistentResource removeResource); boolean relationshipAlreadyExists(String fieldName, PersistentResource toAdd); void addRelation(String fieldName, PersistentResource newRelation); void deleteResource(); @Override String getId(); void setId(String id); boolean isIdGenerated(); @Override Optional<String> getUUID(); PersistentResource getRelation(String relation, String id); Set<PersistentResource> getRelation(String relation,
            List<String> ids,
            Optional<FilterExpression> filter,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); Set<PersistentResource> getRelationCheckedFiltered(String relationName,
            Optional<FilterExpression> filterExpression,
            Optional<Sorting> sorting,
            Optional<Pagination> pagination); RelationshipType getRelationshipType(String relation); Object getAttribute(String attr); @Override T getObject(); void setObject(T obj); @Override @JsonIgnore Class<T> getResourceClass(); @Override String getType(); @Override int hashCode(); @Override boolean equals(Object obj); ResourceLineage getLineage(); EntityDictionary getDictionary(); @Override RequestScope getRequestScope(); Resource toResource(); Resource toResourceWithSortingAndPagination(); Resource toResource(final Supplier<Map<String, Relationship>> relationshipSupplier,
                               final Supplier<Map<String, Object>> attributeSupplier); static Object getValue(Object target, String fieldName, RequestScope requestScope);  }
@Test() public void testRemoveToManyRelationSuccess() { Child child = newChild(1); Parent parent1 = newParent(1, child); Parent parent2 = newParent(2, child); Parent parent3 = newParent(3, child); child.setParents(Sets.newHashSet(parent1, parent2, parent3)); DataStoreTransaction tx = mock(DataStoreTransaction.class); User goodUser = new User(1); RequestScope goodScope = buildRequestScope(tx, goodUser); PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope); PersistentResource<Object> removeResource = new PersistentResource<>(parent1, null, "1", goodScope); childResource.removeRelation("parents", removeResource); assertEquals(2, child.getParents().size(), "The many-2-many relationship should be cleared"); assertEquals(0, parent1.getChildren().size(), "The many-2-many inverse relationship should be cleared"); assertEquals(1, parent3.getChildren().size(), "The many-2-many inverse relationship should not be cleared"); assertEquals(1, parent3.getChildren().size(), "The many-2-many inverse relationship should not be cleared"); goodScope.saveOrCreateObjects(); verify(tx, times(1)).save(child, goodScope); verify(tx, times(1)).save(parent1, goodScope); verify(tx, never()).save(parent2, goodScope); verify(tx, never()).save(parent3, goodScope); }
@Test() public void testRemoveToOneRelationSuccess() { FunWithPermissions fun = new FunWithPermissions(); Child child = newChild(1); fun.setRelation3(child); DataStoreTransaction tx = mock(DataStoreTransaction.class); User goodUser = new User(1); RequestScope goodScope = buildRequestScope(tx, goodUser); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", goodScope); PersistentResource<Object> removeResource = new PersistentResource<>(child, null, "1", goodScope); funResource.removeRelation("relation3", removeResource); assertNull(fun.getRelation3(), "The one-2-one relationship should be cleared"); goodScope.saveOrCreateObjects(); verify(tx, times(1)).save(fun, goodScope); verify(tx, never()).save(child, goodScope); }
@Test() public void testRemoveNonexistingToOneRelation() { FunWithPermissions fun = new FunWithPermissions(); Child ownedChild = newChild(1); Child unownedChild = newChild(2); fun.setRelation3(ownedChild); DataStoreTransaction tx = mock(DataStoreTransaction.class); User goodUser = new User(1); RequestScope goodScope = buildRequestScope(tx, goodUser); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", goodScope); PersistentResource<Object> removeResource = new PersistentResource<>(unownedChild, null, "1", goodScope); funResource.removeRelation("relation3", removeResource); assertEquals(ownedChild, fun.getRelation3(), "The one-2-one relationship should NOT be cleared"); verify(tx, never()).save(fun, goodScope); verify(tx, never()).save(ownedChild, goodScope); }
@Test() public void testRemoveNonexistingToManyRelation() { Child child = newChild(1); Parent parent1 = newParent(1, child); Parent parent2 = newParent(2, child); Parent parent3 = newParent(3, child); child.setParents(Sets.newHashSet(parent1, parent2, parent3)); Parent unownedParent = newParent(4, null); DataStoreTransaction tx = mock(DataStoreTransaction.class); User goodUser = new User(1); RequestScope goodScope = buildRequestScope(tx, goodUser); PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope); PersistentResource<Object> removeResource = new PersistentResource<>(unownedParent, null, "1", goodScope); childResource.removeRelation("parents", removeResource); assertEquals(3, child.getParents().size(), "The many-2-many relationship should not be cleared"); assertEquals(1, parent1.getChildren().size(), "The many-2-many inverse relationship should not be cleared"); assertEquals(1, parent3.getChildren().size(), "The many-2-many inverse relationship should not be cleared"); assertEquals(1, parent3.getChildren().size(), "The many-2-many inverse relationship should not be cleared"); verify(tx, never()).save(child, goodScope); verify(tx, never()).save(parent1, goodScope); verify(tx, never()).save(parent2, goodScope); verify(tx, never()).save(parent3, goodScope); }