diff --git a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs index 42a867469..e35be4053 100644 --- a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs +++ b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs @@ -434,11 +434,11 @@ public CreateDialogDialogActivityDtoValidator( .When(x => x.Type != DialogActivityType.Values.Information); RuleFor(x => x.TransmissionId) .Null() - .WithMessage($"A {nameof(DialogActivityType.Values.DialogOpened)} activity cannot reference a transmission.") - .When(x => x.Type == DialogActivityType.Values.DialogOpened); + .WithMessage($"Only activities of type {nameof(DialogActivityType.Values.TransmissionOpened)} can reference a transmission.") + .When(x => x.Type != DialogActivityType.Values.TransmissionOpened); RuleFor(x => x.TransmissionId) .NotEmpty() - .WithMessage($"A {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.") + .WithMessage($"An activity of type {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.") .When(x => x.Type == DialogActivityType.Values.TransmissionOpened); } } diff --git a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs index 909d47e7a..f7ea74dff 100644 --- a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs +++ b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs @@ -400,11 +400,11 @@ public UpdateDialogDialogActivityDtoValidator( .When(x => x.Type != DialogActivityType.Values.Information); RuleFor(x => x.TransmissionId) .Null() - .WithMessage($"A {nameof(DialogActivityType.Values.DialogOpened)} activity cannot reference a transmission.") - .When(x => x.Type == DialogActivityType.Values.DialogOpened); + .WithMessage($"Only activities of type {nameof(DialogActivityType.Values.TransmissionOpened)} can reference a transmission.") + .When(x => x.Type != DialogActivityType.Values.TransmissionOpened); RuleFor(x => x.TransmissionId) .NotEmpty() - .WithMessage($"A {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.") + .WithMessage($"An activity of type {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.") .When(x => x.Type == DialogActivityType.Values.TransmissionOpened); } } diff --git a/tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/ServiceOwner/Activities/ActivityValidatorTests.cs b/tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/ServiceOwner/Activities/ActivityValidatorTests.cs new file mode 100644 index 000000000..03e5bcb68 --- /dev/null +++ b/tests/Digdir.Domain.Dialogporten.Application.Unit.Tests/Features/V1/ServiceOwner/Activities/ActivityValidatorTests.cs @@ -0,0 +1,62 @@ +using AutoMapper; +using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations; +using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Common.Actors; +using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create; +using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update; +using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Activities; +using Digdir.Library.Entity.Abstractions.Features.Identifiable; +using Digdir.Tool.Dialogporten.GenerateFakeData; +using FluentAssertions; + +using UpdateActivityDto = + Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update.ActivityDto; +using CreateActivityDto = + Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create.ActivityDto; + +namespace Digdir.Domain.Dialogporten.Application.Unit.Tests.Features.V1.ServiceOwner.Activities; + +public class ActivityValidatorTests +{ + public static IEnumerable ActivityTypes() => + from DialogActivityType.Values activityType in Enum.GetValues(typeof(DialogActivityType.Values)) + select new object[] { activityType, }; + + [Theory, MemberData(nameof(ActivityTypes))] + public void Only_TransmissionOpened_Is_Allowed_To_Set_TransmissionId( + DialogActivityType.Values activityType) + { + // Arrange + var mapper = new MapperConfiguration(cfg => cfg.CreateMap()).CreateMapper(); + + var activity = DialogGenerator.GenerateFakeDialogActivity(type: activityType); + activity.TransmissionId = IdentifiableExtensions.CreateVersion7(); + + var localizationValidator = new LocalizationDtosValidator(); + var actorValidator = new ActorValidator(); + + var createValidator = new CreateDialogDialogActivityDtoValidator(localizationValidator, actorValidator); + var updateValidator = new UpdateDialogDialogActivityDtoValidator(localizationValidator, actorValidator); + + // Act + var createValidation = createValidator.Validate(activity); + var updateValidation = updateValidator.Validate(mapper.Map(activity)); + + // Assert + if (activityType == DialogActivityType.Values.TransmissionOpened) + { + createValidation.IsValid.Should().BeTrue(); + updateValidation.IsValid.Should().BeTrue(); + } + else + { + createValidation.IsValid.Should().BeFalse(); + updateValidation.IsValid.Should().BeFalse(); + + createValidation.Errors.Should().ContainSingle(); + updateValidation.Errors.Should().ContainSingle(); + + createValidation.Errors.First().ErrorMessage.Should().Contain("TransmissionOpened"); + updateValidation.Errors.First().ErrorMessage.Should().Contain("TransmissionOpened"); + } + } +}