DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update.

Original pull request: #600.
This commit is contained in:
Christoph Strobl
2018-08-13 13:31:13 +02:00
committed by Mark Paluch
parent 208bd6ae52
commit 67c3f02dcc
2 changed files with 7 additions and 9 deletions

View File

@@ -893,18 +893,14 @@ public class Update {
/**
* Forces values to be added at the given {@literal position}.
*
* @param position needs to be greater than or equal to zero.
* @param position the position offset. As of MongoDB 3.6 use a negative value to indicate starting from the end,
* counting (but not including) the last element of the array.
* @return never {@literal null}.
* @since 1.7
*/
public PushOperatorBuilder atPosition(int position) {
if (position < 0) {
throw new IllegalArgumentException("Position must be greater than or equal to zero.");
}
this.modifiers.addModifier(new PositionModifier(position));
return this;
}

View File

@@ -390,9 +390,11 @@ public class UpdateTests {
.isEqualTo(new Document().append("$bit", new Document("key", new Document("xor", 10L))));
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-943
public void pushShouldThrowExceptionWhenGivenNegativePosition() {
new Update().push("foo").atPosition(-1).each("booh");
@Test // DATAMONGO-943, // DATAMONGO-2055
public void pushShouldAllowNegativePosition() {
assertThat(new Update().push("foo").atPosition(-1).each("booh").toString()).isEqualTo(
"{ \"$push\" : { \"foo\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : -1} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"booh\"]} } } } } }");
}
@Test // DATAMONGO-1346