/driver-core/src/test/unit/com/mongodb/client/model/densify/DensifyOptionsTest.java

https://github.com/jyemin/mongo-java-driver · Java · 106 lines · 85 code · 6 blank · 15 comment · 0 complexity · 8647c6317defb6b32f082f28bd38b276 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  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. * http://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 com.mongodb.client.model.densify;
  17. import org.bson.BsonArray;
  18. import org.bson.BsonDocument;
  19. import org.bson.BsonInt32;
  20. import org.bson.BsonString;
  21. import org.junit.jupiter.api.Test;
  22. import static java.util.Arrays.asList;
  23. import static java.util.Collections.singleton;
  24. import static java.util.Collections.singletonList;
  25. import static org.junit.jupiter.api.Assertions.assertAll;
  26. import static org.junit.jupiter.api.Assertions.assertEquals;
  27. final class DensifyOptionsTest {
  28. @Test
  29. void densifyOptions() {
  30. assertEquals(
  31. new BsonDocument(),
  32. DensifyOptions.densifyOptions()
  33. .toBsonDocument()
  34. );
  35. }
  36. @Test
  37. void partitionByFields() {
  38. assertAll(
  39. () -> assertEquals(
  40. new BsonDocument()
  41. .append("partitionByFields", new BsonArray(singletonList(new BsonString("$fieldName")))),
  42. DensifyOptions.densifyOptions()
  43. .partitionByFields("$fieldName")
  44. .toBsonDocument()
  45. ),
  46. () -> assertEquals(
  47. new BsonDocument()
  48. .append("partitionByFields", new BsonArray(asList(new BsonString("$fieldName1"), new BsonString("$fieldName2")))),
  49. DensifyOptions.densifyOptions()
  50. .partitionByFields("$fieldName1", "$fieldName2")
  51. .toBsonDocument()
  52. ),
  53. () -> assertEquals(
  54. new BsonDocument()
  55. .append("partitionByFields", new BsonArray(asList(new BsonString("$fieldName1"), new BsonString("$fieldName2")))),
  56. DensifyOptions.densifyOptions()
  57. .partitionByFields(asList("$fieldName1", "$fieldName2"))
  58. .toBsonDocument()
  59. ),
  60. () -> assertEquals(
  61. new BsonDocument()
  62. .append("partitionByFields", new BsonArray(singletonList(new BsonString("fieldName2")))),
  63. DensifyOptions.densifyOptions()
  64. .partitionByFields("$fieldName1")
  65. .partitionByFields(singleton("fieldName2"))
  66. .toBsonDocument()
  67. ),
  68. () -> assertEquals(
  69. new BsonDocument(),
  70. DensifyOptions.densifyOptions()
  71. .partitionByFields(singleton("$fieldName1"))
  72. .partitionByFields()
  73. .toBsonDocument()
  74. )
  75. );
  76. }
  77. @Test
  78. void option() {
  79. assertEquals(
  80. DensifyOptions.densifyOptions()
  81. .option("partitionByFields", new BsonArray(singletonList(new BsonString("fieldName"))))
  82. .toBsonDocument(),
  83. DensifyOptions.densifyOptions()
  84. .option("partitionByFields", singleton("fieldName"))
  85. .toBsonDocument()
  86. );
  87. }
  88. @Test
  89. void options() {
  90. assertEquals(
  91. new BsonDocument()
  92. .append("partitionByFields", new BsonArray(singletonList(new BsonString("fieldName"))))
  93. .append("name", new BsonInt32(42)),
  94. DensifyOptions.densifyOptions()
  95. .partitionByFields("fieldName")
  96. .option("name", 42)
  97. .toBsonDocument()
  98. );
  99. }
  100. }