From 0230bedeb6d070e5a233656b45175eb751978221 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 7 Mar 2013 21:40:00 +0600 Subject: [PATCH] Reinstated server registration repositories and db table --- src/Umbraco.Core/Models/ServerRegistration.cs | 85 ++-- .../Factories/ServerRegistrationFactory.cs | 50 +- .../Persistence/Mappers/MappingResolver.cs | 7 +- .../Mappers/ServerRegistrationMapper.cs | 68 ++- .../ServerRegistrationRepository.cs | 188 ++++---- .../Persistence/RepositoryFactory.cs | 16 +- .../Services/ServerRegistrationService.cs | 171 ++++--- .../ServerRegistrationRepositoryTest.cs | 429 +++++++++--------- 8 files changed, 498 insertions(+), 516 deletions(-) diff --git a/src/Umbraco.Core/Models/ServerRegistration.cs b/src/Umbraco.Core/Models/ServerRegistration.cs index 8d5b0b8ec5..3fdb96265f 100644 --- a/src/Umbraco.Core/Models/ServerRegistration.cs +++ b/src/Umbraco.Core/Models/ServerRegistration.cs @@ -1,54 +1,53 @@ using System; using Umbraco.Core.Models.EntityBase; +using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Sync; namespace Umbraco.Core.Models { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. + + internal class ServerRegistration : Entity, IServerAddress, IAggregateRoot + { + public ServerRegistration() + { - //internal class ServerRegistration : Entity, IServerAddress, IAggregateRoot - //{ - // public ServerRegistration() - // { - - // } + } - // /// - // /// Creates an item with pre-filled properties - // /// - // /// - // /// - // /// - // /// - // /// - // public ServerRegistration(int id, string serverAddress, string computerName, DateTime createDate, DateTime updateDate) - // { - // UpdateDate = updateDate; - // CreateDate = createDate; - // Key = Id.ToString().EncodeAsGuid(); - // Id = id; - // ServerAddress = serverAddress; - // ComputerName = computerName; - // } + /// + /// Creates an item with pre-filled properties + /// + /// + /// + /// + /// + /// + public ServerRegistration(int id, string serverAddress, string computerName, DateTime createDate, DateTime updateDate) + { + UpdateDate = updateDate; + CreateDate = createDate; + Key = Id.ToString().EncodeAsGuid(); + Id = id; + ServerAddress = serverAddress; + ComputerName = computerName; + } - // /// - // /// Creates a new instance for persisting a new item - // /// - // /// - // /// - // /// - // public ServerRegistration(string serverAddress, string computerName, DateTime createDate) - // { - // CreateDate = createDate; - // UpdateDate = createDate; - // Key = 0.ToString().EncodeAsGuid(); - // ServerAddress = serverAddress; - // ComputerName = computerName; - // } + /// + /// Creates a new instance for persisting a new item + /// + /// + /// + /// + public ServerRegistration(string serverAddress, string computerName, DateTime createDate) + { + CreateDate = createDate; + UpdateDate = createDate; + Key = 0.ToString().EncodeAsGuid(); + ServerAddress = serverAddress; + ComputerName = computerName; + } - // public string ServerAddress { get; set; } - // public string ComputerName { get; set; } - // public bool IsActive { get; set; } - //} + public string ServerAddress { get; set; } + public string ComputerName { get; set; } + public bool IsActive { get; set; } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs b/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs index b6e2742ca7..8b3eea2388 100644 --- a/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs @@ -4,34 +4,32 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. + + internal class ServerRegistrationFactory : IEntityFactory + { + #region Implementation of IEntityFactory - //internal class ServerRegistrationFactory : IEntityFactory - //{ - // #region Implementation of IEntityFactory + public ServerRegistration BuildEntity(ServerRegistrationDto dto) + { + return new ServerRegistration(dto.Id, dto.Address, dto.ComputerName, dto.DateRegistered, dto.LastNotified); + } - // public ServerRegistration BuildEntity(ServerRegistrationDto dto) - // { - // return new ServerRegistration(dto.Id, dto.Address, dto.ComputerName, dto.DateRegistered, dto.LastNotified); - // } + public ServerRegistrationDto BuildDto(ServerRegistration entity) + { + var dto = new ServerRegistrationDto() + { + Address = entity.ServerAddress, + DateRegistered = entity.CreateDate, + IsActive = entity.IsActive, + LastNotified = entity.UpdateDate, + ComputerName = entity.ComputerName + }; + if (entity.HasIdentity) + dto.Id = short.Parse(entity.Id.ToString(CultureInfo.InvariantCulture)); - // public ServerRegistrationDto BuildDto(ServerRegistration entity) - // { - // var dto = new ServerRegistrationDto() - // { - // Address = entity.ServerAddress, - // DateRegistered = entity.CreateDate, - // IsActive = entity.IsActive, - // LastNotified = entity.UpdateDate, - // ComputerName = entity.ComputerName - // }; - // if (entity.HasIdentity) - // dto.Id = short.Parse(entity.Id.ToString(CultureInfo.InvariantCulture)); + return dto; + } - // return dto; - // } - - // #endregion - //} + #endregion + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs b/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs index 40e9861d28..92dd51c5b2 100644 --- a/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs +++ b/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs @@ -8,11 +8,8 @@ namespace Umbraco.Core.Persistence.Mappers { internal static BaseMapper ResolveMapperByType(Type type) { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. - - //if (type == typeof (ServerRegistration)) - // return ServerRegistrationMapper.Instance; + if (type == typeof(ServerRegistration)) + return ServerRegistrationMapper.Instance; if (type == typeof (IContent) || type == typeof (Content)) return ContentMapper.Instance; diff --git a/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs b/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs index d7f5028dbf..bf53eb9bd0 100644 --- a/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs @@ -6,48 +6,46 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Mappers { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. + + internal sealed class ServerRegistrationMapper : BaseMapper + { + private static readonly ConcurrentDictionary PropertyInfoCache = new ConcurrentDictionary(); - //internal sealed class ServerRegistrationMapper : BaseMapper - //{ - // private static readonly ConcurrentDictionary PropertyInfoCache = new ConcurrentDictionary(); + internal static readonly ServerRegistrationMapper Instance = new ServerRegistrationMapper(); - // internal static readonly ServerRegistrationMapper Instance = new ServerRegistrationMapper(); + private ServerRegistrationMapper() + { + BuildMap(); + } - // private ServerRegistrationMapper() - // { - // BuildMap(); - // } + #region Overrides of BaseMapper - // #region Overrides of BaseMapper + internal override void BuildMap() + { + CacheMap(src => src.Id, dto => dto.Id); + CacheMap(src => src.IsActive, dto => dto.IsActive); + CacheMap(src => src.ServerAddress, dto => dto.Address); + CacheMap(src => src.CreateDate, dto => dto.DateRegistered); + CacheMap(src => src.UpdateDate, dto => dto.LastNotified); + CacheMap(src => src.ComputerName, dto => dto.ComputerName); + } - // internal override void BuildMap() - // { - // CacheMap(src => src.Id, dto => dto.Id); - // CacheMap(src => src.IsActive, dto => dto.IsActive); - // CacheMap(src => src.ServerAddress, dto => dto.Address); - // CacheMap(src => src.CreateDate, dto => dto.DateRegistered); - // CacheMap(src => src.UpdateDate, dto => dto.LastNotified); - // CacheMap(src => src.ComputerName, dto => dto.ComputerName); - // } + internal override string Map(string propertyName) + { + if (!PropertyInfoCache.ContainsKey(propertyName)) + return string.Empty; - // internal override string Map(string propertyName) - // { - // if (!PropertyInfoCache.ContainsKey(propertyName)) - // return string.Empty; + var dtoTypeProperty = PropertyInfoCache[propertyName]; - // var dtoTypeProperty = PropertyInfoCache[propertyName]; + return base.GetColumnName(dtoTypeProperty.Type, dtoTypeProperty.PropertyInfo); + } - // return base.GetColumnName(dtoTypeProperty.Type, dtoTypeProperty.PropertyInfo); - // } + internal override void CacheMap(Expression> sourceMember, Expression> destinationMember) + { + var property = base.ResolveMapping(sourceMember, destinationMember); + PropertyInfoCache.AddOrUpdate(property.SourcePropertyName, property, (x, y) => property); + } - // internal override void CacheMap(Expression> sourceMember, Expression> destinationMember) - // { - // var property = base.ResolveMapping(sourceMember, destinationMember); - // PropertyInfoCache.AddOrUpdate(property.SourcePropertyName, property, (x, y) => property); - // } - - // #endregion - //} + #endregion + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/ServerRegistrationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ServerRegistrationRepository.cs index 1640443df1..4f104b50c7 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ServerRegistrationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ServerRegistrationRepository.cs @@ -11,123 +11,121 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence.Repositories { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. + + internal class ServerRegistrationRepository : PetaPocoRepositoryBase + { + public ServerRegistrationRepository(IDatabaseUnitOfWork work) + : base(work) + { + } - //internal class ServerRegistrationRepository : PetaPocoRepositoryBase - //{ - // public ServerRegistrationRepository(IDatabaseUnitOfWork work) - // : base(work) - // { - // } + public ServerRegistrationRepository(IDatabaseUnitOfWork work, IRepositoryCacheProvider cache) + : base(work, cache) + { + } - // public ServerRegistrationRepository(IDatabaseUnitOfWork work, IRepositoryCacheProvider cache) - // : base(work, cache) - // { - // } + protected override ServerRegistration PerformGet(int id) + { + var sql = GetBaseQuery(false); + sql.Where(GetBaseWhereClause(), new { Id = id }); - // protected override ServerRegistration PerformGet(int id) - // { - // var sql = GetBaseQuery(false); - // sql.Where(GetBaseWhereClause(), new { Id = id }); + var serverDto = Database.First(sql); + if (serverDto == null) + return null; - // var serverDto = Database.First(sql); - // if (serverDto == null) - // return null; + var factory = new ServerRegistrationFactory(); + var entity = factory.BuildEntity(serverDto); - // var factory = new ServerRegistrationFactory(); - // var entity = factory.BuildEntity(serverDto); + ((ICanBeDirty)entity).ResetDirtyProperties(); - // ((ICanBeDirty)entity).ResetDirtyProperties(); + return entity; + } - // return entity; - // } + protected override IEnumerable PerformGetAll(params int[] ids) + { + if (ids.Any()) + { + foreach (var id in ids) + { + yield return Get(id); + } + } + else + { + var serverDtos = Database.Fetch("WHERE id > 0"); + foreach (var serverDto in serverDtos) + { + yield return Get(serverDto.Id); + } + } + } - // protected override IEnumerable PerformGetAll(params int[] ids) - // { - // if (ids.Any()) - // { - // foreach (var id in ids) - // { - // yield return Get(id); - // } - // } - // else - // { - // var serverDtos = Database.Fetch("WHERE id > 0"); - // foreach (var serverDto in serverDtos) - // { - // yield return Get(serverDto.Id); - // } - // } - // } + protected override IEnumerable PerformGetByQuery(IQuery query) + { + var sqlClause = GetBaseQuery(false); + var translator = new SqlTranslator(sqlClause, query); + var sql = translator.Translate(); - // protected override IEnumerable PerformGetByQuery(IQuery query) - // { - // var sqlClause = GetBaseQuery(false); - // var translator = new SqlTranslator(sqlClause, query); - // var sql = translator.Translate(); + var dtos = Database.Fetch(sql); - // var dtos = Database.Fetch(sql); + foreach (var dto in dtos) + { + yield return Get(dto.Id); + } - // foreach (var dto in dtos) - // { - // yield return Get(dto.Id); - // } + } - // } + protected override Sql GetBaseQuery(bool isCount) + { + var sql = new Sql(); + sql.Select(isCount ? "COUNT(*)" : "*") + .From(); + return sql; + } - // protected override Sql GetBaseQuery(bool isCount) - // { - // var sql = new Sql(); - // sql.Select(isCount ? "COUNT(*)" : "*") - // .From(); - // return sql; - // } + protected override string GetBaseWhereClause() + { + return "id = @Id"; + } - // protected override string GetBaseWhereClause() - // { - // return "id = @Id"; - // } + protected override IEnumerable GetDeleteClauses() + { + var list = new List + { + "DELETE FROM umbracoServer WHERE id = @Id" + }; + return list; + } - // protected override IEnumerable GetDeleteClauses() - // { - // var list = new List - // { - // "DELETE FROM umbracoServer WHERE id = @Id" - // }; - // return list; - // } + protected override Guid NodeObjectTypeId + { + get { throw new NotImplementedException(); } + } - // protected override Guid NodeObjectTypeId - // { - // get { throw new NotImplementedException(); } - // } + protected override void PersistNewItem(ServerRegistration entity) + { + ((Entity)entity).AddingEntity(); - // protected override void PersistNewItem(ServerRegistration entity) - // { - // ((Entity)entity).AddingEntity(); + var factory = new ServerRegistrationFactory(); + var dto = factory.BuildDto(entity); - // var factory = new ServerRegistrationFactory(); - // var dto = factory.BuildDto(entity); + var id = Convert.ToInt32(Database.Insert(dto)); + entity.Id = id; - // var id = Convert.ToInt32(Database.Insert(dto)); - // entity.Id = id; + ((ICanBeDirty)entity).ResetDirtyProperties(); + } - // ((ICanBeDirty)entity).ResetDirtyProperties(); - // } + protected override void PersistUpdatedItem(ServerRegistration entity) + { + ((Entity)entity).UpdatingEntity(); - // protected override void PersistUpdatedItem(ServerRegistration entity) - // { - // ((Entity)entity).UpdatingEntity(); + var factory = new ServerRegistrationFactory(); + var dto = factory.BuildDto(entity); - // var factory = new ServerRegistrationFactory(); - // var dto = factory.BuildDto(entity); + Database.Update(dto); - // Database.Update(dto); + ((ICanBeDirty)entity).ResetDirtyProperties(); + } - // ((ICanBeDirty)entity).ResetDirtyProperties(); - // } - - //} + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/RepositoryFactory.cs b/src/Umbraco.Core/Persistence/RepositoryFactory.cs index 0c9eaa9595..aac31b9568 100644 --- a/src/Umbraco.Core/Persistence/RepositoryFactory.cs +++ b/src/Umbraco.Core/Persistence/RepositoryFactory.cs @@ -9,15 +9,13 @@ namespace Umbraco.Core.Persistence /// public class RepositoryFactory { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. - - //internal virtual ServerRegistrationRepository CreateServerRegistrationRepository(IDatabaseUnitOfWork uow) - //{ - // return new ServerRegistrationRepository( - // uow, - // NullCacheProvider.Current); - //} + + internal virtual ServerRegistrationRepository CreateServerRegistrationRepository(IDatabaseUnitOfWork uow) + { + return new ServerRegistrationRepository( + uow, + NullCacheProvider.Current); + } internal virtual IUserTypeRepository CreateUserTypeRepository(IDatabaseUnitOfWork uow) { diff --git a/src/Umbraco.Core/Services/ServerRegistrationService.cs b/src/Umbraco.Core/Services/ServerRegistrationService.cs index 5026b531bf..7b3febd962 100644 --- a/src/Umbraco.Core/Services/ServerRegistrationService.cs +++ b/src/Umbraco.Core/Services/ServerRegistrationService.cs @@ -9,99 +9,96 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { - //NOTE: SD: Commenting out for now until we want to release a distributed cache provider that - // uses internal DNS names for each website to 'call' home intead of the current configuration based approach. + /// + /// Service to manage server registrations in the database + /// + internal class ServerRegistrationService + { + private readonly RepositoryFactory _repositoryFactory; + private readonly IDatabaseUnitOfWorkProvider _uowProvider; - ///// - ///// Service to manage server registrations in the database - ///// - //internal class ServerRegistrationService - //{ - // private readonly RepositoryFactory _repositoryFactory; - // private readonly IDatabaseUnitOfWorkProvider _uowProvider; + public ServerRegistrationService() + : this(new RepositoryFactory()) + { } - // public ServerRegistrationService() - // : this(new RepositoryFactory()) - // {} + public ServerRegistrationService(RepositoryFactory repositoryFactory) + : this(new PetaPocoUnitOfWorkProvider(), repositoryFactory) + { } - // public ServerRegistrationService(RepositoryFactory repositoryFactory) - // : this(new PetaPocoUnitOfWorkProvider(), repositoryFactory) - // {} + public ServerRegistrationService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory) + { + if (provider == null) throw new ArgumentNullException("provider"); + if (repositoryFactory == null) throw new ArgumentNullException("repositoryFactory"); + _uowProvider = provider; + _repositoryFactory = repositoryFactory; + } - // public ServerRegistrationService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory) - // { - // if (provider == null) throw new ArgumentNullException("provider"); - // if (repositoryFactory == null) throw new ArgumentNullException("repositoryFactory"); - // _uowProvider = provider; - // _repositoryFactory = repositoryFactory; - // } + /// + /// Called to 'call home' to ensure the current server has an active record + /// + /// + public void EnsureActive(string address) + { - // /// - // /// Called to 'call home' to ensure the current server has an active record - // /// - // /// - // public void EnsureActive(string address) - // { - - // var uow = _uowProvider.GetUnitOfWork(); - // using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) - // { - // //NOTE: we cannot use Environment.MachineName as this does not work in medium trust - // // found this out in CDF a while back: http://clientdependency.codeplex.com/workitem/13191 + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + //NOTE: we cannot use Environment.MachineName as this does not work in medium trust + // found this out in CDF a while back: http://clientdependency.codeplex.com/workitem/13191 - // var computerName = System.Net.Dns.GetHostName(); - // var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); - // var found = repo.GetByQuery(query).ToArray(); - // ServerRegistration server; - // if (found.Any()) - // { - // server = found.First(); - // server.ServerAddress = address; //This should not really change but it might! - // server.UpdateDate = DateTime.UtcNow; //Stick with Utc dates since these might be globally distributed - // server.IsActive = true; - // } - // else - // { - // server = new ServerRegistration(address, computerName, DateTime.UtcNow); - // } - // repo.AddOrUpdate(server); - // uow.Commit(); - // } - // } + var computerName = System.Net.Dns.GetHostName(); + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); + var found = repo.GetByQuery(query).ToArray(); + ServerRegistration server; + if (found.Any()) + { + server = found.First(); + server.ServerAddress = address; //This should not really change but it might! + server.UpdateDate = DateTime.UtcNow; //Stick with Utc dates since these might be globally distributed + server.IsActive = true; + } + else + { + server = new ServerRegistration(address, computerName, DateTime.UtcNow); + } + repo.AddOrUpdate(server); + uow.Commit(); + } + } - // /// - // /// Deactivates a server by name - // /// - // /// - // public void DeactiveServer(string computerName) - // { - // var uow = _uowProvider.GetUnitOfWork(); - // using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) - // { - // var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); - // var found = repo.GetByQuery(query).ToArray(); - // if (found.Any()) - // { - // var server = found.First(); - // server.IsActive = false; - // repo.AddOrUpdate(server); - // uow.Commit(); - // } - // } - // } + /// + /// Deactivates a server by name + /// + /// + public void DeactiveServer(string computerName) + { + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); + var found = repo.GetByQuery(query).ToArray(); + if (found.Any()) + { + var server = found.First(); + server.IsActive = false; + repo.AddOrUpdate(server); + uow.Commit(); + } + } + } - // /// - // /// Return all active servers - // /// - // /// - // public IEnumerable GetActiveServers() - // { - // var uow = _uowProvider.GetUnitOfWork(); - // using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) - // { - // var query = Query.Builder.Where(x => x.IsActive); - // return repo.GetByQuery(query).ToArray(); - // } - // } - //} + /// + /// Return all active servers + /// + /// + public IEnumerable GetActiveServers() + { + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + var query = Query.Builder.Where(x => x.IsActive); + return repo.GetByQuery(query).ToArray(); + } + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs index 7c5bfd5ebc..ef1bda3c5c 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs @@ -1,252 +1,249 @@ -//NOTE: SD: Commenting out for now until we want to release a distributed cache provider that -// uses internal DNS names for each website to 'call' home intead of the current configuration based approach. +using System; +using System.Data.SqlServerCe; +using System.Linq; +using NUnit.Framework; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence.Querying; +using Umbraco.Core.Persistence.Repositories; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Tests.TestHelpers; -//using System; -//using System.Data.SqlServerCe; -//using System.Linq; -//using NUnit.Framework; -//using Umbraco.Core.Models; -//using Umbraco.Core.Persistence.Querying; -//using Umbraco.Core.Persistence.Repositories; -//using Umbraco.Core.Persistence.UnitOfWork; -//using Umbraco.Tests.TestHelpers; +namespace Umbraco.Tests.Persistence.Repositories +{ + [TestFixture] + public class ServerRegistrationRepositoryTest : BaseDatabaseFactoryTest + { + [SetUp] + public override void Initialize() + { + base.Initialize(); -//namespace Umbraco.Tests.Persistence.Repositories -//{ -// [TestFixture] -// public class ServerRegistrationRepositoryTest : BaseDatabaseFactoryTest -// { -// [SetUp] -// public override void Initialize() -// { -// base.Initialize(); + CreateTestData(); + } -// CreateTestData(); -// } + [Test] + public void Cannot_Add_Duplicate_Computer_Names() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); -// [Test] -// public void Cannot_Add_Duplicate_Computer_Names() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); + // Act + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.Now); + repository.AddOrUpdate(server); -// // Act -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.Now); -// repository.AddOrUpdate(server); + Assert.Throws(unitOfWork.Commit); + } -// Assert.Throws(unitOfWork.Commit); -// } + } -// } + [Test] + public void Cannot_Update_To_Duplicate_Computer_Names() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); -// [Test] -// public void Cannot_Update_To_Duplicate_Computer_Names() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); + // Act + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + var server = repository.Get(1); + server.ComputerName = "COMPUTER2"; + repository.AddOrUpdate(server); + Assert.Throws(unitOfWork.Commit); + } -// // Act -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// var server = repository.Get(1); -// server.ComputerName = "COMPUTER2"; -// repository.AddOrUpdate(server); -// Assert.Throws(unitOfWork.Commit); -// } + } -// } + [Test] + public void Can_Instantiate_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); -// [Test] -// public void Can_Instantiate_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); + // Act + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Assert + Assert.That(repository, Is.Not.Null); + } + } -// // Act -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Assert -// Assert.That(repository, Is.Not.Null); -// } -// } + [Test] + public void Can_Perform_Get_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(1); -// [Test] -// public void Can_Perform_Get_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var server = repository.Get(1); - -// // Assert -// Assert.That(server, Is.Not.Null); -// Assert.That(server.HasIdentity, Is.True); -// Assert.That(server.ServerAddress, Is.EqualTo("http://localhost")); -// } + // 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 -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var servers = repository.GetAll(); + [Test] + public void Can_Perform_GetAll_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var servers = repository.GetAll(); -// // Assert -// Assert.That(servers.Count(), Is.EqualTo(3)); -// } + // Assert + Assert.That(servers.Count(), Is.EqualTo(3)); + } -// } + } -// [Test] -// public void Can_Perform_GetByQuery_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == "COMPUTER3"); -// var result = repository.GetByQuery(query); + [Test] + public void Can_Perform_GetByQuery_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == "COMPUTER3"); + var result = repository.GetByQuery(query); -// // Assert -// Assert.AreEqual(1, result.Count()); -// } -// } + // Assert + Assert.AreEqual(1, result.Count()); + } + } -// [Test] -// public void Can_Perform_Count_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var query = Query.Builder.Where(x => x.ServerAddress.StartsWith("http://")); -// int count = repository.Count(query); + [Test] + public void Can_Perform_Count_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var query = Query.Builder.Where(x => x.ServerAddress.StartsWith("http://")); + int count = repository.Count(query); -// // Assert -// Assert.That(count, Is.EqualTo(2)); -// } -// } + // Assert + Assert.That(count, Is.EqualTo(2)); + } + } -// [Test] -// public void Can_Perform_Add_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.Now); -// repository.AddOrUpdate(server); -// unitOfWork.Commit(); + [Test] + public void Can_Perform_Add_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.Now); + repository.AddOrUpdate(server); + unitOfWork.Commit(); -// // Assert -// Assert.That(server.HasIdentity, Is.True); -// Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4 -// } -// } + // Assert + Assert.That(server.HasIdentity, Is.True); + Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4 + } + } -// [Test] -// public void Can_Perform_Update_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var server = repository.Get(2); -// server.ServerAddress = "https://umbraco.com"; -// server.IsActive = true; + [Test] + public void Can_Perform_Update_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(2); + server.ServerAddress = "https://umbraco.com"; + server.IsActive = true; -// repository.AddOrUpdate(server); -// unitOfWork.Commit(); + repository.AddOrUpdate(server); + unitOfWork.Commit(); -// var serverUpdated = repository.Get(2); + var serverUpdated = repository.Get(2); -// // Assert -// Assert.That(serverUpdated, Is.Not.Null); -// Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com")); -// Assert.That(serverUpdated.IsActive, Is.EqualTo(true)); -// } -// } + // 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 -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var server = repository.Get(3); -// Assert.IsNotNull(server); -// repository.Delete(server); -// unitOfWork.Commit(); + [Test] + public void Can_Perform_Delete_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(3); + Assert.IsNotNull(server); + repository.Delete(server); + unitOfWork.Commit(); -// var exists = repository.Exists(3); + var exists = repository.Exists(3); -// // Assert -// Assert.That(exists, Is.False); -// } -// } + // Assert + Assert.That(exists, Is.False); + } + } -// [Test] -// public void Can_Perform_Exists_On_Repository() -// { -// // Arrange -// var provider = new PetaPocoUnitOfWorkProvider(); -// var unitOfWork = provider.GetUnitOfWork(); -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// // Act -// var exists = repository.Exists(3); -// var doesntExist = repository.Exists(10); + [Test] + public void Can_Perform_Exists_On_Repository() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var exists = repository.Exists(3); + var doesntExist = repository.Exists(10); -// // Assert -// Assert.That(exists, Is.True); -// Assert.That(doesntExist, Is.False); -// } -// } + // Assert + Assert.That(exists, Is.True); + Assert.That(doesntExist, Is.False); + } + } -// [TearDown] -// public override void TearDown() -// { -// base.TearDown(); -// } + [TearDown] + public override void TearDown() + { + base.TearDown(); + } -// public void CreateTestData() -// { -// var provider = new PetaPocoUnitOfWorkProvider(); -// using (var unitOfWork = provider.GetUnitOfWork()) -// using (var repository = new ServerRegistrationRepository(unitOfWork)) -// { -// repository.AddOrUpdate(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.Now) { IsActive = true }); -// repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.Now)); -// repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.Now)); -// unitOfWork.Commit(); -// } + public void CreateTestData() + { + var provider = new PetaPocoUnitOfWorkProvider(); + using (var unitOfWork = provider.GetUnitOfWork()) + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + repository.AddOrUpdate(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.Now) { IsActive = true }); + repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.Now)); + repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.Now)); + unitOfWork.Commit(); + } -// } -// } -//} \ No newline at end of file + } + } +} \ No newline at end of file