Search in sources :

Example 11 with Resource

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);
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType) Data(com.yahoo.elide.jsonapi.models.Data) ToString(lombok.ToString)

Example 12 with Resource

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);
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource)

Example 13 with Resource

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);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) IOException(java.io.IOException) DeleteVisitor(com.yahoo.elide.jsonapi.parser.DeleteVisitor)

Example 14 with Resource

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));
}
Also used : TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Example 15 with Resource

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)));
}
Also used : TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) Relationship(com.yahoo.elide.jsonapi.models.Relationship) NoShareEntity(example.NoShareEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.yahoo.elide.jsonapi.models.Resource)55 Test (org.junit.jupiter.api.Test)35 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)31 PersistentResource (com.yahoo.elide.core.PersistentResource)28 Relationship (com.yahoo.elide.jsonapi.models.Relationship)21 ArrayList (java.util.ArrayList)17 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)16 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)13 Data (com.yahoo.elide.jsonapi.models.Data)13 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)9 LinkedHashSet (java.util.LinkedHashSet)9 Parent (example.Parent)8 HashSet (java.util.HashSet)8 EntityProjection (com.yahoo.elide.core.request.EntityProjection)7 Child (example.Child)7 LinkedHashMap (java.util.LinkedHashMap)7 RequestScope (com.yahoo.elide.core.RequestScope)6 TestUser (com.yahoo.elide.core.security.TestUser)6 User (com.yahoo.elide.core.security.User)6 TestRequestScope (com.yahoo.elide.core.TestRequestScope)5