1+ /*
2+ * Copyright (c) 2022 Contributors to the Eclipse Foundation
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 .eclipse .jnosql .databases .neo4j .mapping ;
16+
17+ import jakarta .data .repository .Param ;
18+ import jakarta .inject .Inject ;
19+ import org .eclipse .jnosql .communication .semistructured .DeleteQuery ;
20+ import org .eclipse .jnosql .mapping .core .Converters ;
21+ import org .eclipse .jnosql .mapping .core .spi .EntityMetadataExtension ;
22+ import org .eclipse .jnosql .mapping .metadata .EntitiesMetadata ;
23+ import org .eclipse .jnosql .mapping .reflection .Reflections ;
24+ import org .eclipse .jnosql .mapping .semistructured .EntityConverter ;
25+ import org .jboss .weld .junit5 .auto .AddExtensions ;
26+ import org .jboss .weld .junit5 .auto .AddPackages ;
27+ import org .jboss .weld .junit5 .auto .EnableAutoWeld ;
28+ import org .junit .jupiter .api .BeforeEach ;
29+ import org .junit .jupiter .api .Test ;
30+ import org .mockito .ArgumentCaptor ;
31+ import org .mockito .Mockito ;
32+
33+ import java .lang .reflect .Proxy ;
34+ import java .time .Duration ;
35+ import java .util .Collections ;
36+ import java .util .List ;
37+ import java .util .Map ;
38+ import java .util .Optional ;
39+
40+ import static org .junit .jupiter .api .Assertions .assertEquals ;
41+ import static org .mockito .ArgumentMatchers .any ;
42+ import static org .mockito .ArgumentMatchers .eq ;
43+ import static org .mockito .Mockito .verify ;
44+ import static org .mockito .Mockito .when ;
45+
46+ @ EnableAutoWeld
47+ @ AddPackages (value = {Converters .class , Neo4JRepository .class , EntityConverter .class })
48+ @ AddPackages (Reflections .class )
49+ @ AddExtensions ({EntityMetadataExtension .class , Neo4JExtension .class })
50+ public class Neo4jRepositoryProxyTest {
51+
52+ private Neo4JTemplate template ;
53+
54+ @ Inject
55+ private Converters converters ;
56+
57+ @ Inject
58+ private EntitiesMetadata entitiesMetadata ;
59+
60+ private HumanRepository personRepository ;
61+
62+ @ BeforeEach
63+ public void setUp () {
64+ this .template = Mockito .mock (Neo4JTemplate .class );
65+ Neo4JRepositoryProxy handler = new Neo4JRepositoryProxy (template ,
66+ HumanRepository .class , converters , entitiesMetadata );
67+
68+ when (template .insert (any (Contact .class ))).thenReturn (new Contact ());
69+ when (template .insert (any (Contact .class ), any (Duration .class ))).thenReturn (new Contact ());
70+ when (template .update (any (Contact .class ))).thenReturn (new Contact ());
71+ this .personRepository = (HumanRepository ) Proxy .newProxyInstance (HumanRepository .class .getClassLoader (),
72+ new Class []{HumanRepository .class },
73+ handler );
74+ }
75+
76+
77+ @ Test
78+ public void shouldFindByName () {
79+ personRepository .findByName ("Ada" );
80+ verify (template ).cypher ("MATCH (p:Person) WHERE p.name = $1 RETURN p" , Map .of ("name" , "Ada" ));
81+ }
82+
83+ @ Test
84+ public void shouldDeleteByName () {
85+ personRepository .deleteByName ("Ada" );
86+ verify (template ).cypher ("MATCH (p:Person {name: $name}) DELETE p" , Collections .singletonMap ("name" , "Ada" ));
87+ }
88+
89+ @ Test
90+ public void shouldFindAll () {
91+ personRepository .findAllQuery ();
92+ verify (template ).cypher ("MATCH (p:Person) RETURN p" , Collections .emptyMap ());
93+ }
94+
95+ @ Test
96+ public void shouldFindByNameCQL () {
97+ personRepository .findByName ("Ada" );
98+ verify (template ).cypher ("MATCH (p:Person) WHERE p.name = $1 RETURN p" , Collections .singletonMap ("name" , "Ada" ));
99+ }
100+
101+ @ Test
102+ public void shouldFindByName2CQL () {
103+ ArgumentCaptor <Map > captor = ArgumentCaptor .forClass (Map .class );
104+
105+ personRepository .findByName2 ("Ada" );
106+ verify (template ).cypher (Mockito .eq ("MATCH (p:Person) WHERE p.name = $name RETURN p" ), captor .capture ());
107+ Map map = captor .getValue ();
108+ assertEquals ("Ada" , map .get ("name" ));
109+ }
110+
111+ @ Test
112+ public void shouldSaveUsingInsert () {
113+ Contact contact = new Contact ("Ada" , 10 );
114+ personRepository .save (contact );
115+ verify (template ).insert (eq (contact ));
116+ }
117+
118+ @ Test
119+ public void shouldSaveUsingUpdate () {
120+ Contact contact = new Contact ("Ada-2" , 10 );
121+ when (template .find (Contact .class , "Ada-2" )).thenReturn (Optional .of (contact ));
122+ personRepository .save (contact );
123+ verify (template ).update (eq (contact ));
124+ }
125+
126+ @ Test
127+ public void shouldDelete (){
128+ personRepository .deleteById ("id" );
129+ verify (template ).delete (Contact .class , "id" );
130+ }
131+
132+
133+ @ Test
134+ public void shouldDeleteEntity (){
135+ Contact contact = new Contact ("Ada" , 10 );
136+ personRepository .delete (contact );
137+ verify (template ).delete (Contact .class , contact .getName ());
138+ }
139+
140+ interface HumanRepository extends Neo4JRepository <Contact , String > {
141+
142+ @ Cypher ("MATCH (p:Person {name: $name}) DELETE p" )
143+ void deleteByName (@ Param ("name" ) String name );
144+
145+ @ Cypher ("MATCH (p:Person) RETURN p" )
146+ List <Contact > findAllQuery ();
147+
148+ @ Cypher ("MATCH (p:Person) WHERE p.name = $1 RETURN p" )
149+ List <Contact > findByName (@ Param ("name" ) String name );
150+
151+ @ Cypher ("MATCH (p:Person) WHERE p.name = $name RETURN p" )
152+ List <Contact > findByName2 (@ Param ("name" ) String name );
153+ }
154+
155+
156+ }
0 commit comments