|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2021 Nitrite author or authors. |
| 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 | + |
| 18 | +package org.dizitart.no2.integration.collection; |
| 19 | + |
| 20 | +import org.dizitart.no2.Nitrite; |
| 21 | +import org.dizitart.no2.collection.Document; |
| 22 | +import org.dizitart.no2.collection.NitriteCollection; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static org.dizitart.no2.filters.FluentFilter.where; |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | + |
| 33 | +/** |
| 34 | + * Regression test for <a href="https://github.com/nitrite/nitrite-java/issues/1335">Issue 1335</a>. |
| 35 | + * <p> |
| 36 | + * A document built from a plain {@link Map} (e.g. deserialized JSON) keeps nested |
| 37 | + * objects as {@link Map}s instead of {@link Document}s. Embedded field access such as |
| 38 | + * {@code address.city} did not descend into those maps, so filters on nested fields |
| 39 | + * never matched. |
| 40 | + * |
| 41 | + * @author Anindya Chatterjee |
| 42 | + */ |
| 43 | +public class Issue1335Test { |
| 44 | + private Nitrite db; |
| 45 | + private NitriteCollection collection; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + db = Nitrite.builder().openOrCreate(); |
| 50 | + collection = db.getCollection("issue1335"); |
| 51 | + |
| 52 | + Map<String, Object> address = new HashMap<>(); |
| 53 | + address.put("city", "bangalore"); |
| 54 | + Map<String, Object> person = new HashMap<>(); |
| 55 | + person.put("name", "amit"); |
| 56 | + person.put("address", address); |
| 57 | + collection.insert(Document.createDocument(person)); |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + public void tearDown() { |
| 62 | + db.close(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testNestedMapField() { |
| 67 | + assertEquals(1, collection.find(where("name").eq("amit")).size()); |
| 68 | + assertEquals(1, collection.find(where("address.city").eq("bangalore")).size()); |
| 69 | + assertEquals(0, collection.find(where("address.city").notEq("bangalore")).size()); |
| 70 | + } |
| 71 | +} |
0 commit comments