|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + * https://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.index; |
| 17 | + |
| 18 | +import static org.springframework.data.mongodb.test.util.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | + |
| 22 | +import org.bson.Document; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.data.mongodb.core.index.IndexOptions.Unique; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Christoph Strobl |
| 28 | + */ |
| 29 | +class IndexOptionsUnitTests { |
| 30 | + |
| 31 | + @Test // GH-4851 |
| 32 | + void noneIsEmpty() { |
| 33 | + |
| 34 | + IndexOptions options = IndexOptions.none(); |
| 35 | + |
| 36 | + assertThat(options.getExpire()).isNull(); |
| 37 | + assertThat(options.getUnique()).isNull(); |
| 38 | + assertThat(options.isHidden()).isNull(); |
| 39 | + assertThat(options.toDocument()).isEqualTo(new Document()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test // GH-4851 |
| 43 | + void uniqueSetsFlag() { |
| 44 | + |
| 45 | + IndexOptions options = IndexOptions.unique(); |
| 46 | + |
| 47 | + assertThat(options.getUnique()).isEqualTo(Unique.YES); |
| 48 | + assertThat(options.toDocument()).containsEntry("unique", true); |
| 49 | + |
| 50 | + options.setUnique(Unique.NO); |
| 51 | + assertThat(options.toDocument()).containsEntry("unique", false); |
| 52 | + |
| 53 | + options.setUnique(Unique.PREPARE); |
| 54 | + assertThat(options.toDocument()).containsEntry("prepareUnique", true); |
| 55 | + } |
| 56 | + |
| 57 | + @Test // GH-4851 |
| 58 | + void hiddenSetsFlag() { |
| 59 | + |
| 60 | + IndexOptions options = IndexOptions.hidden(); |
| 61 | + |
| 62 | + assertThat(options.isHidden()).isTrue(); |
| 63 | + assertThat(options.toDocument()).containsEntry("hidden", true); |
| 64 | + } |
| 65 | + |
| 66 | + @Test // GH-4851 |
| 67 | + void expireAfterSetsExpiration() { |
| 68 | + |
| 69 | + Duration duration = Duration.ofMinutes(2); |
| 70 | + IndexOptions options = IndexOptions.expireAfter(duration); |
| 71 | + |
| 72 | + assertThat(options.getExpire()).isEqualTo(duration); |
| 73 | + assertThat(options.toDocument()).containsEntry("expireAfterSeconds", duration.toSeconds()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test // GH-4851 |
| 77 | + void expireAfterForZeroAndNegativeDuration() { |
| 78 | + |
| 79 | + assertThat(IndexOptions.expireAfter(Duration.ZERO).toDocument()).containsEntry("expireAfterSeconds", 0L); |
| 80 | + assertThat(IndexOptions.expireAfter(Duration.ofSeconds(-1)).toDocument()).isEmpty(); |
| 81 | + } |
| 82 | +} |
0 commit comments