use of graphqlEndpointTestModels.Book in project elide by yahoo.
the class GraphQLEndpointTest method testLifeCycleHooks.
@Test
void testLifeCycleHooks() throws Exception {
/* Separate user 1 so it doesn't interfere */
SecurityContext user = Mockito.mock(SecurityContext.class);
User principal = new User().withName("1");
Mockito.when(user.getUserPrincipal()).thenReturn(principal);
Book book = new Book();
book.setId(1);
book.setTitle("my new book!");
String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
String expected = document(selection(field("book", selections(field("id", "1"), field("title", "my new book!"))))).toResponse();
Response response = endpoint.post(uriInfo, requestHeaders, user, graphQLRequestToJSON(graphQLRequest));
assert200EqualBody(response, expected);
String expectedLog = "On Title Update Pre Security\nOn Title Update Pre Commit\nOn Title Update Post Commit\n";
assertEquals(principal.getLog(), expectedLog);
}
use of graphqlEndpointTestModels.Book in project elide by yahoo.
the class GraphQLEndpointTest method testFailedCommitCheck.
@Test
void testFailedCommitCheck() throws IOException {
Book book = new Book();
book.setId(1);
book.setTitle("update title");
// NOTE: User 3 cannot update books.
String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
Response response = endpoint.post(uriInfo, requestHeaders, user3, graphQLRequestToJSON(graphQLRequest));
assertHasErrors(response);
verify(elide).mapError(any());
}
use of graphqlEndpointTestModels.Book in project elide by yahoo.
the class GraphQLEndpointTest method testAuditLogging.
@Test
void testAuditLogging() throws IOException {
Mockito.reset(audit);
Book book = new Book();
book.setTitle("my new book!");
String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title")))))).toQuery();
endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
verify(audit, Mockito.times(1)).log(Mockito.any());
verify(audit, Mockito.times(1)).commit();
verify(audit, Mockito.times(1)).clear();
}
use of graphqlEndpointTestModels.Book in project elide by yahoo.
the class GraphQLEndpointTest method setupTest.
@BeforeEach
public void setupTest() throws Exception {
HashMapDataStore inMemoryStore = new HashMapDataStore(DefaultClassScanner.getInstance(), Book.class.getPackage());
Map<String, Class<? extends Check>> checkMappings = new HashMap<>();
checkMappings.put(UserChecks.IS_USER_1, UserChecks.IsUserId.One.class);
checkMappings.put(UserChecks.IS_USER_2, UserChecks.IsUserId.Two.class);
checkMappings.put(CommitChecks.IS_NOT_USER_3, CommitChecks.IsNotUser3.class);
elide = spy(new Elide(new ElideSettingsBuilder(inMemoryStore).withEntityDictionary(EntityDictionary.builder().checks(checkMappings).build()).withAuditLogger(audit).build()));
elide.doScans();
endpoint = new GraphQLEndpoint(elide);
DataStoreTransaction tx = inMemoryStore.beginTransaction();
// Initial data
Book book1 = new Book();
Author author1 = new Author();
Author author2 = new Author();
DisallowTransfer noShare = new DisallowTransfer();
book1.setId(1L);
book1.setTitle("My first book");
book1.setAuthors(Sets.newHashSet(author1));
author1.setId(1L);
author1.setName("Ricky Carmichael");
author1.setBooks(Sets.newHashSet(book1));
author1.setBookTitlesAndAwards(Stream.of(new AbstractMap.SimpleImmutableEntry<>("Bookz", "Pulitzer Prize"), new AbstractMap.SimpleImmutableEntry<>("Lost in the Data", "PEN/Faulkner Award")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
author2.setId(2L);
author2.setName("The Silent Author");
author2.setBookTitlesAndAwards(Stream.of(new AbstractMap.SimpleImmutableEntry<>("Working Hard or Hardly Working", "Booker Prize")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
noShare.setId(1L);
tx.createObject(book1, null);
tx.createObject(author1, null);
tx.createObject(author2, null);
tx.createObject(noShare, null);
tx.save(book1, null);
tx.save(author1, null);
tx.save(author2, null);
tx.save(noShare, null);
tx.commit(null);
}
use of graphqlEndpointTestModels.Book in project elide by yahoo.
the class GraphQLEndpointTest method testSuccessfulMutation.
@Test
void testSuccessfulMutation() throws JSONException {
Author author = new Author();
author.setId(2L);
Book book = new Book();
book.setId(123);
book.setTitle("my new book!");
book.setAuthors(Sets.newHashSet(author));
String graphQLRequest = document(mutation(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title"), field("user1SecretField")))))).toQuery();
String expected = document(selection(field("book", selections(field("id", "2"), field("title", "my new book!"), field("user1SecretField", "this is a secret for user 1 only1"))))).toResponse();
Response response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
assert200EqualBody(response, expected);
graphQLRequest = document(selection(field("book", selections(field("id"), field("title"), field("authors", selections(field("id"), field("name"))))))).toQuery();
expected = document(selection(field("book", selections(field("id", "1"), field("title", "My first book"), field("authors", selections(field("id", "1"), field("name", "Ricky Carmichael")))), selections(field("id", "2"), field("title", "my new book!"), field("authors", selections(field("id", "2"), field("name", "The Silent Author"))))))).toResponse();
response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
assert200EqualBody(response, expected);
}
Aggregations