/bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java

https://github.com/jyemin/mongo-java-driver · Java · 189 lines · 127 code · 33 blank · 29 comment · 0 complexity · 607a2bcb88928e7cc30af3e23ff9a678 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 org.bson.codecs.record;
  17. import org.bson.BsonArray;
  18. import org.bson.BsonDocument;
  19. import org.bson.BsonDocumentReader;
  20. import org.bson.BsonDocumentWriter;
  21. import org.bson.BsonInt32;
  22. import org.bson.BsonObjectId;
  23. import org.bson.BsonString;
  24. import org.bson.codecs.DecoderContext;
  25. import org.bson.codecs.EncoderContext;
  26. import org.bson.codecs.configuration.CodecConfigurationException;
  27. import org.bson.codecs.record.samples.TestRecordWithDeprecatedAnnotations;
  28. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnConstructor;
  29. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnMethod;
  30. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonDiscriminatorOnRecord;
  31. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonExtraElementsOnAccessor;
  32. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonExtraElementsOnComponent;
  33. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonIdOnAccessor;
  34. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonIdOnCanonicalConstructor;
  35. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonIgnoreOnAccessor;
  36. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonIgnoreOnComponent;
  37. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonPropertyOnAccessor;
  38. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonPropertyOnCanonicalConstructor;
  39. import org.bson.codecs.record.samples.TestRecordWithIllegalBsonRepresentationOnAccessor;
  40. import org.bson.codecs.record.samples.TestRecordWithPojoAnnotations;
  41. import org.bson.conversions.Bson;
  42. import org.bson.types.ObjectId;
  43. import org.junit.jupiter.api.Test;
  44. import java.util.List;
  45. import static org.junit.jupiter.api.Assertions.assertEquals;
  46. import static org.junit.jupiter.api.Assertions.assertThrows;
  47. public class RecordCodecTest {
  48. @Test
  49. public void testRecordWithDeprecatedAnnotations() {
  50. var codec = new RecordCodec<>(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
  51. var identifier = new ObjectId();
  52. var testRecord = new TestRecordWithDeprecatedAnnotations("Lucas", 14, List.of("soccer", "basketball"), identifier.toHexString());
  53. var document = new BsonDocument();
  54. var writer = new BsonDocumentWriter(document);
  55. // when
  56. codec.encode(writer, testRecord, EncoderContext.builder().build());
  57. // then
  58. assertEquals(
  59. new BsonDocument("_id", new BsonObjectId(identifier))
  60. .append("name", new BsonString("Lucas"))
  61. .append("hobbies", new BsonArray(List.of(new BsonString("soccer"), new BsonString("basketball"))))
  62. .append("a", new BsonInt32(14)),
  63. document);
  64. assertEquals("_id", document.getFirstKey());
  65. // when
  66. var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
  67. // then
  68. assertEquals(testRecord, decoded);
  69. }
  70. @Test
  71. public void testRecordWithPojoAnnotations() {
  72. var codec = new RecordCodec<>(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
  73. var identifier = new ObjectId();
  74. var testRecord = new TestRecordWithPojoAnnotations("Lucas", 14, List.of("soccer", "basketball"), identifier.toHexString());
  75. var document = new BsonDocument();
  76. var writer = new BsonDocumentWriter(document);
  77. // when
  78. codec.encode(writer, testRecord, EncoderContext.builder().build());
  79. // then
  80. assertEquals(
  81. new BsonDocument("_id", new BsonObjectId(identifier))
  82. .append("name", new BsonString("Lucas"))
  83. .append("hobbies", new BsonArray(List.of(new BsonString("soccer"), new BsonString("basketball"))))
  84. .append("a", new BsonInt32(14)),
  85. document);
  86. assertEquals("_id", document.getFirstKey());
  87. // when
  88. var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
  89. // then
  90. assertEquals(testRecord, decoded);
  91. }
  92. @Test
  93. public void testRecordWithNulls() {
  94. var codec = new RecordCodec<>(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
  95. var identifier = new ObjectId();
  96. var testRecord = new TestRecordWithDeprecatedAnnotations(null, 14, null, identifier.toHexString());
  97. var document = new BsonDocument();
  98. var writer = new BsonDocumentWriter(document);
  99. // when
  100. codec.encode(writer, testRecord, EncoderContext.builder().build());
  101. // then
  102. assertEquals(
  103. new BsonDocument("_id", new BsonObjectId(identifier))
  104. .append("a", new BsonInt32(14)),
  105. document);
  106. // when
  107. var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
  108. // then
  109. assertEquals(testRecord, decoded);
  110. }
  111. @Test
  112. public void testRecordWithExtraData() {
  113. var codec = new RecordCodec<>(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
  114. var identifier = new ObjectId();
  115. var testRecord = new TestRecordWithDeprecatedAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString());
  116. var document = new BsonDocument("_id", new BsonObjectId(identifier))
  117. .append("nationality", new BsonString("British"))
  118. .append("name", new BsonString("Felix"))
  119. .append("hobbies", new BsonArray(List.of(new BsonString("rugby"), new BsonString("badminton"))))
  120. .append("a", new BsonInt32(13));
  121. // when
  122. var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
  123. // then
  124. assertEquals(testRecord, decoded);
  125. }
  126. @Test
  127. public void testExceptionsForAnnotationsNotOnRecordComponent() {
  128. assertThrows(CodecConfigurationException.class, () ->
  129. new RecordCodec<>(TestRecordWithIllegalBsonIdOnAccessor.class, Bson.DEFAULT_CODEC_REGISTRY));
  130. assertThrows(CodecConfigurationException.class, () ->
  131. new RecordCodec<>(TestRecordWithIllegalBsonIdOnCanonicalConstructor.class, Bson.DEFAULT_CODEC_REGISTRY));
  132. assertThrows(CodecConfigurationException.class, () ->
  133. new RecordCodec<>(TestRecordWithIllegalBsonPropertyOnAccessor.class, Bson.DEFAULT_CODEC_REGISTRY));
  134. assertThrows(CodecConfigurationException.class, () ->
  135. new RecordCodec<>(TestRecordWithIllegalBsonPropertyOnCanonicalConstructor.class, Bson.DEFAULT_CODEC_REGISTRY));
  136. assertThrows(CodecConfigurationException.class, () ->
  137. new RecordCodec<>(TestRecordWithIllegalBsonRepresentationOnAccessor.class, Bson.DEFAULT_CODEC_REGISTRY));
  138. }
  139. @Test
  140. public void testExceptionsForUnsupportedAnnotations() {
  141. assertThrows(CodecConfigurationException.class, () ->
  142. new RecordCodec<>(TestRecordWithIllegalBsonDiscriminatorOnRecord.class, Bson.DEFAULT_CODEC_REGISTRY));
  143. assertThrows(CodecConfigurationException.class, () ->
  144. new RecordCodec<>(TestRecordWithIllegalBsonCreatorOnConstructor.class, Bson.DEFAULT_CODEC_REGISTRY));
  145. assertThrows(CodecConfigurationException.class, () ->
  146. new RecordCodec<>(TestRecordWithIllegalBsonCreatorOnMethod.class, Bson.DEFAULT_CODEC_REGISTRY));
  147. assertThrows(CodecConfigurationException.class, () ->
  148. new RecordCodec<>(TestRecordWithIllegalBsonIgnoreOnComponent.class, Bson.DEFAULT_CODEC_REGISTRY));
  149. assertThrows(CodecConfigurationException.class, () ->
  150. new RecordCodec<>(TestRecordWithIllegalBsonIgnoreOnAccessor.class, Bson.DEFAULT_CODEC_REGISTRY));
  151. assertThrows(CodecConfigurationException.class, () ->
  152. new RecordCodec<>(TestRecordWithIllegalBsonExtraElementsOnComponent.class, Bson.DEFAULT_CODEC_REGISTRY));
  153. assertThrows(CodecConfigurationException.class, () ->
  154. new RecordCodec<>(TestRecordWithIllegalBsonExtraElementsOnAccessor.class, Bson.DEFAULT_CODEC_REGISTRY));
  155. }
  156. }