PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/WireProtocol/Messages/Encoders/JsonEncoders/ReplyMessageJsonEncoderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 228 lines | 194 code | 18 blank | 16 comment | 0 complexity | 768d09fbb16ad4fea904700ac0b38298 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using FluentAssertions;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Bson.Serialization.Serializers;
  22. using NUnit.Framework;
  23. namespace MongoDB.Driver.Core.WireProtocol.Messages.Encoders.JsonEncoders
  24. {
  25. [TestFixture]
  26. public class ReplyMessageJsonEncoderTests
  27. {
  28. #region static
  29. // static fields
  30. private static readonly bool __awaitCapable = true;
  31. private static readonly long __cursorId = 3;
  32. private static readonly bool __cursorNotFound = false;
  33. private static readonly List<BsonDocument> __documents = new List<BsonDocument>(new[] { new BsonDocument("_id", 1), new BsonDocument("_id", 2) });
  34. private static readonly MessageEncoderSettings __messageEncoderSettings = new MessageEncoderSettings();
  35. private static readonly int __numberReturned = 2;
  36. private static readonly BsonDocument __queryFailureDocument = new BsonDocument("ok", 0);
  37. private static readonly ReplyMessage<BsonDocument> __queryFailureMessage;
  38. private static readonly string __queryFailureMessageJson;
  39. private static readonly int __requestId = 1;
  40. private static readonly int __responseTo = 2;
  41. private static readonly IBsonSerializer<BsonDocument> __serializer = BsonDocumentSerializer.Instance;
  42. private static readonly int __startingFrom = 4;
  43. private static readonly ReplyMessage<BsonDocument> __testMessage;
  44. private static readonly string __testMessageJson;
  45. // static constructor
  46. static ReplyMessageJsonEncoderTests()
  47. {
  48. __testMessage = new ReplyMessage<BsonDocument>(__awaitCapable, __cursorId, __cursorNotFound, __documents, __numberReturned, false, null, __requestId, __responseTo, __serializer, __startingFrom);
  49. __testMessageJson =
  50. "{ " +
  51. "\"opcode\" : \"reply\", " +
  52. "\"requestId\" : 1, " +
  53. "\"responseTo\" : 2, " +
  54. "\"cursorId\" : NumberLong(3), " +
  55. "\"numberReturned\" : 2, " +
  56. "\"startingFrom\" : 4, " +
  57. "\"awaitCapable\" : true, " +
  58. "\"documents\" : [{ \"_id\" : 1 }, { \"_id\" : 2 }]" +
  59. " }";
  60. __queryFailureMessage = new ReplyMessage<BsonDocument>(false, __cursorId, true, null, 0, true, __queryFailureDocument, __requestId, __responseTo, __serializer, 0);
  61. __queryFailureMessageJson =
  62. "{ " +
  63. "\"opcode\" : \"reply\", " +
  64. "\"requestId\" : 1, " +
  65. "\"responseTo\" : 2, " +
  66. "\"cursorId\" : NumberLong(3), " +
  67. "\"cursorNotFound\" : true, " +
  68. "\"numberReturned\" : 0, " +
  69. "\"queryFailure\" : { \"ok\" : 0 }" +
  70. " }";
  71. }
  72. #endregion
  73. [Test]
  74. public void Constructor_should_not_throw_if_textReader_and_textWriter_are_both_provided()
  75. {
  76. using (var textReader = new StringReader(""))
  77. using (var textWriter = new StringWriter())
  78. {
  79. Action action = () => new ReplyMessageJsonEncoder<BsonDocument>(textReader, textWriter, __messageEncoderSettings, __serializer);
  80. action.ShouldNotThrow();
  81. }
  82. }
  83. [Test]
  84. public void Constructor_should_not_throw_if_only_textReader_is_provided()
  85. {
  86. using (var textReader = new StringReader(""))
  87. {
  88. Action action = () => new ReplyMessageJsonEncoder<BsonDocument>(textReader, null, __messageEncoderSettings, __serializer);
  89. action.ShouldNotThrow();
  90. }
  91. }
  92. [Test]
  93. public void Constructor_should_not_throw_if_only_textWriter_is_provided()
  94. {
  95. using (var textWriter = new StringWriter())
  96. {
  97. Action action = () => new ReplyMessageJsonEncoder<BsonDocument>(null, textWriter, __messageEncoderSettings, __serializer);
  98. action.ShouldNotThrow();
  99. }
  100. }
  101. [Test]
  102. public void Constructor_should_throw_if_textReader_and_textWriter_are_both_null()
  103. {
  104. Action action = () => new ReplyMessageJsonEncoder<BsonDocument>(null, null, __messageEncoderSettings, __serializer);
  105. action.ShouldThrow<ArgumentException>();
  106. }
  107. [Test]
  108. public void Constructor_should_throw_if_serializer_is_null()
  109. {
  110. using (var textReader = new StringReader(""))
  111. using (var textWriter = new StringWriter())
  112. {
  113. Action action = () => new ReplyMessageJsonEncoder<BsonDocument>(textReader, textWriter, __messageEncoderSettings, null);
  114. action.ShouldThrow<ArgumentNullException>();
  115. }
  116. }
  117. [Test]
  118. public void ReadMessage_should_read_a_message()
  119. {
  120. using (var textReader = new StringReader(__testMessageJson))
  121. {
  122. var subject = new ReplyMessageJsonEncoder<BsonDocument>(textReader, null, __messageEncoderSettings, __serializer);
  123. var message = subject.ReadMessage();
  124. message.AwaitCapable.Should().Be(__awaitCapable);
  125. message.CursorId.Should().Be(__cursorId);
  126. message.CursorNotFound.Should().Be(__cursorNotFound);
  127. message.Documents.Should().Equal(__documents);
  128. message.NumberReturned.Should().Be(__numberReturned);
  129. message.QueryFailure.Should().Be(false);
  130. message.QueryFailureDocument.Should().BeNull();
  131. message.Serializer.Should().BeSameAs(__serializer);
  132. message.StartingFrom.Should().Be(__startingFrom);
  133. message.RequestId.Should().Be(__requestId);
  134. message.ResponseTo.Should().Be(__responseTo);
  135. }
  136. }
  137. [Test]
  138. public void ReadMessage_should_read_a_query_failure_message()
  139. {
  140. using (var textReader = new StringReader(__queryFailureMessageJson))
  141. {
  142. var subject = new ReplyMessageJsonEncoder<BsonDocument>(textReader, null, __messageEncoderSettings, __serializer);
  143. var message = subject.ReadMessage();
  144. message.AwaitCapable.Should().Be(false);
  145. message.CursorId.Should().Be(__cursorId);
  146. message.CursorNotFound.Should().Be(true);
  147. message.Documents.Should().BeNull();
  148. message.NumberReturned.Should().Be(0);
  149. message.QueryFailure.Should().Be(true);
  150. message.QueryFailureDocument.Should().Be(__queryFailureDocument);
  151. message.Serializer.Should().BeSameAs(__serializer);
  152. message.StartingFrom.Should().Be(0);
  153. message.RequestId.Should().Be(__requestId);
  154. message.ResponseTo.Should().Be(__responseTo);
  155. }
  156. }
  157. [Test]
  158. public void ReadMessage_should_throw_if_textReader_was_not_provided()
  159. {
  160. using (var textWriter = new StringWriter())
  161. {
  162. var subject = new ReplyMessageJsonEncoder<BsonDocument>(null, textWriter, __messageEncoderSettings, __serializer);
  163. Action action = () => subject.ReadMessage();
  164. action.ShouldThrow<InvalidOperationException>();
  165. }
  166. }
  167. [Test]
  168. public void WriteMessage_should_throw_if_textWriter_was_not_provided()
  169. {
  170. using (var textReader = new StringReader(""))
  171. {
  172. var subject = new ReplyMessageJsonEncoder<BsonDocument>(textReader, null, __messageEncoderSettings, __serializer);
  173. Action action = () => subject.WriteMessage(__testMessage);
  174. action.ShouldThrow<InvalidOperationException>();
  175. }
  176. }
  177. [Test]
  178. public void WriteMessage_should_throw_if_message_is_null()
  179. {
  180. using (var textWriter = new StringWriter())
  181. {
  182. var subject = new ReplyMessageJsonEncoder<BsonDocument>(null, textWriter, __messageEncoderSettings, __serializer);
  183. Action action = () => subject.WriteMessage(null);
  184. action.ShouldThrow<ArgumentNullException>();
  185. }
  186. }
  187. [Test]
  188. public void WriteMessage_should_write_a_message()
  189. {
  190. using (var textWriter = new StringWriter())
  191. {
  192. var subject = new ReplyMessageJsonEncoder<BsonDocument>(null, textWriter, __messageEncoderSettings, __serializer);
  193. subject.WriteMessage(__testMessage);
  194. var json = textWriter.ToString();
  195. json.Should().Be(__testMessageJson);
  196. }
  197. }
  198. [Test]
  199. public void WriteMessage_should_write_a_query_failure_message()
  200. {
  201. using (var textWriter = new StringWriter())
  202. {
  203. var subject = new ReplyMessageJsonEncoder<BsonDocument>(null, textWriter, __messageEncoderSettings, __serializer);
  204. subject.WriteMessage(__queryFailureMessage);
  205. var json = textWriter.ToString();
  206. json.Should().Be(__queryFailureMessageJson);
  207. }
  208. }
  209. }
  210. }