using System;
using System.Text.Json;
using Dpz.Core.Service.ObjectStorage;
using Dpz.Core.Service.ObjectStorage.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Dpz.Core.Server.ObjectStorage.Test;

public abstract class Basic
{
    private readonly IServiceProvider _serviceProvider;

    protected Basic()
    {
        var config = new ConfigurationBuilder().AddJsonFile("appsettings.Test.json").Build();
        Configuration = config;
        IServiceCollection services = new ServiceCollection();
        services.AddSingleton<IConfiguration>(_ => config);
        services.AddUpyunObjectStorage();
        services.AddShardCloudFileService();
        services.AddVideoPretreatmentService();
        services.AddVideoSyncHandleService();
        _serviceProvider = services
            .AddLogging(configure => configure.AddConsole())
            .BuildServiceProvider();
    }

    protected IConfiguration Configuration { get; }

    protected IObjectStorageOperation ObjectStorageService =>
        _serviceProvider.GetService<IObjectStorageOperation>()
        ?? throw new NullReferenceException();

    protected IVideoCloudService VideoCloudService =>
        _serviceProvider.GetService<IVideoCloudService>() ?? throw new NullReferenceException();

    protected T GetService<T>() =>
        _serviceProvider.GetService<T>() ?? throw new NullReferenceException();

    protected static string ToJson(object obj)
    {
        return JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
    }
}
评论加载中...