refactor(config): base_url and api_key are required

recyclarr
Robert Dailey 3 years ago
parent 070a961ed0
commit 12ff259c09

@ -69,10 +69,10 @@ namespace Trash.Tests.Config
[Test]
public void LoadMany_CorrectNumberOfIterations()
{
StreamReader MockYaml(params object[] args)
static StreamReader MockYaml(params object[] args)
{
var str = new StringBuilder("sonarr:");
const string templateYaml = "\n - base_url: {0}";
const string templateYaml = "\n - base_url: {0}\n api_key: abc";
str.Append(args.Aggregate("", (current, p) => current + templateYaml.FormatWith(p)));
return StreamBuilder.FromString(str.ToString());
}
@ -99,9 +99,9 @@ namespace Trash.Tests.Config
var expected = new List<SonarrConfiguration>
{
new() {BaseUrl = "1"},
new() {BaseUrl = "2"},
new() {BaseUrl = "3"}
new() {ApiKey = "abc", BaseUrl = "1"},
new() {ApiKey = "abc", BaseUrl = "2"},
new() {ApiKey = "abc", BaseUrl = "3"}
};
var actual = loader.LoadMany(fakeFiles, "sonarr").ToList();

@ -0,0 +1,66 @@
using System;
using System.IO;
using System.IO.Abstractions;
using FluentAssertions;
using JetBrains.Annotations;
using NSubstitute;
using NUnit.Framework;
using Trash.Config;
using YamlDotNet.Core;
using YamlDotNet.Serialization.ObjectFactories;
namespace Trash.Tests.Config
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class ServiceConfigurationTest
{
// This test class must be public otherwise it cannot be deserialized by YamlDotNet
[UsedImplicitly]
public class TestServiceConfiguration : ServiceConfiguration
{
public const string ServiceName = "test_service";
public override string BuildUrl()
{
throw new NotImplementedException();
}
}
[Test]
public void Deserialize_BaseUrlMissing_Throw()
{
const string yaml = @"
test_service:
- api_key: b
";
var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());
Action act = () => loader.LoadFromStream(new StringReader(yaml), TestServiceConfiguration.ServiceName);
act.Should().Throw<YamlException>()
.WithMessage("*Property 'base_url' is required");
}
[Test]
public void Deserialize_ApiKeyMissing_Throw()
{
const string yaml = @"
test_service:
- base_url: a
";
var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());
Action act = () => loader.LoadFromStream(new StringReader(yaml), TestServiceConfiguration.ServiceName);
act.Should().Throw<YamlException>()
.WithMessage("*Property 'api_key' is required");
}
}
}

@ -1,8 +1,13 @@
namespace Trash.Config
using System.ComponentModel.DataAnnotations;
namespace Trash.Config
{
public abstract class ServiceConfiguration
{
[Required(ErrorMessage = "Property 'base_url' is required")]
public string BaseUrl { get; init; } = "";
[Required(ErrorMessage = "Property 'api_key' is required")]
public string ApiKey { get; init; } = "";
public abstract string BuildUrl();

Loading…
Cancel
Save