public void Add_ExternalSourceExists()
        {
            // Arrange
            var sourceId       = "sourceId";
            var userName       = "******";
            var unitOfWork     = unitOfWorkMockSetup.Object;
            var contextManager = new TestContextManager(unitOfWork, unitOfWork);

            UserIdentificationMock.Setup(s => s.UserName).Returns(userName);
            ExternalSourceRepoMock.Setup(s => s.All())
            .Returns(new List <ExternalSource>()
            {
                new ExternalSource {
                    SourceId = sourceId, RelationId = userName, ObjectType = typeof(Model.Models.ServiceChannel).Name
                }
            }.AsQueryable());
            var serviceUtilities = new ServiceUtilities(UserIdentificationMock.Object, LockingManager, contextManager, UserOrganizationService,
                                                        VersioningManager, UserInfoService, UserOrganizationChecker);

            var service = new ChannelService(contextManager, UserIdentification, translationManagerMockSetup.Object, TranslationManagerVModel,
                                             Logger, ServiceChannelLogic, serviceUtilities, CommonService, new VmListItemLogic(), DataUtils, new VmOwnerReferenceLogic(),
                                             AddressService, CacheManager, PublishingStatusCache, VersioningManager, UserOrganizationChecker, UrlService);

            // Act
            Action act = () => service.AddServiceChannel(new VmOpenApiServiceChannelIn()
            {
                SourceId = sourceId
            }, false, DefaultVersion, null);

            // Assert
            act.ShouldThrowExactly <ExternalSourceExistsException>(string.Format(CoreMessages.OpenApi.ExternalSourceExists, sourceId));
        }
        private IActionResult PostServiceChannel <TModel, TValidator>(TModel vmBase, TValidator validator) where TModel : class, IVmOpenApiServiceChannelIn where TValidator : ServiceChannelValidator <TModel>
        {
            // Get languages user has added into model
            vmBase.AvailableLanguages = GetServiceChannelAvailableLanguages(vmBase).ToList();
            // For post method the required and available languages are the same
            validator.RequiredLanguages  = vmBase.AvailableLanguages;
            validator.AvailableLanguages = vmBase.AvailableLanguages;

            // Set publishing status as Draft or Published
            if (vmBase.PublishingStatus != PublishingStatus.Published.ToString())
            {
                vmBase.PublishingStatus = PublishingStatus.Draft.ToString();
            }

            // Validate request data
            validator.Validate(ModelState);
            // Check validation status
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            SetAddressProperties(vmBase);

            return(Ok(ChannelService.AddServiceChannel(vmBase, Settings.AllowAnonymous, versionNumber)));
        }
        public void Add_CannotGetUser()
        {
            // Arrange
            var unitOfWork     = unitOfWorkMockSetup.Object;
            var contextManager = new TestContextManager(unitOfWork, unitOfWork);

            UserIdentificationMock.Setup(s => s.UserName).Returns((string)null);
            var serviceUtilities = new ServiceUtilities(UserIdentificationMock.Object, LockingManager, contextManager, UserOrganizationService,
                                                        VersioningManager, UserInfoService, UserOrganizationChecker);

            var service = new ChannelService(contextManager, UserIdentification, translationManagerMockSetup.Object, TranslationManagerVModel,
                                             Logger, ServiceChannelLogic, serviceUtilities, CommonService, new VmListItemLogic(), DataUtils, new VmOwnerReferenceLogic(),
                                             AddressService, CacheManager, PublishingStatusCache, VersioningManager, UserOrganizationChecker, UrlService);

            // Act
            Action act = () => service.AddServiceChannel(new VmOpenApiServiceChannelIn(), false, DefaultVersion, null);

            // Assert
            act.ShouldThrowExactly <Exception>(CoreMessages.OpenApi.RelationIdNotFound);
        }