Skip to content

Commit

Permalink
revert junit throws (#365)
Browse files Browse the repository at this point in the history
* revert junit throws
  • Loading branch information
godanny86 authored Jun 10, 2022
1 parent 475df84 commit 89bb06a
Showing 1 changed file with 135 additions and 130 deletions.
265 changes: 135 additions & 130 deletions it.tests/src/main/java/com/adobe/aem/guides/wknd/it/tests/GraphQlIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.net.URISyntaxException;
import java.util.HashMap;
Expand All @@ -32,7 +30,9 @@
import org.apache.sling.testing.clients.instance.InstanceConfiguration;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -48,165 +48,170 @@
*/
public class GraphQlIT {

private static final String TEST_AUTHOR_FIRST_NAME = "Ian";

private static final String TEST_AUTHOR_LAST_NAME = "Provo";

private static final String WKND_SHARED_GRAPHQL_ENDPOINT = "/content/_cq_graphql/wknd-shared/endpoint.json";
private static final String TEST_AUTHOR_FIRST_NAME = "Ian";

private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class);
private static final String TEST_AUTHOR_LAST_NAME = "Provo";

@ClassRule
public static final CQAuthorPublishClassRule cqBaseClassRule = new CQAuthorPublishClassRule();
private static final String WKND_SHARED_GRAPHQL_ENDPOINT = "/content/_cq_graphql/wknd-shared/endpoint.json";

static AEMHeadlessClient headlessClientAuthor;
private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class);

