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
14 changes: 12 additions & 2 deletions Development Project/Development Project.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31410.357
# Visual Studio Version 18
VisualStudioVersion = 18.5.11716.220 stable
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Interview.Web", "Interview.Web\Interview.Web.csproj", "{EE17B748-4D84-46AE-9E83-8D04B92DD6A9}"
EndProject
Expand All @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sparcpoint.Core", "Sparcpoi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sparcpoint.SqlServer.Abstractions", "Sparcpoint.SqlServer.Abstractions\Sparcpoint.SqlServer.Abstractions.csproj", "{4E72CD4A-C9FE-4A04-9F07-A770E3AD7297}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interview.Web.Tests", "Interview.Web.Tests\Interview.Web.Tests.csproj", "{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,14 @@ Global
{4E72CD4A-C9FE-4A04-9F07-A770E3AD7297}.Release|Any CPU.Build.0 = Release|Any CPU
{4E72CD4A-C9FE-4A04-9F07-A770E3AD7297}.Release|x86.ActiveCfg = Release|Any CPU
{4E72CD4A-C9FE-4A04-9F07-A770E3AD7297}.Release|x86.Build.0 = Release|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Debug|x86.ActiveCfg = Debug|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Debug|x86.Build.0 = Debug|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Release|Any CPU.Build.0 = Release|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Release|x86.ActiveCfg = Release|Any CPU
{E94AE77D-BCB5-2BB9-ECF7-03B0DFD866EE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Interview.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Sparcpoint.Abstract.Services;
using Sparcpoint.Models.DTOs;
using Sparcpoint.Models.Entity;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Transactions;

namespace Interview.Web.Tests.Controllers
{
//EVAL: added tests for InventoryController covering all APIs and possible scenarios including exception handling and not found scenarios
[TestClass]
public class InventoryControllerTests
{
private Mock<IInventoryService> _mockInventoryService;
private Mock<ILogger<InventoryController>> _mockLogger;
private InventoryController _controller;

[TestInitialize]
public void Setup()
{
_mockInventoryService = new Mock<IInventoryService>();
_mockLogger = new Mock<ILogger<InventoryController>>();
_controller = new InventoryController(_mockInventoryService.Object, _mockLogger.Object);
}


[TestMethod]
public async Task AddProduct_ReturnsOk_WhenProductIsAdded()
{
// Arrange
var request = new AddToInventoryRequestDto { ProductInstanceId = 1 };
_mockInventoryService.Setup(s => s.AddProductToInventoryAsync(request)).ReturnsAsync(1);

// Act
var result = await _controller.AddProduct(request);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
_mockInventoryService.Verify(s => s.AddProductToInventoryAsync(request), Times.Once);
}

[TestMethod]
public async Task DeleteTransaction_ReturnsNoContent_WhenTransactionIsDeleted()
{
// Arrange
var transactionId = 1;
_mockInventoryService.Setup(s => s.RemoveInventoryTransactionAsync(transactionId)).ReturnsAsync(1);

// Act
var result = await _controller.RemoveTransaction(transactionId);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
_mockInventoryService.Verify(s => s.RemoveInventoryTransactionAsync(transactionId), Times.Once);
}

[TestMethod]
public async Task DeleteTransaction_ReturnsNotFound_WhenTransactionDoesNotExist()
{
// Arrange
var transactionId = 999;
_mockInventoryService.Setup(s => s.RemoveInventoryTransactionAsync(transactionId)).ReturnsAsync(0);

// Act
var result = await _controller.RemoveTransaction(transactionId);

// Assert
Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));
_mockInventoryService.Verify(s => s.RemoveInventoryTransactionAsync(transactionId), Times.Once);
}

[TestMethod]
public async Task DeleteProduct_ReturnsNoContent_WhenProductIsDeleted()
{
// Arrange
var productId = 1;
_mockInventoryService.Setup(s => s.RemoveProductFromInventoryAsync(productId)).ReturnsAsync(1);

// Act
var result = await _controller.RemoveProduct(productId);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
_mockInventoryService.Verify(s => s.RemoveProductFromInventoryAsync(productId), Times.Once);
}

[TestMethod]
public async Task DeleteProduct_ReturnsNotFound_WhenProductDoesNotExist()
{
// Arrange
var productId = 999;
_mockInventoryService.Setup(s => s.RemoveProductFromInventoryAsync(productId)).ReturnsAsync(0);

// Act
var result = await _controller.RemoveProduct(productId);

// Assert
Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));
_mockInventoryService.Verify(s => s.RemoveProductFromInventoryAsync(productId), Times.Once);
}

