public void VsSessionHost_ResetBinding_ErrorInReadingSolutionBinding()
        {
            // Arrange
            var tracker      = new ConfigurableActiveSolutionTracker();
            var testSubject  = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation {
                Key = "bla"
            };

            this.stateManager.SetBoundProject(boundProject);
            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            var section = ConfigurableSectionController.CreateDefault();

            testSubject.SetActiveSection(section);

            // Sanity
            this.stateManager.BoundProject.Should().Be(boundProject);
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(0);

            // Introduce an error
            this.solutionBinding.ReadSolutionBindingAction = () => { throw new Exception("boom"); };

            // Act (i.e. simulate loading a different solution)
            using (new AssertIgnoreScope()) // Ignore exception assert
            {
                tracker.SimulateActiveSolutionChanged();
            }

            // Assert
            this.stateManager.BoundProject.Should().BeNull();
        }
        public void VsSessionHost_ResetBinding_BoundSolutionWithActiveSectionScenario()
        {
            // Arrange
            var tracker      = new ConfigurableActiveSolutionTracker();
            var testSubject  = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation {
                Key = "bla"
            };

            this.stateManager.SetBoundProject(boundProject);
            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            var  section       = ConfigurableSectionController.CreateDefault();
            bool refreshCalled = false;

            section.RefreshCommand = new RelayCommand(() => refreshCalled = true);
            testSubject.SetActiveSection(section);

            // Sanity
            this.stateManager.BoundProject.Should().Be(boundProject);
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(0);

            // Act (simulate solution opened event)
            tracker.SimulateActiveSolutionChanged();

            // Assert
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(1);
            this.stateManager.BoundProjectKey.Should().Be(boundProject.Key, "Key was not set, will not be able to mark project as bound after refresh");
            this.stateManager.BoundProject.Should().Be(boundProject);
            refreshCalled.Should().BeTrue("Expected the refresh command to be called");
        }
        public void VsSessionHost_ResetBinding_NoOpenSolutionScenario()
        {
            // Arrange
            var tracker = new ConfigurableActiveSolutionTracker();

            this.CreateTestSubject(tracker);
            // Previous binding information that should be cleared once there's no solution
            var boundProject = new Integration.Service.ProjectInformation {
                Key = "bla"
            };

            this.stateManager.BoundProjectKey = boundProject.Key;
            this.stateManager.SetBoundProject(boundProject);

            // Sanity
            this.stateManager.BoundProject.Should().Be(boundProject);
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(0);

            // Act
            tracker.SimulateActiveSolutionChanged();

            // Assert
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(1);
            this.stateManager.BoundProject.Should().BeNull();
            this.stateManager.BoundProjectKey.Should().BeNull("Expecting the key to be reset to null");
        }
        public void VsSessionHost_ResetBinding_BoundSolutionWithNoActiveSectionScenario()
        {
            // Arrange
            var tracker      = new ConfigurableActiveSolutionTracker();
            var testSubject  = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation {
                Key = "bla"
            };

            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            this.stateManager.SetBoundProject(boundProject);

            // Sanity
            this.stateManager.BoundProject.Should().Be(boundProject);
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(0);

            // Act (simulate solution opened event)
            tracker.SimulateActiveSolutionChanged();

            // Assert that nothing has changed (should defer all the work to when the section is connected)
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(1);
            this.stateManager.BoundProject.Should().Be(boundProject);
            this.stateManager.BoundProjectKey.Should().BeNull("The key should only be set when there's active section to allow marking it once fetched all the projects");

            // Act (set active section)
            var  section       = ConfigurableSectionController.CreateDefault();
            bool refreshCalled = false;

            section.RefreshCommand = new RelayCommand(() => refreshCalled = true);
            testSubject.SetActiveSection(section);

            // Assert (section has refreshed, no further aborts were required)
            this.stepRunner.AbortAllNumberOfCalls.Should().Be(1);
            this.stateManager.BoundProjectKey.Should().Be(boundProject.Key, "Key was not set, will not be able to mark project as bound after refresh");
            this.stateManager.BoundProject.Should().Be(boundProject);
            refreshCalled.Should().BeTrue("Expected the refresh command to be called");
        }
        public void VsSessionHost_ResetBinding_ErrorInReadingSolutionBinding()
        {
            // Setup
            var tracker = new ConfigurableActiveSolutionTracker();
            var testSubject = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation { Key = "bla" };
            this.stateManager.SetBoundProject(boundProject);
            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            var section = ConfigurableSectionController.CreateDefault();
            testSubject.SetActiveSection(section);

            // Sanity
            this.stateManager.AssertBoundProject(boundProject);
            this.stepRunner.AssertAbortAllCalled(0);

            // Introduce an error
            this.solutionBinding.ReadSolutionBindingAction = () => { throw new Exception("boom"); };

            // Act (i.e. simulate loading a different solution)
            using (new AssertIgnoreScope()) // Ignore exception assert
            {
                tracker.SimulateActiveSolutionChanged();
            }

            // Verify
            this.stateManager.AssertNoBoundProject();
        }
        public void VsSessionHost_ResetBinding_BoundSolutionWithActiveSectionScenario()
        {
            // Setup
            var tracker = new ConfigurableActiveSolutionTracker();
            var testSubject = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation { Key = "bla" };
            this.stateManager.SetBoundProject(boundProject);
            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            var section = ConfigurableSectionController.CreateDefault();
            bool refreshCalled = false;
            section.RefreshCommand = new RelayCommand(() => refreshCalled = true);
            testSubject.SetActiveSection(section);

            // Sanity
            this.stateManager.AssertBoundProject(boundProject);
            this.stepRunner.AssertAbortAllCalled(0);

            // Act (simulate solution opened event)
            tracker.SimulateActiveSolutionChanged();

            // Verify
            this.stepRunner.AssertAbortAllCalled(1);
            Assert.AreEqual(boundProject.Key, this.stateManager.BoundProjectKey, "Key was not set, will not be able to mark project as bound after refresh");
            this.stateManager.AssertBoundProject(boundProject);
            Assert.IsTrue(refreshCalled, "Expected the refresh command to be called");
        }
        public void VsSessionHost_ResetBinding_BoundSolutionWithNoActiveSectionScenario()
        {
            // Setup
            var tracker = new ConfigurableActiveSolutionTracker();
            var testSubject = this.CreateTestSubject(tracker);
            var boundProject = new Integration.Service.ProjectInformation { Key = "bla" };
            this.solutionBinding.CurrentBinding = new Persistence.BoundSonarQubeProject(new Uri("http://bound"), boundProject.Key);
            this.stateManager.SetBoundProject(boundProject);

            // Sanity
            this.stateManager.AssertBoundProject(boundProject);
            this.stepRunner.AssertAbortAllCalled(0);

            // Act (simulate solution opened event)
            tracker.SimulateActiveSolutionChanged();

            // Verify that nothing has changed (should defer all the work to when the section is connected)
            this.stepRunner.AssertAbortAllCalled(1);
            this.stateManager.AssertBoundProject(boundProject);
            Assert.IsNull(this.stateManager.BoundProjectKey, "The key should only be set when there's active section to allow marking it once fetched all the projects");

            // Act (set active section)
            var section = ConfigurableSectionController.CreateDefault();
            bool refreshCalled = false;
            section.RefreshCommand = new RelayCommand(() => refreshCalled = true);
            testSubject.SetActiveSection(section);

            // Verify (section has refreshed, no further aborts were required)
            this.stepRunner.AssertAbortAllCalled(1);
            Assert.AreEqual(boundProject.Key, this.stateManager.BoundProjectKey, "Key was not set, will not be able to mark project as bound after refresh");
            this.stateManager.AssertBoundProject(boundProject);
            Assert.IsTrue(refreshCalled, "Expected the refresh command to be called");
        }
        public void VsSessionHost_ResetBinding_NoOpenSolutionScenario()
        {
            // Setup
            var tracker = new ConfigurableActiveSolutionTracker();
            this.CreateTestSubject(tracker);
            // Previous binding information that should be cleared once there's no solution
            var boundProject = new Integration.Service.ProjectInformation { Key = "bla" };
            this.stateManager.BoundProjectKey = boundProject.Key;
            this.stateManager.SetBoundProject(boundProject);

            // Sanity
            this.stateManager.AssertBoundProject(boundProject);
            this.stepRunner.AssertAbortAllCalled(0);

            // Act
            tracker.SimulateActiveSolutionChanged();

            // Verify
            this.stepRunner.AssertAbortAllCalled(1);
            this.stateManager.AssertNoBoundProject();
            Assert.IsNull(this.stateManager.BoundProjectKey, "Expecting the key to be reset to null");
        }