@BeforeClass
public static void beforeClass() {
try {
@Rule
public ExpectedException thrown = ExpectedException.none();

@ClassRule
public static final CQAuthorPublishClassRule cqBaseClassRule = new CQAuthorPublishClassRule();

static AEMHeadlessClient headlessClientAuthor;

@BeforeClass
public static void beforeClass() {
try {
headlessClientAuthor = getHeadlessClient(cqBaseClassRule.authorRule.getConfiguration());
} catch (URISyntaxException e) {
LOGGER.error("Error attempting to initialize headless client", e);
}
}
}

private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException {
private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException {
final String graphQLEndpoint = instanceConfig.getUrl() + WKND_SHARED_GRAPHQL_ENDPOINT;
return AEMHeadlessClient.builder() //
.endpoint(graphQLEndpoint) //
.basicAuth(instanceConfig.getAdminUser(), instanceConfig.getAdminPassword()).build();
}

@Test
public void testQuery() {

String query = "{\n" + //
" articleList{\n" + //
" items{ \n" + //
" _path\n" + //
" authorFragment{\n" + //
return AEMHeadlessClient.builder() //
.endpoint(graphQLEndpoint) //
.basicAuth(instanceConfig.getAdminUser(), instanceConfig.getAdminPassword()).build();
}

@Test
public void testQuery() {

String query = "{\n" + //
" articleList{\n" + //
" items{ \n" + //
" _path\n" + //
" authorFragment{\n" + //
" firstName\n" + //
" lastName\n" + //
" }\n" + //
" } \n" + //
" }\n" + //
"}";
GraphQlResponse response = headlessClientAuthor.runQuery(query);
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode articleList = responseData.get("articleList");
assertNotNull(articleList);
JsonNode articleListItems = articleList.get("items");
assertNotNull(articleListItems);
assertEquals(7, articleListItems.size());
assertNotNull(articleListItems.get(0).get("_path"));
assertNotNull(articleListItems.get(0).get("authorFragment"));
}

@Test
public void testQueryWithSyntaxError() {
" } \n" + //
" }\n" + //
"}";
GraphQlResponse response = headlessClientAuthor.runQuery(query);
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode articleList = responseData.get("articleList");
assertNotNull(articleList);
JsonNode articleListItems = articleList.get("items");
assertNotNull(articleListItems);
assertEquals(7, articleListItems.size());
assertNotNull(articleListItems.get(0).get("_path"));
assertNotNull(articleListItems.get(0).get("authorFragment"));
}

@Test
public void testQueryWithSyntaxError() {

thrown.expect(AEMHeadlessClientException.class);
thrown.expectMessage("Invalid Syntax : offending token");

String query = "{\n" + //
" articleList{\n" + //
" items{ \n" + //
" _path\n" + //
" author\n";
" articleList{\n" + //
" items{ \n" + //
" _path\n" + //
" author\n";

AEMHeadlessClientException exception = assertThrows(AEMHeadlessClientException.class,
() -> headlessClientAuthor.runQuery(query));
assertTrue(exception.getMessage().contains("Invalid Syntax : offending token"));
}
headlessClientAuthor.runQuery(query);
}

@Test
public void testQueryWithErrorResponse() {
@Test
public void testQueryWithErrorResponse() {

thrown.expect(AEMHeadlessClientException.class);
thrown.expectMessage("Field 'nonExisting' in type 'QueryType' is undefined");

String query = "{ nonExisting { items{ _path } } }";
AEMHeadlessClientException exception = assertThrows(AEMHeadlessClientException.class,
() -> headlessClientAuthor.runQuery(query));

assertTrue(exception.getMessage().contains("Field 'nonExisting' in type 'QueryType' is undefined"));
headlessClientAuthor.runQuery(query);

}
}

@Test
public void testQueryWithParameters() {
@Test
public void testQueryWithParameters() {

String query = "query($authorFirstName: String, $authorLastName: String) {\n" +
"articleList(filter: {\n" +
"authorFragment: {\n" +
String query = "query($authorFirstName: String, $authorLastName: String) {\n" +
"articleList(filter: {\n" +
"authorFragment: {\n" +
"firstName: {\n" +
"_expressions: {\n" +
"value: $authorFirstName\n" +
"}\n" +
"_expressions: {\n" +
"value: $authorFirstName\n" +
"}\n" +
"}\n" +
"lastName: {\n" +
"_expressions: {\n" +
"value: $authorLastName\n" +
"}\n" +
"_expressions: {\n" +
"value: $authorLastName\n" +
"}\n" +
"}\n" +
"}\n" +
"}) {\n" +
"items {\n" +
"}\n" +
"}) {\n" +
"items {\n" +
"_path\n" +
"authorFragment {\n" +
"firstName\n" +
"lastName\n" +
"firstName\n" +
"lastName\n" +
"}\n" +
"}\n" +
"}\n" +
"}\n" +
"}\n" +
"}";
"}";

Map<String, Object> vars = new HashMap<>();
vars.put("authorFirstName", TEST_AUTHOR_FIRST_NAME);
Map<String, Object> vars = new HashMap<>();
vars.put("authorFirstName", TEST_AUTHOR_FIRST_NAME);
vars.put("authorLastName", TEST_AUTHOR_LAST_NAME);

GraphQlResponse response = headlessClientAuthor.runQuery(query, vars);
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode articleList = responseData.get("articleList");
assertNotNull(articleList);
JsonNode articleListItems = articleList.get("items");
assertNotNull(articleListItems);
assertEquals(1, articleListItems.size());
assertEquals(TEST_AUTHOR_FIRST_NAME, articleListItems.get(0).get("authorFragment").get("firstName").asText());
GraphQlResponse response = headlessClientAuthor.runQuery(query, vars);
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode articleList = responseData.get("articleList");
assertNotNull(articleList);
JsonNode articleListItems = articleList.get("items");
assertNotNull(articleListItems);
assertEquals(1, articleListItems.size());
assertEquals(TEST_AUTHOR_FIRST_NAME, articleListItems.get(0).get("authorFragment").get("firstName").asText());
assertEquals(TEST_AUTHOR_LAST_NAME, articleListItems.get(0).get("authorFragment").get("lastName").asText());
}

@Test
public void testPersistedQuery() {

GraphQlResponse response = headlessClientAuthor.runPersistedQuery("/wknd-shared/adventures-all");
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode adventureListList = responseData.get("adventureList");
assertNotNull(adventureListList);
JsonNode adventureListItems = adventureListList.get("items");
assertNotNull(adventureListItems);
assertEquals(16, adventureListItems.size());
JsonNode firstAdvantureItem = adventureListItems.get(0);
assertNotNull(firstAdvantureItem.get("_path"));
assertNotNull(firstAdvantureItem.get("title"));
assertNotNull(firstAdvantureItem.get("price"));
assertNotNull(firstAdvantureItem.get("tripLength"));
assertNotNull(firstAdvantureItem.get("primaryImage"));

}

@Test
public void testListPersistedQueries() {

List<PersistedQuery> listPersistedQueries = headlessClientAuthor.listPersistedQueries("wknd-shared");

assertFalse(listPersistedQueries.isEmpty());
PersistedQuery adventuresQuery = listPersistedQueries.stream()
.filter(p -> p.getShortPath().equals("/wknd-shared/adventures-all")).findFirst().get();
assertEquals("/wknd-shared/settings/graphql/persistentQueries/adventures-all", adventuresQuery.getLongPath());
assertThat(adventuresQuery.getQuery(), containsString("adventureList {"));
}
}

@Test
public void testPersistedQuery() {

GraphQlResponse response = headlessClientAuthor.runPersistedQuery("/wknd-shared/adventures-all");
assertNull(response.getErrors());
JsonNode responseData = response.getData();

assertNotNull(responseData);
JsonNode adventureListList = responseData.get("adventureList");
assertNotNull(adventureListList);
JsonNode adventureListItems = adventureListList.get("items");
assertNotNull(adventureListItems);
assertEquals(16, adventureListItems.size());
JsonNode firstAdvantureItem = adventureListItems.get(0);
assertNotNull(firstAdvantureItem.get("_path"));
assertNotNull(firstAdvantureItem.get("title"));
assertNotNull(firstAdvantureItem.get("price"));
assertNotNull(firstAdvantureItem.get("tripLength"));
assertNotNull(firstAdvantureItem.get("primaryImage"));

}

@Test
public void testListPersistedQueries() {

List<PersistedQuery> listPersistedQueries = headlessClientAuthor.listPersistedQueries("wknd-shared");

assertFalse(listPersistedQueries.isEmpty());
PersistedQuery adventuresQuery = listPersistedQueries.stream()
.filter(p -> p.getShortPath().equals("/wknd-shared/adventures-all")).findFirst().get();
assertEquals("/wknd-shared/settings/graphql/persistentQueries/adventures-all", adventuresQuery.getLongPath());
assertThat(adventuresQuery.getQuery(), containsString("adventureList {"));
}

}

0 comments on commit 89bb06a

Please sign in to comment.