// Tests for cases that are valid.
        // Allowed updates for publishing status:
        // Draft     -> Draft or Published
        // Published -> Modified, Published, Archived
        // Archived  -> Modified, Published, Archived
        public void StateValid(string currentPublishingStatus, string newPublishingStatus)
        {
            // Arrange
            var validator = new PublishingStatusValidator(newPublishingStatus, currentPublishingStatus);

            // Act
            validator.Validate(controller.ModelState);

            // Assert
            controller.ModelState.IsValid.Should().BeTrue();
        }
        public void StatusInvalid(string currentPublishingStatus, string newPublishingStatus)
        {
            // Arrange
            var validator = new PublishingStatusValidator(newPublishingStatus, currentPublishingStatus);

            // Act
            Action act = () => validator.Validate(controller.ModelState);

            // Assert
            act.ShouldThrowExactly <Exception>().WithMessage("The field is invalid. Please use one of these: *");
        }
        private IActionResult PutServiceAndChannelByService(string serviceId, V7VmOpenApiServiceAndChannelRelationAstiInBase request)
        {
            // Validate the items
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            if (!string.IsNullOrEmpty(serviceId))
            {
                request.ServiceId = serviceId.ParseToGuid();

                // check that service exists
                if (!request.ServiceId.HasValue || !serviceService.ServiceExists(request.ServiceId.Value))
                {
                    return(NotFound(new VmError()
                    {
                        ErrorMessage = $"Service with id '{serviceId}' not found."
                    }));
                }

                var currentVersion = serviceService.GetServiceByIdSimple(request.ServiceId.Value);
                if (currentVersion == null || string.IsNullOrEmpty(currentVersion.PublishingStatus))
                {
                    this.ModelState.AddModelError("Service id", $"Version for service with id '{serviceId}' not found.");
                }
                else
                {
                    // Validate publishing status
                    PublishingStatusValidator status = new PublishingStatusValidator(PublishingStatus.Published.ToString(), currentVersion.PublishingStatus);
                    status.Validate(ModelState);
                }

                // Validate the items
                if (!ModelState.IsValid)
                {
                    return(new BadRequestObjectResult(ModelState));
                }
            }
            else
            {
                return(NotFound(new VmError()
                {
                    ErrorMessage = $"Service id has to be set."
                }));
            }

            // Check the item values from db and validate
            request.ChannelRelations.ForEach(r =>
            {
                r.ServiceGuid = request.ServiceId.Value;
                r.ChannelGuid = r.ServiceChannelId.ParseToGuidWithExeption();
                r.ExtraTypes.ForEach(e => { e.ServiceGuid = r.ServiceGuid; e.ChannelGuid = r.ChannelGuid; });
            });

            // Asti users have Eeva rights - so channel visibility is not checked!
            var channels = new ServiceConnectionListValidator(request.ChannelRelations, channelService, request.IsASTI ? UserRoleEnum.Eeva : UserRole());

            channels.Validate(this.ModelState);

            // Validate the items
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var srv = serviceAndChannelService.SaveServiceConnections(request, versionNumber);

            if (srv == null)
            {
                return(NotFound(new VmError()
                {
                    ErrorMessage = $"Service with id '{serviceId}' not found."
                }));
            }

            return(Ok(srv));
        }