From 36f393965d955f3f99f321b4f7ce3fba16e46ddd Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 8 Mar 2017 20:30:28 +0100 Subject: [PATCH] Tests fixed and some adaptions to C# 7... yeah, less code --- .../Schemas/Validators/PatternValidator.cs | 24 +++++++---------- .../Validators/StringLengthValidator.cs | 26 +++++++++---------- .../Commands/EnrichWithTimestampHandler.cs | 4 +-- .../Reflection/PropertyAccessor.cs | 4 +-- .../Utils/EntityMapper.cs | 17 +++--------- .../Schemas/SchemaHistoryEventsCreator.cs | 22 +++++++--------- .../CommandHandlers/EnrichWithActorHandler.cs | 13 +++++++--- .../CommandHandlers/EnrichWithAppIdHandler.cs | 4 +-- .../EnrichWithSchemaIdHandler.cs | 4 +-- .../SetVersionAsETagHandler.cs | 4 +-- .../DefaultDomainObjectFactoryTests.cs | 24 ++++++++++++++--- .../DefaultDomainObjectRepositoryTests.cs | 4 +-- .../Json/ConverterContractResolverTests.cs | 2 +- .../TestHelpers/HandlerTestBase.cs | 5 ++++ 14 files changed, 79 insertions(+), 78 deletions(-) diff --git a/src/Squidex.Core/Schemas/Validators/PatternValidator.cs b/src/Squidex.Core/Schemas/Validators/PatternValidator.cs index 45401d8f7..b3f024fa5 100644 --- a/src/Squidex.Core/Schemas/Validators/PatternValidator.cs +++ b/src/Squidex.Core/Schemas/Validators/PatternValidator.cs @@ -30,22 +30,18 @@ namespace Squidex.Core.Schemas.Validators public Task ValidateAsync(object value, ICollection errors) { - var stringValue = value as string; - - if (stringValue == null) - { - return TaskHelper.Done; - } - - if (!regex.IsMatch(stringValue)) + if (value is string stringValue) { - if (string.IsNullOrWhiteSpace(errorMessage)) - { - errors.Add(" is not valid"); - } - else + if (!regex.IsMatch(stringValue)) { - errors.Add(errorMessage); + if (string.IsNullOrWhiteSpace(errorMessage)) + { + errors.Add(" is not valid"); + } + else + { + errors.Add(errorMessage); + } } } diff --git a/src/Squidex.Core/Schemas/Validators/StringLengthValidator.cs b/src/Squidex.Core/Schemas/Validators/StringLengthValidator.cs index b1e01fb61..92155e961 100644 --- a/src/Squidex.Core/Schemas/Validators/StringLengthValidator.cs +++ b/src/Squidex.Core/Schemas/Validators/StringLengthValidator.cs @@ -11,6 +11,8 @@ using System.Collections.Generic; using System.Threading.Tasks; using Squidex.Infrastructure.Tasks; +// ReSharper disable InvertIf + namespace Squidex.Core.Schemas.Validators { public class StringLengthValidator : IValidator @@ -31,21 +33,17 @@ namespace Squidex.Core.Schemas.Validators public Task ValidateAsync(object value, ICollection errors) { - var stringValue = value as string; - - if (stringValue == null) - { - return TaskHelper.Done; - } - - if (minLength.HasValue && stringValue.Length < minLength.Value) - { - errors.Add($" must have more than '{minLength}' characters"); - } - - if (maxLength.HasValue && stringValue.Length > maxLength.Value) + if (value is string stringValue) { - errors.Add($" must have less than '{maxLength}' characters"); + if (minLength.HasValue && stringValue.Length < minLength.Value) + { + errors.Add($" must have more than '{minLength}' characters"); + } + + if (maxLength.HasValue && stringValue.Length > maxLength.Value) + { + errors.Add($" must have less than '{maxLength}' characters"); + } } return TaskHelper.Done; diff --git a/src/Squidex.Infrastructure/CQRS/Commands/EnrichWithTimestampHandler.cs b/src/Squidex.Infrastructure/CQRS/Commands/EnrichWithTimestampHandler.cs index cccfb402a..855c11865 100644 --- a/src/Squidex.Infrastructure/CQRS/Commands/EnrichWithTimestampHandler.cs +++ b/src/Squidex.Infrastructure/CQRS/Commands/EnrichWithTimestampHandler.cs @@ -25,9 +25,7 @@ namespace Squidex.Infrastructure.CQRS.Commands public Task HandleAsync(CommandContext context) { - var timestampCommand = context.Command as ITimestampCommand; - - if (timestampCommand != null) + if (context.Command is ITimestampCommand timestampCommand) { timestampCommand.Timestamp = clock.GetCurrentInstant(); } diff --git a/src/Squidex.Infrastructure/Reflection/PropertyAccessor.cs b/src/Squidex.Infrastructure/Reflection/PropertyAccessor.cs index 1bdeccfe0..355dd088d 100644 --- a/src/Squidex.Infrastructure/Reflection/PropertyAccessor.cs +++ b/src/Squidex.Infrastructure/Reflection/PropertyAccessor.cs @@ -26,7 +26,7 @@ namespace Squidex.Infrastructure.Reflection } else { - getMethod = x => throw new NotSupportedException(); + getMethod = x => { throw new NotSupportedException(); }; } if (propertyInfo.CanWrite) @@ -35,7 +35,7 @@ namespace Squidex.Infrastructure.Reflection } else { - setMethod = (x, y) => throw new NotSupportedException(); + setMethod = (x, y) => { throw new NotSupportedException(); }; } } diff --git a/src/Squidex.Read.MongoDb/Utils/EntityMapper.cs b/src/Squidex.Read.MongoDb/Utils/EntityMapper.cs index c86311bbc..f9b6c005d 100644 --- a/src/Squidex.Read.MongoDb/Utils/EntityMapper.cs +++ b/src/Squidex.Read.MongoDb/Utils/EntityMapper.cs @@ -58,9 +58,7 @@ namespace Squidex.Read.MongoDb.Utils private static void SetVersion(EnvelopeHeaders headers, MongoEntity entity) { - var withVersion = entity as IEntityWithVersion; - - if (withVersion != null) + if (entity is IEntityWithVersion withVersion) { withVersion.Version = headers.EventStreamNumber(); } @@ -68,9 +66,7 @@ namespace Squidex.Read.MongoDb.Utils private static void SetCreatedBy(SquidexEvent @event, MongoEntity entity) { - var withCreatedBy = entity as IEntityWithCreatedBy; - - if (withCreatedBy != null) + if (entity is IEntityWithCreatedBy withCreatedBy) { withCreatedBy.CreatedBy = @event.Actor; } @@ -78,9 +74,7 @@ namespace Squidex.Read.MongoDb.Utils private static void SetLastModifiedBy(SquidexEvent @event, MongoEntity entity) { - var withModifiedBy = entity as IEntityWithLastModifiedBy; - - if (withModifiedBy != null) + if (entity is IEntityWithLastModifiedBy withModifiedBy) { withModifiedBy.LastModifiedBy = @event.Actor; } @@ -88,10 +82,7 @@ namespace Squidex.Read.MongoDb.Utils private static void SetAppId(SquidexEvent @event, MongoEntity entity) { - var appEntity = entity as IAppRefEntity; - var appEvent = @event as AppEvent; - - if (appEntity != null && appEvent != null) + if (entity is IAppRefEntity appEntity && @event is AppEvent appEvent) { appEntity.AppId = appEvent.AppId.Id; } diff --git a/src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs b/src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs index d7d622956..9c395f9a3 100644 --- a/src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs +++ b/src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs @@ -64,25 +64,21 @@ namespace Squidex.Read.Schemas protected override Task CreateEventCoreAsync(Envelope @event) { - var schemaEvent = @event.Payload as SchemaEvent; - - if (schemaEvent == null) + if (@event.Payload is SchemaEvent schemaEvent) { - return Task.FromResult(null); - } + string channel = $"schemas.{schemaEvent.SchemaId.Name}"; - string channel = $"schemas.{schemaEvent.SchemaId.Name}"; + var result = ForEvent(@event.Payload, channel).AddParameter("Name", schemaEvent.SchemaId.Name); - var result = ForEvent(@event.Payload, channel).AddParameter("Name", schemaEvent.SchemaId.Name); + if (schemaEvent is FieldEvent fieldEvent) + { + result.AddParameter("Field", fieldEvent.FieldId.Name); + } - var fieldEvent = schemaEvent as FieldEvent; - - if (fieldEvent != null) - { - result.AddParameter("Field", fieldEvent.FieldId.Name); + return Task.FromResult(result); } - return Task.FromResult(result); + return Task.FromResult(null); } } } \ No newline at end of file diff --git a/src/Squidex/Pipeline/CommandHandlers/EnrichWithActorHandler.cs b/src/Squidex/Pipeline/CommandHandlers/EnrichWithActorHandler.cs index ed0fc424c..ae7cdfd1d 100644 --- a/src/Squidex/Pipeline/CommandHandlers/EnrichWithActorHandler.cs +++ b/src/Squidex/Pipeline/CommandHandlers/EnrichWithActorHandler.cs @@ -30,15 +30,20 @@ namespace Squidex.Pipeline.CommandHandlers public Task HandleAsync(CommandContext context) { - var squidexCommand = context.Command as SquidexCommand; - - if (squidexCommand != null) + if (context.Command is SquidexCommand squidexCommand) { var actorToken = FindActorFromSubject() ?? FindActorFromClient(); - squidexCommand.Actor = actorToken ?? throw new SecurityException("No actor with subject or client id available"); +#pragma warning disable + if (actorToken == null) + { + throw new SecurityException("No actor with subject or client id available"); + } +#pragma warning enable + + squidexCommand.Actor = actorToken; } return TaskHelper.False; diff --git a/src/Squidex/Pipeline/CommandHandlers/EnrichWithAppIdHandler.cs b/src/Squidex/Pipeline/CommandHandlers/EnrichWithAppIdHandler.cs index 993ce1e7e..5c69772d3 100644 --- a/src/Squidex/Pipeline/CommandHandlers/EnrichWithAppIdHandler.cs +++ b/src/Squidex/Pipeline/CommandHandlers/EnrichWithAppIdHandler.cs @@ -29,9 +29,7 @@ namespace Squidex.Pipeline.CommandHandlers public Task HandleAsync(CommandContext context) { - var appCommand = context.Command as AppCommand; - - if (appCommand != null) + if (context.Command is AppCommand appCommand) { var appFeature = httpContextAccessor.HttpContext.Features.Get(); diff --git a/src/Squidex/Pipeline/CommandHandlers/EnrichWithSchemaIdHandler.cs b/src/Squidex/Pipeline/CommandHandlers/EnrichWithSchemaIdHandler.cs index 5ca3f4c8a..a4be18e38 100644 --- a/src/Squidex/Pipeline/CommandHandlers/EnrichWithSchemaIdHandler.cs +++ b/src/Squidex/Pipeline/CommandHandlers/EnrichWithSchemaIdHandler.cs @@ -33,9 +33,7 @@ namespace Squidex.Pipeline.CommandHandlers public async Task HandleAsync(CommandContext context) { - var schemaCommand = context.Command as SchemaCommand; - - if (schemaCommand != null) + if (context.Command is SchemaCommand schemaCommand) { var routeValues = actionContextAccessor.ActionContext.RouteData.Values; diff --git a/src/Squidex/Pipeline/CommandHandlers/SetVersionAsETagHandler.cs b/src/Squidex/Pipeline/CommandHandlers/SetVersionAsETagHandler.cs index 37e18f5fb..74d01c118 100644 --- a/src/Squidex/Pipeline/CommandHandlers/SetVersionAsETagHandler.cs +++ b/src/Squidex/Pipeline/CommandHandlers/SetVersionAsETagHandler.cs @@ -25,9 +25,7 @@ namespace Squidex.Pipeline.CommandHandlers public Task HandleAsync(CommandContext context) { - var result = context.Result() as EntitySavedResult; - - if (result != null) + if (context.Result() is EntitySavedResult result) { httpContextAccessor.HttpContext.Response.Headers["ETag"] = new StringValues(result.Version.ToString()); } diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs index 742b509cc..531a3edef 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectFactoryTests.cs @@ -19,7 +19,8 @@ namespace Squidex.Infrastructure.CQRS.Commands { private sealed class DO : DomainObjectBase { - public DO(Guid id, int version) : base(id, version) + public DO(Guid id, int version) + : base(id, version) { } @@ -35,7 +36,7 @@ namespace Squidex.Infrastructure.CQRS.Commands var factoryFunction = new DomainObjectFactoryFunction(passedId => { - return new DO(passedId, 0); + return new DO(passedId, -1); }); serviceProvider.Setup(x => x.GetService(typeof(DomainObjectFactoryFunction))).Returns(factoryFunction); @@ -47,7 +48,24 @@ namespace Squidex.Infrastructure.CQRS.Commands var domainObject = sut.CreateNew(typeof(DO), id); Assert.Equal(id, domainObject.Id); - Assert.Equal(0, domainObject.Version); + Assert.Equal(-1, domainObject.Version); + } + + [Fact] + public void Should_throw_if_new_entity_has_invalid_version() + { + var serviceProvider = new Mock(); + + var factoryFunction = new DomainObjectFactoryFunction(passedId => + { + return new DO(passedId, 0); + }); + + serviceProvider.Setup(x => x.GetService(typeof(DomainObjectFactoryFunction))).Returns(factoryFunction); + + var sut = new DefaultDomainObjectFactory(serviceProvider.Object); + + Assert.Throws(() => sut.CreateNew(typeof(DO), Guid.NewGuid())); } } } diff --git a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs index 6a4178e1b..c35e40e2c 100644 --- a/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs +++ b/tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs @@ -141,7 +141,7 @@ namespace Squidex.Infrastructure.CQRS.Commands eventDataFormatter.Setup(x => x.ToEventData(It.Is>(e => e.Payload == event1), commitId)).Returns(eventData1); eventDataFormatter.Setup(x => x.ToEventData(It.Is>(e => e.Payload == event2), commitId)).Returns(eventData2); - eventStore.Setup(x => x.AppendEventsAsync(commitId, streamName, 122, It.Is>(e => e.Count() == 2))) + eventStore.Setup(x => x.AppendEventsAsync(commitId, streamName, 123, It.Is>(e => e.Count() == 2))) .Returns(TaskHelper.Done) .Verifiable(); @@ -167,7 +167,7 @@ namespace Squidex.Infrastructure.CQRS.Commands eventDataFormatter.Setup(x => x.ToEventData(It.Is>(e => e.Payload == event1), commitId)).Returns(eventData1); eventDataFormatter.Setup(x => x.ToEventData(It.Is>(e => e.Payload == event2), commitId)).Returns(eventData2); - eventStore.Setup(x => x.AppendEventsAsync(commitId, streamName, 122, new List { eventData1, eventData2 })) + eventStore.Setup(x => x.AppendEventsAsync(commitId, streamName, 123, new List { eventData1, eventData2 })) .Throws(new WrongEventVersionException(1, 2)) .Verifiable(); diff --git a/tests/Squidex.Infrastructure.Tests/Json/ConverterContractResolverTests.cs b/tests/Squidex.Infrastructure.Tests/Json/ConverterContractResolverTests.cs index b19bae215..ba92a6551 100644 --- a/tests/Squidex.Infrastructure.Tests/Json/ConverterContractResolverTests.cs +++ b/tests/Squidex.Infrastructure.Tests/Json/ConverterContractResolverTests.cs @@ -52,7 +52,7 @@ namespace Squidex.Infrastructure.Json var json = JsonConvert.SerializeObject(new MyClass { MyProperty = value }, serializerSettings); - Assert.Equal(@"{ ""MyProperty"": ""TODAY"" }", json); + Assert.Equal(@"{""myProperty"":""TODAY""}", json); } [Fact] diff --git a/tests/Squidex.Write.Tests/TestHelpers/HandlerTestBase.cs b/tests/Squidex.Write.Tests/TestHelpers/HandlerTestBase.cs index 4679d6a5c..bc958663a 100644 --- a/tests/Squidex.Write.Tests/TestHelpers/HandlerTestBase.cs +++ b/tests/Squidex.Write.Tests/TestHelpers/HandlerTestBase.cs @@ -13,6 +13,8 @@ using Squidex.Infrastructure; using Squidex.Infrastructure.CQRS; using Squidex.Infrastructure.CQRS.Commands; +#pragma warning disable IDE0019 + namespace Squidex.Write.TestHelpers { public abstract class HandlerTestBase where T : DomainObjectBase @@ -146,3 +148,6 @@ namespace Squidex.Write.TestHelpers } } } + + +#pragma warning restore IDE0019 \ No newline at end of file