|
| 1 | +/* |
| 2 | + * Copyright 2015 the original 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 | +package org.springframework.data.mongodb.core; |
| 17 | + |
| 18 | +import static org.hamcrest.core.Is.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | +import static org.springframework.data.mongodb.core.query.Criteria.*; |
| 21 | +import static org.springframework.data.mongodb.core.query.Query.*; |
| 22 | + |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.data.mongodb.config.AbstractMongoConfiguration; |
| 31 | +import org.springframework.data.mongodb.repository.MongoRepository; |
| 32 | +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
| 33 | +import org.springframework.test.context.ContextConfiguration; |
| 34 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 35 | + |
| 36 | +import com.mongodb.Mongo; |
| 37 | +import com.mongodb.MongoClient; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Christoph Strobl |
| 41 | + */ |
| 42 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 43 | +@ContextConfiguration |
| 44 | +public class NoExplicitIdTests { |
| 45 | + |
| 46 | + @Configuration |
| 47 | + @EnableMongoRepositories(considerNestedRepositories = true) |
| 48 | + static class Config extends AbstractMongoConfiguration { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected String getDatabaseName() { |
| 52 | + return "test"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Mongo mongo() throws Exception { |
| 57 | + return new MongoClient(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Autowired MongoOperations mongoOps; |
| 62 | + @Autowired TypeWithoutExplicitIdPropertyRepository repo; |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setUp() { |
| 66 | + mongoOps.dropCollection(TypeWithoutIdProperty.class); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @see DATAMONGO-1289 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void saveAndRetrieveTypeWithoutIdPorpertyViaTemplate() { |
| 74 | + |
| 75 | + TypeWithoutIdProperty noid = new TypeWithoutIdProperty(); |
| 76 | + noid.someString = "o.O"; |
| 77 | + |
| 78 | + mongoOps.save(noid); |
| 79 | + |
| 80 | + TypeWithoutIdProperty retrieved = mongoOps.findOne(query(where("someString").is(noid.someString)), |
| 81 | + TypeWithoutIdProperty.class); |
| 82 | + |
| 83 | + assertThat(retrieved.someString, is(noid.someString)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @see DATAMONGO-1289 |
| 88 | + */ |
| 89 | + @Test |
| 90 | + public void saveAndRetrieveTypeWithoutIdPorpertyViaRepository() { |
| 91 | + |
| 92 | + TypeWithoutIdProperty noid = new TypeWithoutIdProperty(); |
| 93 | + noid.someString = "o.O"; |
| 94 | + |
| 95 | + repo.save(noid); |
| 96 | + |
| 97 | + TypeWithoutIdProperty retrieved = repo.findBySomeString(noid.someString); |
| 98 | + assertThat(retrieved.someString, is(noid.someString)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @see DATAMONGO-1289 |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void saveAndRetrieveTypeWithoutIdPorpertyViaRepositoryFindOne() { |
| 106 | + |
| 107 | + TypeWithoutIdProperty noid = new TypeWithoutIdProperty(); |
| 108 | + noid.someString = "o.O"; |
| 109 | + |
| 110 | + repo.save(noid); |
| 111 | + |
| 112 | + Map<String, Object> map = mongoOps.findOne(query(where("someString").is(noid.someString)), Map.class, |
| 113 | + "typeWithoutIdProperty"); |
| 114 | + |
| 115 | + TypeWithoutIdProperty retrieved = repo.findOne(map.get("_id").toString()); |
| 116 | + assertThat(retrieved.someString, is(noid.someString)); |
| 117 | + } |
| 118 | + |
| 119 | + static class TypeWithoutIdProperty { |
| 120 | + |
| 121 | + String someString; |
| 122 | + } |
| 123 | + |
| 124 | + static interface TypeWithoutExplicitIdPropertyRepository extends MongoRepository<TypeWithoutIdProperty, String> { |
| 125 | + |
| 126 | + TypeWithoutIdProperty findBySomeString(String someString); |
| 127 | + } |
| 128 | +} |
0 commit comments