|
| 1 | +/* |
| 2 | + * Copyright 2017 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.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import reactor.core.publisher.Flux; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.test.StepVerifier; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.bson.Document; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.ClassRule; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; |
| 34 | +import org.springframework.data.mongodb.test.util.MongoVersionRule; |
| 35 | +import org.springframework.data.util.Version; |
| 36 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 37 | + |
| 38 | +import com.mongodb.reactivestreams.client.MongoClients; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Mark Paluch |
| 42 | + */ |
| 43 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 44 | +public class ReactiveMongoTemplateCollationTests { |
| 45 | + |
| 46 | + public static @ClassRule MongoVersionRule REQUIRES_AT_LEAST_3_4_0 = MongoVersionRule.atLeast(Version.parse("3.4.0")); |
| 47 | + public static final String COLLECTION_NAME = "collation-1"; |
| 48 | + |
| 49 | + @Configuration |
| 50 | + static class Config extends AbstractReactiveMongoConfiguration { |
| 51 | + |
| 52 | + @Override |
| 53 | + public com.mongodb.reactivestreams.client.MongoClient mongoClient() { |
| 54 | + return MongoClients.create(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected String getDatabaseName() { |
| 59 | + return "collation-tests"; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Autowired ReactiveMongoTemplate template; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setUp() { |
| 67 | + StepVerifier.create(template.dropCollection(COLLECTION_NAME)).verifyComplete(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test // DATAMONGO-1693 |
| 71 | + public void createCollectionWithCollation() { |
| 72 | + |
| 73 | + StepVerifier.create(template.createCollection(COLLECTION_NAME, CollectionOptions.just(Collation.of("en_US")))) // |
| 74 | + .expectNextCount(1) // |
| 75 | + .verifyComplete(); |
| 76 | + |
| 77 | + Mono<Document> collation = getCollationInfo(COLLECTION_NAME); |
| 78 | + StepVerifier.create(collation) // |
| 79 | + .consumeNextWith(document -> assertThat(document.get("locale")).isEqualTo("en_US")) // |
| 80 | + .verifyComplete(); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private Mono<Document> getCollationInfo(String collectionName) { |
| 85 | + |
| 86 | + return getCollectionInfo(collectionName) // |
| 87 | + .map(it -> it.get("options", Document.class)) // |
| 88 | + .map(it -> it.get("collation", Document.class)); |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressWarnings("unchecked") |
| 92 | + private Mono<Document> getCollectionInfo(String collectionName) { |
| 93 | + |
| 94 | + return template.execute(db -> { |
| 95 | + |
| 96 | + return Flux |
| 97 | + .from(db.runCommand(new Document() // |
| 98 | + .append("listCollections", 1) // |
| 99 | + .append("filter", new Document("name", collectionName)))) // |
| 100 | + .map(it -> it.get("cursor", Document.class)) |
| 101 | + .flatMapIterable(it -> (List<Document>) it.get("firstBatch", List.class)); |
| 102 | + }).next(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments