|
| 1 | +/* Copyright 2016 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 | + |
| 16 | +using System; |
| 17 | +using System.Net; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Bson.IO; |
| 21 | +using MongoDB.Bson.Serialization; |
| 22 | +using MongoDB.Bson.Serialization.Serializers; |
| 23 | +using MongoDB.Driver.Core.Clusters; |
| 24 | +using MongoDB.Driver.Core.Servers; |
| 25 | +using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; |
| 26 | +using NSubstitute; |
| 27 | +using NUnit.Framework; |
| 28 | + |
| 29 | +namespace MongoDB.Driver.Core.Operations |
| 30 | +{ |
| 31 | + [TestFixture] |
| 32 | + public class CommandOperationBaseTests |
| 33 | + { |
| 34 | + // public methods |
| 35 | + [Test] |
| 36 | + public void AdditionalOptions_get_and_set_should_work( |
| 37 | + [Values(null, "{ additional : 1 }")] string additionalOptionsString) |
| 38 | + { |
| 39 | + var subject = CreateSubject<BsonDocument>(); |
| 40 | + var additionalOptions = additionalOptionsString == null ? null : BsonDocument.Parse(additionalOptionsString); |
| 41 | + |
| 42 | + subject.AdditionalOptions = additionalOptions; |
| 43 | + var result = subject.AdditionalOptions; |
| 44 | + |
| 45 | + result.Should().BeSameAs(additionalOptions); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public void Command_get_should_return_expected_result() |
| 50 | + { |
| 51 | + var command = new BsonDocument("command", 1); |
| 52 | + var subject = CreateSubject<BsonDocument>(command: command); |
| 53 | + |
| 54 | + var result = subject.Command; |
| 55 | + |
| 56 | + result.Should().BeSameAs(command); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void CommandValidator_get_and_set_should_work() |
| 61 | + { |
| 62 | + var subject = CreateSubject<BsonDocument>(); |
| 63 | + var commandValidator = Substitute.For<IElementNameValidator>(); |
| 64 | + |
| 65 | + subject.CommandValidator = commandValidator; |
| 66 | + var result = subject.CommandValidator; |
| 67 | + |
| 68 | + result.Should().BeSameAs(commandValidator); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void CommandValidator_set_should_throw_when_value_is_null() |
| 73 | + { |
| 74 | + var subject = CreateSubject<BsonDocument>(); |
| 75 | + |
| 76 | + Action action = () => subject.CommandValidator = null; |
| 77 | + |
| 78 | + action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("value"); |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public void Comment_get_and_set_should_work( |
| 83 | + [Values(null, "comment")] string comment) |
| 84 | + { |
| 85 | + var subject = CreateSubject<BsonDocument>(); |
| 86 | + |
| 87 | + subject.Comment = comment; |
| 88 | + var result = subject.Comment; |
| 89 | + |
| 90 | + result.Should().BeSameAs(comment); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void constructor_should_initialize_instance() |
| 95 | + { |
| 96 | + var databaseNamespace = new DatabaseNamespace("databaseName"); |
| 97 | + var command = new BsonDocument("command", 1); |
| 98 | + var resultSerializer = new BsonDocumentSerializer(); |
| 99 | + var messageEncoderSettings = new MessageEncoderSettings(); |
| 100 | + |
| 101 | + var result = new FakeCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 102 | + |
| 103 | + result.AdditionalOptions.Should().BeNull(); |
| 104 | + result.Command.Should().BeSameAs(command); |
| 105 | + result.CommandValidator.Should().BeOfType<NoOpElementNameValidator>(); |
| 106 | + result.Comment.Should().BeNull(); |
| 107 | + result.DatabaseNamespace.Should().BeSameAs(databaseNamespace); |
| 108 | + result.ResultSerializer.Should().BeSameAs(resultSerializer); |
| 109 | + result.MessageEncoderSettings.Should().BeSameAs(messageEncoderSettings); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public void constructor_should_initialize_instance_when_messageEncoderSettings_is_null() |
| 114 | + { |
| 115 | + var databaseNamespace = new DatabaseNamespace("databaseName"); |
| 116 | + var command = new BsonDocument("command", 1); |
| 117 | + var resultSerializer = new BsonDocumentSerializer(); |
| 118 | + MessageEncoderSettings messageEncoderSettings = null; |
| 119 | + |
| 120 | + var result = new FakeCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 121 | + |
| 122 | + result.MessageEncoderSettings.Should().BeNull(); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void constructor_should_throw_when_command_is_null() |
| 127 | + { |
| 128 | + var databaseNamespace = new DatabaseNamespace("databaseName"); |
| 129 | + BsonDocument command = null; |
| 130 | + var resultSerializer = new BsonDocumentSerializer(); |
| 131 | + var messageEncoderSettings = new MessageEncoderSettings(); |
| 132 | + |
| 133 | + Action action = () => new FakeCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 134 | + |
| 135 | + action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("command"); |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public void constructor_should_throw_when_databaseNamespace_is_null() |
| 140 | + { |
| 141 | + DatabaseNamespace databaseNamespace = null; |
| 142 | + var command = new BsonDocument("command", 1); |
| 143 | + var resultSerializer = new BsonDocumentSerializer(); |
| 144 | + var messageEncoderSettings = new MessageEncoderSettings(); |
| 145 | + |
| 146 | + Action action = () => new FakeCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 147 | + |
| 148 | + action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("databaseNamespace"); |
| 149 | + } |
| 150 | + |
| 151 | + [Test] |
| 152 | + public void constructor_should_throw_when_resultSerializer_is_null() |
| 153 | + { |
| 154 | + var databaseNamespace = new DatabaseNamespace("databaseName"); |
| 155 | + var command = new BsonDocument("command", 1); |
| 156 | + BsonDocumentSerializer resultSerializer = null; |
| 157 | + var messageEncoderSettings = new MessageEncoderSettings(); |
| 158 | + |
| 159 | + Action action = () => new FakeCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 160 | + |
| 161 | + action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("resultSerializer"); |
| 162 | + } |
| 163 | + |
| 164 | + [Test] |
| 165 | + public void DatabaseNamespace_get_should_return_expected_result() |
| 166 | + { |
| 167 | + var databaseNamespace = new DatabaseNamespace("databaseName"); |
| 168 | + var subject = CreateSubject<BsonDocument>(databaseNamespace: databaseNamespace); |
| 169 | + |
| 170 | + var result = subject.DatabaseNamespace; |
| 171 | + |
| 172 | + result.Should().BeSameAs(databaseNamespace); |
| 173 | + } |
| 174 | + |
| 175 | + [Test] |
| 176 | + public void MessageEncoderSettings_get_should_return_expected_result( |
| 177 | + [Values(false, true)] bool useNull) |
| 178 | + { |
| 179 | + var messageEncoderSettings = useNull ? null : new MessageEncoderSettings(); |
| 180 | + var subject = CreateSubject<BsonDocument>(messageEncoderSettings: messageEncoderSettings); |
| 181 | + |
| 182 | + var result = subject.MessageEncoderSettings; |
| 183 | + |
| 184 | + result.Should().BeSameAs(messageEncoderSettings); |
| 185 | + } |
| 186 | + |
| 187 | + [Test] |
| 188 | + public void ResultSerializer_get_should_return_expected_result() |
| 189 | + { |
| 190 | + var resultSerializer = Substitute.For<IBsonSerializer<BsonDocument>>(); |
| 191 | + var subject = CreateSubject<BsonDocument>(resultSerializer: resultSerializer); |
| 192 | + |
| 193 | + var result = subject.ResultSerializer; |
| 194 | + |
| 195 | + result.Should().BeSameAs(resultSerializer); |
| 196 | + } |
| 197 | + |
| 198 | + // private methods |
| 199 | + private ServerDescription CreateServerDescription(ServerType serverType) |
| 200 | + { |
| 201 | + var endPoint = new DnsEndPoint("localhost", 27017); |
| 202 | + var serverId = new ServerId(new ClusterId(), endPoint); |
| 203 | + return new ServerDescription(serverId, endPoint, type: serverType); |
| 204 | + } |
| 205 | + |
| 206 | + private CommandOperationBase<TCommandResult> CreateSubject<TCommandResult>( |
| 207 | + DatabaseNamespace databaseNamespace = null, |
| 208 | + BsonDocument command = null, |
| 209 | + IBsonSerializer<TCommandResult> resultSerializer = null, |
| 210 | + MessageEncoderSettings messageEncoderSettings = null) |
| 211 | + { |
| 212 | + databaseNamespace = databaseNamespace ?? new DatabaseNamespace("databaseName"); |
| 213 | + command = command ?? new BsonDocument("command", 1); |
| 214 | + resultSerializer = resultSerializer ?? BsonSerializer.LookupSerializer<TCommandResult>(); |
| 215 | + return new FakeCommandOperation<TCommandResult>(databaseNamespace, command, resultSerializer, messageEncoderSettings); |
| 216 | + } |
| 217 | + |
| 218 | + // nested types |
| 219 | + private class FakeCommandOperation<TCommandResult> : CommandOperationBase<TCommandResult> |
| 220 | + { |
| 221 | + public FakeCommandOperation( |
| 222 | + DatabaseNamespace databaseNamespace, |
| 223 | + BsonDocument command, |
| 224 | + IBsonSerializer<TCommandResult> resultSerializer, |
| 225 | + MessageEncoderSettings messageEncoderSettings) |
| 226 | + : base (databaseNamespace, command, resultSerializer, messageEncoderSettings) |
| 227 | + { |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments