-
Notifications
You must be signed in to change notification settings - Fork 17
unit test cases for controllers and services #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
v-kwarhad
wants to merge
10
commits into
microsoft:main
Choose a base branch
from
v-kwarhad:users/v-kwarhad/UnitTestCases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
df636b7
unit test cases for controllers and services
v-kwarhad 243382d
Revert "unit test cases for controllers and services"
v-kwarhad 59e83fd
make unit test cases for other files.
v-kwarhad 3067de3
Created unit test cases for other classes
v-kwarhad 8d53042
create unit test cases for other classes.
v-kwarhad 52e1073
Created unit test cases for other classes
v-kwarhad 05e6de5
created unit test cases for some other classes.
v-kwarhad 3f918c1
Resolved conflict
v-kwarhad 9161bde
Code changes
v-kwarhad 8a6604d
Removed Unwanted space
v-kwarhad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/service/Tests/Api.Tests/BackgroundTest/CacheBuilderBackgroundServiceTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/service/Tests/Api.Tests/ControllerTests/BaseClassExposedToTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
99
src/service/Tests/Api.Tests/ControllerTests/BaseControllerTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why private has been removed