Search in sources :

Example 1 with Author

use of graphqlEndpointTestModels.Author in project elide by yahoo.

the class GraphQLEndpointTest method testNonShareable.

@Test
void testNonShareable() throws IOException, JSONException {
    DisallowTransfer noShare = new DisallowTransfer();
    noShare.setId(1L);
    Author author = new Author();
    author.setId(123L);
    author.setName("my new author");
    author.setNoShare(noShare);
    String graphQLRequest = document(mutation(selection(field("book", selections(field("id"), field("authors", arguments(argument("op", "UPSERT"), argument("data", author)), selections(field("id"), field("name"), field("noShare", selection(field("id")))))))))).toQuery();
    Response response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
    assertHasErrors(response);
    graphQLRequest = document(selection(field("book", selections(field("id"), field("authors", selections(field("id"), field("name"), field("noShare", selection(field("id"))))))))).toQuery();
    String expected = document(selection(field("book", selections(field("id", "1"), field("authors", selections(field("id", "1"), field("name", "Ricky Carmichael"), field("noShare", "", false))))))).toResponse();
    response = endpoint.post(uriInfo, requestHeaders, user1, graphQLRequestToJSON(graphQLRequest));
    assert200EqualBody(response, expected);
}
Also used : Response(javax.ws.rs.core.Response) Author(graphqlEndpointTestModels.Author) DisallowTransfer(graphqlEndpointTestModels.DisallowTransfer) Test(org.junit.jupiter.api.Test)

Example 2 with Author

use of graphqlEndpointTestModels.Author 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);
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Check(com.yahoo.elide.core.security.checks.Check) DisallowTransfer(graphqlEndpointTestModels.DisallowTransfer) AbstractMap(java.util.AbstractMap) ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) Book(graphqlEndpointTestModels.Book) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Author(graphqlEndpointTestModels.Author) Elide(com.yahoo.elide.Elide) CommitChecks(graphqlEndpointTestModels.security.CommitChecks) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with Author

use of graphqlEndpointTestModels.Author 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);
}
Also used : Response(javax.ws.rs.core.Response) Book(graphqlEndpointTestModels.Book) Author(graphqlEndpointTestModels.Author) Test(org.junit.jupiter.api.Test)

Example 4 with Author

use of graphqlEndpointTestModels.Author in project elide by yahoo.

the class GraphQLEndpointTest method testFailedMutationAndRead.

@Test
void testFailedMutationAndRead() throws IOException, JSONException {
    Author author = new Author();
    author.setId(2L);
    Book book = new Book();
    book.setId(1);
    book.setTitle("my new book!");
    book.setAuthors(Sets.newHashSet(author));
    String graphQLRequest = document(selection(field("book", arguments(argument("op", "UPSERT"), argument("data", book)), selections(field("id"), field("title"))))).toQuery();
    Response response = endpoint.post(uriInfo, requestHeaders, user2, graphQLRequestToJSON(graphQLRequest));
    assertHasErrors(response);
    graphQLRequest = document(selection(field("book", selections(field("id"), field("title"))))).toQuery();
    String expected = document(selection(field("book", selections(field("id", "1"), field("title", "My first book"))))).toResponse();
    response = endpoint.post(uriInfo, requestHeaders, user2, graphQLRequestToJSON(graphQLRequest));
    assert200EqualBody(response, expected);
}
Also used : Response(javax.ws.rs.core.Response) Book(graphqlEndpointTestModels.Book) Author(graphqlEndpointTestModels.Author) Test(org.junit.jupiter.api.Test)

Aggregations

Author (graphqlEndpointTestModels.Author)4 Book (graphqlEndpointTestModels.Book)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.jupiter.api.Test)3 DisallowTransfer (graphqlEndpointTestModels.DisallowTransfer)2 Elide (com.yahoo.elide.Elide)1 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)1 Check (com.yahoo.elide.core.security.checks.Check)1 CommitChecks (graphqlEndpointTestModels.security.CommitChecks)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1