Skip to content

Commit

Permalink
Merge classes into ISolutionBindingRepository (#5225)
Browse files Browse the repository at this point in the history
Fixes #5210
  • Loading branch information
ugras-ergun-sonarsource committed Feb 28, 2024
1 parent 8b55924 commit 86ca88d
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 381 deletions.
70 changes: 26 additions & 44 deletions src/ConnectedMode.UnitTests/Binding/BindingInfoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Binding
[TestClass]
public class BindingInfoProviderTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<BoundConnectionInfoProvider, IBoundConnectionInfoProvider>(
MefTestHelpers.CreateExport<ISolutionBindingRepository>());
}

[TestMethod]
public void MefCtor_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<BoundConnectionInfoProvider>();
}

[TestMethod]
public void GetExistingBindings_NoBindings_ReturnsEmpty()
{
Expand All @@ -58,17 +71,16 @@ public void GetExistingBindings_MakeSureCallInBackground()
[TestMethod]
public void GetExistingBindings_HaveBindings_ReturnsBinding()
{
var unintrusiveBindingPathProvider = CreateUnintrusiveBindingPathProvider("C:\\Bindings\\Binding1\\binding.config", "C:\\Bindings\\Binding2\\binding.config");

var solutionBindingFileLoader = new Mock<ISolutionBindingFileLoader>();
var solutionBindingRepository = new Mock<ISolutionBindingRepository>();

var binding1 = CreateBoundSonarQubeProject("https://sonarqube.somedomain.com", null, "projectKey1");
var binding2 = CreateBoundSonarQubeProject("https://sonarcloud.io", "organisation", "projectKey2");

solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding1\\binding.config")).Returns(binding1);
solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding2\\binding.config")).Returns(binding2);
var bindings = new[] { binding1, binding2 };

solutionBindingRepository.Setup(sbr => sbr.List()).Returns(bindings);

var testSubject = CreateTestSubject(unintrusiveBindingPathProvider: unintrusiveBindingPathProvider, solutionBindingFileLoader: solutionBindingFileLoader.Object);
var testSubject = CreateTestSubject(solutionBindingRepository: solutionBindingRepository.Object);

var result = testSubject.GetExistingBindings().ToList();

Expand All @@ -79,61 +91,31 @@ public void GetExistingBindings_HaveBindings_ReturnsBinding()
result[1].Organization.Should().Be("organisation");
}

[TestMethod]
public void GetExistingBindings_BindingConfigMissing_SkipFile()
{
var unintrusiveBindingPathProvider = CreateUnintrusiveBindingPathProvider("C:\\Bindings\\Binding1\\binding.config", "C:\\Bindings\\Binding2\\binding.config");

var solutionBindingFileLoader = new Mock<ISolutionBindingFileLoader>();

var binding1 = CreateBoundSonarQubeProject("https://sonarqube.somedomain.com", null, "projectKey1");
var binding2 = CreateBoundSonarQubeProject("https://sonarcloud.io", "organisation", "projectKey2");

solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding1\\binding.config")).Returns(binding1);
solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding2\\binding.config")).Returns((BoundSonarQubeProject)null);

var testSubject = CreateTestSubject(unintrusiveBindingPathProvider: unintrusiveBindingPathProvider, solutionBindingFileLoader: solutionBindingFileLoader.Object);

var result = testSubject.GetExistingBindings();

result.Should().HaveCount(1);
}

[TestMethod]
public void GetExistingBindings_SameBindingMultipleTime_ReturnsDistinct()
{
var unintrusiveBindingPathProvider = CreateUnintrusiveBindingPathProvider("C:\\Bindings\\Binding1\\binding.config", "C:\\Bindings\\Binding2\\binding.config");

var solutionBindingFileLoader = new Mock<ISolutionBindingFileLoader>();
var solutionBindingRepository = new Mock<ISolutionBindingRepository>();

var binding1 = CreateBoundSonarQubeProject("https://sonarqube.somedomain.com", null, "projectKey1");
var binding2 = CreateBoundSonarQubeProject("https://sonarqube.somedomain.com", null, "projectKey2");

solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding1\\binding.config")).Returns(binding1);
solutionBindingFileLoader.Setup(sbf => sbf.Load("C:\\Bindings\\Binding2\\binding.config")).Returns(binding2);
var bindings = new[] { binding1, binding2 };

var testSubject = CreateTestSubject(unintrusiveBindingPathProvider: unintrusiveBindingPathProvider, solutionBindingFileLoader: solutionBindingFileLoader.Object);
solutionBindingRepository.Setup(sbr => sbr.List()).Returns(bindings);

var testSubject = CreateTestSubject(solutionBindingRepository: solutionBindingRepository.Object);

var result = testSubject.GetExistingBindings();

result.Should().HaveCount(1);
}

private static IUnintrusiveBindingPathProvider CreateUnintrusiveBindingPathProvider(params string[] bindigFolders)
private static BoundConnectionInfoProvider CreateTestSubject(ISolutionBindingRepository solutionBindingRepository = null, IThreadHandling threadHandling = null)
{
var unintrusiveBindingPathProvider = new Mock<IUnintrusiveBindingPathProvider>();
unintrusiveBindingPathProvider.Setup(u => u.GetBindingPaths()).Returns(bindigFolders);
return unintrusiveBindingPathProvider.Object;
}

private static BoundConnectionInfoProvider CreateTestSubject(IUnintrusiveBindingPathProvider unintrusiveBindingPathProvider = null, ISolutionBindingFileLoader solutionBindingFileLoader = null, IThreadHandling threadHandling = null)
{
unintrusiveBindingPathProvider ??= CreateUnintrusiveBindingPathProvider();

solutionBindingFileLoader ??= Mock.Of<ISolutionBindingFileLoader>();
solutionBindingRepository ??= Mock.Of<ISolutionBindingRepository>();
threadHandling ??= new NoOpThreadHandler();

var testSubject = new BoundConnectionInfoProvider(unintrusiveBindingPathProvider, solutionBindingFileLoader, threadHandling);
var testSubject = new BoundConnectionInfoProvider(solutionBindingRepository, threadHandling);
return testSubject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<UnintrusiveConfigurationProvider, IConfigurationProvider>(
MefTestHelpers.CreateExport<IUnintrusiveBindingPathProvider>(),
MefTestHelpers.CreateExport<ISolutionBindingDataReader>());
MefTestHelpers.CreateExport<ISolutionBindingRepository>());
}

[TestMethod]
public void GetConfig_NoConfig_ReturnsStandalone()
{
// Arrange
var pathProvider = CreatePathProvider(null);
var configReader = new Mock<ISolutionBindingDataReader>();
var testSubject = CreateTestSubject(pathProvider, configReader.Object);
var configRepository = new Mock<ISolutionBindingRepository>();
var testSubject = CreateTestSubject(pathProvider, configRepository.Object);

// Act
var actual = testSubject.GetConfiguration();
Expand All @@ -50,7 +50,7 @@ public void GetConfig_NoConfig_ReturnsStandalone()
actual.Should().NotBeNull();
actual.Project.Should().BeNull();
actual.Mode.Should().Be(SonarLintMode.Standalone);
configReader.Invocations.Should().BeEmpty();
configRepository.Invocations.Should().BeEmpty();
}

[TestMethod]
Expand All @@ -60,7 +60,7 @@ public void GetConfig_Bound_ReturnsExpectedConfig()
var expectedProject = new BoundSonarQubeProject();

var pathProvider = CreatePathProvider("c:\\users\\foo\\bindings\\xxx.config");
var configReader = CreateReader(expectedProject);
var configReader = CreateRepo(expectedProject);
var testSubject = CreateTestSubject(pathProvider, configReader.Object);

// Act
Expand All @@ -79,7 +79,7 @@ public void GetConfig_ConfigReaderReturnsNull_ReturnsStandalone()
{
// Arrange
var pathProvider = CreatePathProvider("c:\\users\\foo\\bindings\\xxx.config");
var configReader = CreateReader(null);
var configReader = CreateRepo(null);
var testSubject = CreateTestSubject(pathProvider, configReader.Object);

// Act
Expand All @@ -91,10 +91,10 @@ public void GetConfig_ConfigReaderReturnsNull_ReturnsStandalone()
}

private static UnintrusiveConfigurationProvider CreateTestSubject(IUnintrusiveBindingPathProvider pathProvider,
ISolutionBindingDataReader configReader = null)
ISolutionBindingRepository configRepo = null)
{
configReader ??= Mock.Of<ISolutionBindingDataReader>();
return new UnintrusiveConfigurationProvider(pathProvider, configReader);
configRepo ??= Mock.Of<ISolutionBindingRepository>();
return new UnintrusiveConfigurationProvider(pathProvider, configRepo);
}

private static IUnintrusiveBindingPathProvider CreatePathProvider(string pathToReturn)
Expand All @@ -104,14 +104,14 @@ private static IUnintrusiveBindingPathProvider CreatePathProvider(string pathToR
return pathProvider.Object;
}

