-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #718 from Klantinteractie-Servicesysteem/DIM@6043-…
…--Unittests-toevoegen Dim@6043 unittests toevoegen
- Loading branch information
Showing
15 changed files
with
655 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 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,74 @@ | ||
using Kiss.Bff.Beheer.Data; | ||
using Kiss.Bff.Beheer.Faq; | ||
using Kiss.Bff.ZaakGerichtWerken.Contactmomenten; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Kiss.Bff.Test | ||
{ | ||
[TestClass] | ||
public class GetFaqTests : TestHelper | ||
{ | ||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
InitializeDatabase(); | ||
} | ||
|
||
[TestCleanup] | ||
public void Cleanup() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
dbContext.Database.EnsureDeleted(); | ||
dbContext.Dispose(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Get_ReturnsTopQuestions() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
// Arrange | ||
var controller = new GetFaq(dbContext); | ||
|
||
var testData = new List<ContactmomentDetails>(); | ||
var topQuestionsCount = 10; | ||
|
||
// Add 500 questions | ||
for (int i = 1; i <= 500; i++) | ||
{ | ||
testData.Add(new ContactmomentDetails | ||
{ | ||
Id = i.ToString(), | ||
Vraag = $"Question {i}", | ||
Einddatum = DateTime.UtcNow | ||
}); | ||
} | ||
|
||
dbContext.ContactMomentDetails.AddRange(testData); | ||
dbContext.SaveChanges(); | ||
|
||
// Act | ||
var result = controller.Get() as OkObjectResult; | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(200, result.StatusCode); | ||
|
||
var resultList = result.Value as IEnumerable<string>; | ||
Assert.IsNotNull(resultList); | ||
|
||
// Ensure that the result contains the top questions | ||
var expectedQuestions = testData | ||
.OrderByDescending(x => x.Einddatum) | ||
.Where(x => !string.IsNullOrWhiteSpace(x.Vraag)) | ||
.GroupBy(x => x.Vraag) | ||
.OrderByDescending(x => x.Count()) | ||
.Take(topQuestionsCount) | ||
.Select(x => x.Key); | ||
|
||
CollectionAssert.AreEqual(expectedQuestions.ToList(), resultList.ToList()); | ||
} | ||
|
||
} | ||
} |
This file contains 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 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 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 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,104 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Security.Claims; | ||
using Kiss.Bff.Beheer.Data; | ||
using Kiss.Bff.ZaakGerichtWerken.Contactmomenten; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Kiss.Bff.Test | ||
{ | ||
[TestClass] | ||
public class ReadContactmomentenDetailsTests : TestHelper | ||
{ | ||
private IServiceProvider? _serviceProvider; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
InitializeDatabase(); | ||
SeedTestData(); | ||
|
||
var services = new ServiceCollection(); | ||
var user = new ClaimsPrincipal(new ClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimTypes.NameIdentifier, "testuser"), | ||
new Claim(ClaimTypes.Role, Policies.RedactiePolicy) | ||
})); | ||
services.AddSingleton<IHttpContextAccessor>(_ => new HttpContextAccessor { HttpContext = new DefaultHttpContext { User = user } }); | ||
_serviceProvider = services.BuildServiceProvider(); | ||
} | ||
|
||
[TestCleanup] | ||
public void Cleanup() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
dbContext.Database.EnsureDeleted(); | ||
dbContext.Dispose(); | ||
} | ||
|
||
private void SeedTestData() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
var contactmoment1 = new ContactmomentDetails | ||
{ | ||
Id = "1", | ||
Startdatum = DateTime.Now, | ||
Einddatum = DateTime.Now.AddHours(1), | ||
Gespreksresultaat = "Result 1", | ||
Vraag = "Question 1" | ||
}; | ||
|
||
var contactmoment2 = new ContactmomentDetails | ||
{ | ||
Id = "2", | ||
Startdatum = DateTime.Now, | ||
Einddatum = DateTime.Now.AddHours(1), | ||
Gespreksresultaat = "Result 2", | ||
Vraag = "Question 2" | ||
}; | ||
|
||
dbContext.ContactMomentDetails.AddRange(contactmoment1, contactmoment2); | ||
dbContext.SaveChanges(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Get_ValidId_ReturnsOk() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
// Arrange | ||
var controller = new ReadContactmomentenDetails(dbContext); | ||
var validId = "1"; | ||
|
||
// Act | ||
var result = await controller.Get(validId, CancellationToken.None) as OkObjectResult; | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(200, result.StatusCode); | ||
|
||
var contactmoment = result.Value as ContactmomentDetails; | ||
Assert.IsNotNull(contactmoment); | ||
Assert.AreEqual(validId, contactmoment.Id); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Get_InvalidId_ReturnsNotFound() | ||
{ | ||
using var dbContext = new BeheerDbContext(_dbContextOptions); | ||
// Arrange | ||
var controller = new ReadContactmomentenDetails(dbContext); | ||
var invalidId = "nonexistent"; | ||
|
||
// Act | ||
var result = await controller.Get(invalidId, CancellationToken.None) as NotFoundResult; | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
Assert.AreEqual(404, result.StatusCode); | ||
} | ||
} | ||
} |
Oops, something went wrong.