diff --git a/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettings.cs b/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettings.cs
deleted file mode 100644
index 3f388a4..0000000
--- a/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettings.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using Liquid.Core.Settings;
-
-namespace Liquid.Repository.Mongo.Configuration
-{
- ///
- /// Interface for MongoDB repository data entity settings.
- ///
- public interface IMongoEntitySettings
- {
- ///
- /// Gets or sets the name of the collection where an entity is persisted.
- ///
- ///
- /// The name of the collection.
- ///
- string CollectionName { get; set; }
-
- ///
- /// Gets or sets the partition (shard) key.
- ///
- ///
- /// The partition (shard) key.
- ///
- string ShardKey { get; set; }
-
- ///
- /// Gets or sets the name of the configuration section where the Mongo DB settings are configured.
- ///
- ///
- /// The name of the Database settings configuration section related to one or more entities.
- ///
- string DatabaseSettingsSectionName { get; set; }
-
- ///
- /// Gets or sets the Mongo DB database settings.
- ///
- ///
- /// The database settings.
- ///
- DatabaseSettings DatabaseSettings { get; set; }
- }
-}
\ No newline at end of file
diff --git a/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettingsFactory.cs b/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettingsFactory.cs
deleted file mode 100644
index 9d24d54..0000000
--- a/src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettingsFactory.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using Liquid.Core.Exceptions;
-using Liquid.Repository.Mongo.Exceptions;
-using System;
-
-namespace Liquid.Repository.Mongo.Configuration
-{
- ///
- /// Mongo Db Entity settings factory interface.
- ///
- public interface IMongoEntitySettingsFactory
- {
- ///
- /// Gets or creates an instance of using the name of an Entity.
- ///
- /// The name of an specific Entity.
- ///
- /// entityName
- ///
- ///
- /// When the configuration section name for the entity isn't found or malformed.
- ///
- ///
- /// When the configuration section name for the database referenced on the entity settings isn't found or malformed.
- ///
- MongoEntitySettings GetSettings(string entityName);
-
- ///
- /// Gets or creates an instance of using the type name of an Entity.
- ///
- /// The type of an specific Entity.
- MongoEntitySettings GetSettings();
- }
-}
\ No newline at end of file
diff --git a/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettings.cs b/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettings.cs
index ffef15e..6bab5af 100644
--- a/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettings.cs
+++ b/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettings.cs
@@ -1,13 +1,25 @@
using Liquid.Core.Settings;
+using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Liquid.Repository.Mongo.Configuration
{
+ ///
+ /// Properties set list of service bus configurations.
+ ///
+ public class MongoDbSettings
+ {
+ ///
+ /// Properties set list of service bus configurations.
+ ///
+ public List Settings { get; set; }
+ }
+
///
/// MongoDB repository data entity settings.
///
[ExcludeFromCodeCoverage]
- public class MongoEntitySettings : IMongoEntitySettings
+ public class MongoEntitySettings
{
///
public string CollectionName { get; set; }
@@ -15,10 +27,20 @@ public class MongoEntitySettings : IMongoEntitySettings
///
public string ShardKey { get; set; }
- ///
- public string DatabaseSettingsSectionName { get; set; }
+ ///
+ /// Gets or sets the database connection string.
+ ///
+ ///
+ /// The database connection string.
+ ///
+ public string ConnectionString { get; set; }
- ///
- public DatabaseSettings DatabaseSettings { get; set; }
+ ///
+ /// Gets or sets the name of the database.
+ ///
+ ///
+ /// The name of the database.
+ ///
+ public string DatabaseName { get; set; }
}
}
diff --git a/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettingsFactory.cs b/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettingsFactory.cs
deleted file mode 100644
index 0378169..0000000
--- a/src/Liquid.Repository.Mongo/Configuration/MongoEntitySettingsFactory.cs
+++ /dev/null
@@ -1,79 +0,0 @@
-using Liquid.Core.Exceptions;
-using Liquid.Core.Settings;
-using Liquid.Repository.Mongo.Exceptions;
-using Microsoft.Extensions.Configuration;
-using System;
-
-namespace Liquid.Repository.Mongo.Configuration
-{
- ///
- /// Mongo Db Entity settings factory implementation.
- ///
- public class MongoEntitySettingsFactory : IMongoEntitySettingsFactory
- {
- private readonly IConfiguration _repositorySettings;
- private readonly string _entitiesConfigurationRootSectionName = "Liquid:RepositorySettings:Entities:{0}";
-
- ///
- /// Mongo Db Entity settings factory constructor.
- ///
- /// The configuration used to create .
- /// Name of the configuration section where all entities have their repository settings configured. Default: "Liquid:RepositorySettings:Entities".
- ///
- /// repositorySettings
- ///
- public MongoEntitySettingsFactory(IConfiguration repositorySettings, string entitiesConfigurationRootSectionName = "Liquid:RepositorySettings:Entities")
- {
- if (repositorySettings is null) throw new ArgumentNullException(nameof(repositorySettings));
- if (!string.IsNullOrEmpty(entitiesConfigurationRootSectionName)) _entitiesConfigurationRootSectionName = entitiesConfigurationRootSectionName;
-
- if (!_entitiesConfigurationRootSectionName.EndsWith(":{0}")) _entitiesConfigurationRootSectionName = string.Concat(_entitiesConfigurationRootSectionName, ":{0}");
-
- _repositorySettings = repositorySettings;
- }
-
- ///
- public MongoEntitySettings GetSettings()
- {
- return GetSettings(typeof(TEntity).Name);
- }
-
- ///
- public MongoEntitySettings GetSettings(string entityName)
- {
- if (string.IsNullOrEmpty(entityName)) throw new ArgumentNullException(nameof(entityName));
-
- var entityConfigurationSectionName = string.Format(_entitiesConfigurationRootSectionName, entityName);
-
- // MongoEntitySettings will be retrieved from the configuration providers using the configuration section name
- var entitySettings = _repositorySettings.GetSection(entityConfigurationSectionName)?.Get();
- if (entitySettings is null) throw new MongoEntitySettingsDoesNotExistException(entityName);
-
- // DatabaseSettings will be retrieved from the configuration providers using the configuration section name configured for an Entity
- // or the default configuration section name. See GetDefaultDatabaseConfigurationSectionName() private method below.
- var databaseConfigurationSectionName = entitySettings.DatabaseSettingsSectionName ?? GetDefaultDatabaseConfigurationSectionName();
- entitySettings.DatabaseSettings = _repositorySettings.GetSection(databaseConfigurationSectionName)?.Get();
- if (entitySettings.DatabaseSettings is null) throw new LiquidDatabaseSettingsDoesNotExistException(databaseConfigurationSectionName);
-
- return entitySettings;
- }
-
- ///
- /// Returns a section name, based on _entitiesConfigurationRootSectionName, trying to going up two levels and concatenating ":DefaultDatabaseSettings".
- /// Samples:
- /// if _entitiesConfigurationRootSectionName = "Liquid:RepositorySettings:Entities:{0}" it will return "Liquid:RepositorySettings:DefaultDatabaseSettings"
- /// if _entitiesConfigurationRootSectionName = "MyRepository:Entities:{0}" it will return "MyRepository:DefaultDatabaseSettings"
- /// if _entitiesConfigurationRootSectionName = "MyEntities:{0}" it will return "DefaultDatabaseSettings"
- ///
- private string GetDefaultDatabaseConfigurationSectionName()
- {
- var sectionElements = _entitiesConfigurationRootSectionName.Split(':');
-
- if (sectionElements.Length < 3)
- return "DefaultDatabaseSettings";
-
- Array.Resize(ref sectionElements, sectionElements.Length - 2);
- return string.Concat(string.Join(':', sectionElements), ":DefaultDatabaseSettings");
- }
- }
-}
diff --git a/src/Liquid.Repository.Mongo/Exceptions/MongoEntitySettingsDoesNotExistException.cs b/src/Liquid.Repository.Mongo/Exceptions/MongoEntitySettingsDoesNotExistException.cs
index 9543c90..328898e 100644
--- a/src/Liquid.Repository.Mongo/Exceptions/MongoEntitySettingsDoesNotExistException.cs
+++ b/src/Liquid.Repository.Mongo/Exceptions/MongoEntitySettingsDoesNotExistException.cs
@@ -25,10 +25,5 @@ public MongoEntitySettingsDoesNotExistException(string entityName)
public MongoEntitySettingsDoesNotExistException(string message, Exception innerException) : base(message, innerException)
{
}
-
- ///
- protected MongoEntitySettingsDoesNotExistException(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }
}
}
\ No newline at end of file
diff --git a/src/Liquid.Repository.Mongo/Exceptions/MongoException.cs b/src/Liquid.Repository.Mongo/Exceptions/MongoException.cs
index 2507f26..8a8f977 100644
--- a/src/Liquid.Repository.Mongo/Exceptions/MongoException.cs
+++ b/src/Liquid.Repository.Mongo/Exceptions/MongoException.cs
@@ -40,9 +40,5 @@ public MongoException(string message, Exception innerException) : base(message,
{
}
- ///
- protected MongoException(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- }
}
}
diff --git a/src/Liquid.Repository.Mongo/Extensions/IServiceCollectionExtensions.cs b/src/Liquid.Repository.Mongo/Extensions/IServiceCollectionExtensions.cs
index d81ab81..57610cc 100644
--- a/src/Liquid.Repository.Mongo/Extensions/IServiceCollectionExtensions.cs
+++ b/src/Liquid.Repository.Mongo/Extensions/IServiceCollectionExtensions.cs
@@ -3,6 +3,7 @@
using Liquid.Core.Implementations;
using Liquid.Core.Interfaces;
using Liquid.Repository.Mongo.Configuration;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -21,15 +22,24 @@ public static class IServiceCollectionExtensions
/// Type of entity that the repository should correspond to
/// Entity identifier type.
/// Extended ServiceCollection object.
- /// Name of the configuration section where all entities have their repository settings configured. Default: "Liquid:RepositorySettings:Entities".
+ /// Name of the configuration section where all entities have their repository settings configured.
+ /// Name of the collection in the database that the repository should correspond to.
/// Specifies whether the telemetry should be activated or not for this repository. Default: True.
- public static IServiceCollection AddLiquidMongoRepository(this IServiceCollection services, string entitiesConfigurationRootSectionName = "Liquid:RepositorySettings:Entities", bool activateTelemetry = true)
+ public static IServiceCollection AddLiquidMongoRepository(this IServiceCollection services, string sectionName, string collectionName, bool activateTelemetry = true)
where TEntity : LiquidEntity, new()
{
services.TryAddSingleton();
- services.TryAddSingleton(provider => { return ActivatorUtilities.CreateInstance(provider, entitiesConfigurationRootSectionName); });
- services.AddScoped, MongoDataContext>();
+ services.AddOptions()
+ .Configure((settings, configuration) =>
+ {
+ configuration.GetSection(sectionName).Bind(settings);
+ });
+
+ services.AddScoped>((provider) =>
+ {
+ return ActivatorUtilities.CreateInstance>(provider, collectionName);
+ });
if (activateTelemetry)
diff --git a/src/Liquid.Repository.Mongo/IMongoClientFactory.cs b/src/Liquid.Repository.Mongo/IMongoClientFactory.cs
index e88a488..422d31b 100644
--- a/src/Liquid.Repository.Mongo/IMongoClientFactory.cs
+++ b/src/Liquid.Repository.Mongo/IMongoClientFactory.cs
@@ -1,4 +1,5 @@
using Liquid.Core.Settings;
+using Liquid.Repository.Mongo.Configuration;
using MongoDB.Driver;
namespace Liquid.Repository.Mongo
@@ -11,7 +12,8 @@ public interface IMongoClientFactory
///
/// Provide a new instance of with db conection started.
///
- /// Database settings used to create unique clients based on DatabaseSettings hash code.
- IMongoClient GetClient(DatabaseSettings databaseSettings);
+ ///
+ ///
+ IMongoClient GetClient(string collectionName, out MongoEntitySettings settings);
}
}
diff --git a/src/Liquid.Repository.Mongo/Liquid.Repository.Mongo.csproj b/src/Liquid.Repository.Mongo/Liquid.Repository.Mongo.csproj
index 1a1526e..344cdd2 100644
--- a/src/Liquid.Repository.Mongo/Liquid.Repository.Mongo.csproj
+++ b/src/Liquid.Repository.Mongo/Liquid.Repository.Mongo.csproj
@@ -1,7 +1,7 @@
- net6.0
+ net8.0
Liquid.Repository.Mongo
MIT
Avanade Brazil
@@ -10,7 +10,7 @@
Avanade 2019
https://github.com/Avanade/Liquid-Application-Framework
logo.png
- 6.0.0
+ 8.0.0-alpha-01
true
true
Full
@@ -25,7 +25,9 @@
-
+
+
+
@@ -35,8 +37,4 @@
-
-
-
-
diff --git a/src/Liquid.Repository.Mongo/MongoClientFactory.cs b/src/Liquid.Repository.Mongo/MongoClientFactory.cs
index a8f7822..eea40f4 100644
--- a/src/Liquid.Repository.Mongo/MongoClientFactory.cs
+++ b/src/Liquid.Repository.Mongo/MongoClientFactory.cs
@@ -1,39 +1,48 @@
using Liquid.Core.Settings;
+using Liquid.Repository.Mongo.Configuration;
+using Liquid.Repository.Mongo.Exceptions;
+using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
+using System.Linq;
namespace Liquid.Repository.Mongo
{
///
public class MongoClientFactory : IMongoClientFactory
{
- private readonly IDictionary _mongoClients;
+ private readonly IOptions _settings;
+ private readonly IDictionary _mongoClients;
///
/// Initializes a new instance of the class.
///
- public MongoClientFactory()
+ public MongoClientFactory(IOptions settings)
{
- _mongoClients = new Dictionary();
+ _mongoClients = new Dictionary();
+ _settings = settings;
}
///
- public IMongoClient GetClient(DatabaseSettings databaseSettings)
+ public IMongoClient GetClient(string collectionName, out MongoEntitySettings settings)
{
- if (databaseSettings is null) throw new ArgumentNullException(nameof(databaseSettings));
+ if (collectionName is null) throw new ArgumentNullException(nameof(collectionName));
+ settings = _settings.Value.Settings.FirstOrDefault(x => x.CollectionName == collectionName);
+
+ if (settings is null) throw new MongoEntitySettingsDoesNotExistException(collectionName);
// Try to get from the created clients collection, otherwise creates a new client
- IMongoClient mongoClient = _mongoClients.TryGetValue(databaseSettings.GetHashCode(), out mongoClient) ? mongoClient : CreateClient(databaseSettings);
+ IMongoClient mongoClient = _mongoClients.TryGetValue(collectionName, out mongoClient) ? mongoClient : CreateClient(settings);
return mongoClient;
}
- private IMongoClient CreateClient(DatabaseSettings databaseSettings)
+ private IMongoClient CreateClient(MongoEntitySettings databaseSettings)
{
var mongoClient = new MongoClient(databaseSettings.ConnectionString);
- _mongoClients.Add(databaseSettings.GetHashCode(), mongoClient);
+ _mongoClients.Add(databaseSettings.CollectionName, mongoClient);
return mongoClient;
}
diff --git a/src/Liquid.Repository.Mongo/MongoDataContext.cs b/src/Liquid.Repository.Mongo/MongoDataContext.cs
index 343aa6b..78aa795 100644
--- a/src/Liquid.Repository.Mongo/MongoDataContext.cs
+++ b/src/Liquid.Repository.Mongo/MongoDataContext.cs
@@ -1,5 +1,6 @@
using Liquid.Repository.Mongo.Configuration;
using Liquid.Repository.Mongo.Exceptions;
+using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Threading.Tasks;
@@ -56,24 +57,22 @@ public class MongoDataContext : IMongoDataContext
/// Initializes a new instance of the class.
///
/// Mongo client generator.
- /// Mongo DB settings generator.
+ /// ///
///
/// clientProvider
/// or
/// settingsFactory
///
- public MongoDataContext(IMongoClientFactory clientProvider, IMongoEntitySettingsFactory settingsFactory)
+ public MongoDataContext(IMongoClientFactory clientProvider, string collectionName)
{
if (clientProvider is null) throw new ArgumentNullException(nameof(clientProvider));
- if (settingsFactory is null) throw new ArgumentNullException(nameof(settingsFactory));
-
- _settings = settingsFactory.GetSettings();
+ if (collectionName is null) throw new ArgumentNullException(nameof(collectionName));
+
+ _mongoClient = clientProvider.GetClient(collectionName, out _settings);
if (_settings is null) throw new MongoEntitySettingsDoesNotExistException(nameof(TEntity));
- _mongoClient = clientProvider.GetClient(_settings.DatabaseSettings);
-
- SetDatabase(_settings.DatabaseSettings.DatabaseName);
+ SetDatabase(_settings.DatabaseName);
}
///
diff --git a/src/Liquid.Repository.Mongo/MongoRepository.cs b/src/Liquid.Repository.Mongo/MongoRepository.cs
index 2fd54e0..8d90020 100644
--- a/src/Liquid.Repository.Mongo/MongoRepository.cs
+++ b/src/Liquid.Repository.Mongo/MongoRepository.cs
@@ -42,9 +42,7 @@ public MongoRepository(IMongoDataContext dataContext)
{
MongoDataContext = dataContext ?? throw new ArgumentNullException(nameof(dataContext));
- _settings = dataContext.Settings;
-
- MongoDataContext.SetDatabase(_settings.DatabaseSettings.DatabaseName);
+ _settings = dataContext.Settings;
}
///
diff --git a/src/Liquid.WebApi.Http/Liquid.WebApi.Http.csproj b/src/Liquid.WebApi.Http/Liquid.WebApi.Http.csproj
index 95542b7..72b7d6c 100644
--- a/src/Liquid.WebApi.Http/Liquid.WebApi.Http.csproj
+++ b/src/Liquid.WebApi.Http/Liquid.WebApi.Http.csproj
@@ -10,7 +10,7 @@
Avanade 2019
https://github.com/Avanade/Liquid-Application-Framework
logo.png
- 8.0.0-alpha-01
+ 8.0.0-alpha-02
true
true
Full
@@ -23,7 +23,7 @@
-
+
diff --git a/test/Liquid.Repository.Mongo.Tests/IServiceCollectionExtensionsTests.cs b/test/Liquid.Repository.Mongo.Tests/IServiceCollectionExtensionsTests.cs
index 9407702..c3ca485 100644
--- a/test/Liquid.Repository.Mongo.Tests/IServiceCollectionExtensionsTests.cs
+++ b/test/Liquid.Repository.Mongo.Tests/IServiceCollectionExtensionsTests.cs
@@ -6,19 +6,17 @@
using Microsoft.Extensions.Logging;
using EphemeralMongo;
using NSubstitute;
-using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Liquid.Core.Interfaces;
+using Xunit;
namespace Liquid.Repository.Mongo.Tests
{
[ExcludeFromCodeCoverage]
public class IServiceCollectionExtensionsTests
{
- internal const string _databaseConfigurationSectionName = "MyMongoDbSettings";
- internal const string _entityConfigurationSectionName = "MyMongoEntityOptions:Entities";
internal const string _databaseName = "TestDatabase";
private IServiceCollection _services;
@@ -26,8 +24,7 @@ public class IServiceCollectionExtensionsTests
private IConfiguration _configuration;
private IMongoRunner _runner;
- [SetUp]
- public void Setup()
+ public IServiceCollectionExtensionsTests()
{
var options = new MongoRunnerOptions
{
@@ -41,48 +38,36 @@ public void Setup()
_services = new ServiceCollection();
_services.AddSingleton(Substitute.For>());
-
- var mongoDatabaseConfiguration = new Dictionary
- {
- {$"{_databaseConfigurationSectionName}:{_databaseName}:DatabaseName", _databaseName},
- {$"{_databaseConfigurationSectionName}:{_databaseName}:ConnectionString", _runner.ConnectionString},
- };
+
var mongoEntityConfiguration = new Dictionary
{
- {"MyMongoEntityOptions:DefaultDatabaseSettings:DatabaseName", _databaseName},
- {"MyMongoEntityOptions:DefaultDatabaseSettings:ConnectionString", _runner.ConnectionString},
- {$"{_entityConfigurationSectionName}:TestEntity:CollectionName", "TestEntities"},
- {$"{_entityConfigurationSectionName}:TestEntity:ShardKey", "id"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:CollectionName", "AnotherTestEntities"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:ShardKey", "id"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:DatabaseSettingsSectionName", $"{_databaseConfigurationSectionName}:{_databaseName}"}
+ {"MyMongoEntityOptions:Settings:1:DatabaseName", _databaseName},
+ {"MyMongoEntityOptions:Settings:1:ConnectionString", _runner.ConnectionString},
+ {"MyMongoEntityOptions:Settings:1:CollectionName", "TestEntity"},
+ {"MyMongoEntityOptions:Settings:1:ShardKey", "id"},
+ {"MyMongoEntityOptions:Settings:2:DatabaseName", _databaseName},
+ {"MyMongoEntityOptions:Settings:2:ConnectionString", _runner.ConnectionString},
+ {"MyMongoEntityOptions:Settings:2:CollectionName", "AnotherTestEntity"},
+ {"MyMongoEntityOptions:Settings:2:ShardKey", "id"},
};
_configuration = new ConfigurationBuilder()
- .AddInMemoryCollection(mongoDatabaseConfiguration)
.AddInMemoryCollection(mongoEntityConfiguration).Build();
_services.AddSingleton(_configuration);
}
- [TearDown]
- public void DisposeResources()
- {
- _configuration = null;
- _serviceProvider = null;
- _services = null;
- _runner.Dispose();
- _runner = null;
- }
-
- [Test]
+ [Fact]
public void AddLiquidMongoRepository_WhenAdded_ServicesIsFilledForTestEntity()
{
- _services.AddLiquidMongoRepository(_entityConfigurationSectionName);
+ _services.AddLiquidMongoRepository("MyMongoEntityOptions","TestEntity");
+ _services.AddLiquidMongoRepository("MyMongoEntityOptions", "AnotherTestEntity");
_serviceProvider = _services.BuildServiceProvider();
- Assert.IsNotNull(_serviceProvider.GetService>());
- Assert.IsNotNull(_serviceProvider.GetService>());
+ Assert.NotNull(_serviceProvider.GetService>());
+ Assert.NotNull(_serviceProvider.GetService>());
+ Assert.NotNull(_serviceProvider.GetService>());
+ Assert.NotNull(_serviceProvider.GetService>());
}
}
diff --git a/test/Liquid.Repository.Mongo.Tests/Liquid.Repository.Mongo.Tests.csproj b/test/Liquid.Repository.Mongo.Tests/Liquid.Repository.Mongo.Tests.csproj
index 98c28b5..e3349cc 100644
--- a/test/Liquid.Repository.Mongo.Tests/Liquid.Repository.Mongo.Tests.csproj
+++ b/test/Liquid.Repository.Mongo.Tests/Liquid.Repository.Mongo.Tests.csproj
@@ -1,29 +1,32 @@
- net6.0
+ net8.0
false
-
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
-
+
-
+
-
-
-
-
+
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
diff --git a/test/Liquid.Repository.Mongo.Tests/MongoClientFactoryTests.cs b/test/Liquid.Repository.Mongo.Tests/MongoClientFactoryTests.cs
index 6e2afc3..1c64b81 100644
--- a/test/Liquid.Repository.Mongo.Tests/MongoClientFactoryTests.cs
+++ b/test/Liquid.Repository.Mongo.Tests/MongoClientFactoryTests.cs
@@ -1,8 +1,11 @@
using EphemeralMongo;
using Liquid.Core.Settings;
-using NUnit.Framework;
+using Liquid.Repository.Mongo.Configuration;
+using Microsoft.Extensions.Options;
+using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
+using Xunit;
namespace Liquid.Repository.Mongo.Tests
{
@@ -12,11 +15,8 @@ public class MongoClientFactoryTests
private IMongoClientFactory _sut;
internal static IMongoRunner _runner;
internal const string _databaseName = "TestDatabase";
- private DatabaseSettings _correctDatabaseSettings;
- private DatabaseSettings _wrongDatabaseSettings;
-
- [SetUp]
- protected void SetContext()
+ private IOptions _options;
+ public MongoClientFactoryTests()
{
var options = new MongoRunnerOptions
{
@@ -26,48 +26,57 @@ protected void SetContext()
_runner = MongoRunner.Run(options);
- _correctDatabaseSettings = new DatabaseSettings()
- {
- DatabaseName = _databaseName,
- ConnectionString = _runner.ConnectionString
- };
- _wrongDatabaseSettings = new DatabaseSettings()
+ _options = Substitute.For>();
+
+ var settings = new MongoDbSettings()
{
- DatabaseName = $"{_databaseName}-2",
- ConnectionString = "incorrect connection string"
+ Settings = new System.Collections.Generic.List()
+ {
+ new MongoEntitySettings()
+ {
+ CollectionName = "TestEntities",
+ ShardKey = "id",
+ ConnectionString = _runner.ConnectionString,
+ DatabaseName = _databaseName
+ },
+ new MongoEntitySettings()
+ {
+ CollectionName = "TestEntities2",
+ ShardKey = "id",
+ ConnectionString = "incorrect connection string",
+ DatabaseName = $"{_databaseName}-2"
+ }
+ }
};
- _sut = new MongoClientFactory();
- }
+ _options.Value.Returns(settings);
-
- [TearDown]
- public void DisposeResources()
- {
- _correctDatabaseSettings = null;
- _sut = null;
- _runner.Dispose();
- _runner = null;
+ _sut = new MongoClientFactory(_options);
}
- [Test]
+
+ [Fact]
public void MongoClientFactory_WhenSettingsIsNull_ThrowException()
{
- Assert.Throws(() => _sut.GetClient(null));
+ MongoEntitySettings settings = null;
+ Assert.Throws(() => _sut.GetClient(null, out settings));
}
- [Test]
+ [Fact]
public void GetClient_WhenDatabaseIdsExists_ClientCreated()
{
- var result = _sut.GetClient(_correctDatabaseSettings);
- Assert.IsNotNull(result.GetDatabase(_databaseName));
+ MongoEntitySettings settings = null;
+ var result = _sut.GetClient("TestEntities", out settings);
+
+ Assert.NotNull(result);
}
- [Test]
+ [Fact]
public void GetClient_WhenDatabaseSettingsIsWrong_ThrowException()
{
- Assert.Throws(() => _sut.GetClient(_wrongDatabaseSettings));
+ MongoEntitySettings settings = null;
+ Assert.Throws(() => _sut.GetClient("TestEntities2", out settings));
}
}
}
diff --git a/test/Liquid.Repository.Mongo.Tests/MongoDataContextTests.cs b/test/Liquid.Repository.Mongo.Tests/MongoDataContextTests.cs
index 2e2fd66..3d0eecf 100644
--- a/test/Liquid.Repository.Mongo.Tests/MongoDataContextTests.cs
+++ b/test/Liquid.Repository.Mongo.Tests/MongoDataContextTests.cs
@@ -3,57 +3,49 @@
using Liquid.Repository.Mongo.Tests.Mock;
using MongoDB.Driver;
using NSubstitute;
-using NUnit.Framework;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
+using Xunit;
namespace Liquid.Repository.Mongo.Tests
{
[ExcludeFromCodeCoverage]
- class MongoDataContextTests
+ public class MongoDataContextTests
{
private MongoDataContext _sut;
private IMongoClient _client;
- private IMongoEntitySettingsFactory _settingsFactory;
private IMongoClientFactory _provider;
- private IMongoEntitySettings _options;
- [SetUp]
- protected void SetContext()
+ public MongoDataContextTests()
{
_client = Substitute.For();
- _options = new MongoEntitySettings()
+ var _options = new MongoEntitySettings()
{
CollectionName = "TestEntities",
ShardKey = "id",
- DatabaseSettingsSectionName = "Test:Database",
- DatabaseSettings = new DatabaseSettings()
- {
- ConnectionString = "test connection string",
- DatabaseName = "TestDatabase"
- }
- };
+ ConnectionString = "test connection string",
+ DatabaseName = "TestDatabase"
- _settingsFactory = Substitute.For();
- _settingsFactory.GetSettings().Returns(_options);
+ };
_provider = Substitute.For();
- _provider.GetClient(_options.DatabaseSettings).Returns(_client);
- _sut = new MongoDataContext(_provider, _settingsFactory);
+ _provider.GetClient("TestEntities", out _).Returns(x => { x[1] = _options; return _client; });
+
+ _sut = new MongoDataContext(_provider, "TestEntities");
}
- [Test]
- public void MongoDataContext_WhenCreatedWithNullArguments_ThrowsException()
+ [Fact]
+ public void MongoDataContext_WhenCreatedWithNullArguments_ThrowsException()
{
- Assert.Throws(() => new MongoDataContext(null, _settingsFactory));
+ Assert.Throws(() => new MongoDataContext(null, "TestEntities"));
Assert.Throws(() => new MongoDataContext(_provider, null));
}
- [Test]
+ [Fact]
public async Task StartTransaction_WhenDBInitialized_Sucess()
{
await _sut.StartTransactionAsync();
@@ -62,7 +54,7 @@ public async Task StartTransaction_WhenDBInitialized_Sucess()
}
- [Test]
+ [Fact]
public async Task CommitAsync_WhenTansactionIsStarted_Sucess()
{
await _sut.StartTransactionAsync();
@@ -73,15 +65,15 @@ public async Task CommitAsync_WhenTansactionIsStarted_Sucess()
}
- [Test]
- public void CommitAsync_WhenTansactionIsntStarted_ThrowException()
+ [Fact]
+ public async Task CommitAsync_WhenTansactionIsntStarted_ThrowException()
{
var task = _sut.CommitAsync();
- Assert.ThrowsAsync(() => task);
+ await Assert.ThrowsAsync(() => task);
}
- [Test]
+ [Fact]
public async Task RollbackAsync_WhenTansactionIsStarted_Sucess()
{
@@ -92,16 +84,16 @@ public async Task RollbackAsync_WhenTansactionIsStarted_Sucess()
}
- [Test]
- public void RollbackAsync_WhenTansactionIsntStarted_ThrowException()
+ [Fact]
+ public async Task RollbackAsync_WhenTansactionIsntStarted_ThrowException()
{
var task = _sut.RollbackTransactionAsync();
- Assert.ThrowsAsync(() => task);
+ await Assert.ThrowsAsync(() => task);
}
- [Test]
+ [Fact]
public async Task Dispose_WhenTansactionIsStarted_Sucess()
{
await _sut.StartTransactionAsync();
@@ -114,7 +106,7 @@ public async Task Dispose_WhenTansactionIsStarted_Sucess()
}
- [Test]
+ [Fact]
public async Task Dispose_WhenTansactionIsntStarted_HandlerDisposed()
{
await _sut.StartTransactionAsync();
diff --git a/test/Liquid.Repository.Mongo.Tests/MongoEntitySettingsFactoryTests.cs b/test/Liquid.Repository.Mongo.Tests/MongoEntitySettingsFactoryTests.cs
deleted file mode 100644
index d95641f..0000000
--- a/test/Liquid.Repository.Mongo.Tests/MongoEntitySettingsFactoryTests.cs
+++ /dev/null
@@ -1,118 +0,0 @@
-using Liquid.Core.Exceptions;
-using Liquid.Repository.Mongo.Configuration;
-using Liquid.Repository.Mongo.Exceptions;
-using Liquid.Repository.Mongo.Tests.Mock;
-using Microsoft.Extensions.Configuration;
-using NUnit.Framework;
-using System;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-
-namespace Liquid.Repository.Mongo.Tests
-{
- [ExcludeFromCodeCoverage]
- public class MongoEntitySettingsFactoryTests
- {
- internal const string _databaseConfigurationSectionName = "MyMongoDbSettings";
- internal const string _entityConfigurationSectionName = "MyMongoEntityOptions:Entities";
- internal const string _databaseName = "TestDatabase";
- internal const string _connectionString = "test connection string";
-
- private IMongoEntitySettingsFactory _sutCustomConfiguration;
- private IMongoEntitySettingsFactory _sutStandardConfiguration;
- private IConfiguration _configuration;
-
- [SetUp]
- protected void SetContext()
- {
- var mongoDatabaseConfiguration = new Dictionary
- {
- {$"{_databaseConfigurationSectionName}:{_databaseName}:DatabaseName", _databaseName},
- {$"{_databaseConfigurationSectionName}:{_databaseName}:ConnectionString", _connectionString},
- };
-
- var mongoEntityConfiguration = new Dictionary
- {
- {"MyMongoEntityOptions:DefaultDatabaseSettings:DatabaseName", _databaseName},
- {"MyMongoEntityOptions:DefaultDatabaseSettings:ConnectionString", _connectionString},
- {$"{_entityConfigurationSectionName}:TestEntity:CollectionName", "TestEntities"},
- {$"{_entityConfigurationSectionName}:TestEntity:ShardKey", "id"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:CollectionName", "AnotherTestEntities"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:ShardKey", "id"},
- {$"{_entityConfigurationSectionName}:AnotherTestEntity:DatabaseSettingsSectionName", $"{_databaseConfigurationSectionName}:{_databaseName}"},
- {$"{_entityConfigurationSectionName}:WrongDBSettings:CollectionName", "AnotherTestEntities"},
- {$"{_entityConfigurationSectionName}:WrongDBSettings:ShardKey", "id"},
- {$"{_entityConfigurationSectionName}:WrongDBSettings:DatabaseSettingsSectionName", "WrongSectionName"}
- };
-
- var liquidStandardConfiguration = new Dictionary
- {
- {"Liquid:RepositorySettings:DefaultDatabaseSettings:DatabaseName", _databaseName},
- {"Liquid:RepositorySettings:DefaultDatabaseSettings:ConnectionString", _connectionString},
- {"Liquid:RepositorySettings:Entities:TestEntity:CollectionName", "TestEntities"},
- {"Liquid:RepositorySettings:Entities:TestEntity:ShardKey", "id"},
- };
-
- _configuration = new ConfigurationBuilder()
- .AddInMemoryCollection(mongoDatabaseConfiguration)
- .AddInMemoryCollection(mongoEntityConfiguration)
- .AddInMemoryCollection(liquidStandardConfiguration)
- .Build();
-
- _sutCustomConfiguration = new MongoEntitySettingsFactory(_configuration, _entityConfigurationSectionName);
- _sutStandardConfiguration = new MongoEntitySettingsFactory(_configuration);
- }
-
- [Test]
- public void MongoEntitySettingsFactory_WhenConfigurationIsNull_ThrowException()
- {
- Assert.Throws(() => new MongoEntitySettingsFactory(null));
- }
-
- [Test]
- public void GetSettings_WhenEntityNameIsNullOrEmpty_ThrowException()
- {
- Assert.Throws(() => _sutCustomConfiguration.GetSettings(null));
- Assert.Throws(() => _sutCustomConfiguration.GetSettings(string.Empty));
- }
-
- [Test]
- public void GetSettings_WhenEntitySettingsDoesntExist_ThrowException()
- {
- Assert.Throws(() => _sutStandardConfiguration.GetSettings());
- Assert.Throws(() => _sutCustomConfiguration.GetSettings("WrongEntityName"));
- }
-
- [Test]
- public void GetSettings_WhenDatabaseSettingsDoesntExist_ThrowException()
- {
- Assert.Throws(() => _sutCustomConfiguration.GetSettings("WrongDBSettings"));
- }
-
- [Test]
- public void GetSettings_WhenSettingsAreOk_SettingsCreated()
- {
- var result = _sutCustomConfiguration.GetSettings();
- Assert.IsNotNull(result);
- Assert.AreEqual("TestEntities", result.CollectionName);
- Assert.AreEqual("id", result.ShardKey);
- Assert.AreEqual(_databaseName, result.DatabaseSettings.DatabaseName);
- Assert.AreEqual(_connectionString, result.DatabaseSettings.ConnectionString);
-
- result = _sutCustomConfiguration.GetSettings();
- Assert.IsNotNull(result);
- Assert.AreEqual("AnotherTestEntities", result.CollectionName);
- Assert.AreEqual("id", result.ShardKey);
- Assert.AreEqual($"{_databaseConfigurationSectionName}:{_databaseName}", result.DatabaseSettingsSectionName);
- Assert.AreEqual(_databaseName, result.DatabaseSettings.DatabaseName);
- Assert.AreEqual(_connectionString, result.DatabaseSettings.ConnectionString);
-
- result = _sutStandardConfiguration.GetSettings();
- Assert.IsNotNull(result);
- Assert.AreEqual("TestEntities", result.CollectionName);
- Assert.AreEqual("id", result.ShardKey);
- Assert.AreEqual(_databaseName, result.DatabaseSettings.DatabaseName);
- Assert.AreEqual(_connectionString, result.DatabaseSettings.ConnectionString);
- }
- }
-}
diff --git a/test/Liquid.Repository.Mongo.Tests/MongoRepositoryTests.cs b/test/Liquid.Repository.Mongo.Tests/MongoRepositoryTests.cs
index 89e05ae..99e3ba3 100644
--- a/test/Liquid.Repository.Mongo.Tests/MongoRepositoryTests.cs
+++ b/test/Liquid.Repository.Mongo.Tests/MongoRepositoryTests.cs
@@ -4,17 +4,17 @@
using Liquid.Repository.Mongo.Tests.Mock;
using MongoDB.Driver;
using NSubstitute;
-using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
+using Xunit;
namespace Liquid.Repository.Mongo.Tests
{
[ExcludeFromCodeCoverage]
- class MongoRepositoryTests
+ public class MongoRepositoryTests
{
private IMongoDataContext _dbDataContext;
private ILiquidRepository _sut;
@@ -23,8 +23,7 @@ class MongoRepositoryTests
internal static string _collectionName = "TestEntities";
private IMongoCollection _collection;
- [SetUp]
- protected void SetContext()
+ public MongoRepositoryTests()
{
_entity = new TestEntity()
@@ -39,10 +38,8 @@ protected void SetContext()
{
CollectionName = _collectionName,
ShardKey = "id",
- DatabaseSettings = new DatabaseSettings() {
- DatabaseName = _databaseName,
- ConnectionString = "test connection string"
- }
+ DatabaseName = _databaseName,
+ ConnectionString = "test connection string"
};
_dbDataContext = Substitute.For>();
@@ -55,7 +52,7 @@ protected void SetContext()
_collection = GetCollection();
_dbDataContext.Database.GetCollection(_collectionName)
- .Returns(_collection);
+ .Returns(_collection);
_sut = new MongoRepository(_dbDataContext);
}
@@ -74,13 +71,13 @@ private IMongoCollection GetCollection()
}
- [Test]
+ [Fact]
public void MongoRepository_WhenCreatedWithNoDataContext_ThrowException()
{
Assert.Throws(() => new MongoRepository(null));
}
- [Test]
+ [Fact]
public async Task ValidateCollection_WhenCollectionExists_Success()
{
await _sut.AddAsync(_entity);
@@ -88,7 +85,7 @@ public async Task ValidateCollection_WhenCollectionExists_Success()
_dbDataContext.Database.Received(1).GetCollection(_collectionName);
}
- [Test]
+ [Fact]
public async Task AddAsync_WhenActionIsSuccessful_CallInsertOneMethod()
{
@@ -99,17 +96,17 @@ public async Task AddAsync_WhenActionIsSuccessful_CallInsertOneMethod()
await _collection.Received(1).InsertOneAsync(_entity);
}
- [Test]
- public void AddAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task AddAsync_WhenClientThrowsError_ThrowException()
{
_collection.When(o => o.InsertOneAsync(Arg.Any())).Do((call) => throw new Exception());
var test = _sut.AddAsync(_entity);
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
}
- [Test]
+ [Fact]
public async Task FindAllAsync_WhenCollectionExists_ReturnItens()
{
var result = await _sut.FindAllAsync();
@@ -117,21 +114,21 @@ public async Task FindAllAsync_WhenCollectionExists_ReturnItens()
_dbDataContext.Database.Received(1).GetCollection(_collectionName);
Assert.NotNull(result);
- Assert.AreEqual(result.FirstOrDefault(), _entity);
+ Assert.Equal(result.FirstOrDefault(), _entity);
}
- [Test]
- public void FindAllAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task FindAllAsync_WhenClientThrowsError_ThrowException()
{
_dbDataContext.Database.When(o => o.GetCollection(Arg.Any())).Do((call) => throw new Exception());
var test = _sut.FindAllAsync();
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
}
- [Test]
+ [Fact]
public async Task FindByIdAsync_WhenItemExists_ReturnItem()
{
@@ -139,22 +136,22 @@ public async Task FindByIdAsync_WhenItemExists_ReturnItem()
_dbDataContext.Database.Received(1).GetCollection(_collectionName);
- Assert.IsTrue(result == _entity);
+ Assert.True(result == _entity);
}
- [Test]
- public void FindByIdAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task FindByIdAsync_WhenClientThrowsError_ThrowException()
{
_collection.When(o => o.FindAsync(Arg.Any>())).Do((call) => throw new Exception());
var test = _sut.FindByIdAsync(1234);
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
- }
+ }
- [Test]
+ [Fact]
public async Task RemoveByIdAsync_WhenActionIsSuccessful_CallDeleteOneMethod()
{
await _sut.RemoveByIdAsync(_entity.Id);
@@ -165,17 +162,17 @@ public async Task RemoveByIdAsync_WhenActionIsSuccessful_CallDeleteOneMethod()
}
- [Test]
- public void RemoveByIdAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task RemoveByIdAsync_WhenClientThrowsError_ThrowException()
{
_collection.When(o => o.DeleteOneAsync(Arg.Any>())).Do((call) => throw new Exception());
var test = _sut.RemoveByIdAsync(_entity.Id);
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
}
- [Test]
+ [Fact]
public async Task UpdateAsync_WhenActionIsSuccessful_CallReplaceOneMethod()
{
@@ -187,17 +184,17 @@ public async Task UpdateAsync_WhenActionIsSuccessful_CallReplaceOneMethod()
}
- [Test]
- public void UpdateAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task UpdateAsync_WhenClientThrowsError_ThrowException()
{
_collection.When(o => o.ReplaceOneAsync(Arg.Any>(), _entity, Arg.Any())).Do((call) => throw new Exception());
var test = _sut.UpdateAsync(_entity);
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
}
- [Test]
+ [Fact]
public async Task WhereAsync_WhenItensExists_ReturnItens()
{
var result = await _sut.WhereAsync(e => e.Id.Equals(_entity.Id));
@@ -205,17 +202,17 @@ public async Task WhereAsync_WhenItensExists_ReturnItens()
_dbDataContext.Database.Received().GetCollection(_collectionName);
Assert.NotNull(result);
- Assert.AreEqual(result.FirstOrDefault(), _entity);
+ Assert.Equal(result.FirstOrDefault(), _entity);
}
- [Test]
- public void WhereAsync_WhenClientThrowsError_ThrowException()
+ [Fact]
+ public async Task WhereAsync_WhenClientThrowsError_ThrowException()
{
_collection.When(o => o.FindAsync(Arg.Any>())).Do((call) => throw new Exception());
var test = _sut.WhereAsync(e => e.Id.Equals(_entity.Id));
- Assert.ThrowsAsync(() => test);
+ await Assert.ThrowsAsync(() => test);
}
}
}