Skip to content

Commit 89bb06a

Browse files
authored
revert junit throws (#365)
* revert junit throws
1 parent 475df84 commit 89bb06a

File tree

1 file changed

+135
-130
lines changed
  • it.tests/src/main/java/com/adobe/aem/guides/wknd/it/tests

1 file changed

+135
-130
lines changed

it.tests/src/main/java/com/adobe/aem/guides/wknd/it/tests/GraphQlIT.java

+135-130
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertNull;
24-
import static org.junit.Assert.assertThrows;
25-
import static org.junit.Assert.assertTrue;
2624

2725
import java.net.URISyntaxException;
2826
import java.util.HashMap;
@@ -32,7 +30,9 @@
3230
import org.apache.sling.testing.clients.instance.InstanceConfiguration;
3331
import org.junit.BeforeClass;
3432
import org.junit.ClassRule;
33+
import org.junit.Rule;
3534
import org.junit.Test;
35+
import org.junit.rules.ExpectedException;
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838

@@ -48,165 +48,170 @@
4848
*/
4949
public class GraphQlIT {
5050

51-
private static final String TEST_AUTHOR_FIRST_NAME = "Ian";
52-
53-
private static final String TEST_AUTHOR_LAST_NAME = "Provo";
54-
55-
private static final String WKND_SHARED_GRAPHQL_ENDPOINT = "/content/_cq_graphql/wknd-shared/endpoint.json";
51+
private static final String TEST_AUTHOR_FIRST_NAME = "Ian";
5652

57-
private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class);
53+
private static final String TEST_AUTHOR_LAST_NAME = "Provo";
5854

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

62-
static AEMHeadlessClient headlessClientAuthor;
57+
private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class);
6358

