@@ -73,61 +73,120 @@ public void validEmbeddedDocPersists() throws Exception {
7373 insertTypedData (data );
7474 }
7575
76- private void insertTypedData (final Object data ) {
76+ @ Test
77+ public void regexQueryWorks () throws Exception {
7778 deleteAll (new Handler <Message <JsonObject >>() {
7879 public void handle (Message <JsonObject > reply ) {
7980 final String testValue = "{\" testKey\" : \" testValue\" }" ;
80- final DBObject obj = MongoUtil .convertJsonToBson (testValue );
81- obj .put ("data" , data );
8281
83- JsonObject docWithDate = MongoUtil .convertBsonToJson (obj );
82+ JsonObject data = new JsonObject ()
83+ .putObject ("data" , new JsonObject (testValue ));
84+
85+ JsonObject json = createSaveQuery (data );
86+ final DBObject dataDb = MongoUtil .convertJsonToBson (data );
87+
88+ JsonObject matcher = new JsonObject ()
89+ .putObject ("data.testKey" , new JsonObject ()
90+ .putString ("$regex" , ".*estValu.*" ));
91+
92+ JsonObject query = createMatcher (matcher );
93+
94+
95+ eb .send (ADDRESS , json , assertStored (query , dataDb , data ));
96+ }
97+ });
98+ }
99+
100+ @ Test
101+ public void elemMatchQueryWorks () throws Exception {
102+ deleteAll (new Handler <Message <JsonObject >>() {
103+ public void handle (Message <JsonObject > reply ) {
104+
105+ List data = new ArrayList ();
106+ data .add (1 );
107+ data .add (2 );
108+ data .add (4 );
109+
110+ final DBObject testValueDb = new BasicDBObject ();
111+ testValueDb .put ("data" , data );
112+ JsonObject document = MongoUtil .convertBsonToJson (testValueDb );
113+ JsonObject json = createSaveQuery (document );
114+ final DBObject dataDb = MongoUtil .convertJsonToBson (document );
84115
85- JsonObject json = new JsonObject ()
86- .putString ("collection" , COLLECTION )
87- .putString ("action" , "save" ).putObject ("document" , docWithDate );
116+ JsonObject matcher = new JsonObject ()
117+ .putObject ("data" , new JsonObject ()
118+ .putObject ("$elemMatch" , new JsonObject ()
119+ .putNumber ("$gte" , 0 )
120+ .putNumber ("$lt" , 5 )));
88121
89- eb .send (ADDRESS , json , new Handler <Message <JsonObject >>() {
90- public void handle (Message <JsonObject > reply ) {
91- assertEquals (reply .body ().toString (), "ok" , reply .body ().getString ("status" ));
122+ JsonObject query = createMatcher (matcher );
92123
93- assertStored (testValue , data , obj );
94- }
95- });
124+
125+ eb .send (ADDRESS , json , assertStored (query , dataDb , data ));
126+ }
127+ });
128+ }
129+
130+ private void insertTypedData (final Object data ) {
131+ deleteAll (new Handler <Message <JsonObject >>() {
132+ public void handle (Message <JsonObject > reply ) {
133+ final String testValue = "{\" testKey\" : \" testValue\" }" ;
134+ final DBObject testValueDb = MongoUtil .convertJsonToBson (testValue );
135+ testValueDb .put ("data" , data );
136+
137+ JsonObject document = MongoUtil .convertBsonToJson (testValueDb );
138+ JsonObject save = createSaveQuery (document );
139+ JsonObject matcher = new JsonObject (testValue );
140+ JsonObject query = createMatcher (matcher );
141+
142+ eb .send (ADDRESS , save , assertStored (query , testValueDb , data ));
96143 }
97144 });
145+ }
98146
147+ private JsonObject createSaveQuery (JsonObject document ) {
148+ return new JsonObject ()
149+ .putString ("collection" , PersistorTestParent .COLLECTION )
150+ .putString ("action" , "save" )
151+ .putObject ("document" , document );
99152 }
100153
101- private void assertStored (String testValue , final Object data , final DBObject sentObject ) {
102- JsonObject matcher = new JsonObject (testValue );
103- JsonObject query = new JsonObject ()
104- .putString ("collection" , COLLECTION )
154+ private JsonObject createMatcher (JsonObject matcher ) {
155+ return new JsonObject ()
156+ .putString ("collection" , PersistorTestParent .COLLECTION )
105157 .putString ("action" , "find" )
106158 .putObject ("matcher" , matcher );
159+ }
107160
108- eb .send (ADDRESS , query , new Handler <Message <JsonObject >>() {
109- public void handle (Message <JsonObject > reply ) {
110- assertEquals (reply .body ().toString (), "ok" , reply .body ().getString ("status" ));
111- JsonArray results = reply .body ().getArray ("results" );
112-
113- if (results .size () > 0 ) {
114- JsonObject result = results .get (0 );
115- DBObject dbObj = MongoUtil .convertJsonToBson (result );
116- dbObj .removeField ("_id" );
117- Object storedData = dbObj .get ("data" );
118- if (storedData instanceof Binary ) {
119- VertxAssert .assertArrayEquals ((byte []) data , ((Binary ) storedData ).getData ());
120- } else {
121- VertxAssert .assertEquals (sentObject , dbObj );
161+ private Handler <Message <JsonObject >> assertStored (final JsonObject query , final DBObject sentDbObject , final Object dataSaved ) {
162+ return new Handler <Message <JsonObject >>() {
163+ public void handle (Message <JsonObject > reply ) {
164+ assertEquals (reply .body ().toString (), "ok" , reply .body ().getString ("status" ));
165+
166+ eb .send (ADDRESS , query , new Handler <Message <JsonObject >>() {
167+ public void handle (Message <JsonObject > reply ) {
168+ assertEquals (reply .body ().toString (), "ok" , reply .body ().getString ("status" ));
169+ JsonArray results = reply .body ().getArray ("results" );
170+
171+ if (results .size () > 0 ) {
172+ JsonObject result = results .get (0 );
173+ DBObject dbObj = MongoUtil .convertJsonToBson (result );
174+ dbObj .removeField ("_id" );
175+ Object storedData = dbObj .get ("data" );
176+ if (storedData instanceof Binary ) {
177+ VertxAssert .assertArrayEquals ((byte []) dataSaved , ((Binary ) storedData ).getData ());
178+ } else {
179+ VertxAssert .assertEquals (sentDbObject , dbObj );
180+ }
181+ testComplete ();
182+ } else {
183+ VertxAssert .fail ("Stored object not found in DB" );
184+ }
122185 }
123- testComplete ();
124- } else {
125- VertxAssert .fail ("Stored object not found in DB" );
126186 }
127- }
128- }
129-
130- );
187+ );
188+ }
189+ };
131190 }
132191}
133192
0 commit comments