PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 153 lines | 124 code | 13 blank | 16 comment | 0 complexity | dbc19cc6af614ae974baed2ef7966e8f 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.IO;
  17. using FluentAssertions;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Driver.Core.WireProtocol.Messages;
  20. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders.JsonEncoders;
  21. using NUnit.Framework;
  22. namespace MongoDB.Driver.Core.WireProtocol.Messages.Encoders.JsonEncoders
  23. {
  24. [TestFixture]
  25. public class GetMoreMessageJsonEncoderTests
  26. {
  27. #region static
  28. // static fields
  29. private static readonly int __batchSize = 3;
  30. private static readonly CollectionNamespace __collectionNamespace = new CollectionNamespace("d", "c");
  31. private static readonly long __cursorId = 2;
  32. private static readonly MessageEncoderSettings __messageEncoderSettings = new MessageEncoderSettings();
  33. private static readonly int __requestId = 1;
  34. private static readonly GetMoreMessage __testMessage;
  35. private static readonly string __testMessageJson;
  36. // static constructor
  37. static GetMoreMessageJsonEncoderTests()
  38. {
  39. __testMessage = new GetMoreMessage(__requestId, __collectionNamespace, __cursorId, __batchSize);
  40. __testMessageJson =
  41. "{ " +
  42. "\"opcode\" : \"getMore\", " +
  43. "\"requestId\" : 1, " +
  44. "\"database\" : \"d\", " +
  45. "\"collection\" : \"c\", " +
  46. "\"cursorId\" : NumberLong(2), " +
  47. "\"batchSize\" : 3" +
  48. " }";
  49. }
  50. #endregion
  51. [Test]
  52. public void Constructor_should_not_throw_if_textReader_and_textWriter_are_both_provided()
  53. {
  54. using (var textReader = new StringReader(""))
  55. using (var textWriter = new StringWriter())
  56. {
  57. Action action = () => new GetMoreMessageJsonEncoder(textReader, textWriter, __messageEncoderSettings);
  58. action.ShouldNotThrow();
  59. }
  60. }
  61. [Test]
  62. public void Constructor_should_not_throw_if_only_textReader_is_provided()
  63. {
  64. using (var textReader = new StringReader(""))
  65. {
  66. Action action = () => new GetMoreMessageJsonEncoder(textReader, null, __messageEncoderSettings);
  67. action.ShouldNotThrow();
  68. }
  69. }
  70. [Test]
  71. public void Constructor_should_not_throw_if_only_textWriter_is_provided()
  72. {
  73. using (var textWriter = new StringWriter())
  74. {
  75. Action action = () => new GetMoreMessageJsonEncoder(null, textWriter, __messageEncoderSettings);
  76. action.ShouldNotThrow();
  77. }
  78. }
  79. [Test]
  80. public void Constructor_should_throw_if_textReader_and_textWriter_are_both_null()
  81. {
  82. Action action = () => new GetMoreMessageJsonEncoder(null, null, __messageEncoderSettings);
  83. action.ShouldThrow<ArgumentException>();
  84. }
  85. [Test]
  86. public void ReadMessage_should_read_a_message()
  87. {
  88. using (var textReader = new StringReader(__testMessageJson))
  89. {
  90. var subject = new GetMoreMessageJsonEncoder(textReader, null, __messageEncoderSettings);
  91. var message = subject.ReadMessage();
  92. message.BatchSize.Should().Be(__batchSize);
  93. message.CollectionNamespace.Should().Be(__collectionNamespace);
  94. message.CursorId.Should().Be(__cursorId);
  95. message.RequestId.Should().Be(__requestId);
  96. }
  97. }
  98. [Test]
  99. public void ReadMessage_should_throw_if_textReader_was_not_provided()
  100. {
  101. using (var textWriter = new StringWriter())
  102. {
  103. var subject = new GetMoreMessageJsonEncoder(null, textWriter, __messageEncoderSettings);
  104. Action action = () => subject.ReadMessage();
  105. action.ShouldThrow<InvalidOperationException>();
  106. }
  107. }
  108. [Test]
  109. public void WriteMessage_should_throw_if_textWriter_was_not_provided()
  110. {
  111. using (var textReader = new StringReader(""))
  112. {
  113. var subject = new GetMoreMessageJsonEncoder(textReader, null, __messageEncoderSettings);
  114. Action action = () => subject.WriteMessage(__testMessage);
  115. action.ShouldThrow<InvalidOperationException>();
  116. }
  117. }
  118. [Test]
  119. public void WriteMessage_should_throw_if_message_is_null()
  120. {
  121. using (var textWriter = new StringWriter())
  122. {
  123. var subject = new GetMoreMessageJsonEncoder(null, textWriter, __messageEncoderSettings);
  124. Action action = () => subject.WriteMessage(null);
  125. action.ShouldThrow<ArgumentNullException>();
  126. }
  127. }
  128. [Test]
  129. public void WriteMessage_should_write_a_message()
  130. {
  131. using (var textWriter = new StringWriter())
  132. {
  133. var subject = new GetMoreMessageJsonEncoder(null, textWriter, __messageEncoderSettings);
  134. subject.WriteMessage(__testMessage);
  135. var json = textWriter.ToString();
  136. json.Should().Be(__testMessageJson);
  137. }
  138. }
  139. }
  140. }