|
21 | 21 | import static org.junit.Assert.assertFalse;
|
22 | 22 | import static org.junit.Assert.assertNotNull;
|
23 | 23 | import static org.junit.Assert.assertNull;
|
24 |
| -import static org.junit.Assert.assertThrows; |
25 |
| -import static org.junit.Assert.assertTrue; |
26 | 24 |
|
27 | 25 | import java.net.URISyntaxException;
|
28 | 26 | import java.util.HashMap;
|
|
32 | 30 | import org.apache.sling.testing.clients.instance.InstanceConfiguration;
|
33 | 31 | import org.junit.BeforeClass;
|
34 | 32 | import org.junit.ClassRule;
|
| 33 | +import org.junit.Rule; |
35 | 34 | import org.junit.Test;
|
| 35 | +import org.junit.rules.ExpectedException; |
36 | 36 | import org.slf4j.Logger;
|
37 | 37 | import org.slf4j.LoggerFactory;
|
38 | 38 |
|
|
48 | 48 | */
|
49 | 49 | public class GraphQlIT {
|
50 | 50 |
|
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"; |
56 | 52 |
|
57 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class); |
| 53 | + private static final String TEST_AUTHOR_LAST_NAME = "Provo"; |
58 | 54 |
|
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"; |
61 | 56 |
|
62 |
| - static AEMHeadlessClient headlessClientAuthor; |
| 57 | + private static final Logger LOGGER = LoggerFactory.getLogger(GraphQlIT.class); |
63 | 58 |
|
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 { |
67 | 70 | headlessClientAuthor = getHeadlessClient(cqBaseClassRule.authorRule.getConfiguration());
|
68 | 71 | } catch (URISyntaxException e) {
|
69 | 72 | LOGGER.error("Error attempting to initialize headless client", e);
|
70 | 73 | }
|
71 |
| - } |
| 74 | + } |
72 | 75 |
|
73 |
| - private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException { |
| 76 | + private static AEMHeadlessClient getHeadlessClient(InstanceConfiguration instanceConfig) throws URISyntaxException { |
74 | 77 | 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" + // |
88 | 91 | " firstName\n" + //
|
89 | 92 | " lastName\n" + //
|
90 | 93 | " }\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 | + |
110 | 117 | 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"; |
115 | 122 |
|
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 | + } |
120 | 125 |
|
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"); |
123 | 131 |
|
124 | 132 | 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); |
129 | 134 |
|
130 |
| - } |
| 135 | + } |
131 | 136 |
|
132 |
| - @Test |
133 |
| - public void testQueryWithParameters() { |
| 137 | + @Test |
| 138 | + public void testQueryWithParameters() { |
134 | 139 |
|
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" + |
138 | 143 | "firstName: {\n" +
|
139 |
| - "_expressions: {\n" + |
140 |
| - "value: $authorFirstName\n" + |
141 |
| - "}\n" + |
| 144 | + "_expressions: {\n" + |
| 145 | + "value: $authorFirstName\n" + |
| 146 | + "}\n" + |
142 | 147 | "}\n" +
|
143 | 148 | "lastName: {\n" +
|
144 |
| - "_expressions: {\n" + |
145 |
| - "value: $authorLastName\n" + |
146 |
| - "}\n" + |
| 149 | + "_expressions: {\n" + |
| 150 | + "value: $authorLastName\n" + |
| 151 | + "}\n" + |
147 | 152 | "}\n" +
|
148 |
| - "}\n" + |
149 |
| - "}) {\n" + |
150 |
| - "items {\n" + |
| 153 | + "}\n" + |
| 154 | + "}) {\n" + |
| 155 | + "items {\n" + |
151 | 156 | "_path\n" +
|
152 | 157 | "authorFragment {\n" +
|
153 |
| - "firstName\n" + |
154 |
| - "lastName\n" + |
| 158 | + "firstName\n" + |
| 159 | + "lastName\n" + |
| 160 | + "}\n" + |
| 161 | + "}\n" + |
155 | 162 | "}\n" +
|
156 |
| - "}\n" + |
157 |
| - "}\n" + |
158 |
| - "}"; |
| 163 | + "}"; |
159 | 164 |
|
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); |
162 | 167 | vars.put("authorLastName", TEST_AUTHOR_LAST_NAME);
|
163 | 168 |
|
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()); |
175 | 180 | 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 | + } |
211 | 216 |
|
212 | 217 | }
|
0 commit comments