private static Mock<ISolutionBindingDataReader> CreateReader(BoundSonarQubeProject projectToReturn)
private static Mock<ISolutionBindingRepository> CreateRepo(BoundSonarQubeProject projectToReturn)
{
var reader = new Mock<ISolutionBindingDataReader>();
reader.Setup(x => x.Read(It.IsAny<string>())).Returns(projectToReturn);
return reader;
var repo = new Mock<ISolutionBindingRepository>();
repo.Setup(x => x.Read(It.IsAny<string>())).Returns(projectToReturn);
return repo;
}

private static void CheckExpectedFileRead(Mock<ISolutionBindingDataReader> configReader, string expectedFilePath)
private static void CheckExpectedFileRead(Mock<ISolutionBindingRepository> configReader, string expectedFilePath)
=> configReader.Verify(x => x.Read(expectedFilePath), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ObsoleteConfigurationProviderTests
[TestMethod]
public void MefCtor_CheckIsExported()
=> MefTestHelpers.CheckTypeCanBeImported<ObsoleteConfigurationProvider, IObsoleteConfigurationProvider>(
MefTestHelpers.CreateExport<ISolutionBindingDataReader>(),
MefTestHelpers.CreateExport<ISolutionBindingRepository>(),
MefTestHelpers.CreateExport<ISolutionInfoProvider>());

[TestMethod]
Expand All @@ -46,13 +46,13 @@ public void Ctor_DoesNotCallServices()
// -> should not call services that swtich threads
var legacyProvider = new Mock<ISolutionBindingPathProvider>();
var connectedProvider = new Mock<ISolutionBindingPathProvider>();
var slnDataReader = new Mock<ISolutionBindingDataReader>();
var slnDataRepository = new Mock<ISolutionBindingRepository>();

_ = CreateTestSubject(legacyProvider.Object, connectedProvider.Object, slnDataReader.Object);
_ = CreateTestSubject(legacyProvider.Object, connectedProvider.Object, slnDataRepository.Object);

legacyProvider.Invocations.Should().BeEmpty();
connectedProvider.Invocations.Should().BeEmpty();
slnDataReader.Invocations.Should().BeEmpty();
slnDataRepository.Invocations.Should().BeEmpty();
}

[TestMethod]
Expand All @@ -79,9 +79,9 @@ public void GetConfig_NewConfigOnly_ReturnsConnected()
// Arrange
var legacyPathProvider = CreatePathProvider(null);
var newPathProvider = CreatePathProvider("c:\\new");

var expectedProject = new BoundSonarQubeProject();
var reader = CreateReader("c:\\new", expectedProject);
var reader = CreateRpo("c:\\new", expectedProject);

var testSubject = CreateTestSubject(legacyPathProvider, newPathProvider, reader);

Expand All @@ -102,7 +102,7 @@ public void GetConfig_LegacyConfigOnly_ReturnsLegacy()
var legacyPathProvider = CreatePathProvider("c:\\old");

var expectedProject = new BoundSonarQubeProject();
var reader = CreateReader("c:\\old", expectedProject);
var reader = CreateRpo("c:\\old", expectedProject);

var testSubject = CreateTestSubject(legacyPathProvider, null, reader);

Expand All @@ -123,11 +123,11 @@ public void GetConfig_NoLegacyProjectAtFileLocation_NoConnectedProjectAtFileLoca
var legacyPathProvider = CreatePathProvider("c:\\legacy");
var newPathProvider = CreatePathProvider("c:\\new");

var reader = new Mock<ISolutionBindingDataReader>();
reader.Setup(x => x.Read("c:\\legacy")).Returns(null as BoundSonarQubeProject);
reader.Setup(x => x.Read("c:\\new")).Returns(null as BoundSonarQubeProject);
var repo = new Mock<ISolutionBindingRepository>();
repo.Setup(x => x.Read("c:\\legacy")).Returns(null as BoundSonarQubeProject);
repo.Setup(x => x.Read("c:\\new")).Returns(null as BoundSonarQubeProject);

var testSubject = CreateTestSubject(legacyPathProvider, newPathProvider, reader.Object);
var testSubject = CreateTestSubject(legacyPathProvider, newPathProvider, repo.Object);

// Act
var actual = testSubject.GetConfiguration();
Expand All @@ -145,12 +145,12 @@ public void GetConfig_NoLegacyProjectAtFileLocation_ConnectedProjectAtFileLocati
var legacyPathProvider = CreatePathProvider("c:\\legacy");
var newPathProvider = CreatePathProvider("c:\\new");

