1+ /*
2+ * Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com .introproventures .graphql .jpa .query .schema ;
18+
19+ import static org .assertj .core .api .Assertions .assertThat ;
20+
21+ import java .util .Arrays ;
22+ import java .util .HashMap ;
23+ import java .util .Map ;
24+
25+ import javax .persistence .EntityManager ;
26+
27+ import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaExecutor ;
28+ import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaSchemaBuilder ;
29+ import org .junit .Test ;
30+ import org .junit .runner .RunWith ;
31+ import org .springframework .beans .factory .annotation .Autowired ;
32+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
33+ import org .springframework .boot .test .context .SpringBootTest ;
34+ import org .springframework .boot .test .context .SpringBootTest .WebEnvironment ;
35+ import org .springframework .context .annotation .Bean ;
36+ import org .springframework .test .context .TestPropertySource ;
37+ import org .springframework .test .context .junit4 .SpringRunner ;
38+
39+
40+ @ RunWith (SpringRunner .class )
41+ @ SpringBootTest (webEnvironment =WebEnvironment .NONE )
42+ @ TestPropertySource ({"classpath:hibernate.properties" })
43+ public class GraphQLEnumVariableBindingsTests {
44+
45+ @ SpringBootApplication
46+ static class Application {
47+ @ Bean
48+ public GraphQLExecutor graphQLExecutor (final GraphQLSchemaBuilder graphQLSchemaBuilder ) {
49+ return new GraphQLJpaExecutor (graphQLSchemaBuilder .build ());
50+ }
51+
52+ @ Bean
53+ public GraphQLSchemaBuilder graphQLSchemaBuilder (final EntityManager entityManager ) {
54+
55+ return new GraphQLJpaSchemaBuilder (entityManager )
56+ .name ("GraphQLBooks" )
57+ .description ("Books JPA test schema" );
58+ }
59+
60+ }
61+
62+ @ Autowired
63+ private GraphQLExecutor executor ;
64+
65+
66+ @ Test
67+ public void queryEnumSimpleVariableBinding () {
68+ //given
69+ String query = "query($genre: Genre) {" +
70+ " Books(where: {" +
71+ " genre: {EQ: $genre}" +
72+ " }) {" +
73+ " select {" +
74+ " id" +
75+ " title" +
76+ " genre" +
77+ " }" +
78+ " }" +
79+ "}" ;
80+
81+ Map <String , Object > variables = new HashMap <String , Object >() {{
82+ put ("genre" , "NOVEL" );
83+ }};
84+
85+
86+ String expected = "{Books={select=["
87+ + "{id=2, title=War and Peace, genre=NOVEL}, "
88+ + "{id=3, title=Anna Karenina, genre=NOVEL}"
89+ + "]}}" ;
90+
91+ //when
92+ Object result = executor .execute (query , variables ).getData ();
93+
94+ // then
95+ assertThat (result .toString ()).isEqualTo (expected );
96+ }
97+
98+ @ Test
99+ public void queryEnumSimpleArrayVariableBinding () {
100+ //given
101+ String query = "query($genre: Genre) {" +
102+ " Books(where: {" +
103+ " genre: {IN: [$genre]}" +
104+ " }) {" +
105+ " select {" +
106+ " id" +
107+ " title" +
108+ " genre" +
109+ " }" +
110+ " }" +
111+ "}" ;
112+
113+ Map <String , Object > variables = new HashMap <String , Object >() {{
114+ put ("genre" , "NOVEL" );
115+ }};
116+
117+
118+ String expected = "{Books={select=["
119+ + "{id=2, title=War and Peace, genre=NOVEL}, "
120+ + "{id=3, title=Anna Karenina, genre=NOVEL}"
121+ + "]}}" ;
122+
123+ //when
124+ Object result = executor .execute (query , variables ).getData ();
125+
126+ // then
127+ assertThat (result .toString ()).isEqualTo (expected );
128+ }
129+
130+ @ Test
131+ public void queryEnumArrayVariableBindingInNestedRelation () {
132+ //given
133+ String query = "query($genres: [Genre!]) {" +
134+ " Authors(where: {" +
135+ " books: {" +
136+ " genre: {IN: $genres}" +
137+ " }" +
138+ " }) {" +
139+ " select {" +
140+ " id" +
141+ " name" +
142+ " books {" +
143+ " id" +
144+ " title" +
145+ " genre" +
146+ " }" +
147+ " }" +
148+ " }" +
149+ "}" ;
150+
151+ Map <String , Object > variables = new HashMap <String , Object >() {{
152+ put ("genres" , Arrays .asList ("NOVEL" ));
153+ }};
154+
155+
156+ String expected = "{Authors={select=[{"
157+ + "id=1, name=Leo Tolstoy, books=["
158+ + "{id=2, title=War and Peace, genre=NOVEL}, "
159+ + "{id=3, title=Anna Karenina, genre=NOVEL}"
160+ + "]}"
161+ + "]}}" ;
162+
163+ //when
164+ Object result = executor .execute (query , variables ).getData ();
165+
166+ // then
167+ assertThat (result .toString ()).isEqualTo (expected );
168+ }
169+
170+ @ Test
171+ public void queryEnumArrayVariableBindingInEmbeddedRelation () {
172+ //given
173+ String query = "query($genres: [Genre!]) {" +
174+ " Authors {" +
175+ " select {" +
176+ " id" +
177+ " name" +
178+ " books(where: {" +
179+ " genre: {IN: $genres}" +
180+ " }) {" +
181+ " id" +
182+ " title" +
183+ " genre" +
184+ " }" +
185+ " }" +
186+ " } " +
187+ "}" ;
188+
189+ Map <String , Object > variables = new HashMap <String , Object >() {{
190+ put ("genres" , Arrays .asList ("NOVEL" ));
191+ }};
192+
193+
194+ String expected = "{Authors={select=["
195+ + "{id=1, name=Leo Tolstoy, books=["
196+ + "{id=2, title=War and Peace, genre=NOVEL}, "
197+ + "{id=3, title=Anna Karenina, genre=NOVEL}"
198+ + "]}, "
199+ + "{id=4, name=Anton Chekhov, books=[]}"
200+ + "]}}" ;
201+
202+ //when
203+ Object result = executor .execute (query , variables ).getData ();
204+
205+ // then
206+ assertThat (result .toString ()).isEqualTo (expected );
207+ }
208+ }
0 commit comments