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

http://github.com/SpringSource/spring-data-mongodb · Java · 138 lines · 87 code · 31 blank · 20 comment · 0 complexity · f917e9e0134bd02b4d0f38c7da613841 MD5 · raw file

  1. /*
  2. * Copyright 2020 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;
  17. import static org.assertj.core.api.Assertions.*;
  18. import static org.springframework.data.mongodb.core.query.Criteria.*;
  19. import static org.springframework.data.mongodb.core.query.Query.*;
  20. import lombok.EqualsAndHashCode;
  21. import lombok.ToString;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import org.junit.jupiter.api.Test;
  25. import org.junit.jupiter.api.extension.ExtendWith;
  26. import org.springframework.data.mongodb.core.mapping.Field;
  27. import org.springframework.data.mongodb.core.mapping.Unwrapped;
  28. import org.springframework.data.mongodb.core.query.Query;
  29. import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
  30. import org.springframework.data.mongodb.test.util.Template;
  31. /**
  32. * Integration tests for {@link Unwrapped}.
  33. *
  34. * @author Christoph Strobl
  35. */
  36. @ExtendWith(MongoTemplateExtension.class)
  37. class MongoTemplateUnwrappedTests {
  38. private static @Template MongoTemplate template;
  39. @Test // DATAMONGO-1902
  40. void readWrite() {
  41. WithUnwrapped source = new WithUnwrapped();
  42. source.id = "id-1";
  43. source.embeddableValue = new UnwrappableType();
  44. source.embeddableValue.stringValue = "string-val";
  45. source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
  46. source.embeddableValue.atFieldAnnotatedValue = "@Field";
  47. template.save(source);
  48. assertThat(template.findOne(query(where("id").is(source.id)), WithUnwrapped.class)).isEqualTo(source);
  49. }
  50. @Test // DATAMONGO-1902
  51. void filterOnUnwrappedValue() {
  52. WithUnwrapped source = new WithUnwrapped();
  53. source.id = "id-1";
  54. source.embeddableValue = new UnwrappableType();
  55. source.embeddableValue.stringValue = "string-val";
  56. source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
  57. source.embeddableValue.atFieldAnnotatedValue = "@Field";
  58. template.save(source);
  59. assertThat(template.findOne(
  60. Query.query(where("embeddableValue.stringValue").is(source.embeddableValue.stringValue)), WithUnwrapped.class))
  61. .isEqualTo(source);
  62. }
  63. @Test // DATAMONGO-1902
  64. void readWritePrefixed() {
  65. WithPrefixedUnwrapped source = new WithPrefixedUnwrapped();
  66. source.id = "id-1";
  67. source.embeddableValue = new UnwrappableType();
  68. source.embeddableValue.stringValue = "string-val";
  69. source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
  70. source.embeddableValue.atFieldAnnotatedValue = "@Field";
  71. template.save(source);
  72. assertThat(template.findOne(query(where("id").is(source.id)), WithPrefixedUnwrapped.class)).isEqualTo(source);
  73. }
  74. @Test // DATAMONGO-1902
  75. void filterOnPrefixedUnwrappedValue() {
  76. WithPrefixedUnwrapped source = new WithPrefixedUnwrapped();
  77. source.id = "id-1";
  78. source.embeddableValue = new UnwrappableType();
  79. source.embeddableValue.stringValue = "string-val";
  80. source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
  81. source.embeddableValue.atFieldAnnotatedValue = "@Field";
  82. template.save(source);
  83. assertThat(
  84. template.findOne(Query.query(where("embeddableValue.stringValue").is(source.embeddableValue.stringValue)),
  85. WithPrefixedUnwrapped.class)).isEqualTo(source);
  86. }
  87. @EqualsAndHashCode
  88. @ToString
  89. static class WithUnwrapped {
  90. String id;
  91. @Unwrapped.Nullable UnwrappableType embeddableValue;
  92. }
  93. @EqualsAndHashCode
  94. @ToString
  95. static class WithPrefixedUnwrapped {
  96. String id;
  97. @Unwrapped.Nullable("prefix-") UnwrappableType embeddableValue;
  98. }
  99. @EqualsAndHashCode
  100. @ToString
  101. static class UnwrappableType {
  102. String stringValue;
  103. List<String> listValue;
  104. @Field("with-at-field-annotation") //
  105. String atFieldAnnotatedValue;
  106. }
  107. }