64-
@BeforeClass
65-
public static void beforeClass() {
66-
try {
59+
@Rule
60+
public ExpectedException thrown = ExpectedException.none();
61+
62+
@ClassRule
63+
public static final CQAuthorPublishClassRule cqBaseClassRule = new CQAuthorPublishClassRule();
64+
65+
static AEMHeadlessClient headlessClientAuthor;
66+
67+
@BeforeClass
68+
public static void beforeClass() {
69+
try {
6770
headlessClientAuthor = getHeadlessClient(cqBaseClassRule.authorRule.getConfiguration());
6871
} catch (URISyntaxException e) {
6972
LOGGER.error("Error attempting to initialize headless client", e);
7073
}
71-
}
74+
}
7275

73-
private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException {
76+
private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException {
7477
final String graphQLEndpoint = instanceConfig.getUrl() + WKND_SHARED_GRAPHQL_ENDPOINT;
75-
return AEMHeadlessClient.builder() //
76-
.endpoint(graphQLEndpoint) //
77-
.basicAuth(instanceConfig.getAdminUser(), instanceConfig.getAdminPassword()).build();
78-
}
79-
80-
@Test
81-
public void testQuery() {
82-
83-
String query = "{\n" + //
84-
" articleList{\n" + //
85-
" items{ \n" + //
86-
" _path\n" + //
87-
" authorFragment{\n" + //
78+
return AEMHeadlessClient.builder() //
79+
.endpoint(graphQLEndpoint) //
80+
.basicAuth(instanceConfig.getAdminUser(), instanceConfig.getAdminPassword()).build();
81+
}
82+
83+
@Test
84+
public void testQuery() {
85+
86+
String query = "{\n" + //
87+
" articleList{\n" + //
88+
" items{ \n" + //
89+
" _path\n" + //
90+
" authorFragment{\n" + //
8891
" firstName\n" + //
8992
" lastName\n" + //
9093
" }\n" + //
91-
" } \n" + //
92-
" }\n" + //
93-
"}";
94-
GraphQlResponse response = headlessClientAuthor.runQuery(query);
95-
assertNull(response.getErrors());
96-
JsonNode responseData = response.getData();
97-
98-
assertNotNull(responseData);
99-
JsonNode articleList = responseData.get("articleList");
100-
assertNotNull(articleList);
101-
JsonNode articleListItems = articleList.get("items");
102-
assertNotNull(articleListItems);
103-
assertEquals(7, articleListItems.size());
104-
assertNotNull(articleListItems.get(0).get("_path"));
105-
assertNotNull(articleListItems.get(0).get("authorFragment"));
106-
}
107-
108-
@Test
109-
public void testQueryWithSyntaxError() {
94+
" } \n" + //
95+
" }\n" + //
96+
"}";
97+
GraphQlResponse response = headlessClientAuthor.runQuery(query);
98+
assertNull(response.getErrors());
99+
JsonNode responseData = response.getData();
100+
101+
assertNotNull(responseData);
102+
JsonNode articleList = responseData.get("articleList");
103+
assertNotNull(articleList);
104+
JsonNode articleListItems = articleList.get("items");
105+
assertNotNull(articleListItems);
106+
assertEquals(7, articleListItems.size());
107+
assertNotNull(articleListItems.get(0).get("_path"));
108+
assertNotNull(articleListItems.get(0).get("authorFragment"));
109+
}
110+
111+
@Test
112+
public void testQueryWithSyntaxError() {
113+
114+
thrown.expect(AEMHeadlessClientException.class);
115+
thrown.expectMessage("Invalid Syntax : offending token");
116+
110117
String query = "{\n" + //
111-
" articleList{\n" + //
112-
" items{ \n" + //
113-
" _path\n" + //
114-
" author\n";
118+
" articleList{\n" + //
119+
" items{ \n" + //
120+
" _path\n" + //
121+
" author\n";
115122

116-
AEMHeadlessClientException exception = assertThrows(AEMHeadlessClientException.class,
117-
() -> headlessClientAuthor.runQuery(query));
118-
assertTrue(exception.getMessage().contains("Invalid Syntax : offending token"));
119-
}
123+
headlessClientAuthor.runQuery(query);
124+
}
120125

121-
@Test
122-
public void testQueryWithErrorResponse() {
126+
@Test
127+
public void testQueryWithErrorResponse() {
128+
129+
thrown.expect(AEMHeadlessClientException.class);
130+
thrown.expectMessage("Field 'nonExisting' in type 'QueryType' is undefined");
123131

124132
String query = "{ nonExisting { items{ _path } } }";
125-
AEMHeadlessClientException exception = assertThrows(AEMHeadlessClientException.class,
126-
() -> headlessClientAuthor.runQuery(query));
127-
128-
assertTrue(exception.getMessage().contains("Field 'nonExisting' in type 'QueryType' is undefined"));
133+
headlessClientAuthor.runQuery(query);
129134

130-
}
135+
}
131136

132-
@Test
133-
public void testQueryWithParameters() {
137+
@Test
138+
public void testQueryWithParameters() {
134139

135-
String query = "query($authorFirstName: String, $authorLastName: String) {\n" +
136-
"articleList(filter: {\n" +
137-
"authorFragment: {\n" +
140+
String query = "query($authorFirstName: String, $authorLastName: String) {\n" +
141+
"articleList(filter: {\n" +
142+
"authorFragment: {\n" +
138143
"firstName: {\n" +
139-
"_expressions: {\n" +
140-
"value: $authorFirstName\n" +
141-
"}\n" +
144+
"_expressions: {\n" +
145+
"value: $authorFirstName\n" +
146+
"}\n" +
142147
"}\n" +
143148
"lastName: {\n" +
144-
"_expressions: {\n" +
145-
"value: $authorLastName\n" +
146-
"}\n" +
149+
"_expressions: {\n" +
150+
"value: $authorLastName\n" +
151+
"}\n" +
147152
"}\n" +
148-
"}\n" +
149-
"}) {\n" +
150-
"items {\n" +
153+
"}\n" +
154+
"}) {\n" +
155+
"items {\n" +
151156
"_path\n" +
152157
"authorFragment {\n" +
153-
"firstName\n" +
154-
"lastName\n" +
158+
"firstName\n" +
159+
"lastName\n" +
160+
"}\n" +
161+
"}\n" +
155162
"}\n" +
156-
"}\n" +
157-
"}\n" +
158-
"}";
163+
"}";
159164

160-
Map<String, Object> vars = new HashMap<>();
161-
vars.put("authorFirstName", TEST_AUTHOR_FIRST_NAME);
165+
Map<String, Object> vars = new HashMap<>();
166+
vars.put("authorFirstName", TEST_AUTHOR_FIRST_NAME);
162167
vars.put("authorLastName", TEST_AUTHOR_LAST_NAME);
163168

164-
GraphQlResponse response = headlessClientAuthor.runQuery(query, vars);
165-
assertNull(response.getErrors());
166-
JsonNode responseData = response.getData();
167-
168-
assertNotNull(responseData);
169-
JsonNode articleList = responseData.get("articleList");
170-
assertNotNull(articleList);
171-
JsonNode articleListItems = articleList.get("items");
172-
assertNotNull(articleListItems);
173-
assertEquals(1, articleListItems.size());
174-
assertEquals(TEST_AUTHOR_FIRST_NAME, articleListItems.get(0).get("authorFragment").get("firstName").asText());
169+
GraphQlResponse response = headlessClientAuthor.runQuery(query, vars);
170+
assertNull(response.getErrors());
171+
JsonNode responseData = response.getData();
172+
173+
assertNotNull(responseData);
174+
JsonNode articleList = responseData.get("articleList");
175+
assertNotNull(articleList);
176+
JsonNode articleListItems = articleList.get("items");
177+
assertNotNull(articleListItems);
178+
assertEquals(1, articleListItems.size());
179+
assertEquals(TEST_AUTHOR_FIRST_NAME, articleListItems.get(0).get("authorFragment").get("firstName").asText());
175180
assertEquals(TEST_AUTHOR_LAST_NAME, articleListItems.get(0).get("authorFragment").get("lastName").asText());
176-
}
177-
178-
@Test
179-
public void testPersistedQuery() {
180-
181-
GraphQlResponse response = headlessClientAuthor.runPersistedQuery("/wknd-shared/adventures-all");
182-
assertNull(response.getErrors());
183-
JsonNode responseData = response.getData();
184-
185-
assertNotNull(responseData);
186-
JsonNode adventureListList = responseData.get("adventureList");
187-
assertNotNull(adventureListList);
188-
JsonNode adventureListItems = adventureListList.get("items");
189-
assertNotNull(adventureListItems);
190-
assertEquals(16, adventureListItems.size());
191-
JsonNode firstAdvantureItem = adventureListItems.get(0);
192-
assertNotNull(firstAdvantureItem.get("_path"));
193-
assertNotNull(firstAdvantureItem.get("title"));
194-
assertNotNull(firstAdvantureItem.get("price"));
195-
assertNotNull(firstAdvantureItem.get("tripLength"));
196-
assertNotNull(firstAdvantureItem.get("primaryImage"));
197-
198-
}
199-
200-
@Test
201-
public void testListPersistedQueries() {
202-
203-
List<PersistedQuery> listPersistedQueries = headlessClientAuthor.listPersistedQueries("wknd-shared");
204-
205-
assertFalse(listPersistedQueries.isEmpty());
206-
PersistedQuery adventuresQuery = listPersistedQueries.stream()
207-
.filter(p -> p.getShortPath().equals("/wknd-shared/adventures-all")).findFirst().get();
208-
assertEquals("/wknd-shared/settings/graphql/persistentQueries/adventures-all", adventuresQuery.getLongPath());
209-
assertThat(adventuresQuery.getQuery(), containsString("adventureList {"));
210-
}
181+
}
182+
183+
@Test
184+
public void testPersistedQuery() {
185+
186+
GraphQlResponse response = headlessClientAuthor.runPersistedQuery("/wknd-shared/adventures-all");
187+
assertNull(response.getErrors());
188+
JsonNode responseData = response.getData();
189+
190+
assertNotNull(responseData);
191+
JsonNode adventureListList = responseData.get("adventureList");
192+
assertNotNull(adventureListList);
193+
JsonNode adventureListItems = adventureListList.get("items");
194+
assertNotNull(adventureListItems);
195+
assertEquals(16, adventureListItems.size());
196+
JsonNode firstAdvantureItem = adventureListItems.get(0);
197+
assertNotNull(firstAdvantureItem.get("_path"));
198+
assertNotNull(firstAdvantureItem.get("title"));
199+
assertNotNull(firstAdvantureItem.get("price"));
200+
assertNotNull(firstAdvantureItem.get("tripLength"));
201+
assertNotNull(firstAdvantureItem.get("primaryImage"));
202+
203+
}
204+
205+
@Test
206+
public void testListPersistedQueries() {
207+
208+
List<PersistedQuery> listPersistedQueries = headlessClientAuthor.listPersistedQueries("wknd-shared");
209+
210+
assertFalse(listPersistedQueries.isEmpty());
211+
PersistedQuery adventuresQuery = listPersistedQueries.stream()
212+
.filter(p -> p.getShortPath().equals("/wknd-shared/adventures-all")).findFirst().get();
213+
assertEquals("/wknd-shared/settings/graphql/persistentQueries/adventures-all", adventuresQuery.getLongPath());
214+
assertThat(adventuresQuery.getQuery(), containsString("adventureList {"));
215+
}
211216

212217
}

0 commit comments

Comments
 (0)