PersistentResource implements com.yahoo.elide.security.PersistentResource<T> { public PersistentResource getRelation(String relation, String id) { Set<PersistentResource> resources = getRelation(relation, Collections.singletonList(id), Optional.empty(), Optional.empty(), Optional.empty()); if (resources.isEmpty()) { return null; } for (PersistentResource resource : resources) { if (resource.matchesId(id)) { return resource; } } return null; } 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 testGetRelationSuccess() { FunWithPermissions fun = new FunWithPermissions(); Child child1 = newChild(1); Child child2 = newChild(2); Child child3 = newChild(3); Set<Child> children = Sets.newHashSet(child1, child2, child3); fun.setRelation2(children); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodUserScope); when(goodUserScope.getTransaction().getRelation(any(), eq(fun), eq("relation2"), any(), any(), any(), any())).thenReturn(children); Set<PersistentResource> results = getRelation(funResource, "relation2"); assertEquals(3, results.size(), "All of relation elements should be returned."); }
@Test public void testGetRelationFilteredSuccess() { FunWithPermissions fun = new FunWithPermissions(); Child child1 = newChild(1); Child child2 = newChild(-2); Child child3 = newChild(3); Set<Child> children = Sets.newHashSet(child1, child2, child3); fun.setRelation2(Sets.newHashSet(child1, child2, child3)); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodUserScope); when(goodUserScope.getTransaction().getRelation(any(), eq(fun), eq("relation2"), any(), any(), any(), any())).thenReturn(children); Set<PersistentResource> results = getRelation(funResource, "relation2"); assertEquals(2, results.size(), "Only filtered relation elements should be returned."); }
@Test public void testGetRelationForbiddenByEntity() { NoReadEntity noread = new NoReadEntity(); PersistentResource<NoReadEntity> noreadResource = new PersistentResource<>(noread, null, "3", goodUserScope); assertThrows(ForbiddenAccessException.class, () -> getRelation(noreadResource, "child")); }
@Test public void testGetRelationForbiddenByField() { FunWithPermissions fun = new FunWithPermissions(); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", badUserScope); assertThrows(ForbiddenAccessException.class, () -> getRelation(funResource, "relation1")); }
@Test public void testGetRelationForbiddenByEntityAllowedByField() { FirstClassFields firstClassFields = new FirstClassFields(); PersistentResource<FirstClassFields> fcResource = new PersistentResource<>(firstClassFields, null, "3", badUserScope); getRelation(fcResource, "public2"); }
@Test public void testGetRelationForbiddenByEntity2() { FirstClassFields firstClassFields = new FirstClassFields(); PersistentResource<FirstClassFields> fcResource = new PersistentResource<>(firstClassFields, null, "3", badUserScope); assertThrows(ForbiddenAccessException.class, () -> getRelation(fcResource, "private2")); }
@Test public void testGetRelationInvalidRelation() { FunWithPermissions fun = new FunWithPermissions(); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodUserScope); assertThrows(InvalidAttributeException.class, () -> getRelation(funResource, "invalid")); }
@Test public void testGetRelationByInvalidId() { FunWithPermissions fun = new FunWithPermissions(); Child child1 = newChild(1); Child child2 = newChild(2); Child child3 = newChild(3); fun.setRelation2(Sets.newHashSet(child1, child2, child3)); User goodUser = new User(1); DataStoreTransaction tx = mock(DataStoreTransaction.class); when(tx.getRelation(eq(tx), any(), any(), any(), any(), any(), any())).thenReturn(Sets.newHashSet(child1)); RequestScope goodScope = buildRequestScope(tx, goodUser); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodScope); assertThrows(InvalidObjectIdentifierException.class, () -> funResource.getRelation("relation2", "-1000")); }
@Test public void testGetRelationsNoEntityAccess() { FunWithPermissions fun = new FunWithPermissions(); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodUserScope); Set set = getRelation(funResource, "relation4"); assertEquals(0, set.size()); }
@Test public void testGetRelationsNoEntityAccess2() { FunWithPermissions fun = new FunWithPermissions(); PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodUserScope); Set set = getRelation(funResource, "relation5"); assertEquals(0, set.size()); }