public void SolutionBindingSerializer_WriteSolutionBinding_AddConfigFileToSolutionItemsFolder()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var         serverUri       = new Uri("http://xxx.www.zzz/yyy:9000");
            var         creds           = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var         projectKey      = "MyProject Key";
            var         toWrite         = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);
            ProjectMock solutionProject = (ProjectMock)this.projectSystemHelper.SolutionItemsProject;

            // Act
            string output = testSubject.WriteSolutionBinding(toWrite);

            // Assert that not actually done anything until the pending files were written
            this.store.data.Should().NotContainKey(serverUri);
            solutionProject.Files.ContainsKey(output).Should().BeFalse("Not expected to be added to solution items folder just yet");

            // Act (write pending)
            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();

            // Assert
            this.store.data.Should().ContainKey(serverUri);
            solutionProject.Files.ContainsKey(output).Should().BeTrue("File {0} was not added to project", output);

            // Act (write again)
            string output2 = testSubject.WriteSolutionBinding(toWrite);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();

            // Assert
            output2.Should().Be(output, "Should be the same file");
            this.store.data.Should().ContainKey(serverUri);
            solutionProject.Files.ContainsKey(output).Should().BeTrue("File {0} should remain in the project", output);
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_ReadSolutionBinding_OnRealStore()
        {
            // Arrange
            var testSubject = new SolutionBindingSerializer(this.serviceProvider);
            var serverUri   = new Uri("http://xxx.www.zzz/yyy:9000");
            var projectKey  = "MyProject Key";

            this.store.DeleteCredentials(serverUri);

            // Case 1: has credentials
            var creds   = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var written = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);

            // Act (write + read)
            BoundSonarQubeProject read = null;

            try
            {
                testSubject.WriteSolutionBinding(written);
                this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
                read = testSubject.ReadSolutionBinding();
            }
            finally
            {
                this.store.DeleteCredentials(serverUri);
            }

            // Assert
            var newCreds = read.Credentials as BasicAuthCredentials;

            newCreds.Should().NotBe(creds, "Different credential instance were expected");
            newCreds.UserName.Should().Be(creds.UserName);
            newCreds?.Password.ToUnsecureString().Should().Be(creds.Password.ToUnsecureString());
            read.ServerUri.Should().Be(written.ServerUri);
            this.outputPane.AssertOutputStrings(0);

            // Case 2: has not credentials (anonymous)
            creds   = null;
            written = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);

            // Act (write + read)
            read = null;
            try
            {
                testSubject.WriteSolutionBinding(written);
                read = testSubject.ReadSolutionBinding();
            }
            finally
            {
                this.store.DeleteCredentials(serverUri);
            }

            // Assert
            read.Credentials.Should().BeNull();
            read.ServerUri.Should().Be(written.ServerUri);
            this.outputPane.AssertOutputStrings(0);
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_ReadSolutionBinding()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var creds      = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var projectKey = "MyProject Key";
            var written    = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);

            // Act (write)
            string output = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
            output.Should().NotBeNull("Expected a real file");
            this.TestContext.AddResultFile(output);
            File.Exists(output).Should().BeTrue("Expected a real file");

            // Assert
            this.store.data.Should().ContainKey(serverUri);

            // Act (read)
            BoundSonarQubeProject read = testSubject.ReadSolutionBinding();

            // Assert
            var newCreds = read.Credentials as BasicAuthCredentials;

            newCreds.Should().NotBe(creds, "Different credential instance were expected");
            newCreds.UserName.Should().Be(creds.UserName);
            newCreds.Password.ToUnsecureString().Should().Be(creds.Password.ToUnsecureString());
            read.ServerUri.Should().Be(written.ServerUri);
            this.outputPane.AssertOutputStrings(0);
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_ReadSolutionBinding()
        {
            // Setup
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var creds      = new BasicAuthCredentials("user", "pwd".ConvertToSecureString());
            var projectKey = "MyProject Key";
            var written    = new BoundSonarQubeProject(serverUri, projectKey, creds);

            // Act (write)
            string output = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
            Assert.IsNotNull(output, "Expected a real file");
            this.TestContext.AddResultFile(output);
            Assert.IsTrue(File.Exists(output), "Expected a real file");

            // Verify
            this.store.AssertHasCredentials(serverUri);

            // Act (read)
            BoundSonarQubeProject read = testSubject.ReadSolutionBinding();

            // Verify
            var newCreds = read.Credentials as BasicAuthCredentials;

            Assert.AreNotEqual(creds, newCreds, "Different credential instance were expected");
            Assert.AreEqual(creds.UserName, newCreds.UserName);
            Assert.AreEqual(creds.Password.ConvertToUnsecureString(), newCreds.Password.ConvertToUnsecureString());
            Assert.AreEqual(written.ServerUri, read.ServerUri);
            this.outputPane.AssertOutputStrings(0);
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_ArgChecks()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();

            // Act + Assert
            Exceptions.Expect <ArgumentNullException>(() => testSubject.WriteSolutionBinding(null));
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_IOError()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var    serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var    creds      = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var    projectKey = "MyProject Key";
            var    written    = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);
            string output     = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();

            using (new FileStream(output, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                // Act (write again)
                string output2 = testSubject.WriteSolutionBinding(written);
                this.sourceControlledFileSystem.WritePendingErrorsExpected();

                // Assert
                output2.Should().Be(output, "Same output is expected");
                this.outputPane.AssertOutputStrings(1);
            }
        }
        public void SolutionBindingSerializer_WriteSolutionBinding_ReadSolutionBinding_WithProfiles()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var creds      = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var projectKey = "MyProject Key";
            var written    = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds)
            {
                Profiles = new Dictionary <Language, ApplicableQualityProfile>()
                {
                    { Language.VBNET, new ApplicableQualityProfile {
                          ProfileKey = "VB"
                      } },
                    { Language.CSharp, new ApplicableQualityProfile {
                          ProfileKey = "CS", ProfileTimestamp = DateTime.Now
                      } }
                }
            };

            // Act (write)
            string output = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
            output.Should().NotBeNull("Expected a real file");
            this.TestContext.AddResultFile(output);
            File.Exists(output).Should().BeTrue("Expected a real file");

            // Assert
            this.store.data.Should().ContainKey(serverUri);

            // Act (read)
            BoundSonarQubeProject read = testSubject.ReadSolutionBinding();

            // Assert
            var newCreds = read.Credentials as BasicAuthCredentials;

            newCreds.Should().NotBe(creds, "Different credential instance were expected");
            newCreds.UserName.Should().Be(creds.UserName);
            newCreds.Password.ToUnsecureString().Should().Be(creds.Password.ToUnsecureString());
            read.ServerUri.Should().Be(written.ServerUri);
            (read.Profiles?.Count ?? 0).Should().Be(2);
            read.Profiles[Language.VBNET].ProfileKey.Should().Be(written.Profiles[Language.VBNET].ProfileKey);
            read.Profiles[Language.VBNET].ProfileTimestamp.Should().Be(written.Profiles[Language.VBNET].ProfileTimestamp);
            read.Profiles[Language.CSharp].ProfileKey.Should().Be(written.Profiles[Language.CSharp].ProfileKey);
            read.Profiles[Language.CSharp].ProfileTimestamp.Should().Be(written.Profiles[Language.CSharp].ProfileTimestamp);
            this.outputPane.AssertOutputStrings(0);
        }
        public void SolutionBindingSerializer_ReadSolutionBinding_InvalidData()
        {
            // Arrange
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var    serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var    creds      = new BasicAuthCredentials("user", "pwd".ToSecureString());
            var    projectKey = "MyProject Key";
            var    written    = new BoundSonarQubeProject(serverUri, projectKey, "projectName", creds);
            string output     = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
            output.Should().NotBeNull("Expected a real file");
            File.WriteAllText(output, "bla bla bla: bla");

            // Act (read)
            BoundSonarQubeProject read = testSubject.ReadSolutionBinding();

            // Assert
            read.Should().BeNull("Not expecting any binding information in case of error");
            this.outputPane.AssertOutputStrings(1);
        }
        public void SolutionBindingSerializer_ReadSolutionBinding_IOError()
        {
            // Setup
            SolutionBindingSerializer testSubject = this.CreateTestSubject();
            var    serverUri  = new Uri("http://xxx.www.zzz/yyy:9000");
            var    creds      = new BasicAuthCredentials("user", "pwd".ConvertToSecureString());
            var    projectKey = "MyProject Key";
            var    written    = new BoundSonarQubeProject(serverUri, projectKey, creds);
            string output     = testSubject.WriteSolutionBinding(written);

            this.sourceControlledFileSystem.WritePendingNoErrorsExpected();
            using (new FileStream(output, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                // Act (read)
                BoundSonarQubeProject read = testSubject.ReadSolutionBinding();

                // Verify
                Assert.IsNull(read, "Not expecting any binding information in case of error");
                this.outputPane.AssertOutputStrings(1);
            }
        }