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

https://github.com/spring-projects/spring-data-mongodb · Java · 85 lines · 48 code · 13 blank · 24 comment · 0 complexity · bd933d09f88721aff77e2ddc4b03190d MD5 · raw file

  1. /*
  2. * Copyright 2014-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.aggregation.Aggregation.*;
  19. import org.bson.Document;
  20. import org.junit.jupiter.api.BeforeEach;
  21. import org.junit.jupiter.api.Test;
  22. /**
  23. * Unit tests for {@link AggregationOptions}.
  24. *
  25. * @author Thomas Darimont
  26. * @author Mark Paluch
  27. * @author Christoph Strobl
  28. * @author Yadhukrishna S Pai
  29. * @since 1.6
  30. */
  31. class AggregationOptionsTests {
  32. private final Document dummyHint = new Document("dummyField", 1);
  33. AggregationOptions aggregationOptions;
  34. @BeforeEach
  35. void setup() {
  36. aggregationOptions = newAggregationOptions().explain(true) //
  37. .cursorBatchSize(1) //
  38. .allowDiskUse(true) //
  39. .comment("hola") //
  40. .hint(dummyHint) //
  41. .build();
  42. }
  43. @Test // DATAMONGO-960, DATAMONGO-1836
  44. void aggregationOptionsBuilderShouldSetOptionsAccordingly() {
  45. assertThat(aggregationOptions.isAllowDiskUse()).isTrue();
  46. assertThat(aggregationOptions.isExplain()).isTrue();
  47. assertThat(aggregationOptions.getCursor()).contains(new Document("batchSize", 1));
  48. assertThat(aggregationOptions.getHint()).contains(dummyHint);
  49. }
  50. @Test // DATAMONGO-1637, DATAMONGO-2153, DATAMONGO-1836
  51. void shouldInitializeFromDocument() {
  52. Document document = new Document();
  53. document.put("cursor", new Document("batchSize", 1));
  54. document.put("explain", true);
  55. document.put("allowDiskUse", true);
  56. document.put("comment", "hola");
  57. document.put("hint", dummyHint);
  58. aggregationOptions = AggregationOptions.fromDocument(document);
  59. assertThat(aggregationOptions.isAllowDiskUse()).isTrue();
  60. assertThat(aggregationOptions.isExplain()).isTrue();
  61. assertThat(aggregationOptions.getCursor()).contains(new Document("batchSize", 1));
  62. assertThat(aggregationOptions.getCursorBatchSize()).isEqualTo(1);
  63. assertThat(aggregationOptions.getComment()).contains("hola");
  64. assertThat(aggregationOptions.getHint()).contains(dummyHint);
  65. }
  66. @Test // DATAMONGO-960, DATAMONGO-2153, DATAMONGO-1836
  67. void aggregationOptionsToString() {
  68. assertThat(aggregationOptions.toDocument()).isEqualTo(Document
  69. .parse("{ " + "\"allowDiskUse\" : true , " + "\"explain\" : true , " + "\"cursor\" : { \"batchSize\" : 1}, "
  70. + "\"comment\": \"hola\", " + "\"hint\" : { \"dummyField\" : 1}" + "}"));
  71. }
  72. }