public void ValidModel(ProvisionTypeEnum provisionType, bool organizationsSet)
        {
            // Arrange
            var id   = Guid.NewGuid();
            var list = new List <Guid> {
                id
            };

            commonServiceMockSetup.Setup(s => s.OrganizationExists(id, PublishingStatus.Published)).Returns(true);
            var validator = new ServiceProducerListValidator(new List <VmOpenApiServiceProducerIn>
            {
                new VmOpenApiServiceProducerIn
                {
                    ProvisionType         = provisionType.ToString(),
                    Organizations         = organizationsSet ? list : null,
                    AdditionalInformation = !organizationsSet ? new List <VmOpenApiLanguageItem> {
                        new VmOpenApiLanguageItem {
                            Value = "test"
                        }
                    } : null
                }
            }, list, commonService);

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

            // Assert
            controller.ModelState.IsValid.Should().BeTrue();
        }
        private VmServiceProducer CreateModel(ProvisionTypeEnum provisionType, string organizationId, string additionalInformation)
        {
            var producer = new VmServiceProducer
            {
                ProvisionType         = CacheManager.TypesCache.Get <ProvisionType>(provisionType.ToString()),
                AdditionalInformation = new Dictionary <string, string>()
                {
                    { "fi", additionalInformation }
                }
            };

            if (provisionType == ProvisionTypeEnum.SelfProduced)
            {
                producer.SelfProducers = string.IsNullOrEmpty(organizationId)
                    ? new List <Guid>()
                    : new List <Guid> {
                    Guid.Parse(organizationId)
                };
            }
            else
            {
                producer.Organization = string.IsNullOrEmpty(organizationId)
                    ? (Guid?)null
                    : Guid.Parse(organizationId);
            }

            return(producer);
        }
        public void TranslateServiceProducerEntityTest(ProvisionTypeEnum provisionType, string organizationId, string additionalInformation)
        {
            var model       = CreateModel(provisionType, organizationId, additionalInformation);
            var toTranslate = new List <VmServiceProducer> {
                model
            };

            RegisterDbSet(new List <ServiceProducer>(), unitOfWorkMockSetup);
            RegisterDbSet(new List <ServiceProducerAdditionalInformation>(), unitOfWorkMockSetup);
            RegisterDbSet(new List <ServiceProducerOrganization>(), unitOfWorkMockSetup);

            var translations = RunTranslationModelToEntityTest <VmServiceProducer, ServiceProducer>(translators, toTranslate, unitOfWorkMock);
            var translation  = translations.First();

            Assert.Equal(toTranslate.Count, translations.Count);
            CheckTranslation(provisionType, model, translation);
        }
        public void ProvisionTypeIsSet_NoOrganizationOrAdditionalInformationSet(ProvisionTypeEnum provisionType)
        {
            // Arrange
            var validator = new ServiceProducerListValidator(new List <VmOpenApiServiceProducerIn>
            {
                new VmOpenApiServiceProducerIn
                {
                    ProvisionType = provisionType.ToString(),
                }
            }, null, commonService);

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

            // Assert
            controller.ModelState.IsValid.Should().BeFalse();
        }
        private void CheckTranslation(ProvisionTypeEnum provisionType, VmServiceProducer source, ServiceProducer target)
        {
            target.Id.Should().NotBe(Guid.Empty);
            target.ProvisionTypeId.Should().Be(source.ProvisionType);
            switch (provisionType)
            {
            case ProvisionTypeEnum.SelfProduced:
                target.AdditionalInformations.Count.Should().Be(0);
                target.Organizations.Count.Should().Be(source.SelfProducers.Count);
                if (source.SelfProducers.Count > 0)
                {
                    target.Organizations.First().OrganizationId.Should().Be(source.SelfProducers.First());
                }
                break;

            case ProvisionTypeEnum.PurchaseServices:
                if (source.Organization.HasValue)
                {
                    target.Organizations.Count.Should().Be(1);
                    target.Organizations.First().OrganizationId.Should().Be(source.Organization.Value);
                }
                else
                {
                    target.Organizations.Should().BeEmpty();
                }

                if (string.IsNullOrEmpty(source.AdditionalInformation["fi"]))
                {
                    target.AdditionalInformations.Should().BeEmpty();
                }
                else
                {
                    var ai = target.AdditionalInformations.FirstOrDefault();
                    ai.Should().NotBeNull();
                    ai.Text.Should().Be(source.AdditionalInformation["fi"]);
                    ai.LocalizationId.Should().Be(LanguageCode.fi.ToString().GetGuid());
                }
                break;

            case ProvisionTypeEnum.Other:
                if (source.Organization.HasValue)
                {
                    target.Organizations.Count.Should().Be(1);
                    target.Organizations.First().OrganizationId.Should().Be(source.Organization.Value);
                    target.AdditionalInformations.Should().BeEmpty();
                }
                else if (!string.IsNullOrEmpty(source.AdditionalInformation["fi"]))
                {
                    target.Organizations.Should().BeEmpty();
                    var ai = target.AdditionalInformations.FirstOrDefault();
                    ai.Should().NotBeNull();
                    ai.Text.Should().Be(source.AdditionalInformation["fi"]);
                    ai.LocalizationId.Should().Be(LanguageCode.fi.ToString().GetGuid());
                }
                else
                {
                    target.Organizations.Should().BeEmpty();
                    target.AdditionalInformations.Should().BeEmpty();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(provisionType), provisionType, null);
            }
        }