From ba0232b1876a743c3629f8f1aab35315ddf5b474 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 27 Jul 2012 10:39:31 +0200 Subject: [PATCH] DATAMONGO-494 - QueryMapper now forwards entity metadata into nested $(n)or criterias. Introduced helper class to ease assertions on DBObjects as well. --- .../mongodb/core/convert/QueryMapper.java | 2 +- .../data/mongodb/core/DBObjectUtils.java | 81 +++++++++++++++++++ .../core/convert/QueryMapperUnitTests.java | 26 +++++- 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DBObjectUtils.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 9f81181bd..440ba1702 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -100,7 +100,7 @@ public class QueryMapper { BasicBSONList newConditions = new BasicBSONList(); Iterator iter = conditions.iterator(); while (iter.hasNext()) { - newConditions.add(getMappedObject((DBObject) iter.next(), null)); + newConditions.add(getMappedObject((DBObject) iter.next(), entity)); } value = newConditions; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DBObjectUtils.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DBObjectUtils.java new file mode 100644 index 000000000..a9be3ba3a --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DBObjectUtils.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import com.mongodb.BasicDBList; +import com.mongodb.DBObject; + +/** + * Helper classes to ease assertions on {@link DBObject}s. + * + * @author Oliver Gierke + */ +public abstract class DBObjectUtils { + + private DBObjectUtils() { + + } + + /** + * Expects the field with the given key to be not {@literal null} and a {@link DBObject} in turn and returns it. + * + * @param source the {@link DBObject} to lookup the nested one + * @param key the key of the field to lokup the nested {@link DBObject} + * @return + */ + public static DBObject getAsDBObject(DBObject source, String key) { + return getTypedValue(source, key, DBObject.class); + } + + /** + * Expects the field with the given key to be not {@literal null} and a {@link BasicDBList}. + * + * @param source the {@link DBObject} to lookup the {@link BasicDBList} in + * @param key the key of the field to find the {@link BasicDBList} in + * @return + */ + public static BasicDBList getAsDBList(DBObject source, String key) { + return getTypedValue(source, key, BasicDBList.class); + } + + /** + * Expects the list element with the given index to be a non-{@literal null} {@link DBObject} and returns it. + * + * @param source the {@link BasicDBList} to look up the {@link DBObject} element in + * @param index the index of the element expected to contain a {@link DBObject} + * @return + */ + public static DBObject getAsDBObject(BasicDBList source, int index) { + + assertThat(source.size(), greaterThanOrEqualTo(index + 1)); + Object value = source.get(index); + assertThat(value, is(instanceOf(DBObject.class))); + return (DBObject) value; + } + + @SuppressWarnings("unchecked") + private static T getTypedValue(DBObject source, String key, Class type) { + + Object value = source.get(key); + assertThat(value, is(notNullValue())); + assertThat(value, is(instanceOf(type))); + + return (T) value; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index 6fb7cfa7a..c1c294596 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.springframework.data.mongodb.core.query.Criteria.*; import static org.springframework.data.mongodb.core.query.Query.*; +import static org.springframework.data.mongodb.core.DBObjectUtils.*; import java.math.BigInteger; import java.util.ArrayList; @@ -51,7 +52,6 @@ import com.mongodb.QueryBuilder; * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) -@SuppressWarnings("unused") public class QueryMapperUnitTests { QueryMapper mapper; @@ -233,6 +233,26 @@ public class QueryMapperUnitTests { assertThat(publishers.get("$ne"), is(instanceOf(String.class))); } + /** + * @see DATAMONGO-494 + */ + @Test + public void usesEntityMetadataInOr() { + + Query query = query(new Criteria().orOperator(where("foo").is("bar"))); + DBObject result = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Sample.class)); + + assertThat(result.keySet(), hasSize(1)); + assertThat(result.keySet(), hasItem("$or")); + + BasicDBList ors = getAsDBList(result, "$or"); + assertThat(ors, hasSize(1)); + DBObject criterias = getAsDBObject(ors, 0); + assertThat(criterias.keySet(), hasSize(1)); + assertThat(criterias.get("_id"), is(notNullValue())); + assertThat(criterias.get("foo"), is(nullValue())); + } + class IdWrapper { Object id; }