public void MediaServicesV3StreamingPolicyServiceWithDRMSettings()
        {
            // Act
            var streamingService = new MediaServicesV3CustomStreamingPolicyService();

            // Assert
            Xunit.Assert.NotNull(streamingService);
        }
        public void MediaServicesV3StreamingPolicyServiceWithDRMSettingsGetContentKeyPolicyAsync(string streamingPolicyName, bool expectedValue, Type typeResult, Type typeException)
        {
            // Act
            var streamPolService = new MediaServicesV3CustomStreamingPolicyService();

            if (expectedValue)
            {
                var pol = streamPolService.GetCustomStreamingPolicyFromMemory(streamingPolicyName);
                if (typeResult != null)
                {
                    Xunit.Assert.NotNull(pol);
                    Xunit.Assert.IsType(typeResult, pol);
                }
                else
                {
                    Xunit.Assert.Null(pol);
                }
            }
            else
            {
                _ = Xunit.Assert.Throws(typeException, () => streamPolService.GetCustomStreamingPolicyFromMemory(streamingPolicyName));
            }
        }
        public async void MediaServicesV3ServiceCreateLocatorTestWithPolicyUpdate(RequestMediaServicesLocatorCreateDTO locatorCreateDTO, string updatePolicy, ContentKeyPolicy contentKeyPolicy)
        {
            var amsV3SdkWrapper  = Mock.Of <IMediaServicesV3SdkWrapper>();
            var storageService   = Mock.Of <IStorageService>();
            var settingsProvider = Mock.Of <ISettingsProvider>();

            // Arrange
            string assetName  = "myassetname";
            var    amsAccount = new MediaService(storageAccounts: new List <StorageAccount>()
            {
                new StorageAccount()
                {
                    Id = MediaServicesV3PublicationTestData.DefaultStorageId
                }
            });

            // Arrange Mocks
            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <Asset>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new Asset(name: assetName));

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.MediaservicesGetAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(amsAccount);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingEndpointsListAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(MockPageSe);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorListPathsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(Paths);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorCreateAsync(It.IsAny <string>(), It.IsAny <StreamingLocator>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new StreamingLocator(assetName: "asset45", streamingPolicyName: locatorCreateDTO.StreamingPolicyName, defaultContentKeyPolicyName: locatorCreateDTO.ContentKeyPolicyName, contentKeys: new List <StreamingLocatorContentKey>()
            {
                new StreamingLocatorContentKey(Guid.NewGuid(), StreamingLocatorContentKeyType.CommonEncryptionCenc), new StreamingLocatorContentKey(Guid.NewGuid(), StreamingLocatorContentKeyType.CommonEncryptionCbcs)
            }));

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.ContentKeyPolicyGetAsync(locatorCreateDTO.ContentKeyPolicyName, It.IsAny <CancellationToken>()))
            .ReturnsAsync(contentKeyPolicy);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingPolicyGetAsync(MediaServicesV3PublicationTestData.GoodDRMStreamingPolicy, It.IsAny <CancellationToken>()))
            .ReturnsAsync(new StreamingPolicy());

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.ContentKeyPolicyCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <IEnumerable <ContentKeyPolicyOption> >(), It.IsAny <string>(), It.IsAny <CancellationToken>()));

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmOpenIdConnectDiscoveryDocument"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmOpenIdConnectDiscoveryDocument);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayPfxPassword"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayPfxPassword);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayAskHex"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayAskHex);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayCertificateB64"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayCertAsSecretStringStatic);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmEnableContentKeyPolicyUpdate"))
            .Returns(updatePolicy);

            IMediaServicesV3ContentKeyPolicyService      amsContentKeyPolService2 = new MediaServicesV3ContentKeyPolicyService(settingsProvider, LogKey);
            IMediaServicesV3CustomStreamingPolicyService amsStreamingPolService2  = new MediaServicesV3CustomStreamingPolicyService();

            // Act
            var amsV3PubServices = new MediaServicesV3PublicationService(amsContentKeyPolService2, amsStreamingPolService2, amsV3SdkWrapper, storageService, settingsProvider, Log);

            // Assert
            var result = await amsV3PubServices.LocatorCreateAsync(
                locatorCreateDTO.ContainerUri,
                locatorCreateDTO.StreamingPolicyName,
                locatorCreateDTO.ContentKeyPolicyName,
                locatorCreateDTO.TimeBasedFilter,
                locatorCreateDTO.OperationContext,
                locatorCreateDTO.GenerateAudioFilters).ConfigureAwait(false);

            result.ShouldBeOfType <ServiceOperationResultMediaServicesV3LocatorCreate>();
            result.LocatorName.ShouldBeOfType <string>();
        }
        public async void MediaServicesV3ServiceCreateLocatorFailureTests(RequestMediaServicesLocatorCreateDTO locatorCreateDTO, bool expectedValue, Type typeException)
        {
            // Arrange
            var    amsV3SdkWrapper         = Mock.Of <IMediaServicesV3SdkWrapper>();
            var    storageService          = Mock.Of <IStorageService>();
            var    amsContentKeyPolService = Mock.Of <IMediaServicesV3ContentKeyPolicyService>();
            var    settingsProvider        = Mock.Of <ISettingsProvider>();
            string assetName  = "myassetname";
            var    amsAccount = new MediaService(storageAccounts: new List <StorageAccount>()
            {
                new StorageAccount()
                {
                    Id = MediaServicesV3PublicationTestData.DefaultStorageId
                }
            });

            // Arrange Mocks
            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue(EnvironmentTypeConstants.EnvironmentTypeSettingName))
            .Returns(EnvironmentTypeConstants.EnvironmentTypeDevelopment);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmEnableContentKeyPolicyUpdate"))
            .Returns("true");

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <Asset>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new Asset(name: assetName));

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetFiltersCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <string>(), TimingIsWrong(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.MediaservicesGetAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(amsAccount);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingEndpointsListAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(MockPageSe);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorListPathsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(Paths);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorCreateAsync(It.IsAny <string>(), It.IsAny <StreamingLocator>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new StreamingLocator(assetName: "asset45", streamingPolicyName: locatorCreateDTO.StreamingPolicyName, defaultContentKeyPolicyName: locatorCreateDTO.ContentKeyPolicyName, contentKeys: new List <StreamingLocatorContentKey>()));

            if (locatorCreateDTO.ContentKeyPolicyName == MediaServicesV3PublicationTestData.GoodDataDtoDRM.ContentKeyPolicyName)
            {
                ContentKeyPolicy pol = null;

                Mock.Get(amsV3SdkWrapper)
                .Setup(x => x.ContentKeyPolicyGetAsync(MediaServicesV3PublicationTestData.GoodDataDtoDRM.ContentKeyPolicyName, It.IsAny <CancellationToken>()))
                .ReturnsAsync(pol);

                Mock.Get(amsV3SdkWrapper)
                .Setup(x => x.ContentKeyPolicyCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <List <ContentKeyPolicyOption> >(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
                .ThrowsAsync(new Exception());
            }

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetFiltersCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AssetFilter>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            Mock.Get(amsContentKeyPolService)
            .Setup(x => x.GetContentKeyPolicyFromMemory(It.IsAny <string>()))
            .Returns(new MediaServicesV3CustomContentKeyPolicyMultiDrmKey(
                         MediaServicesV3PublicationTestData.AmsDrmOpenIdConnectDiscoveryDocument,
                         MediaServicesV3PublicationTestData.AmsDrmFairPlayPfxPassword,
                         MediaServicesV3PublicationTestData.AmsDrmFairPlayAskHex,
                         MediaServicesV3PublicationTestData.AmsDrmFairPlayPfx));

            IMediaServicesV3CustomStreamingPolicyService amsStreamingPolService2 = new MediaServicesV3CustomStreamingPolicyService();

            // Act
            var amsV3PubServices = new MediaServicesV3PublicationService(amsContentKeyPolService, amsStreamingPolService2, amsV3SdkWrapper, storageService, settingsProvider, Log);

            // Assert

            if (expectedValue)
            {
            }
            else
            {
                _ = await Xunit.Assert.ThrowsAsync(typeException, async() =>
                                                   await amsV3PubServices.LocatorCreateAsync(
                                                       locatorCreateDTO.ContainerUri,
                                                       locatorCreateDTO.StreamingPolicyName,
                                                       locatorCreateDTO.ContentKeyPolicyName,
                                                       locatorCreateDTO.TimeBasedFilter,
                                                       locatorCreateDTO.OperationContext,
                                                       locatorCreateDTO.GenerateAudioFilters).ConfigureAwait(false)).ConfigureAwait(false);
            }
        }
        public async void MediaServicesV3ServiceCreateLocatorTest(RequestMediaServicesLocatorCreateDTO locatorCreateDTO, bool expectedValue, Type typeException, string environmentType)
        {
            // Arrange
            var storageService   = Mock.Of <IStorageService>();
            var amsV3SdkWrapper  = Mock.Of <IMediaServicesV3SdkWrapper>();
            var settingsProvider = Mock.Of <ISettingsProvider>();

            string assetName  = "myassetname";
            var    amsAccount = new MediaService(storageAccounts: new List <StorageAccount>()
            {
                new StorageAccount()
                {
                    Id = MediaServicesV3PublicationTestData.DefaultStorageId
                }
            });

            // Arrange Mocks
            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <Asset>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new Asset(name: assetName));

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetFiltersCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <string>(), TimingIsWrong(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.MediaservicesGetAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(amsAccount);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingEndpointsListAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(MockPageSe);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorListPathsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(Paths);

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.StreamingLocatorCreateAsync(It.IsAny <string>(), It.IsAny <StreamingLocator>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new StreamingLocator(assetName: "asset45", streamingPolicyName: locatorCreateDTO.StreamingPolicyName, defaultContentKeyPolicyName: locatorCreateDTO.ContentKeyPolicyName, contentKeys: new List <StreamingLocatorContentKey>()));

            if (locatorCreateDTO.ContentKeyPolicyName == MediaServicesV3PublicationTestData.GoodContentKeyPolicyDrm)
            {
                // let's add a CENC and CBCS keys
                Mock.Get(amsV3SdkWrapper)
                .Setup(x => x.StreamingLocatorCreateAsync(It.IsAny <string>(), It.IsAny <StreamingLocator>(), It.IsAny <CancellationToken>()))
                .ReturnsAsync(new StreamingLocator(assetName: "asset45", streamingPolicyName: locatorCreateDTO.StreamingPolicyName, defaultContentKeyPolicyName: locatorCreateDTO.ContentKeyPolicyName, contentKeys: new List <StreamingLocatorContentKey>()
                {
                    new StreamingLocatorContentKey(Guid.NewGuid(), StreamingLocatorContentKeyType.CommonEncryptionCenc), new StreamingLocatorContentKey(Guid.NewGuid(), StreamingLocatorContentKeyType.CommonEncryptionCbcs)
                }));
            }
            else
            {
                Mock.Get(amsV3SdkWrapper)
                .Setup(x => x.StreamingLocatorCreateAsync(It.IsAny <string>(), It.IsAny <StreamingLocator>(), It.IsAny <CancellationToken>()))
                .ReturnsAsync(new StreamingLocator(assetName: "asset45", streamingPolicyName: locatorCreateDTO.StreamingPolicyName, defaultContentKeyPolicyName: locatorCreateDTO.ContentKeyPolicyName, contentKeys: new List <StreamingLocatorContentKey>()));
            }

            Mock.Get(amsV3SdkWrapper)
            .Setup(x => x.AssetFiltersCreateOrUpdateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AssetFilter>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new AssetFilter());

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue(EnvironmentTypeConstants.EnvironmentTypeSettingName))
            .Returns(environmentType);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmOpenIdConnectDiscoveryDocument"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmOpenIdConnectDiscoveryDocument);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayPfxPassword"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayPfxPassword);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayAskHex"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayAskHex);

            Mock.Get(settingsProvider)
            .Setup(x => x.GetAppSettingsValue("AmsDrmFairPlayCertificateB64"))
            .Returns(MediaServicesV3PublicationTestData.AmsDrmFairPlayCertAsSecretStringStatic);

            Mock.Get(storageService)
            .Setup(x => x.ListBlobsAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(MediaServicesV3PublicationTestData.GoodBlobItemListWithIsmFile);

            Mock.Get(storageService)
            .Setup(x => x.DownloadHttpRangeAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>(), default))
            .ReturnsAsync(MediaServicesV3PublicationTestData.GoodIsmFileBlobDownloadInfo);

            IMediaServicesV3ContentKeyPolicyService      amsContentKeyPolService2 = new MediaServicesV3ContentKeyPolicyService(settingsProvider, LogKey);
            IMediaServicesV3CustomStreamingPolicyService amsStreamingPolService2  = new MediaServicesV3CustomStreamingPolicyService();

            // Act
            var amsV3PubServices = new MediaServicesV3PublicationService(amsContentKeyPolService2, amsStreamingPolService2, amsV3SdkWrapper, storageService, settingsProvider, Log);

            // Assert
            if (expectedValue == false)
            {
                _ = await Xunit.Assert.ThrowsAsync(typeException, async() =>
                                                   await amsV3PubServices.LocatorCreateAsync(
                                                       locatorCreateDTO.ContainerUri,
                                                       locatorCreateDTO.StreamingPolicyName,
                                                       locatorCreateDTO.ContentKeyPolicyName,
                                                       locatorCreateDTO.TimeBasedFilter,
                                                       locatorCreateDTO.OperationContext,
                                                       locatorCreateDTO.GenerateAudioFilters).ConfigureAwait(false)).ConfigureAwait(false);
            }
            else
            {
                var result = await amsV3PubServices.LocatorCreateAsync(
                    locatorCreateDTO.ContainerUri,
                    locatorCreateDTO.StreamingPolicyName,
                    locatorCreateDTO.ContentKeyPolicyName,
                    locatorCreateDTO.TimeBasedFilter,
                    locatorCreateDTO.OperationContext,
                    locatorCreateDTO.GenerateAudioFilters).ConfigureAwait(false);

                result.ShouldBeOfType <ServiceOperationResultMediaServicesV3LocatorCreate>();
                result.LocatorName.ShouldBeOfType <string>();
            }
        }