[TestMethod]
public async Task GetInventoryCount_ReturnsCount()
{

// Arrange
int productId = 1;
_mockInventoryService.Setup(s => s.GetProuctInventoryCountAsync(productId)).ReturnsAsync(100);

// Act
var result = await _controller.GetInventoryCount(productId);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
_mockInventoryService.Verify(s => s.GetProuctInventoryCountAsync(productId), Times.Once);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Sparcpoint.Models.DTOs;
using Sparcpoint.Abstract.Services;
using Interview.Web.Controllers;
using Sparcpoint.Models.Entity;
using System.Collections.Generic;

namespace Interview.Web.Tests.Controllers
{
//EVAL: added tests for ProductController covering all APIs and possible scenarios including exception handling and not found scenarios
[TestClass]
public class ProductControllerTests
{
private Mock<IProductService> _mockProductService;
private Mock<ILogger<ProductController>> _mockLogger;
private ProductController _controller;

[TestInitialize]
public void Setup()
{
_mockProductService = new Mock<IProductService>();
_mockLogger = new Mock<ILogger<ProductController>>();
_controller = new ProductController(_mockProductService.Object, _mockLogger.Object);
}

[TestMethod]
public async Task CreateProduct_ReturnsInternalServerError_WhenNameIsEmpty()
{
// Arrange
var request = new CreateProductRequestDto { Name = "" };
_mockProductService.Setup(s => s.AddProductAsync(request))
.ThrowsAsync(new ArgumentException("Product name cannot be empty."));

// Act
var result = await _controller.CreateProduct(request);

// Assert
Assert.IsInstanceOfType(result, typeof(ObjectResult));
var objectResult = result as ObjectResult;
Assert.AreEqual(500, objectResult.StatusCode);
Assert.AreEqual("An error occurred while creating the product.", objectResult.Value);
_mockProductService.Verify(s => s.AddProductAsync(request), Times.Once);
}

[TestMethod]
public async Task CreateProduct_ReturnsCreated_WhenProductIsCreated()
{
// Arrange
var request = new CreateProductRequestDto { Name = "Valid Product" };
_mockProductService.Setup(s => s.AddProductAsync(request)).ReturnsAsync(1);

// Act
var result = await _controller.CreateProduct(request);

// Assert
Assert.IsInstanceOfType(result, typeof(ObjectResult));
var objectResult = result as ObjectResult;
Assert.AreEqual(201, objectResult.StatusCode);
_mockProductService.Verify(s => s.AddProductAsync(request), Times.Once);
}

[TestMethod]
public async Task GetProductById_ReturnsProduct_WhenIdIsValid()
{
// Arrange
var productId = 1;
var product = new Product { InstanceId = productId, Name = "Test Product" };
_mockProductService.Setup(s => s.GetProductByIdAsync(productId)).ReturnsAsync(product);

// Act
var result = await _controller.GetProduct(productId);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
var okResult = result as OkObjectResult;
Assert.AreEqual(product, okResult.Value);
_mockProductService.Verify(s => s.GetProductByIdAsync(productId), Times.Once);
}

[TestMethod]
public async Task GetProductById_ReturnsNotFound_WhenIdIsInvalid()
{
// Arrange
var productId = 999;
_mockProductService.Setup(s => s.GetProductByIdAsync(productId))
.ThrowsAsync(new KeyNotFoundException("Product not found."));

// Act
var result = await _controller.GetProduct(productId);

// Assert
Assert.IsInstanceOfType(result, typeof(ObjectResult));
var objectResult = result as ObjectResult;
Assert.AreEqual(500, objectResult.StatusCode);
Assert.AreEqual("An error occurred while searching for products.", objectResult.Value);
_mockProductService.Verify(s => s.GetProductByIdAsync(productId), Times.Once);

}

[TestMethod]
public async Task SearchProducts_ReturnsProducts_WhenCriteriaIsValid()
{
// Arrange
var searchCriteria = new SearchProductRequestDto { Name = "Test" };
var products = new List<Product> { new Product { InstanceId = 1, Name = "Test Product" } };
_mockProductService.Setup(s => s.SearchProductAsync(searchCriteria)).ReturnsAsync(products);

// Act
var result = await _controller.SearchProducts(searchCriteria);

// Assert
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
var okResult = result as OkObjectResult;
Assert.AreEqual(products, okResult.Value);
_mockProductService.Verify(s=> s.SearchProductAsync(searchCriteria), Times.Once);
}
}
}
18 changes: 18 additions & 0 deletions Development Project/Interview.Web.Tests/Interview.Web.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.2.1" />
<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Interview.Web\Interview.Web.csproj" />
<ProjectReference Include="..\Sparcpoint.Core\Sparcpoint.Core.csproj" />
<ProjectReference Include="..\Sparcpoint.Inventory.Database\Sparcpoint.Inventory.Database.sqlproj" />
<ProjectReference Include="..\Sparcpoint.SqlServer.Abstractions\Sparcpoint.SqlServer.Abstractions.csproj" />
</ItemGroup>
</Project>
Loading