Skip to content

Commit

Permalink
Fix IndexOptions.expireAfter setting wrong property.
Browse files Browse the repository at this point in the history
Resolves: #4851
  • Loading branch information
christophstrobl committed Dec 11, 2024
1 parent d637e50 commit e08f102
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ public static IndexOptions hidden() {
public static IndexOptions expireAfter(Duration duration) {

IndexOptions options = new IndexOptions();
options.unique = Unique.YES;
options.expire = duration;
return options;
}

/**
* @return the expiration time. A {@link Duration#isNegative() negative value} represents no expiration, {@literal null} if not set.
*/
@Nullable
public Duration getExpire() {
return expire;
}
Expand Down Expand Up @@ -151,7 +152,6 @@ public Document toDocument() {
document.put("hidden", hidden);
}


if (expire != null && !expire.isNegative()) {
document.put("expireAfterSeconds", expire.getSeconds());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2024 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
*
* https://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.index;

import static org.springframework.data.mongodb.test.util.Assertions.assertThat;

import java.time.Duration;

import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.index.IndexOptions.Unique;

/**
* @author Christoph Strobl
*/
class IndexOptionsUnitTests {

@Test // GH-4851
void noneIsEmpty() {

IndexOptions options = IndexOptions.none();

assertThat(options.getExpire()).isNull();
assertThat(options.getUnique()).isNull();
assertThat(options.isHidden()).isNull();
assertThat(options.toDocument()).isEqualTo(new Document());
}

@Test // GH-4851
void uniqueSetsFlag() {

IndexOptions options = IndexOptions.unique();

assertThat(options.getUnique()).isEqualTo(Unique.YES);
assertThat(options.toDocument()).containsEntry("unique", true);

options.setUnique(Unique.NO);
assertThat(options.toDocument()).containsEntry("unique", false);

options.setUnique(Unique.PREPARE);
assertThat(options.toDocument()).containsEntry("prepareUnique", true);
}

@Test // GH-4851
void hiddenSetsFlag() {

IndexOptions options = IndexOptions.hidden();

assertThat(options.isHidden()).isTrue();
assertThat(options.toDocument()).containsEntry("hidden", true);
}

@Test // GH-4851
void expireAfterSetsExpiration() {

Duration duration = Duration.ofMinutes(2);
IndexOptions options = IndexOptions.expireAfter(duration);

assertThat(options.getExpire()).isEqualTo(duration);
assertThat(options.toDocument()).containsEntry("expireAfterSeconds", duration.toSeconds());
}

@Test // GH-4851
void expireAfterForZeroAndNegativeDuration() {

assertThat(IndexOptions.expireAfter(Duration.ZERO).toDocument()).containsEntry("expireAfterSeconds", 0L);
assertThat(IndexOptions.expireAfter(Duration.ofSeconds(-1)).toDocument()).isEmpty();
}
}

0 comments on commit e08f102

Please sign in to comment.