use of com.yahoo.elide.core.TestRequestScope in project elide by yahoo.
the class JsonApiTest method writeSingle.
@Test
public void writeSingle() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
child.setId(2);
parent.setChildren(Collections.singleton(child));
parent.setFirstName("bob");
child.setParents(Collections.singleton(parent));
child.setFriends(new HashSet<>());
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope).toResource()));
String expected = "{\"data\":{" + "\"type\":\"parent\"," + "\"id\":\"123\"," + "\"attributes\":{\"firstName\":\"bob\"}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[{\"type\":\"child\",\"id\":\"2\"}]}," + "\"spouses\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/spouses\",\"related\":\"http://localhost:8080/json/parent/123/spouses\"}," + "\"data\":[]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123\"}}}";
Data<Resource> data = jsonApiDocument.getData();
String doc = mapper.writeJsonApiDocument(jsonApiDocument);
assertEquals(data, jsonApiDocument.getData());
assertEquals(expected, doc);
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.core.TestRequestScope in project elide by yahoo.
the class JsonApiTest method writeListIncluded.
@Test
public void writeListIncluded() throws JsonProcessingException {
Parent parent = new Parent();
Child child = new Child();
parent.setId(123L);
child.setId(2);
parent.setChildren(Collections.singleton(child));
child.setParents(Collections.singleton(parent));
parent.setFirstName("bob");
child.setFriends(new HashSet<>());
RequestScope userScope = new TestRequestScope(BASE_URL, tx, user, dictionary);
PersistentResource<Parent> pRec = new PersistentResource<>(parent, userScope.getUUIDFor(parent), userScope);
JsonApiDocument jsonApiDocument = new JsonApiDocument();
jsonApiDocument.setData(new Data<>(Collections.singletonList(pRec.toResource())));
jsonApiDocument.addIncluded(new PersistentResource<>(child, pRec, "children", userScope.getUUIDFor(child), userScope).toResource());
// duplicate will be ignored
jsonApiDocument.addIncluded(new PersistentResource<>(child, pRec, "children", userScope.getUUIDFor(child), userScope).toResource());
String expected = "{\"data\":[{" + "\"type\":\"parent\"," + "\"id\":\"123\"," + "\"attributes\":{\"firstName\":\"bob\"}," + "\"relationships\":{" + "\"children\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/children\",\"related\":\"http://localhost:8080/json/parent/123/children\"}," + "\"data\":[{\"type\":\"child\",\"id\":\"2\"}]}," + "\"spouses\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/relationships/spouses\",\"related\":\"http://localhost:8080/json/parent/123/spouses\"}," + "\"data\":[]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123\"}}]," + "\"included\":[{" + "\"type\":\"child\"," + "\"id\":\"2\"," + "\"attributes\":{\"name\":null}," + "\"relationships\":{" + "\"friends\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/friends\",\"related\":\"http://localhost:8080/json/parent/123/children/2/friends\"}," + "\"data\":[]}," + "\"parents\":{" + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2/relationships/parents\",\"related\":\"http://localhost:8080/json/parent/123/children/2/parents\"}," + "\"data\":[{\"type\":\"parent\",\"id\":\"123\"}]}}," + "\"links\":{\"self\":\"http://localhost:8080/json/parent/123/children/2\"}}]" + "}";
Data<Resource> data = jsonApiDocument.getData();
String doc = mapper.writeJsonApiDocument(jsonApiDocument);
assertEquals(data, jsonApiDocument.getData());
assertEquals(expected, doc);
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.core.TestRequestScope in project elide by yahoo.
the class UpdateOnCreateTest method createPermissionOverwrittenForAttributeFailureCase.
@Test
public void createPermissionOverwrittenForAttributeFailureCase() {
com.yahoo.elide.core.RequestScope userFourScope = new TestRequestScope(tx, userFour, dictionary);
UpdateAndCreate updateAndCreateNewObject = new UpdateAndCreate();
when(tx.createNewObject(ClassType.of(UpdateAndCreate.class), userFourScope)).thenReturn(updateAndCreateNewObject);
assertThrows(ForbiddenAccessException.class, () -> {
com.yahoo.elide.core.PersistentResource<UpdateAndCreate> created = com.yahoo.elide.core.PersistentResource.createObject(ClassType.of(UpdateAndCreate.class), userFourScope, Optional.of("7"));
created.updateAttribute("alias", "");
});
}
use of com.yahoo.elide.core.TestRequestScope in project elide by yahoo.
the class UpdateOnCreateTest method createPermissionCheckFieldAnnotationForCreatingAnEntitySuccessCase.
// Create allowed based on field level expression
@Test
public void createPermissionCheckFieldAnnotationForCreatingAnEntitySuccessCase() {
com.yahoo.elide.core.RequestScope userThreeScope = new TestRequestScope(tx, userThree, dictionary);
UpdateAndCreate updateAndCreateNewObject = new UpdateAndCreate();
when(tx.createNewObject(ClassType.of(UpdateAndCreate.class), userThreeScope)).thenReturn(updateAndCreateNewObject);
com.yahoo.elide.core.PersistentResource<UpdateAndCreate> created = com.yahoo.elide.core.PersistentResource.createObject(ClassType.of(UpdateAndCreate.class), userThreeScope, Optional.of("2"));
created.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
use of com.yahoo.elide.core.TestRequestScope in project elide by yahoo.
the class UpdateOnCreateTest method createPermissionCheckFieldAnnotationForCreatingAnEntityFailureCase.
// Create denied based on field level expression
@Test
public void createPermissionCheckFieldAnnotationForCreatingAnEntityFailureCase() {
com.yahoo.elide.core.RequestScope userFourScope = new TestRequestScope(tx, userFour, dictionary);
UpdateAndCreate updateAndCreateNewObject = new UpdateAndCreate();
when(tx.createNewObject(ClassType.of(UpdateAndCreate.class), userFourScope)).thenReturn(updateAndCreateNewObject);
assertThrows(ForbiddenAccessException.class, () -> com.yahoo.elide.core.PersistentResource.createObject(ClassType.of(UpdateAndCreate.class), userFourScope, Optional.of("3")));
}
Aggregations