public async Task ThenItShouldSerializeReturnSerializedObjectIfValidForSchema(BasicObject data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();

            schemaRegistryClientMock.Setup(c => c.ListSchemaVersionsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { 1, 2, 3 });
            schemaRegistryClientMock.Setup(c => c.GetSchemaAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new SchemaDetails
            {
                Id         = 123,
                Version    = 3,
                SchemaType = "JSON",
                Subject    = "some-test-subject",
                Schema     = DefaultBasicObjectSchemaJson,
            });
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            var actual = await serializer.SerializeAsync(data, GetContext());

            var expected = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(data,
                                                                           new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }));

            Assert.AreEqual(expected, actual);
        }
        public async Task ThenItShouldGetSchemaFromRegistry(BasicObject data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();

            schemaRegistryClientMock.Setup(c => c.ListSchemaVersionsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { 1, 2, 3 });
            schemaRegistryClientMock.Setup(c => c.GetSchemaAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new SchemaDetails
            {
                Id         = 123,
                Version    = 3,
                SchemaType = "JSON",
                Subject    = "some-test-subject",
                Schema     = DefaultBasicObjectSchemaJson,
            });
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            await serializer.SerializeAsync(data, GetContext());

            const string expectedSubjectName = "test-topic-value";

            schemaRegistryClientMock.Verify(c => c.ListSchemaVersionsAsync(expectedSubjectName, It.IsAny <CancellationToken>()),
                                            Times.Once);
            schemaRegistryClientMock.Verify(c => c.GetSchemaAsync(expectedSubjectName, 3, It.IsAny <CancellationToken>()),
                                            Times.Once);
        }
        public async Task ThenItShouldSerializeADateTimeCorrectly(DateTime data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();
            var serializer = new KafkaJsonSerializer <DateTime>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            var actual = await serializer.SerializeAsync(data, GetContext());

            var expected = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(data));

            Assert.AreEqual(expected, actual);
        }
        public async Task ThenItShouldSerializeAnObjectCorrectly(BasicObject data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            var actual = await serializer.SerializeAsync(data, GetContext());

            var expected = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(data, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }));

            Assert.AreEqual(expected, actual);
        }
        public async Task ThenItShouldNotGetSchemaFromRegistryIfNoVersions(BasicObject data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            await serializer.SerializeAsync(data, GetContext());

            const string expectedSubjectName = "test-topic-value";

            schemaRegistryClientMock.Verify(c => c.ListSchemaVersionsAsync(expectedSubjectName, It.IsAny <CancellationToken>()),
                                            Times.Once);
            schemaRegistryClientMock.Verify(c => c.GetSchemaAsync(expectedSubjectName, 3, It.IsAny <CancellationToken>()),
                                            Times.Never);
        }
        public void ThenItShouldThrowExceptionIfDataNotValidForSchema()
        {
            var jsonSchema = @"{
                ""$schema"": ""http://json-schema.org/draft-04/schema#"",
                ""title"": ""BasicObject"",
                ""type"": ""object"",
                ""additionalProperties"": false,
                ""properties"": {
                    ""name"": {
                        ""type"": ""boolean""
                    },
                    ""timeStamp"": {
                        ""type"": ""string"",
                        ""format"": ""date-time""
                    }
                }
            }";
            var data       = new BasicObject
            {
                Id        = 99,
                Name      = "test 123",
                TimeStamp = new DateTime(2021, 1, 25, 10, 17, 32),
            };

            var schemaRegistryClientMock = GetSchemaRegistryClientMock();

            schemaRegistryClientMock.Setup(c => c.ListSchemaVersionsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { 1, 2, 3 });
            schemaRegistryClientMock.Setup(c => c.GetSchemaAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new SchemaDetails
            {
                Id         = 123,
                Version    = 3,
                SchemaType = "JSON",
                Subject    = "some-test-subject",
                Schema     = jsonSchema,
            });
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            var actual = Assert.ThrowsAsync <KafkaJsonSchemaSerializationException>(async() =>
                                                                                    await serializer.SerializeAsync(data, GetContext()));

            Assert.AreEqual("Value did not conform to schema. 2 errors found (See ValidationErrors for more details)", actual.Message);
            Assert.IsNotNull(actual.ValidationErrors);
            Assert.AreEqual(2, actual.ValidationErrors.Length);
        }
        public void ThenItShouldThrowExceptionIfSchemaIsNotJson(BasicObject data)
        {
            var schemaRegistryClientMock = GetSchemaRegistryClientMock();

            schemaRegistryClientMock.Setup(c => c.ListSchemaVersionsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { 1, 2, 3 });
            schemaRegistryClientMock.Setup(c => c.GetSchemaAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new SchemaDetails
            {
                Id         = 123,
                Version    = 3,
                SchemaType = "AVRO",
                Subject    = "some-test-subject",
            });
            var serializer = new KafkaJsonSerializer <BasicObject>(schemaRegistryClientMock.Object, DefaultJsonSerializerOptions);

            var actual = Assert.ThrowsAsync <KafkaSerializationException>(async() =>
                                                                          await serializer.SerializeAsync(data, GetContext()));

            Assert.AreEqual("Unable to verify schema for subject test-topic-value, version 3, as the schema is AVRO but expected JSON", actual.Message);
        }