/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/UnwrapAndReadDbObjectCallbackUnitTests.java
http://github.com/SpringSource/spring-data-mongodb · Java · 95 lines · 52 code · 23 blank · 20 comment · 0 complexity · b110ed4ce2a6525e6f8a946509510e39 MD5 · raw file
- /*
- * Copyright 2013 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.CoreMatchers.*;
- import static org.junit.Assert.*;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.Mock;
- import org.mockito.runners.MockitoJUnitRunner;
- import org.springframework.data.mongodb.MongoDbFactory;
- import org.springframework.data.mongodb.core.MongoTemplate.UnwrapAndReadDbObjectCallback;
- import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
- import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
- import com.mongodb.BasicDBObject;
- /**
- * Unit tests for {@link UnwrapAndReadDbObjectCallback}.
- *
- * @author Oliver Gierke
- */
- @RunWith(MockitoJUnitRunner.class)
- public class UnwrapAndReadDbObjectCallbackUnitTests {
- @Mock MongoDbFactory factory;
- UnwrapAndReadDbObjectCallback<Target> callback;
- @Before
- public void setUp() {
- MongoTemplate template = new MongoTemplate(factory);
- MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext());
- this.callback = template.new UnwrapAndReadDbObjectCallback<Target>(converter, Target.class);
- }
- @Test
- public void usesFirstLevelValues() {
- Target target = callback.doWith(new BasicDBObject("foo", "bar"));
- assertThat(target.id, is(nullValue()));
- assertThat(target.foo, is("bar"));
- }
- @Test
- public void unwrapsUnderscoreIdIfBasicDBObject() {
- Target target = callback.doWith(new BasicDBObject("_id", new BasicDBObject("foo", "bar")));
- assertThat(target.id, is(nullValue()));
- assertThat(target.foo, is("bar"));
- }
- @Test
- public void firstLevelPropertiesTrumpNestedOnes() {
- Target target = callback.doWith(new BasicDBObject("_id", new BasicDBObject("foo", "bar")).append("foo", "foobar"));
- assertThat(target.id, is(nullValue()));
- assertThat(target.foo, is("foobar"));
- }
- @Test
- public void keepsUnderscoreIdIfScalarValue() {
- Target target = callback.doWith(new BasicDBObject("_id", "bar").append("foo", "foo"));
- assertThat(target.id, is("bar"));
- assertThat(target.foo, is("foo"));
- }
- static class Target {
- String id;
- String foo;
- }
- }