Пример #1
0
        public async Task CanPublishEventWithBinaryData()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.TopicHost),
                    new AzureKeyCredential(TestEnvironment.TopicKey),
                    options));

            List <EventGridEvent> eventsList = new List <EventGridEvent>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(
                    new EventGridEvent(
                        BinaryData.FromObject(new TestPayload("name", i)),
                        $"Subject-{i}",
                        "Microsoft.MockPublisher.TestEvent",
                        "1.0")
                {
                    Id        = Recording.Random.NewGuid().ToString(),
                    EventTime = Recording.Now
                });
            }

            await client.SendEventsAsync(eventsList);
        }
        public async Task CanCreateBinaryDataFromCustomType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();

            await AssertData(BinaryData.FromObject(payload, serializer));
            await AssertData(await BinaryData.FromObjectAsync(payload, serializer));

            await AssertData(BinaryData.FromObject(payload));
            await AssertData(await BinaryData.FromObjectAsync(payload));

            await AssertData(new BinaryData(payload, type : typeof(TestModel)));
            await AssertData(new BinaryData(payload, null, typeof(TestModel)));
            await AssertData(new BinaryData(payload, serializer, typeof(TestModel)));

            async Task AssertData(BinaryData data)
            {
                Assert.AreEqual(payload.A, data.ToObject <TestModel>(serializer).A);
                Assert.AreEqual(payload.B, data.ToObject <TestModel>(serializer).B);
                Assert.AreEqual(payload.C, data.ToObject <TestModel>(serializer).C);
                Assert.AreEqual(payload.A, (await data.ToObjectAsync <TestModel>(serializer)).A);
                Assert.AreEqual(payload.B, (await data.ToObjectAsync <TestModel>(serializer)).B);
                Assert.AreEqual(payload.C, (await data.ToObjectAsync <TestModel>(serializer)).C);

                Assert.AreEqual(payload.A, data.ToObject <TestModel>().A);
                Assert.AreEqual(payload.B, data.ToObject <TestModel>().B);
                Assert.AreEqual(payload.C, data.ToObject <TestModel>().C);
                Assert.AreEqual(payload.A, (await data.ToObjectAsync <TestModel>()).A);
                Assert.AreEqual(payload.B, (await data.ToObjectAsync <TestModel>()).B);
                Assert.AreEqual(payload.C, (await data.ToObjectAsync <TestModel>()).C);
            }
        }
        public async Task SendJsonBodyMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var client     = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
                var sender     = client.CreateSender(scope.QueueName);
                var serializer = new JsonObjectSerializer();
                var testBody   = new TestBody
                {
                    A = "text",
                    B = 5,
                    C = false
                };
                var body = BinaryData.FromObject(testBody, serializer);
                var msg  = new ServiceBusMessage(body);

                await sender.SendMessageAsync(msg);

                var receiver = client.CreateReceiver(scope.QueueName);
                var received = await receiver.ReceiveMessageAsync();

                var receivedBody = received.Body.ToObject <TestBody>(serializer);
                Assert.AreEqual(testBody.A, receivedBody.A);
                Assert.AreEqual(testBody.B, receivedBody.B);
                Assert.AreEqual(testBody.C, receivedBody.C);
            }
        }
        public void CanSerializeNullData()
        {
            var data = new BinaryData(jsonSerializable: null);

            Assert.IsNull(data.ToObject <object>());
            data = BinaryData.FromObject <object>(null);
            Assert.IsNull(data.ToObject <object>());
        }
        public void FromObjectThrowsOnNullSerializer()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };

            Assert.That(
                () => BinaryData.FromObject(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
            Assert.That(
                async() => await BinaryData.FromObjectAsync(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
        }
        public void ToFromCustomType()
        {
            #region Snippet:BinaryDataToFromCustomModel
            var model = new CustomModel
            {
                A = "some text",
                B = 5,
                C = true
            };

            var data = BinaryData.FromObject(model);
            model = data.ToObject <CustomModel>();
            #endregion
        }
        public void ToObjectThrowsOnNullSerializer()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();
            var data       = BinaryData.FromObject(payload, serializer);

            Assert.That(
                () => data.ToObject <TestModel>(null),
                Throws.InstanceOf <ArgumentNullException>());
            Assert.That(
                async() => await data.ToObjectAsync <TestModel>(null),
                Throws.InstanceOf <ArgumentNullException>());
        }
        public async Task ToObjectThrowsExceptionOnIncompatibleType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };

            AssertData(BinaryData.FromObject(payload));
            AssertData(await BinaryData.FromObjectAsync(payload));

            void AssertData(BinaryData data)
            {
                Assert.That(
                    () => data.ToObject <string>(),
                    Throws.InstanceOf <Exception>());
                Assert.That(
                    async() => await data.ToObjectAsync <string>(),
                    Throws.InstanceOf <Exception>());
            }
        }