var reader = new Mock<ISolutionBindingDataReader>();
var repo = new Mock<ISolutionBindingRepository>();
var expectedProject = new BoundSonarQubeProject();
reader.Setup(x => x.Read("c:\\legacy")).Returns(null as BoundSonarQubeProject);
reader.Setup(x => x.Read("c:\\new")).Returns(expectedProject);
repo.Setup(x => x.Read("c:\\legacy")).Returns(null as BoundSonarQubeProject);
repo.Setup(x => x.Read("c:\\new")).Returns(expectedProject);

var testSubject = CreateTestSubject(legacyPathProvider, newPathProvider, reader.Object);
var testSubject = CreateTestSubject(legacyPathProvider, newPathProvider, repo.Object);

// Act
var actual = testSubject.GetConfiguration();
Expand All @@ -171,7 +171,7 @@ public void GetConfig_LegacyProjectAtFileLocation_ConnectedProjectAtFileLocation
var legacyPathProvider = CreatePathProvider("c:\\legacy");
var newPathProvider = CreatePathProvider("c:\\new");

var reader = new Mock<ISolutionBindingDataReader>();
var reader = new Mock<ISolutionBindingRepository>();
var legacyProject = new BoundSonarQubeProject();
var newProject = new BoundSonarQubeProject();
reader.Setup(x => x.Read("c:\\legacy")).Returns(legacyProject);
Expand All @@ -195,21 +195,21 @@ private static ISolutionBindingPathProvider CreatePathProvider(string pathToRetu
return provider.Object;
}

private static ISolutionBindingDataReader CreateReader(string inputPath, BoundSonarQubeProject projectToReturn)
private static ISolutionBindingRepository CreateRpo(string inputPath, BoundSonarQubeProject projectToReturn)
{
var reader = new Mock<ISolutionBindingDataReader>();
reader.Setup(x => x.Read(inputPath)).Returns(projectToReturn);
return reader.Object;
var repo = new Mock<ISolutionBindingRepository>();
repo.Setup(x => x.Read(inputPath)).Returns(projectToReturn);
return repo.Object;
}

private static ObsoleteConfigurationProvider CreateTestSubject(ISolutionBindingPathProvider legacyProvider = null,
ISolutionBindingPathProvider connectedModePathProvider = null,
ISolutionBindingDataReader slnDataReader = null)
ISolutionBindingRepository slnDataRepo = null)
{
var testSubject = new ObsoleteConfigurationProvider(
legacyProvider ?? Mock.Of<ISolutionBindingPathProvider>(),
connectedModePathProvider ?? Mock.Of<ISolutionBindingPathProvider>(),
slnDataReader ?? Mock.Of<ISolutionBindingDataReader>());
slnDataRepo ?? Mock.Of<ISolutionBindingRepository>());
return testSubject;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence
public class ConfigurationPersisterTests
{
private Mock<IUnintrusiveBindingPathProvider> configFilePathProvider;
private Mock<ISolutionBindingDataWriter> solutionBindingDataWriter;
private Mock<ISolutionBindingRepository> solutionBindingRepository;
private ConfigurationPersister testSubject;

[TestInitialize]
public void TestInitialize()
{
configFilePathProvider = new Mock<IUnintrusiveBindingPathProvider>();
solutionBindingDataWriter = new Mock<ISolutionBindingDataWriter>();
solutionBindingRepository = new Mock<ISolutionBindingRepository>();

testSubject = new ConfigurationPersister(
configFilePathProvider.Object,
solutionBindingDataWriter.Object);
solutionBindingRepository.Object);
}

[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<ConfigurationPersister, IConfigurationPersister>(
MefTestHelpers.CreateExport<IUnintrusiveBindingPathProvider>(),
MefTestHelpers.CreateExport<ISolutionBindingDataWriter>());
MefTestHelpers.CreateExport<ISolutionBindingRepository>());
}

[TestMethod]
Expand All @@ -68,7 +68,7 @@ public void Persist_SaveNewConfig()
var projectToWrite = new BoundSonarQubeProject();
configFilePathProvider.Setup(x => x.GetCurrentBindingPath()).Returns("c:\\new.txt");

solutionBindingDataWriter
solutionBindingRepository
.Setup(x => x.Write("c:\\new.txt", projectToWrite))
.Returns(true);

Expand All @@ -78,7 +78,7 @@ public void Persist_SaveNewConfig()
// Assert
actual.Should().NotBe(null);

solutionBindingDataWriter.Verify(x =>
solutionBindingRepository.Verify(x =>
x.Write("c:\\new.txt", projectToWrite),
Times.Once);
}
Expand Down
Loading

0 comments on commit 86ca88d

Please sign in to comment.