use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class CollectionTerminalState method getData.
private Data getData(Set<PersistentResource> collection, EntityDictionary dictionary) {
Preconditions.checkNotNull(collection);
List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList());
if (parent.isPresent()) {
Type<?> parentClass = parent.get().getResourceType();
String relationshipName = relationName.orElseThrow(IllegalStateException::new);
RelationshipType type = dictionary.getRelationshipType(parentClass, relationshipName);
return new Data<>(resources, type);
}
return new Data<>(resources);
}
use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class RecordTerminalState method handlePatch.
@Override
public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) {
JsonApiDocument jsonApiDocument = state.getJsonApiDocument();
Data<Resource> data = jsonApiDocument.getData();
if (data == null) {
throw new InvalidEntityBodyException("Expected data but found null");
}
if (!data.isToOne()) {
throw new InvalidEntityBodyException("Expected single element but found list");
}
Resource resource = data.getSingleValue();
if (!record.matchesId(resource.getId())) {
throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path");
}
patch(resource, state.getRequestScope());
return constructPatchResponse(record, state);
}
use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class JsonApiPatch method handleRemoveOp.
/**
* Remove data via patch extension.
*/
private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) {
try {
JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
String fullPath;
if (path.contains("relationships")) {
// Reserved keyword for relationships
fullPath = path;
} else {
Data<Resource> data = value.getData();
if (data == null || data.get() == null) {
fullPath = path;
} else {
Collection<Resource> resources = data.get();
String id = getSingleResource(resources).getId();
fullPath = path + "/" + id;
}
}
DeleteVisitor visitor = new DeleteVisitor(new PatchRequestScope(path, value, requestScope));
return visitor.visit(JsonApiParser.parse(fullPath));
} catch (IOException e) {
throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
}
}
use of com.yahoo.elide.jsonapi.models.Resource 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.Resource 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)));
}
Aggregations