Пример #1
0
        [Ignore] //Ignored because throws System.OutOfMemoryException
        public void OnUploadFinished()
        {
            // arrange
            var vm = new FieldFileViewModel();

            var privateAccessor = new PrivateAccessor(vm);
            privateAccessor.SetProperty("UploadError", true);

            var fileProcess = Mock.Create<IFileProcess>(Behavior.Loose);
            fileProcess.IsUploading = true;
            fileProcess.OriginalFileName = "1.doc";
            fileProcess.FileName = "EEE6B881-F39E-4CD0-9B0B-6CF777DCD1DA.doc";
            Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess);

            Mock.NonPublic.Arrange<string>(vm, "Message1").Throws<Exception>();

            try
            {
                // act
                vm.OnUploadFinished(null, null);
            }
            catch (Exception ex)
            {
                // assert
                Assert.Fail("Expected no exception, but got: " + ex.Message);
            }

            // assert
            Assert.IsFalse(fileProcess.IsUploading);

            // arrange
            privateAccessor.SetProperty("UploadError", false);

            Mock.NonPublic.Arrange<string>(vm, "Message1").CallOriginal();

            Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(a => a(null));
                
            Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess);

            var saveWasCalled = false;
            Mock.NonPublic.Arrange(vm, "Save").DoInstead(() => saveWasCalled = true);

            // act
            vm.OnUploadFinished(null, null);

            // assert
            Assert.AreEqual("File '1.doc' uploaded successfully.", vm.Message);
            Assert.AreEqual(FileControlMode.NotifySuccess, vm.CurrentState);
            Assert.IsTrue(saveWasCalled);
            Assert.AreEqual("1.doc", fileProcess.OriginalFileName);
            Assert.AreEqual("EEE6B881-F39E-4CD0-9B0B-6CF777DCD1DA.doc", fileProcess.FileName);

            // arrange
            saveWasCalled = false;

            var logger = Mock.Create<ILogger>(Behavior.CallOriginal);
            Mock.Arrange(() => logger.Log(LogSeverity.Warning, typeof(FieldFileViewModel).ToString(), Arg.IsAny<Exception>())).DoNothing().MustBeCalled();
            vm.Logger = logger;

            Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(action => action(new Exception()));
            fileProcess.FileName = Guid.NewGuid() + ".doc";

            // act
            vm.OnUploadFinished(null, null);

            // assert
            Assert.IsFalse(saveWasCalled);
            Assert.IsNull(fileProcess.OriginalFileName);
            Assert.IsNull(fileProcess.FileName);
            Assert.AreEqual("Error uploading file.", vm.Message);
            Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage"));
            Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState);
            Mock.Assert(logger);

            // arrange
            fileProcess.OriginalFileName = "1.doc";
            fileProcess.FileName = Guid.NewGuid() + ".doc";
            vm.Message = null;
            privateAccessor.SetProperty("ExceptionMessage", null);
            vm.CurrentState = FileControlMode.Download;

            var lavel1WcfErrorInfo = new WcfErrorInfo();
            var lavel2WcfErrorInfo = new WcfErrorInfo();
            var lavel3WcfErrorInfo = new WcfErrorInfo();
            lavel1WcfErrorInfo.InnerError = lavel2WcfErrorInfo;
            lavel2WcfErrorInfo.InnerError = lavel3WcfErrorInfo;

            lavel3WcfErrorInfo.ExceptionTypeName = typeof(DirectoryNotFoundException).FullName;
            lavel3WcfErrorInfo.Message = @"Directory D:\ImportantFiles\ not found";
            lavel3WcfErrorInfo.StackTrace = "at ..., at ...";

            var exception = new DataPortalException(lavel1WcfErrorInfo);
            //dataPortalResult = new DataPortalResult<FileControlUploadFileCommand>(null, exception, null);
            Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(action => action(exception));

            // act
            vm.OnUploadFinished(null, null);

            // assert
            Assert.IsFalse(saveWasCalled);
            Assert.IsNull(fileProcess.OriginalFileName);
            Assert.IsNull(fileProcess.FileName);
            Assert.AreEqual("Error uploading file.", vm.Message);
            Assert.AreEqual(string.Format(@"System.IO.DirectoryNotFoundException{0}Directory D:\ImportantFiles\ not found{0}at ..., at ...", Environment.NewLine),  privateAccessor.GetProperty("ExceptionMessage"));
            Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState);

            // arrange
            fileProcess.OriginalFileName = "1.doc";
            fileProcess.FileName = Guid.NewGuid() + ".doc";
            vm.Message = null;
            privateAccessor.SetProperty("ExceptionMessage", null);
            vm.CurrentState = FileControlMode.Download;

            lavel3WcfErrorInfo.ExceptionTypeName = typeof(WebException).FullName;
            lavel3WcfErrorInfo.Message = "Host not responding";
            lavel3WcfErrorInfo.StackTrace = "at ..., at ...";

            // act
            vm.OnUploadFinished(null, null);

            // assert
            Assert.IsFalse(saveWasCalled);
            Assert.IsNull(fileProcess.OriginalFileName);
            Assert.IsNull(fileProcess.FileName);
            Assert.AreEqual("DocumentProcessor service is not responding. Please contact your administrator to make sure it is running and 'File Processor URI' is set correcltly in the Control Panel.", vm.Message);
            Assert.AreEqual(string.Format(@"System.Net.WebException{0}Host not responding{0}at ..., at ...", Environment.NewLine), privateAccessor.GetProperty("ExceptionMessage"));
            Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState);

            // arrange
            fileProcess.OriginalFileName = "1.doc";
            fileProcess.FileName = Guid.NewGuid() + ".doc";
            vm.Message = null;
            privateAccessor.SetProperty("ExceptionMessage", null);
            vm.CurrentState = FileControlMode.Download;

            lavel3WcfErrorInfo.ExceptionTypeName = typeof(FileNotFoundException).FullName;
            lavel3WcfErrorInfo.Message = @"File D:\ImportantFiles\1.doc not exists";
            lavel3WcfErrorInfo.StackTrace = "at ..., at ...";

            // act
            vm.OnUploadFinished(null, null);

            // assert
            Assert.IsFalse(saveWasCalled);
            Assert.IsNull(fileProcess.OriginalFileName);
            Assert.IsNull(fileProcess.FileName);
            Assert.AreEqual(string.Format(@"Uploaded file in temp location was not found{0}Please verify System Options\System Paths", Environment.NewLine), vm.Message);
            Assert.AreEqual(string.Format(@"System.IO.FileNotFoundException{0}File D:\ImportantFiles\1.doc not exists{0}at ..., at ...", Environment.NewLine), privateAccessor.GetProperty("ExceptionMessage"));
            Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState);
        }