2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-18 16:10:05 +02:00
|
|
|
using System.Data.SqlClient;
|
2018-06-29 19:52:40 +02:00
|
|
|
using System.Linq;
|
2020-09-17 10:35:11 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-06-29 19:52:40 +02:00
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2018-06-29 19:52:40 +02:00
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories.Implement;
|
|
|
|
|
using Umbraco.Core.Scoping;
|
2020-08-18 16:10:05 +02:00
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2020-10-20 18:43:03 +02:00
|
|
|
namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
2020-08-18 16:10:05 +02:00
|
|
|
public class ServerRegistrationRepositoryTest : UmbracoIntegrationTest
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2019-01-17 08:34:29 +01:00
|
|
|
private AppCaches _appCaches;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2020-08-18 16:10:05 +02:00
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2019-11-07 19:39:20 +11:00
|
|
|
_appCaches = AppCaches.Disabled;
|
2018-06-29 19:52:40 +02:00
|
|
|
CreateTestData();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
private ServerRegistrationRepository CreateRepository(IScopeProvider provider) =>
|
|
|
|
|
new ServerRegistrationRepository((IScopeAccessor)provider, LoggerFactory.CreateLogger<ServerRegistrationRepository>());
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Cannot_Add_Duplicate_Server_Identities()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.Now);
|
|
|
|
|
|
2020-08-18 16:10:05 +02:00
|
|
|
Assert.Throws<SqlException>(() => repository.Save(server));
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Cannot_Update_To_Duplicate_Server_Identities()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
IServerRegistration server = repository.Get(1);
|
2018-06-29 19:52:40 +02:00
|
|
|
server.ServerIdentity = "COMPUTER2";
|
2020-09-17 10:35:11 +02:00
|
|
|
|
2020-08-18 16:10:05 +02:00
|
|
|
Assert.Throws<SqlException>(() => repository.Save(server));
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Instantiate_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(repository, Is.Not.Null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_Get_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
IServerRegistration server = repository.Get(1);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(server, Is.Not.Null);
|
|
|
|
|
Assert.That(server.HasIdentity, Is.True);
|
|
|
|
|
Assert.That(server.ServerAddress, Is.EqualTo("http://localhost"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_GetAll_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
IEnumerable<IServerRegistration> servers = repository.GetMany();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(servers.Count(), Is.EqualTo(3));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// queries are not supported due to in-memory caching
|
2020-12-23 11:35:49 +01:00
|
|
|
//// [Test]
|
|
|
|
|
//// public void Can_Perform_GetByQuery_On_Repository()
|
|
|
|
|
//// {
|
|
|
|
|
//// // Arrange
|
|
|
|
|
//// var provider = ScopeProvider;
|
|
|
|
|
//// using (var unitOfWork = provider.GetUnitOfWork())
|
|
|
|
|
//// using (var repository = CreateRepository(provider))
|
|
|
|
|
//// {
|
|
|
|
|
//// // Act
|
|
|
|
|
//// var query = Query<IServerRegistration>.Builder.Where(x => x.ServerIdentity.ToUpper() == "COMPUTER3");
|
|
|
|
|
//// var result = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
//// // Assert
|
|
|
|
|
//// Assert.AreEqual(1, result.Count());
|
|
|
|
|
//// }
|
|
|
|
|
//// }
|
|
|
|
|
|
|
|
|
|
//// [Test]
|
|
|
|
|
//// public void Can_Perform_Count_On_Repository()
|
|
|
|
|
//// {
|
|
|
|
|
//// // Arrange
|
|
|
|
|
//// var provider = ScopeProvider;
|
|
|
|
|
//// using (var unitOfWork = provider.GetUnitOfWork())
|
|
|
|
|
//// using (var repository = CreateRepository(provider))
|
|
|
|
|
//// {
|
|
|
|
|
//// // Act
|
|
|
|
|
//// var query = Query<IServerRegistration>.Builder.Where(x => x.ServerAddress.StartsWith("http://"));
|
|
|
|
|
//// int count = repository.Count(query);
|
|
|
|
|
|
|
|
|
|
//// // Assert
|
|
|
|
|
//// Assert.That(count, Is.EqualTo(2));
|
|
|
|
|
//// }
|
|
|
|
|
//// }
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_Add_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.Now);
|
|
|
|
|
repository.Save(server);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(server.HasIdentity, Is.True);
|
2020-12-23 11:35:49 +01:00
|
|
|
Assert.That(server.Id, Is.EqualTo(4)); // With 3 existing entries the Id should be 4
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_Update_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
IServerRegistration server = repository.Get(2);
|
2018-06-29 19:52:40 +02:00
|
|
|
server.ServerAddress = "https://umbraco.com";
|
|
|
|
|
server.IsActive = true;
|
|
|
|
|
|
|
|
|
|
repository.Save(server);
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
IServerRegistration serverUpdated = repository.Get(2);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(serverUpdated, Is.Not.Null);
|
|
|
|
|
Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com"));
|
|
|
|
|
Assert.That(serverUpdated.IsActive, Is.EqualTo(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_Delete_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
IServerRegistration server = repository.Get(3);
|
2018-06-29 19:52:40 +02:00
|
|
|
Assert.IsNotNull(server);
|
|
|
|
|
repository.Delete(server);
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
bool exists = repository.Exists(3);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(exists, Is.False);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Perform_Exists_On_Repository()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
2020-08-18 16:10:05 +02:00
|
|
|
using (provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2020-12-23 11:35:49 +01:00
|
|
|
bool exists = repository.Exists(3);
|
|
|
|
|
bool doesntExist = repository.Exists(10);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.That(exists, Is.True);
|
|
|
|
|
Assert.That(doesntExist, Is.False);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateTestData()
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
|
|
|
|
using (IScope scope = provider.CreateScope())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
ServerRegistrationRepository repository = CreateRepository(provider);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
repository.Save(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.Now) { IsActive = true });
|
|
|
|
|
repository.Save(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.Now));
|
|
|
|
|
repository.Save(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.Now));
|
|
|
|
|
scope.Complete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|