/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SortOperationUnitTests.java

https://github.com/spring-projects/spring-data-mongodb · Java · 56 lines · 25 code · 10 blank · 21 comment · 0 complexity · 03061f7c018c8d55b0d1a8a65480febc MD5 · raw file

  1. /*
  2. * Copyright 2013-2022 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.aggregation;
  17. import static org.assertj.core.api.Assertions.*;
  18. import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
  19. import org.bson.Document;
  20. import org.junit.jupiter.api.Test;
  21. import org.springframework.data.domain.Sort;
  22. import org.springframework.data.domain.Sort.Direction;
  23. /**
  24. * Unit tests for {@link SortOperation}.
  25. *
  26. * @author Oliver Gierke
  27. * @author Mark Paluch
  28. */
  29. public class SortOperationUnitTests {
  30. @Test
  31. public void createsDocumentForAscendingSortCorrectly() {
  32. SortOperation operation = new SortOperation(Sort.by(Direction.ASC, "foobar"));
  33. Document result = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
  34. Document sortValue = getAsDocument(result, "$sort");
  35. assertThat(sortValue).isNotNull();
  36. assertThat(sortValue.get("foobar")).isEqualTo((Object) 1);
  37. }
  38. @Test
  39. public void createsDocumentForDescendingSortCorrectly() {
  40. SortOperation operation = new SortOperation(Sort.by(Direction.DESC, "foobar"));
  41. Document result = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
  42. Document sortValue = getAsDocument(result, "$sort");
  43. assertThat(sortValue).isNotNull();
  44. assertThat(sortValue.get("foobar")).isEqualTo((Object) (0 - 1));
  45. }
  46. }