// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.Extensions.DependencyInjection;

public class XmlDataContractSerializerMvcOptionsSetupTest
{
    [Fact]
    public void AddsFormatterMapping()
    {
        // Arrange
        var optionsSetup = new XmlDataContractSerializerMvcOptionsSetup(NullLoggerFactory.Instance);
        var options = new MvcOptions();

        // Act
        optionsSetup.Configure(options);

        // Assert
        var mappedContentType = options.FormatterMappings.GetMediaTypeMappingForFormat("xml");
        Assert.Equal("application/xml", mappedContentType);
    }

    [Fact]
    public void DoesNotOverrideExistingMapping()
    {
        // Arrange
        var optionsSetup = new XmlDataContractSerializerMvcOptionsSetup(NullLoggerFactory.Instance);
        var options = new MvcOptions();
        options.FormatterMappings.SetMediaTypeMappingForFormat("xml", "text/xml");

        // Act
        optionsSetup.Configure(options);

        // Assert
        var mappedContentType = options.FormatterMappings.GetMediaTypeMappingForFormat("xml");
        Assert.Equal("text/xml", mappedContentType);
    }

    [Fact]
    public void AddsInputFormatter()
    {
        // Arrange
        var optionsSetup = new XmlDataContractSerializerMvcOptionsSetup(NullLoggerFactory.Instance);
        var options = new MvcOptions();

        // Act
        optionsSetup.Configure(options);

        // Assert
        Assert.IsType<XmlDataContractSerializerInputFormatter>(Assert.Single(options.InputFormatters));
    }

    [Fact]
    public void AddsOutputFormatter()
    {
        // Arrange
        var optionsSetup = new XmlDataContractSerializerMvcOptionsSetup(NullLoggerFactory.Instance);
        var options = new MvcOptions();

        // Act
        optionsSetup.Configure(options);

        // Assert
        Assert.IsType<XmlDataContractSerializerOutputFormatter>(Assert.Single(options.OutputFormatters));
    }
}
