public void TestAccessPolicyCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new AccessPolicyData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("AccessPolicies", It.IsAny<object>()));
            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            IAccessPolicy policy = _mediaContext.AccessPolicies.Create("Empty", TimeSpan.FromSeconds(1), AccessPermissions.None);
            Assert.AreEqual(expected.Name, policy.Name);
            Assert.AreEqual(0, exceptionCount);
        }
        public void TestAccessPolicyDeleteRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var policy = new AccessPolicyData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = policy };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Files", policy));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(policy));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(policy))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            policy.SetMediaContext(_mediaContext);

            policy.Delete();

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestAssetCreateFailedRetryMessageLengthLimitExceeded()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Assets", It.IsAny<object>()));

            int exceptionCount = 10;

            var expected = new AssetData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("test", WebExceptionStatus.MessageLengthLimitExceeded);

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            try
            {
                IAsset asset = _mediaContext.Assets.Create("Empty", AssetCreationOptions.None);
            }
            catch (WebException x)
            {
                Assert.AreEqual(9, exceptionCount);
                Assert.AreEqual(fakeException, x);
                throw;
            }

            Assert.Fail("Expected exception");
        }
        public void TestAssetCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Assets", It.IsAny<object>()));

            int exceptionCount = 2;

            var expected = new AssetData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            IAsset asset = _mediaContext.Assets.Create("Empty", AssetCreationOptions.None);
            Assert.AreEqual(expected.Name, asset.Name);
            Assert.AreEqual(0, exceptionCount);
        }
        public void TestJobUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var job = new JobData { Name = "testData", Id = "id:someid" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = job };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Jobs", job));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(job));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(job))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            job.SetMediaContext(_mediaContext);

            job.Update();

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestJobSubmitRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var job = new JobData { Name = "testData", TemplateId = "id:sometemplate" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = job };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Jobs", job));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(job));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(SaveChangesOptions.Batch, job))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            // Cannot mock DataServiceQuery. Throw artificial exception to mark pass through saving changes.
            string artificialExceptionMessage = "artificialException";
            dataContextMock.Setup((ctxt) => ctxt.CreateQuery<JobData>("Jobs")).Throws(new Exception(artificialExceptionMessage));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            job.SetMediaContext(_mediaContext);

            try
            {
                job.Submit();
            }
            catch (Exception x)
            {
                Assert.AreEqual(artificialExceptionMessage, x.Message);
            }

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestLocatorUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var locator = new LocatorData { Name = "testData", Type = (int)LocatorType.OnDemandOrigin };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = locator };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Locators", locator));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(locator));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(locator))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            locator.SetMediaContext(_mediaContext);

            locator.Update(DateTime.Now);

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestLocatorDeleteRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var locator = new LocatorData { Name = "testData", Type = (int)LocatorType.OnDemandOrigin };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = locator };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Locators", locator));
            dataContextMock.Setup((ctxt) => ctxt.DeleteObject(locator));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(locator))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            // Cannot mock DataServiceQuery. Throw artificial exception to mark pass through saving changes.
            string artificialExceptionMessage = "artificialException";
            dataContextMock.Setup((ctxt) => ctxt.CreateQuery<AssetData>("Assets")).Throws(new Exception(artificialExceptionMessage));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            locator.SetMediaContext(_mediaContext);

            try
            {
            locator.Delete();
            }
            catch (Exception x)
            {
                Assert.AreEqual(artificialExceptionMessage, x.Message);
            }

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestLocatorCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new LocatorData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Files", It.IsAny<object>()));
            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            var asset = new AssetData { Name = "testAssetData" };

            asset.SetMediaContext(_mediaContext);
            ILocator locator = _mediaContext.Locators.CreateLocator(LocatorType.None, asset, new AccessPolicyData());
            Assert.AreEqual(expected.Name, locator.Name);
            Assert.AreEqual(0, exceptionCount);
        }