1+ /*
2+ * Copyright (c) 2017 Otávio Santana and others
3+ * All rights reserved. This program and the accompanying materials
4+ * are made available under the terms of the Eclipse Public License v1.0
5+ * and Apache License v2.0 which accompanies this distribution.
6+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+ * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+ *
9+ * You may elect to redistribute this code under either of these licenses.
10+ *
11+ * Contributors:
12+ *
13+ * Otavio Santana
14+ */
15+ package org .jnosql .artemis .graph .query ;
16+
17+ import org .apache .tinkerpop .gremlin .structure .Edge ;
18+ import org .apache .tinkerpop .gremlin .structure .Graph ;
19+ import org .apache .tinkerpop .gremlin .structure .T ;
20+ import org .apache .tinkerpop .gremlin .structure .Vertex ;
21+ import org .hamcrest .MatcherAssert ;
22+ import org .hamcrest .Matchers ;
23+ import org .jnosql .artemis .Converters ;
24+ import org .jnosql .artemis .Repository ;
25+ import org .jnosql .artemis .graph .cdi .CDIExtension ;
26+ import org .jnosql .artemis .graph .model .Person ;
27+ import org .jnosql .artemis .reflection .ClassRepresentation ;
28+ import org .jnosql .artemis .reflection .ClassRepresentations ;
29+ import org .junit .jupiter .api .BeforeEach ;
30+ import org .junit .jupiter .api .extension .ExtendWith ;
31+ import org .junit .jupiter .params .ParameterizedTest ;
32+ import org .junit .jupiter .params .provider .ValueSource ;
33+
34+ import javax .inject .Inject ;
35+ import java .lang .reflect .Method ;
36+ import java .util .Arrays ;
37+ import java .util .List ;
38+ import java .util .stream .Collectors ;
39+ import java .util .stream .Stream ;
40+
41+ import static org .junit .jupiter .api .Assertions .*;
42+
43+ @ ExtendWith (CDIExtension .class )
44+ class DeleteQueryConverterTest {
45+
46+ @ Inject
47+ private DeleteQueryConverter converter ;
48+
49+ @ Inject
50+ private ClassRepresentations representations ;
51+
52+ @ Inject
53+ private Converters converters ;
54+
55+ @ Inject
56+ private Graph graph ;
57+
58+ @ BeforeEach
59+ public void setUp () {
60+ graph .traversal ().E ().toList ().forEach (Edge ::remove );
61+ graph .traversal ().V ().toList ().forEach (Vertex ::remove );
62+ }
63+
64+ @ ParameterizedTest (name = "Should parser the query {0}" )
65+ @ ValueSource (strings = {"deleteByName" })
66+ public void shouldRunQuery (String methodName ) {
67+ checkEquals (methodName );
68+ }
69+
70+ @ ParameterizedTest (name = "Should parser the query {0}" )
71+ @ ValueSource (strings = {"deleteByNameEquals" })
72+ public void shouldRunQuery1 (String methodName ) {
73+ checkEquals (methodName );
74+ }
75+
76+ @ ParameterizedTest (name = "Should parser the query {0}" )
77+ @ ValueSource (strings = {"deleteByNameNotEquals" })
78+ public void shouldRunQuery2 (String methodName ) {
79+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
80+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
81+
82+ graph .addVertex ("Person" ).property ("name" , "Otavio" );
83+ graph .addVertex ("Person" ).property ("name" , "Ada" );
84+ graph .addVertex ("Person" ).property ("name" , "Poliana" );
85+ ClassRepresentation representation = representations .get (Person .class );
86+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
87+ converters , method , new Object []{"Ada" });
88+
89+ List <Vertex > vertices = converter .apply (queryMethod );
90+ assertEquals (2 , vertices .size ());
91+ assertNotEquals ("Ada" , vertices .get (0 ).value ("name" ));
92+ assertNotEquals ("Ada" , vertices .get (1 ).value ("name" ));
93+ }
94+
95+ @ ParameterizedTest (name = "Should parser the query {0}" )
96+ @ ValueSource (strings = {"deleteByAgeLessThan" })
97+ public void shouldRunQuery3 (String methodName ) {
98+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
99+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
100+
101+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
102+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
103+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
104+ ClassRepresentation representation = representations .get (Person .class );
105+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
106+ converters , method , new Object []{30 });
107+
108+ List <Vertex > vertices = converter .apply (queryMethod );
109+ assertEquals (1 , vertices .size ());
110+ assertEquals ("Poliana" , vertices .get (0 ).value ("name" ));
111+ }
112+
113+ @ ParameterizedTest (name = "Should parser the query {0}" )
114+ @ ValueSource (strings = {"deleteByAgeLessThanEqual" })
115+ public void shouldRunQuery4 (String methodName ) {
116+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
117+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
118+
119+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
120+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
121+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
122+ ClassRepresentation representation = representations .get (Person .class );
123+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
124+ converters , method , new Object []{30 });
125+
126+ List <Vertex > vertices = converter .apply (queryMethod );
127+ assertEquals (2 , vertices .size ());
128+ assertNotEquals ("Ada" , vertices .get (0 ).value ("name" ));
129+ assertNotEquals ("Ada" , vertices .get (1 ).value ("name" ));
130+ }
131+
132+
133+ @ ParameterizedTest (name = "Should parser the query {0}" )
134+ @ ValueSource (strings = {"deleteByAgeGreaterThan" })
135+ public void shouldRunQuery5 (String methodName ) {
136+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
137+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
138+
139+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
140+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
141+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
142+ ClassRepresentation representation = representations .get (Person .class );
143+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
144+ converters , method , new Object []{30 });
145+
146+ List <Vertex > vertices = converter .apply (queryMethod );
147+ assertEquals (1 , vertices .size ());
148+ assertEquals ("Ada" , vertices .get (0 ).value ("name" ));
149+ }
150+
151+ @ ParameterizedTest (name = "Should parser the query {0}" )
152+ @ ValueSource (strings = {"deleteByAgeGreaterThanEqual" })
153+ public void shouldRunQuery6 (String methodName ) {
154+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
155+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
156+
157+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
158+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
159+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
160+ ClassRepresentation representation = representations .get (Person .class );
161+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
162+ converters , method , new Object []{30 });
163+
164+ List <Vertex > vertices = converter .apply (queryMethod );
165+ assertEquals (2 , vertices .size ());
166+ assertNotEquals ("Poliana" , vertices .get (0 ).value ("name" ));
167+ assertNotEquals ("Poliana" , vertices .get (1 ).value ("name" ));
168+ }
169+
170+ @ ParameterizedTest (name = "Should parser the query {0}" )
171+ @ ValueSource (strings = {"deleteByAgeBetween" })
172+ public void shouldRunQuery7 (String methodName ) {
173+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
174+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
175+
176+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
177+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
178+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
179+ ClassRepresentation representation = representations .get (Person .class );
180+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
181+ converters , method , new Object []{29 , 41 });
182+
183+ List <Vertex > vertices = converter .apply (queryMethod );
184+ assertEquals (2 , vertices .size ());
185+ assertNotEquals ("Poliana" , vertices .get (0 ).value ("name" ));
186+ assertNotEquals ("Poliana" , vertices .get (1 ).value ("name" ));
187+ }
188+
189+
190+ @ ParameterizedTest (name = "Should parser the query {0}" )
191+ @ ValueSource (strings = {"deleteByAgeLessThanOrderByName" })
192+ public void shouldRunQuery8 (String methodName ) {
193+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
194+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
195+
196+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
197+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
198+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
199+ ClassRepresentation representation = representations .get (Person .class );
200+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
201+ converters , method , new Object []{100 });
202+
203+ List <Vertex > vertices = converter .apply (queryMethod );
204+ List <Object > names = vertices .stream ().map (v -> v .value ("name" ))
205+ .collect (Collectors .toList ());
206+ assertEquals (3 , vertices .size ());
207+ MatcherAssert .assertThat (names , Matchers .contains ("Ada" , "Otavio" , "Poliana" ));
208+ }
209+
210+
211+ @ ParameterizedTest (name = "Should parser the query {0}" )
212+ @ ValueSource (strings = {"deleteByAgeLessThanOrderByNameDesc" })
213+ public void shouldRunQuery9 (String methodName ) {
214+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
215+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
216+
217+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
218+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
219+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
220+ ClassRepresentation representation = representations .get (Person .class );
221+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
222+ converters , method , new Object []{100 });
223+
224+ List <Vertex > vertices = converter .apply (queryMethod );
225+ List <Object > names = vertices .stream ().map (v -> v .value ("name" ))
226+ .collect (Collectors .toList ());
227+ assertEquals (3 , vertices .size ());
228+ MatcherAssert .assertThat (names , Matchers .contains ("Poliana" , "Otavio" , "Ada" ));
229+ }
230+
231+ @ ParameterizedTest (name = "Should parser the query {0}" )
232+ @ ValueSource (strings = {"deleteByAgeLessThanOrderByNameDescAgeAsc" })
233+ public void shouldRunQuery10 (String methodName ) {
234+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
235+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
236+
237+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
238+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
239+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
240+ ClassRepresentation representation = representations .get (Person .class );
241+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
242+ converters , method , new Object []{100 });
243+
244+ List <Vertex > vertices = converter .apply (queryMethod );
245+ List <Object > names = vertices .stream ().map (v -> v .value ("name" ))
246+ .collect (Collectors .toList ());
247+ assertEquals (3 , vertices .size ());
248+ MatcherAssert .assertThat (names , Matchers .contains ("Poliana" , "Otavio" , "Ada" ));
249+ }
250+
251+ @ ParameterizedTest (name = "Should parser the query {0}" )
252+ @ ValueSource (strings = {"deleteByAgeIn" })
253+ public void shouldRunQuery11 (String methodName ) {
254+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
255+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
256+
257+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
258+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
259+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
260+ ClassRepresentation representation = representations .get (Person .class );
261+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
262+ converters , method , new Object []{Arrays .asList (25 ,40 ,30 )});
263+
264+ List <Vertex > vertices = converter .apply (queryMethod );
265+ List <Object > names = vertices .stream ().map (v -> v .value ("name" ))
266+ .sorted ()
267+ .collect (Collectors .toList ());
268+ assertEquals (3 , vertices .size ());
269+ MatcherAssert .assertThat (names , Matchers .contains ("Ada" , "Otavio" , "Poliana" ));
270+ }
271+
272+ @ ParameterizedTest (name = "Should parser the query {0}" )
273+ @ ValueSource (strings = {"deleteByNameIn" })
274+ public void shouldRunQuery12 (String methodName ) {
275+ Method method = Stream .of (SelectQueryConverterTest .PersonRepository .class .getMethods ())
276+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
277+
278+ graph .addVertex (T .label , "Person" , "name" , "Otavio" , "age" , 30 );
279+ graph .addVertex (T .label , "Person" , "name" , "Ada" , "age" , 40 );
280+ graph .addVertex (T .label , "Person" , "name" , "Poliana" , "age" , 25 );
281+ ClassRepresentation representation = representations .get (Person .class );
282+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
283+ converters , method , new Object []{Arrays .asList ("Otavio" , "Ada" , "Poliana" )});
284+
285+ List <Vertex > vertices = converter .apply (queryMethod );
286+ List <Object > names = vertices .stream ().map (v -> v .value ("name" ))
287+ .sorted ()
288+ .collect (Collectors .toList ());
289+ assertEquals (3 , vertices .size ());
290+ MatcherAssert .assertThat (names , Matchers .contains ("Ada" , "Otavio" , "Poliana" ));
291+ }
292+
293+
294+ private void checkEquals (String methodName ) {
295+ Method method = Stream .of (PersonRepository .class .getMethods ())
296+ .filter (m -> m .getName ().equals (methodName )).findFirst ().get ();
297+
298+ graph .addVertex ("Person" ).property ("name" , "Otavio" );
299+ graph .addVertex ("Person" ).property ("name" , "Ada" );
300+ graph .addVertex ("Person" ).property ("name" , "Poliana" );
301+ ClassRepresentation representation = representations .get (Person .class );
302+ GraphQueryMethod queryMethod = new GraphQueryMethod (representation , graph .traversal ().V (),
303+ converters , method , new Object []{"Ada" });
304+
305+ List <Vertex > vertices = converter .apply (queryMethod );
306+ assertEquals (1 , vertices .size ());
307+ assertEquals ("Ada" , vertices .get (0 ).value ("name" ));
308+ }
309+
310+
311+ interface PersonRepository extends Repository <Person , String > {
312+
313+ List <Person > deleteByName (String name );
314+
315+ List <Person > deleteByNameEquals (String name );
316+
317+ List <Person > deleteByNameNotEquals (String name );
318+
319+ List <Person > deleteByAgeLessThan (Integer age );
320+
321+ List <Person > deleteByAgeLessThanEqual (Integer age );
322+
323+ List <Person > deleteByAgeGreaterThan (Integer age );
324+
325+ List <Person > deleteByAgeGreaterThanEqual (Integer age );
326+
327+ List <Person > deleteByAgeBetween (Integer age , Integer ageB );
328+
329+ List <Person > deleteByAgeLessThanOrderByName (Integer age );
330+
331+ List <Person > deleteByAgeLessThanOrderByNameDesc (Integer age );
332+
333+ List <Person > deleteByAgeLessThanOrderByNameDescAgeAsc (Integer age );
334+
335+ List <Person > deleteByAgeIn (List <Integer > ages );
336+
337+ List <Person > deleteByNameIn (List <String > names );
338+ }
339+
340+ }
0 commit comments