-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ICosmosDBService dependency injection. Tests for parts of ItemControl…
…ler functionality
- Loading branch information
Showing
9 changed files
with
164 additions
and
35 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
handlenett-backend/web-api/HandlenettAPI/Configurations/AzureCosmosDBSettings.cs
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,19 @@ | ||
namespace HandlenettAPI.Configurations | ||
{ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
public class AzureCosmosDBSettings | ||
{ | ||
[Required] | ||
public required string ConnectionString { get; set; } | ||
//required property: Compile time validation | ||
//required attribute: Runtime validation (deserialization, reflection ++ can buypass required property) | ||
|
||
[Required] | ||
public required string DatabaseName { get; set; } | ||
|
||
[Required] | ||
public required string ContainerName { get; set; } | ||
} | ||
|
||
} |
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
15 changes: 15 additions & 0 deletions
15
handlenett-backend/web-api/HandlenettAPI/Interfaces/ICosmosDBService.cs
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,15 @@ | ||
using HandlenettAPI.DTO; | ||
using HandlenettAPI.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace HandlenettAPI.Interfaces | ||
{ | ||
public interface ICosmosDBService | ||
{ | ||
Task<List<Item>> GetByQuery(string cosmosQuery); | ||
Task<Item> Add(ItemPostDTO item, string username); | ||
Task Delete(string id, string partition); | ||
Item? GetById(string id); | ||
Task<Item> Update(string id, ItemPutDTO item, string username); | ||
} | ||
} |
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
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
83 changes: 83 additions & 0 deletions
83
handlenett-backend/web-api/HandlenettAPITests/Controllers/ItemControllerTests.cs
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,83 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using HandlenettAPI.Controllers; | ||
using HandlenettAPI.Services; | ||
using Microsoft.Extensions.Configuration; | ||
using HandlenettAPI.Models; | ||
using HandlenettAPI.Interfaces; | ||
|
||
namespace HandlenettAPITests.Controllers | ||
{ | ||
public class ItemControllerTests | ||
{ | ||
private readonly Mock<ILogger<ItemController>> _mockLogger; | ||
private readonly Mock<ICosmosDBService> _mockCosmosDBService; | ||
private readonly ItemController _controller; | ||
|
||
//TODO: Add test for config values | ||
|
||
public ItemControllerTests() | ||
{ | ||
// Set up mocks | ||
_mockLogger = new Mock<ILogger<ItemController>>(); | ||
_mockCosmosDBService = new Mock<ICosmosDBService>(); | ||
|
||
// Create the controller with mocked dependencies | ||
_controller = new ItemController(_mockLogger.Object, _mockCosmosDBService.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_ReturnsOkWithItems_WhenItemsExist() | ||
{ | ||
// Arrange | ||
var expectedItems = new List<Item> | ||
{ | ||
new Item { Id = "1", Name = "Test Item 1" }, | ||
new Item { Id = "2", Name = "Test Item 2" } | ||
}; | ||
|
||
_mockCosmosDBService | ||
.Setup(service => service.GetByQuery(It.IsAny<string>())) | ||
.ReturnsAsync(expectedItems); | ||
|
||
// Act | ||
var result = await _controller.Get(); | ||
|
||
// Assert | ||
var okResult = Assert.IsType<OkObjectResult>(result.Result); // Check for 200 OK | ||
var items = Assert.IsAssignableFrom<List<Item>>(okResult.Value); // Ensure correct type | ||
Assert.Equal(expectedItems.Count, items.Count); // Verify returned items match | ||
} | ||
|
||
[Fact] | ||
public async Task Get_ReturnsInternalServerError_WhenExceptionIsThrown() | ||
{ | ||
// Arrange | ||
_mockCosmosDBService | ||
.Setup(service => service.GetByQuery(It.IsAny<string>())) | ||
.ThrowsAsync(new Exception("Simulated exception")); | ||
|
||
// Act | ||
var result = await _controller.Get(); | ||
|
||
// Assert | ||
var statusCodeResult = Assert.IsType<ObjectResult>(result.Result); // Check for 500 | ||
Assert.Equal(500, statusCodeResult.StatusCode); // Verify status code | ||
Assert.NotNull(statusCodeResult.Value); // Ensure response body is not null | ||
|
||
// Verify logging | ||
_mockLogger.Verify( | ||
logger => logger.Log( | ||
It.Is<LogLevel>(level => level == LogLevel.Error), | ||
It.IsAny<EventId>(), | ||
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Failed to get items")), | ||
It.IsAny<Exception>(), | ||
It.IsAny<Func<It.IsAnyType, Exception, string>>()), | ||
Times.Once); | ||
} | ||
} | ||
} |