SetProjects() public method

Will clear any existing project view models and will replace them with the specified ones. The project view models will be alphabetically sorted by ProjectInformation.Name for the StringComparer.CurrentCulture
public SetProjects ( IEnumerable projectsToSet ) : void
projectsToSet IEnumerable
return void
        public void ServerViewModel_Ctor()
        {
            // Setup
            var connInfo = new ConnectionInformation(new Uri("https://myawesomeserver:1234/"));
            IEnumerable<ProjectInformation> projects = new[]
            {
                new ProjectInformation { Name = "Project1", Key="1" },
                new ProjectInformation { Name = "Project2", Key="2" },
                new ProjectInformation { Name = "Project3", Key="3" },
                new ProjectInformation { Name = "Project4", Key="4" }
            };
            string[] projectKeys = projects.Select(x => x.Key).ToArray();

            // Case 0: default constructed state
            // Act
            var emptyViewModel = new ServerViewModel(connInfo);

            // Verify
            Assert.IsTrue(emptyViewModel.IsExpanded);
            Assert.IsFalse(emptyViewModel.ShowAllProjects);

            // Case 1, projects with default IsExpanded value
            // Act
            var viewModel = new ServerViewModel(connInfo);
            viewModel.SetProjects(projects);

            // Verify
            string[] vmProjectKeys = viewModel.Projects.Select(x => x.Key).ToArray();

            Assert.IsTrue(viewModel.ShowAllProjects);
            Assert.IsTrue(viewModel.IsExpanded);
            Assert.AreEqual(connInfo.ServerUri, viewModel.Url);
            CollectionAssert.AreEqual(
                expected: projectKeys,
                actual: vmProjectKeys,
                message: $"VM projects [{string.Join(", ", vmProjectKeys)}] do not match input projects [{string.Join(", ", projectKeys)}]"
            );

            // Case 2, null projects with non default IsExpanded value
            // Act
            var viewModel2 = new ServerViewModel(connInfo, isExpanded: false);

            // Verify
            Assert.AreEqual(0, viewModel2.Projects.Count, "Not expecting projects");
            Assert.IsFalse(viewModel2.IsExpanded);
        }
        public void ServerViewModel_SetProjects()
        {
            // Setup
            var connInfo = new ConnectionInformation(new Uri("https://myawesomeserver:1234/"));
            var viewModel = new ServerViewModel(connInfo);
            IEnumerable<ProjectInformation> projects = new[]
            {
                new ProjectInformation { Name = "Project3", Key="1" },
                new ProjectInformation { Name = "Project2", Key="2" },
                new ProjectInformation { Name = "project1", Key="3" },
            };
            string[] expectedOrderedProjectNames = projects.Select(p => p.Name).OrderBy(n => n, StringComparer.CurrentCulture).ToArray();

            // Act
            viewModel.SetProjects(projects);

            // Verify
            string[] actualProjectNames = viewModel.Projects.Select(p => p.ProjectInformation.Name).OrderBy(n => n, StringComparer.CurrentCulture).ToArray();
            CollectionAssert.AreEqual(
               expectedOrderedProjectNames,
               actualProjectNames,
               message: $"VM projects [{string.Join(", ", actualProjectNames)}] do not match the expected projects [{string.Join(", ", expectedOrderedProjectNames)}]"
           );

            // Act again
            var newProject = new ProjectInformation();
            viewModel.SetProjects(new[] { newProject });

            // Verify that the collection was replaced with the new one
            Assert.AreSame(newProject, viewModel.Projects.SingleOrDefault()?.ProjectInformation, "Expected a single project to be present");
        }
        public void ServerViewModel_AutomationName()
        {
            // Setup
            var connInfo = new ConnectionInformation(new Uri("https://myawesomeserver:1234/"));
            var testSubject = new ServerViewModel(connInfo);
            var projects = new[] { new ProjectInformation { Key = "P", Name = "A Project" } };

            var expectedProjects = string.Format(CultureInfo.CurrentCulture, Strings.AutomationServerDescription, connInfo.ServerUri);
            var expectedNoProjects = string.Format(CultureInfo.CurrentCulture, Strings.AutomationServerNoProjectsDescription, connInfo.ServerUri);

            // Test case 1: no projects
            // Act
            var actualNoProjects = testSubject.AutomationName;

            // Verify
            Assert.AreEqual(expectedNoProjects, actualNoProjects, "Unexpected description of SonarQube server without projects");

            // Test case 2: projects
            // Act
            testSubject.SetProjects(projects);
            var actualProjects = testSubject.AutomationName;

            // Verify
            Assert.AreEqual(expectedProjects, actualProjects, "Unexpected description of SonarQube server with projects");
        }