Search in sources :

Example 1 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class JsonApiTest method writeEmptyList.

@Test
public void writeEmptyList() throws JsonProcessingException {
    String expected = "{\"data\":[]}";
    Data<Resource> empty = new Data<>(new ArrayList<>());
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(empty);
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 2 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class JsonApiTest method compareNullAndEmpty.

@Test
public void compareNullAndEmpty() {
    Data<Resource> empty = new Data<>((Resource) null);
    JsonApiDocument jsonApiEmpty = new JsonApiDocument();
    jsonApiEmpty.setData(empty);
    JsonApiDocument jsonApiNull = new JsonApiDocument();
    jsonApiNull.setData(null);
    assertEquals(jsonApiEmpty, jsonApiNull);
    assertEquals(jsonApiEmpty.hashCode(), jsonApiNull.hashCode());
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 3 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class JsonApiTest method writeEmptyObject.

@Test
public void writeEmptyObject() throws JsonProcessingException {
    String expected = "{\"data\":null}";
    Data<Resource> empty = new Data<>((Resource) null);
    JsonApiDocument jsonApiDocument = new JsonApiDocument();
    jsonApiDocument.setData(empty);
    Data<Resource> data = jsonApiDocument.getData();
    String doc = mapper.writeJsonApiDocument(jsonApiDocument);
    assertEquals(data, jsonApiDocument.getData());
    assertEquals(expected, doc);
    checkEquality(jsonApiDocument);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Data(com.yahoo.elide.jsonapi.models.Data) Test(org.junit.jupiter.api.Test)

Example 4 with Data

use of com.yahoo.elide.jsonapi.models.Data 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 5 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class CollectionTerminalState method handlePost.

@Override
public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) {
    RequestScope requestScope = state.getRequestScope();
    JsonApiMapper mapper = requestScope.getMapper();
    newObject = createObject(requestScope);
    parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject));
    return () -> {
        JsonApiDocument returnDoc = new JsonApiDocument();
        returnDoc.setData(new Data<>(newObject.toResource()));
        JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class);
        return Pair.of(HttpStatus.SC_CREATED, responseBody);
    };
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Data(com.yahoo.elide.jsonapi.models.Data) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) RequestScope(com.yahoo.elide.core.RequestScope)

Aggregations

Data (com.yahoo.elide.jsonapi.models.Data)11 Resource (com.yahoo.elide.jsonapi.models.Resource)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)8 PersistentResource (com.yahoo.elide.core.PersistentResource)5 Test (org.junit.jupiter.api.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ArrayList (java.util.ArrayList)3 Async (org.springframework.scheduling.annotation.Async)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResourceNotFoundException (com.faforever.api.web.ResourceNotFoundException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 RequestScope (com.yahoo.elide.core.RequestScope)2 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)2 Relationship (com.yahoo.elide.jsonapi.models.Relationship)2 FeaturedMod (com.faforever.api.data.domain.FeaturedMod)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)1 TestUser (com.yahoo.elide.core.security.TestUser)1 User (com.yahoo.elide.core.security.User)1 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)1