Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/service/Domain/Microsoft.FeatureFlighting.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
<ProjectReference Include="..\Common\Microsoft.FeatureFlighting.Common.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="using Microsoft.FeatureFlighting.Core.Services">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<ProjectReference Include="..\Common\Microsoft.FeatureFlighting.Common.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="using Microsoft.FeatureFlighting.Infrastructure.Cache">
<_Parameter1>Microsoft.FeatureFlighting.Infrastructure.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using AppInsights.EnterpriseTelemetry;
using CQRS.Mediatr.Lite;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureFlighting.API.Background;
using Microsoft.FeatureFlighting.API.Controllers;
using Microsoft.FeatureFlighting.Common.Cache;
using Microsoft.FeatureFlighting.Core.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.FeatureFlighting.API.Tests.BackgroundTest
{
[ExcludeFromCodeCoverage]
[TestCategory("CacheBuilderBackgroundService")]
[TestClass]
public class CacheBuilderBackgroundServiceTest
{
public Mock<IConfiguration> _mockConfiguration;
public Mock<IBackgroundCacheManager> _mockBackgroundCacheManager;
public Mock<ILogger> _mockogger;

public Mock<IHostedService> _mockHostedService;

public CacheBuilderBackgroundService CacheBuilderBackgroundService;

public CacheBuilderBackgroundServiceTest() {
_mockConfiguration = new Mock<IConfiguration>();
_mockogger = new Mock<ILogger>();
_mockBackgroundCacheManager = new Mock<IBackgroundCacheManager>();

var testConfig = new Mock<IConfigurationSection>();
testConfig.Setup(s => s.Value).Returns("preprop,prod");

_mockConfiguration.Setup(c => c.GetSection("Env:Supported")).Returns(testConfig.Object);

var testConfigCache = new Mock<IConfigurationSection>();
testConfigCache.Setup(s => s.Value).Returns("true");

_mockConfiguration.Setup(c => c.GetSection("BackgroundCache:Enabled")).Returns(testConfigCache.Object);

var testConfigPeriod = new Mock<IConfigurationSection>();
testConfigPeriod.Setup(s => s.Value).Returns("10");

_mockConfiguration.Setup(c => c.GetSection("BackgroundCache:Period")).Returns(testConfigPeriod.Object);

Command<IdCommandResult> Command = new UnsubscribeAlertsCommand("testFeature", "tesTenant", "preprop", "123", "1234", "test source");
CacheBuilderBackgroundService = new CacheBuilderBackgroundService(_mockBackgroundCacheManager.Object, _mockogger.Object, _mockConfiguration.Object);
}

[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public async Task StartAsync_Success(bool _isBackgroundRefreshEnabled)
{
var result = CacheBuilderBackgroundService.StartAsync(default).IsCompleted;
Assert.IsTrue(result);
}

[DataTestMethod]
public async Task StopAsync_Success()
{
var result = CacheBuilderBackgroundService.StopAsync(default).IsCompleted;
Assert.IsTrue(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AppInsights.EnterpriseTelemetry;
using Microsoft.Extensions.Configuration;
using Microsoft.FeatureFlighting.API.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.FeatureFlighting.API.Tests.ControllerTests
{
[ExcludeFromCodeCoverage]
[TestClass]
public class BaseClassExposedToTest:BaseController
{
public BaseClassExposedToTest(IConfiguration configuration, ILogger logger) : base(configuration, logger)
{
}

public Tuple<string, string, string, string, string> GetHeaders()
{
return base.GetHeaders();
}

public string GetHeaderValue(string headerKey, string defaultValue)
{
return base.GetHeaderValue(headerKey, defaultValue);
}

public string GetHeaderValue(string headerName)
{
return base.GetHeaderValue(headerName);
}
}
}
99 changes: 99 additions & 0 deletions src/service/Tests/Api.Tests/ControllerTests/BaseControllerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using AppInsights.EnterpriseTelemetry;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.FeatureFlighting.Common.AppExceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

namespace Microsoft.FeatureFlighting.API.Tests.ControllerTests
{
[ExcludeFromCodeCoverage]
[TestCategory("BaseController")]
[TestClass]
public class BaseControllerTest
{

private readonly Mock<IConfiguration> _mockConfiguration;
private readonly Mock<ILogger> _mockLogger;
private readonly BaseClassExposedToTest _baseController;
public BaseControllerTest()
{
_mockConfiguration = new Mock<IConfiguration>();
_mockLogger = new Mock<ILogger>();

var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["x-application"] = "TestApp";
httpContext.Request.Headers["x-environment"] = "preprop";
httpContext.Request.Headers["x-correlationId"] = "TestCorrelationId";
httpContext.Request.Headers["x-messageId"] = "TestMessageId";
httpContext.Request.Headers["x-channel"] = "TestChannel";

var testConfig = new Mock<IConfigurationSection>();
testConfig.Setup(s => s.Value).Returns("preprop,prod");

_mockConfiguration.Setup(c => c.GetSection("Env:Supported")).Returns(testConfig.Object);

_baseController = new BaseClassExposedToTest(_mockConfiguration.Object, _mockLogger.Object)
{
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
}
};
}

[TestMethod]
public async Task GetHeaders_WhenCalledWithValidHeaders_ShouldReturnHeaders()
{
var result = _baseController.GetHeaders();

Assert.AreEqual("TestApp", result.Item1);
Assert.AreEqual("preprop", result.Item2);
Assert.AreEqual("TestCorrelationId", result.Item3);
Assert.AreEqual("TestMessageId", result.Item4);
Assert.AreEqual("TestChannel", result.Item5);
}

[TestMethod]
public void GetHeaders_WhenCalledWithMissingHeaders_ShouldThrowDomainException()
{
var httpContext = new DefaultHttpContext();

var _baseClassExposedToTest = new BaseClassExposedToTest(_mockConfiguration.Object, _mockLogger.Object)
{
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
}
};

Assert.ThrowsException<DomainException>(() => _baseClassExposedToTest.GetHeaders());
}

[TestMethod]
public void GetHeaders_WhenCalledWithUnsupportedEnvironment_ShouldThrowDomainException()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["x-application"] = "TestApp";
httpContext.Request.Headers["x-environment"] = "UnsupportedEnv";
httpContext.Request.Headers["x-correlationId"] = "TestCorrelationId";
httpContext.Request.Headers["x-messageId"] = "TestMessageId";
httpContext.Request.Headers["x-channel"] = "TestChannel";

_mockConfiguration.Setup(c => c.GetSection("Env:Supported").Value).Returns("TestEnv1,TestEnv2");
var _baseClassExposedToTest = new BaseClassExposedToTest(_mockConfiguration.Object, _mockLogger.Object)
{
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
}
};

Assert.ThrowsException<DomainException>(() => _baseClassExposedToTest.GetHeaders());
}

}
}
Loading