use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class RecordTerminalState method patch.
private boolean patch(Resource resource, RequestScope requestScope) {
boolean isUpdated = false;
// Update attributes first
Map<String, Object> attributes = resource.getAttributes();
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String fieldName = entry.getKey();
Object newVal = entry.getValue();
isUpdated |= record.updateAttribute(fieldName, newVal);
}
}
// Relations next
Map<String, Relationship> relationships = resource.getRelationships();
if (relationships != null) {
for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
String fieldName = entry.getKey();
Relationship relationship = entry.getValue();
Set<PersistentResource> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
isUpdated |= record.updateRelation(fieldName, resources);
}
}
return isUpdated;
}
use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class PersistentResourceTest method testTransferPermissionSuccessOnUpdateManyRelationship.
@Test
public void testTransferPermissionSuccessOnUpdateManyRelationship() {
example.User userModel = new example.User();
userModel.setId(1);
NoShareEntity noShare1 = new NoShareEntity();
noShare1.setId(1);
NoShareEntity noShare2 = new NoShareEntity();
noShare2.setId(2);
HashSet<NoShareEntity> noshares = Sets.newHashSet(noShare1, noShare2);
/* The no shares already exist so no exception should be thrown */
userModel.setNoShares(noshares);
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("noshare", "1").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare1);
when(tx.getToManyRelation(any(), eq(userModel), any(), any())).thenReturn(new DataStoreIterableBuilder(noshares).build());
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
boolean returnVal = userResource.updateRelation("noShares", ids.toPersistentResources(goodScope));
assertTrue(returnVal);
assertEquals(1, userModel.getNoShares().size());
assertTrue(userModel.getNoShares().contains(noShare1));
}
use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class PersistentResourceTest method testTransferPermissionErrorOnUpdateManyRelationship.
@Test
public void testTransferPermissionErrorOnUpdateManyRelationship() {
example.User userModel = new example.User();
userModel.setId(1);
NoShareEntity noShare1 = new NoShareEntity();
noShare1.setId(1);
NoShareEntity noShare2 = new NoShareEntity();
noShare2.setId(2);
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("noshare", "1").castToResource());
idList.add(new ResourceIdentifier("noshare", "2").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.loadObject(any(), eq(1L), any())).thenReturn(noShare1);
when(tx.loadObject(any(), eq(2L), any())).thenReturn(noShare2);
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<example.User> userResource = new PersistentResource<>(userModel, goodScope.getUUIDFor(userModel), goodScope);
assertThrows(ForbiddenAccessException.class, () -> userResource.updateRelation("noShares", ids.toPersistentResources(goodScope)));
}
use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class PersistentResourceTest method testSuccessfulOneToOneRelationshipAdd.
@Test
public void testSuccessfulOneToOneRelationshipAdd() throws Exception {
Left left = new Left();
Right right = new Right();
left.setId(2);
right.setId(3);
RequestScope goodScope = buildRequestScope(tx, goodUser);
PersistentResource<Left> leftResource = new PersistentResource<>(left, "2", goodScope);
Relationship ids = new Relationship(null, new Data<>(new ResourceIdentifier("right", "3").castToResource()));
when(tx.loadObject(any(), eq(3L), any())).thenReturn(right);
boolean updated = leftResource.updateRelation("one2one", ids.toPersistentResources(goodScope));
goodScope.saveOrCreateObjects();
verify(tx, times(1)).save(left, goodScope);
verify(tx, times(1)).save(right, goodScope);
verify(tx, times(1)).getToOneRelation(tx, left, getRelationship(ClassType.of(Right.class), "one2one"), goodScope);
assertTrue(updated, "The one-2-one relationship should be added.");
assertEquals(3, left.getOne2one().getId(), "The correct object was set in the one-2-one relationship");
}
use of com.yahoo.elide.jsonapi.models.Relationship in project elide by yahoo.
the class PersistentResourceTest method testSuccessfulManyToManyRelationshipUpdate.
@Test
public /*
* The following are ids for a hypothetical relationship.
* GIVEN:
* all (all the ids in the DB) = 1,2,3,4,5
* mine (everything the current user has access to) = 1,2,3
* requested (what the user wants to change to) = 3,6
* THEN:
* deleted (what gets removed from the DB) = 1,2
* final (what get stored in the relationship) = 3,4,5,6
* BECAUSE:
* notMine = all - mine
* updated = (requested UNION mine) - (requested INTERSECT mine)
* deleted = (mine - requested)
* final = (notMine) UNION requested
*/
void testSuccessfulManyToManyRelationshipUpdate() throws Exception {
Parent parent = new Parent();
RequestScope goodScope = buildRequestScope(tx, goodUser);
Child child1 = newChild(1);
Child child2 = newChild(2);
Child child3 = newChild(3);
// Not accessible to goodUser
Child child4 = newChild(-4);
// Not accessible to goodUser
Child child5 = newChild(-5);
Child child6 = newChild(6);
// All = (1,2,3,4,5)
// Mine = (1,2,3)
Set<Child> allChildren = new HashSet<>();
allChildren.add(child1);
allChildren.add(child2);
allChildren.add(child3);
allChildren.add(child4);
allChildren.add(child5);
parent.setChildren(allChildren);
parent.setSpouses(Sets.newHashSet());
when(tx.getToManyRelation(any(), eq(parent), any(), any())).thenReturn(new DataStoreIterableBuilder(allChildren).build());
PersistentResource<Parent> parentResource = new PersistentResource<>(parent, "1", goodScope);
// Requested = (3,6)
List<Resource> idList = new ArrayList<>();
idList.add(new ResourceIdentifier("child", "3").castToResource());
idList.add(new ResourceIdentifier("child", "6").castToResource());
Relationship ids = new Relationship(null, new Data<>(idList));
when(tx.loadObject(any(), eq(2L), any())).thenReturn(child2);
when(tx.loadObject(any(), eq(3L), any())).thenReturn(child3);
when(tx.loadObject(any(), eq(-4L), any())).thenReturn(child4);
when(tx.loadObject(any(), eq(-5L), any())).thenReturn(child5);
when(tx.loadObject(any(), eq(6L), any())).thenReturn(child6);
// Final set after operation = (3,4,5,6)
Set<Child> expected = new HashSet<>();
expected.add(child3);
expected.add(child4);
expected.add(child5);
expected.add(child6);
boolean updated = parentResource.updateRelation("children", ids.toPersistentResources(goodScope));
goodScope.saveOrCreateObjects();
verify(tx, times(1)).save(parent, goodScope);
verify(tx, times(1)).save(child1, goodScope);
verify(tx, times(1)).save(child2, goodScope);
verify(tx, times(1)).save(child6, goodScope);
verify(tx, never()).save(child4, goodScope);
verify(tx, never()).save(child5, goodScope);
verify(tx, never()).save(child3, goodScope);
assertTrue(updated, "Many-2-many relationship should be updated.");
assertTrue(parent.getChildren().containsAll(expected), "All expected members were updated");
assertTrue(expected.containsAll(parent.getChildren()), "All expected members were updated");
/*
* No tests for reference integrity since the parent is the owner and
* this is a many to many relationship.
*/
}
Aggregations