public void when_several_payload_values_then_takes_first_ones_as_columns()
        {
            int numberOfAllowedItems = 200;

            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                payloadNames: Enumerable.Range(0, 300).Select(i => "arg" + i),
                payload: Enumerable.Range(0, 300).Select(i => (object)i)));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            for (int i = 0; i < numberOfAllowedItems; i++)
            {
                Assert.IsTrue(dict.ContainsKey("Payload_arg" + i), i.ToString());
                Assert.AreEqual<int>(i, dict["Payload_arg" + i].Int32Value.Value);
            }

            for (int i = numberOfAllowedItems; i < 300; i++)
            {
                Assert.IsFalse(dict.ContainsKey("Payload_arg" + i), i.ToString());
            }

            var deserializedPayloadField = JsonConvert.DeserializeObject<Dictionary<string, object>>(dict["Payload"].StringValue);

            foreach (var payloadItem in entity.Payload)
            {
                Assert.IsTrue(deserializedPayloadField.ContainsKey(payloadItem.Key));
                Assert.AreEqual<int>((int)payloadItem.Value, (int)(long)deserializedPayloadField[payloadItem.Key]);
            }
        }
        public void when_having_big_payload_then_truncates()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: new string[] { "arg1" }, payload: new object[] { new string('a', 500000) }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");
            Assert.IsFalse(dict.ContainsKey("Payload_arg1"));
        }
        public void when_having_big_overall_payloads_then_stores_warning_and_does_not_contain_payload()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: Enumerable.Range(0, 50).Select(i => "arg" + i), payload: Enumerable.Range(0, 50).Select(i => new string('a', 2000))));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");

            Assert.AreEqual(0, dict.Keys.Count(x => x.StartsWith("Payload_")));
        }
        public void when_generating_key_then_prefixes_with_instance_name()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                timestamp: DateTimeOffset.UtcNow))
            {
                InstanceName = "MyInstanceName"
            };

            entity.CreateKey(false, 0);

            StringAssert.StartsWith(entity.RowKey, "MyInstanceName");
        }
        public void when_having_big_message_value_then_truncates()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(formattedMessage: new string('a', 500000)));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual(new string('a', 30000) + "--TRUNCATED--", dict["Message"].StringValue);
        }
        public void when_writing_entity_adds_payload_to_dictionary()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: new string[] { "message1", "message2" }, payload: new object[] { "value1", "value2" }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.IsTrue(dict.ContainsKey("Payload_message1"));
            Assert.IsTrue(dict.ContainsKey("Payload_message2"));
        }
        public void when_writing_entity_keeps_proper_type()
        {
            var guid = Guid.NewGuid();
            var binary = new byte[] { 1, 2, 3, 4 };

            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                payloadNames: new string[] { "string1", "int1", "long1", "double1", "bool1", "bool2", "guid1", "binary1" },
                payload: new object[] { "This is a string", 123456, 123456L, 123456D, true, false, guid, binary }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual<string>("This is a string", dict["Payload_string1"].StringValue);
            Assert.AreEqual<int>(123456, dict["Payload_int1"].Int32Value.Value);
            Assert.AreEqual<long>(123456L, dict["Payload_long1"].Int64Value.Value);
            Assert.AreEqual<double>(123456D, dict["Payload_double1"].DoubleValue.Value);
            Assert.AreEqual<Guid>(guid, dict["Payload_guid1"].GuidValue.Value);
            Assert.AreEqual<bool>(true, dict["Payload_bool1"].BooleanValue.Value);
            Assert.AreEqual<bool>(false, dict["Payload_bool2"].BooleanValue.Value);
            Assert.AreEqual<byte[]>(binary, dict["Payload_binary1"].BinaryValue);
        }
        public void when_having_big_overall_payloads_then_stores_warning_and_does_not_contain_payload()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);

            for (int i = 0; i < 50; i++)
            {
                entity.Payload.Add("arg" + i, new string('a', 2000));
            }

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");

            Assert.AreEqual(0, dict.Keys.Count(x => x.StartsWith("Payload_")));
        }
        public void when_having_big_payload_then_truncates()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("arg1", new string('a', 500000));

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");
            Assert.IsFalse(dict.ContainsKey("Payload_arg1"));
        }
        public void when_generating_key_then_prefixes_with_instance_name()
        {
            var entity = new CloudEventEntry();
            entity.EventDate = DateTime.UtcNow;
            entity.InstanceName = "MyInstanceName";

            entity.CreateKey(false, 0);

            StringAssert.StartsWith(entity.RowKey, "MyInstanceName");
        }
        public void when_writing_entity_keeps_proper_type()
        {
            var guid = Guid.NewGuid();
            var binary = new byte[] { 1, 2, 3, 4 };

            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("string1", "This is a string");
            entity.Payload.Add("int1", 123456);
            entity.Payload.Add("long1", 123456L);
            entity.Payload.Add("double1", 123456D);
            entity.Payload.Add("bool1", true);
            entity.Payload.Add("bool2", false);
            entity.Payload.Add("guid1", guid);
            entity.Payload.Add("binary1", binary);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual<string>("This is a string", dict["Payload_string1"].StringValue);
            Assert.AreEqual<int>(123456, dict["Payload_int1"].Int32Value.Value);
            Assert.AreEqual<long>(123456L, dict["Payload_long1"].Int64Value.Value);
            Assert.AreEqual<double>(123456D, dict["Payload_double1"].DoubleValue.Value);
            Assert.AreEqual<Guid>(guid, dict["Payload_guid1"].GuidValue.Value);
            Assert.AreEqual<bool>(true, dict["Payload_bool1"].BooleanValue.Value);
            Assert.AreEqual<bool>(false, dict["Payload_bool2"].BooleanValue.Value);
            Assert.AreEqual<byte[]>(binary, dict["Payload_binary1"].BinaryValue);
        }
        public void when_writing_entity_adds_payload_to_dictionary()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("message1", "value1");
            entity.Payload.Add("message2", "value2");

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.IsTrue(dict.ContainsKey("Payload_message1"));
            Assert.IsTrue(dict.ContainsKey("Payload_message2"));
        }