public void RefreshTest()
        {
            const string reportsPath = "reportsPath";
            const string uploadServiceUrl = "uploadServiceUrl";

            var fileList = new List<string> { "File1.trdx", "File2.trdx", "File3.trdx" };

            var retriever = new ProcessReportListRetrieverMock(fileList, reportsPath, uploadServiceUrl);

            var reportListRetriever = Mock.Create<IProcessReportListRetrieverWrapper>();

            Mock.Arrange(() => reportListRetriever.GetProcessReportList(Arg.IsAny<EventHandler<DataPortalResult<ProcessReportListRetriever>>>()))
                .DoInstead<EventHandler<DataPortalResult<ProcessReportListRetriever>>>(callback => callback(null, new DataPortalResult<ProcessReportListRetriever>(retriever, null, null)));

            var vm = new SelectReportFileViewModel();

            var propertiesChanged = new List<string>();

            vm.PropertyChanged += (o, e) => propertiesChanged.Add(e.PropertyName);

            vm.ProcessReportListRetriever = new Lazy<IProcessReportListRetrieverWrapper>(() => reportListRetriever);

            vm.Refresh();

            Mock.Assert(() => reportListRetriever.GetProcessReportList(Arg.IsAny<EventHandler<DataPortalResult<ProcessReportListRetriever>>>()), Occurs.Once());
            Assert.AreEqual(reportsPath, vm.TargetPhysicalFolder);
            Assert.AreEqual(uploadServiceUrl, vm.UploadServiceUrl);
            Assert.IsTrue(fileList.SequenceEqual(vm.FileList));
            Assert.IsTrue(propertiesChanged.Contains("SelectedFile"));
        }
        public void NotifyPropertyChangedTest()
        {
            var vm = new SelectReportFileViewModel();

            TestsHelper.TestPropertyWithNotifyPropertyChanged(vm, () => vm.SelectedFile);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(vm, () => vm.TargetPhysicalFolder);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(vm, () => vm.UploadServiceUrl);
        }
        public void DownloadReportFileCommand_Normal()
        {
            // arrange
            var vm = new SelectReportFileViewModel();

            var contentControl = new ContentControl { Content = "1.trdx" };

            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().ShowDialog()).Returns(true);

            vm.UploadServiceUrl = "http://localhost:5558/FileUploadHandler.ashx";

            var request = Mock.Create<WebRequest>(Behavior.Loose);
            Mock.Arrange(() => WebRequest.Create(Arg.AnyString)).Returns(request);

            var asyncResult = Mock.Create<IAsyncResult>(Behavior.CallOriginal);

            Mock.Arrange(() => request.BeginGetResponse(Arg.IsAny<AsyncCallback>(), null)).DoInstead<AsyncCallback, object>((callback, state) => callback(asyncResult));

            var response = Mock.Create<WebResponse>(Behavior.Loose);
            Mock.Arrange(() => response.ContentLength).Returns(1024 * 8);

            Mock.Arrange(() => request.EndGetResponse(asyncResult)).Returns(response);

            var stream = Mock.Create<Stream>(Behavior.Loose);

            Mock.Arrange(() => response.GetResponseStream()).Returns(stream);

            var exit = false;
            Mock.Arrange(() => stream.Read(Arg.IsAny<byte[]>(), 0, Arg.AnyInt)).Returns(() =>
            {
                if (!exit)
                {
                    exit = true;
                    return 1;
                }

                return 0;
            });

            Mock.Arrange(() => Deployment.Current.Dispatcher.BeginInvoke(Arg.IsAny<Action>())).DoInstead<Action>(a => a());

            var fileStream = Mock.Create<FileStream>(Behavior.Loose);

            var called = false;
            Mock.Arrange(() => fileStream.Write(Arg.IsAny<byte[]>(), Arg.AnyInt, Arg.AnyInt)).DoInstead(() => called = true);

            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().OpenFile()).Returns(fileStream);

            // act
            vm.DownloadReportFileCommand.Execute(contentControl);

            // assert
            Assert.IsFalse(vm.IsBusy);
            Assert.IsTrue(called);
        }
        public void DownloadReportFileCommand_Return()
        {
            // arrange
            var vm = new SelectReportFileViewModel();

            Mock.Arrange(() => new SaveFileDialog()).Throws<Exception>();

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

            // arrange
            Mock.Reset();

            var contentControl = new ContentControl { Content = "1.trdx" };

            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().ShowDialog()).Returns((bool?)null);

            Mock.Arrange(() => Uri.IsWellFormedUriString(Arg.AnyString, UriKind.Absolute)).Throws<Exception>();

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

            // arrange
            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().ShowDialog()).Returns(false);

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

            // arrange
            Mock.Reset();
            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().ShowDialog()).Returns(true);

            Mock.Arrange(() => new Uri(Arg.AnyString)).Throws<Exception>();

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

            // arrange
            Mock.Reset();
            Mock.Arrange(() => Arg.IsAny<SaveFileDialog>().ShowDialog()).Returns(true);

            vm.UploadServiceUrl = "http://localhost:5558/FileUploadHandler.ashx";

            var request = Mock.Create<WebRequest>(Behavior.Loose);
            Mock.Arrange(() => WebRequest.Create(Arg.AnyString)).Returns(request);

            var asyncResult = Mock.Create<IAsyncResult>(Behavior.CallOriginal);

            Mock.Arrange(() => request.BeginGetResponse(Arg.IsAny<AsyncCallback>(), null)).DoInstead<AsyncCallback, object>((callback, state) => callback(asyncResult));

            var response = Mock.Create<WebResponse>(Behavior.Loose);
            Mock.Arrange(() => request.EndGetResponse(asyncResult)).Returns(response);

            var wasLogged = false;
            var logger = Mock.Create<ILogger>(Behavior.CallOriginal);

            Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(SelectReportFileViewModel).FullName, "ContentLength = 0. Investigate log on server side for more details")).DoInstead(() => wasLogged = true);
            Mock.Arrange(() => vm.Logger).Returns(logger);

            var notifyFailureWasCalled = false;
            var popupFactory = Mock.Create<PopupFactory>(Behavior.Loose);
            Mock.Arrange(() => popupFactory.NotifyFailure(Arg.AnyString, "Popup_Error", 3, false)).DoInstead(() => notifyFailureWasCalled = true);
            Mock.Arrange(() => vm.PopupFactory).Returns(popupFactory);

            // act
            vm.DownloadReportFileCommand.Execute(contentControl);

            // assert
            Assert.IsFalse(vm.IsBusy);
            Assert.IsTrue(wasLogged);
            Assert.IsTrue(notifyFailureWasCalled);
        }
        public void OnUploadCanceled()
        {
            var vm = new SelectReportFileViewModel { ReplaceConnectionStrings = true, ReplaceConnectionStringsVisibility = true, };

            vm.OnUploadCanceled(null, null);

            Assert.IsFalse(vm.ReplaceConnectionStrings);
            Assert.IsFalse(vm.ReplaceConnectionStringsVisibility);
        }
        public void OnFilesSelected()
        {
            var vm = new SelectReportFileViewModel();
            
            vm.OnFilesSelected(null, null);

            Assert.IsTrue(vm.ReplaceConnectionStringsVisibility);
        }
        public void OnUploadFinished_FileListIsRefreshed()
        {
            const string reportsPath = "reportsPath";
            const string uploadServiceUrl = "uploadServiceUrl";

            var fileList = new List<string> { "File1.trdx", "File2.trdx", "File3.trdx" };

            var retriever = new ProcessReportListRetrieverMock();

            var reportListRetriever = Mock.Create<IProcessReportListRetrieverWrapper>();

            Mock.Arrange(() => reportListRetriever.GetProcessReportList(Arg.IsAny<EventHandler<DataPortalResult<ProcessReportListRetriever>>>()))
                .DoInstead<EventHandler<DataPortalResult<ProcessReportListRetriever>>>(callback => callback(null, new DataPortalResult<ProcessReportListRetriever>(retriever, null, null)));

            var vm = new SelectReportFileViewModel { ProcessReportListRetriever = new Lazy<IProcessReportListRetrieverWrapper>(() => reportListRetriever) };

            vm.Refresh();

            retriever = new ProcessReportListRetrieverMock(fileList, reportsPath, uploadServiceUrl);
            
            vm.OnUploadFinished(null, null);

            Mock.Assert(() => reportListRetriever.GetProcessReportList(Arg.IsAny<EventHandler<DataPortalResult<ProcessReportListRetriever>>>()), Occurs.Exactly(2));
            Assert.AreEqual(reportsPath, vm.TargetPhysicalFolder);
            Assert.AreEqual(uploadServiceUrl, vm.UploadServiceUrl);
            Assert.IsTrue(fileList.SequenceEqual(vm.FileList));
        }
        public void WhenReportListRetrievalFails_Refresh_DisplaysNotification()
        {
            var exception = new Exception("test");
            var reportListRetriever = Mock.Create<IProcessReportListRetrieverWrapper>();

            Mock.Arrange(() => reportListRetriever.GetProcessReportList(Arg.IsAny<EventHandler<DataPortalResult<ProcessReportListRetriever>>>()))
                .DoInstead<EventHandler<DataPortalResult<ProcessReportListRetriever>>>(callback => callback(null, new DataPortalResult<ProcessReportListRetriever>(null, exception, null)));

            var popupFactory = Mock.Create<PopupFactory>();

            var vm = new SelectReportFileViewModel {ProcessReportListRetriever = new Lazy<IProcessReportListRetrieverWrapper>(() => reportListRetriever), PopupFactory = popupFactory};

            vm.Refresh();

            Mock.Assert(() => popupFactory.NotifyFailure(exception, Arg.IsAny<string>(), Arg.IsAny<bool>()), Occurs.Once());
        }