|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | + |
| 17 | +package example.springdata.mongodb.querybyexample; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.*; |
| 20 | +import static org.junit.Assert.*; |
| 21 | +import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*; |
| 22 | +import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 29 | +import org.springframework.data.domain.Example; |
| 30 | +import org.springframework.data.domain.ExampleSpec; |
| 31 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 32 | +import org.springframework.data.mongodb.core.query.Query; |
| 33 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration test showing the usage of MongoDB Query-by-Example support through Spring Data repositories. |
| 37 | + * |
| 38 | + * @author Mark Paluch |
| 39 | + */ |
| 40 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 41 | +@SpringApplicationConfiguration(classes = ApplicationConfiguration.class) |
| 42 | +public class MongoOperationsIntegrationTests { |
| 43 | + |
| 44 | + @Autowired MongoOperations operations; |
| 45 | + |
| 46 | + User skyler, walter, flynn, marie, hank; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + |
| 51 | + operations.remove(new Query(), User.class); |
| 52 | + |
| 53 | + this.skyler = new User("Skyler", "White", 45); |
| 54 | + this.walter = new User("Walter", "White", 50); |
| 55 | + this.flynn = new User("Walter Jr. (Flynn)", "White", 17); |
| 56 | + this.marie = new User("Marie", "Schrader", 38); |
| 57 | + this.hank = new User("Hank", "Schrader", 43); |
| 58 | + |
| 59 | + operations.save(this.skyler); |
| 60 | + operations.save(this.walter); |
| 61 | + operations.save(this.flynn); |
| 62 | + operations.save(this.marie); |
| 63 | + operations.save(this.hank); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @see DATAMONGO-1245 |
| 68 | + */ |
| 69 | + @Test |
| 70 | + public void ignoreNullProperties() { |
| 71 | + |
| 72 | + assertThat(operations.findByExample(new User(null, null, 17)), hasItems(flynn)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @see DATAMONGO-1245 |
| 77 | + */ |
| 78 | + @Test |
| 79 | + public void substringMatching() { |
| 80 | + |
| 81 | + Example<User> example = ExampleSpec.of(User.class).// |
| 82 | + withStringMatcherEnding().// |
| 83 | + createExample(new User("er", null, null)); |
| 84 | + |
| 85 | + assertThat(operations.findByExample(example), hasItems(skyler, walter)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @see DATAMONGO-1245 |
| 90 | + */ |
| 91 | + @Test |
| 92 | + public void regexMatching() { |
| 93 | + |
| 94 | + Example<User> example = ExampleSpec.of(User.class).// |
| 95 | + withMatcher("firstname", matcher -> matcher.regex()).// |
| 96 | + createExample(new User("(Skyl|Walt)er", null, null)); |
| 97 | + |
| 98 | + assertThat(operations.findByExample(example), hasItems(skyler, walter)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @see DATAMONGO-1245 |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void matchStartingStringsIgnoreCase() { |
| 106 | + |
| 107 | + Example<User> example = ExampleSpec.of(User.class). // |
| 108 | + withIgnorePaths("age").// |
| 109 | + withMatcher("firstname", startsWith()).// |
| 110 | + withMatcher("lastname", ignoreCase()).// |
| 111 | + createExample(new User("Walter", "WHITE", null)); |
| 112 | + |
| 113 | + assertThat(operations.findByExample(example), hasItems(flynn, walter)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @see DATAMONGO-1245 |
| 118 | + */ |
| 119 | + @Test |
| 120 | + public void configuringMatchersUsingLambdas() { |
| 121 | + |
| 122 | + Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). // |
| 123 | + withMatcher("firstname", matcher -> matcher.startsWith()). // |
| 124 | + withMatcher("lastname", matcher -> matcher.ignoreCase()). // |
| 125 | + createExample(new User("Walter", "WHITE", null)); |
| 126 | + |
| 127 | + assertThat(operations.findByExample(example), hasItems(flynn, walter)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @see DATAMONGO-1245 |
| 132 | + */ |
| 133 | + @Test |
| 134 | + public void valueTransformer() { |
| 135 | + |
| 136 | + Example<User> example = ExampleSpec.of(User.class). // |
| 137 | + withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).// |
| 138 | + createExample(new User(null, "White", 99)); |
| 139 | + |
| 140 | + assertThat(operations.findByExample(example), hasItems(walter)); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments