294 lines
13 KiB
C#
294 lines
13 KiB
C#
|
using AutoMapper;
|
||
|
using DamageAssesment.Api.Locations.Controllers;
|
||
|
using DamageAssesment.Api.Locations.Db;
|
||
|
using DamageAssesment.Api.Locations.Interfaces;
|
||
|
using DamageAssesment.Api.Locations.Profiles;
|
||
|
using DamageAssesment.Api.Locations.Providers;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Moq;
|
||
|
using Xunit;
|
||
|
|
||
|
namespace DamageAssesment.Api.Locations.Test
|
||
|
{
|
||
|
public class LocationsServiceTest
|
||
|
{
|
||
|
|
||
|
[Fact(DisplayName = "Get Location using Location ID")]
|
||
|
public async Task GetLocationsUsingLocationID()
|
||
|
{
|
||
|
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
||
|
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
||
|
.Options;
|
||
|
|
||
|
var dbContext = new LocationDbContext(options);
|
||
|
CreateLocations(dbContext);
|
||
|
//Mapping
|
||
|
var LocationsProfile = new LocationProfile();
|
||
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
||
|
var mapper = new Mapper(configuration);
|
||
|
|
||
|
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
||
|
//Testmethode
|
||
|
var Location = await LocationsProvider.GetLocationByIdAsync("Loc3");
|
||
|
|
||
|
Assert.True(Location.IsSuccess);
|
||
|
Assert.Null(Location.ErrorMessage);
|
||
|
}
|
||
|
[Fact(DisplayName = "Get Locations")]
|
||
|
public async Task GetAllLocationsTest()
|
||
|
{
|
||
|
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
||
|
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
||
|
.Options;
|
||
|
|
||
|
var dbContext = new LocationDbContext(options);
|
||
|
CreateLocations(dbContext);
|
||
|
//Mapping
|
||
|
var LocationsProfile = new LocationProfile();
|
||
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
||
|
var mapper = new Mapper(configuration);
|
||
|
|
||
|
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
||
|
//Testmethode
|
||
|
var Location = await LocationsProvider.GetLocationsAsync();
|
||
|
|
||
|
Assert.True(Location.IsSuccess);
|
||
|
Assert.Null(Location.ErrorMessage);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Delete Location by Id")]
|
||
|
public async Task DeleteLocationTest()
|
||
|
{
|
||
|
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
||
|
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
||
|
.Options;
|
||
|
|
||
|
var dbContext = new LocationDbContext(options);
|
||
|
CreateLocations(dbContext);
|
||
|
//Mapping
|
||
|
var LocationsProfile = new LocationProfile();
|
||
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
||
|
var mapper = new Mapper(configuration);
|
||
|
|
||
|
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
||
|
//Testmethode
|
||
|
var Location = await LocationsProvider.DeleteLocationAsync("Loc2");
|
||
|
|
||
|
Assert.True(Location.IsSuccess);
|
||
|
Assert.NotNull(Location.ErrorMessage);
|
||
|
}
|
||
|
[Fact(DisplayName = "Add Location")]
|
||
|
public async Task AddLocationTest()
|
||
|
{
|
||
|
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
||
|
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
||
|
.Options;
|
||
|
|
||
|
var dbContext = new LocationDbContext(options);
|
||
|
CreateLocations(dbContext);
|
||
|
//Mapping
|
||
|
var LocationsProfile = new LocationProfile();
|
||
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
||
|
var mapper = new Mapper(configuration);
|
||
|
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
||
|
//Testmethode
|
||
|
Db.Location newLocation = new Db.Location() { Id = "Loc9", RegionId = "1", Name = "Test 1", MaintenanceCenter = "1", SchoolType = "US" };
|
||
|
var Location = await LocationsProvider.PostLocationAsync(newLocation);
|
||
|
|
||
|
Assert.True(Location.IsSuccess);
|
||
|
Assert.Null(Location.ErrorMessage);
|
||
|
}
|
||
|
[Fact(DisplayName = "Update Location")]
|
||
|
public async Task UpdateLocationTest()
|
||
|
{
|
||
|
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
||
|
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
||
|
.Options;
|
||
|
var dbContext = new LocationDbContext(options);
|
||
|
CreateLocations(dbContext);
|
||
|
//Mapping
|
||
|
var LocationsProfile = new LocationProfile();
|
||
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
||
|
var mapper = new Mapper(configuration);
|
||
|
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
||
|
//Testmethode
|
||
|
Db.Location updateLocation = new Db.Location() { Id = "Loc1", RegionId = "1", Name = "Tampa", MaintenanceCenter = "1", SchoolType = "NA" };
|
||
|
var Location = await LocationsProvider.UpdateLocationAsync(updateLocation);
|
||
|
var modified = dbContext.Locations.FirstOrDefault(a => a.Id == updateLocation.Id);
|
||
|
Assert.True(Location.IsSuccess);
|
||
|
Assert.NotNull(Location.ErrorMessage);
|
||
|
}
|
||
|
private static void CreateLocations(LocationDbContext dbContext)
|
||
|
{
|
||
|
//Create sample data for testing
|
||
|
if (dbContext.Locations.Count() == 0)
|
||
|
{
|
||
|
for (int i = 1; i < 6; i++)
|
||
|
{
|
||
|
dbContext.Locations.Add(new Db.Location()
|
||
|
{
|
||
|
Id = "Loc"+i.ToString(),
|
||
|
RegionId = i.ToString(),
|
||
|
Name = "Test Location" + Guid.NewGuid().ToString(),
|
||
|
MaintenanceCenter = i.ToString(),
|
||
|
SchoolType = "US"
|
||
|
|
||
|
});
|
||
|
}
|
||
|
dbContext.SaveChanges();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//Tests for regions
|
||
|
|
||
|
[Fact(DisplayName = "Get Regions - Ok case")]
|
||
|
public async Task GetRegionsAsync_ShouldReturnStatusCode200()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getOkResponse();
|
||
|
mockRegionService.Setup(service => service.GetRegionsAsync()).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (OkObjectResult)await regionProvider.GetRegionsAsync();
|
||
|
|
||
|
Assert.Equal(200, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Get Regions - NoContent Case")]
|
||
|
public async Task GetRegionsAsync_ShouldReturnStatusCode204()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getNoContentResponse();
|
||
|
mockRegionService.Setup(service => service.GetRegionsAsync()).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (NoContentResult)await regionProvider.GetRegionsAsync();
|
||
|
|
||
|
Assert.Equal(204, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Get Region by Id - Ok case")]
|
||
|
public async Task GetRegionAsync_ShouldReturnStatusCode200()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getOkResponse("1");
|
||
|
mockRegionService.Setup(service => service.GetRegionByIdAsync("1")).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (OkObjectResult)await regionProvider.GetRegionAsync("1");
|
||
|
|
||
|
Assert.Equal(200, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Get Region by Id - NotFound case")]
|
||
|
public async Task GetRegionAsync_ShouldReturnStatusCode404()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getNotFoundResponse();
|
||
|
mockRegionService.Setup(service => service.GetRegionByIdAsync("99")).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (NotFoundResult)await regionProvider.GetRegionAsync("99");
|
||
|
Assert.Equal(404, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Post Region - Ok case")]
|
||
|
public async Task PostSurveyAsync_ShouldReturnStatusCode200()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getOkResponse("1");
|
||
|
var mockInputRegion = await MockData.getInputRegionData();
|
||
|
mockRegionService.Setup(service => service.PostRegionAsync(mockInputRegion)).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (OkObjectResult)await regionProvider.PostRegionAsync(mockInputRegion);
|
||
|
|
||
|
Assert.Equal(200, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Post Region - BadRequest case")]
|
||
|
public async Task PostSurveyAsync_ShouldReturnStatusCode400()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getBadRequestResponse();
|
||
|
var mockInputRegion = await MockData.getInputRegionData();
|
||
|
mockRegionService.Setup(service => service.PostRegionAsync(mockInputRegion)).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (BadRequestObjectResult)await regionProvider.PostRegionAsync(mockInputRegion);
|
||
|
|
||
|
Assert.Equal(400, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Put Region - Ok case")]
|
||
|
public async Task PutRegionAsync_ShouldReturnStatusCode200()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getOkResponse("1");
|
||
|
var mockInputRegion = await MockData.getInputRegionData();
|
||
|
mockRegionService.Setup(service => service.PutRegionAsync(mockInputRegion)).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (OkObjectResult)await regionProvider.PutRegionAsync(mockInputRegion);
|
||
|
|
||
|
Assert.Equal(200, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Put Region - NotFound case")]
|
||
|
public async Task PutSurveyAsync_ShouldReturnStatusCode404()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getNotFoundResponse();
|
||
|
var mockInputRegion = await MockData.getInputRegionData();
|
||
|
mockRegionService.Setup(service => service.PutRegionAsync(mockInputRegion)).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (NotFoundObjectResult)await regionProvider.PutRegionAsync(mockInputRegion);
|
||
|
|
||
|
Assert.Equal(404, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Put Region - BadRequest case")]
|
||
|
public async Task PutSurveyAsync_ShouldReturnStatusCode400()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getBadRequestResponse();
|
||
|
var mockInputRegion = await MockData.getInputRegionData();
|
||
|
mockRegionService.Setup(service => service.PutRegionAsync(mockInputRegion)).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (BadRequestObjectResult)await regionProvider.PutRegionAsync(mockInputRegion);
|
||
|
|
||
|
Assert.Equal(400, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Delete Region - Ok case")]
|
||
|
public async Task DeleteSurveyAsync_ShouldReturnStatusCode200()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getOkResponse("1");
|
||
|
|
||
|
mockRegionService.Setup(service => service.DeleteRegionAsync("1")).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (OkObjectResult)await regionProvider.DeleteRegionAsync("1");
|
||
|
|
||
|
Assert.Equal(200, result.StatusCode);
|
||
|
}
|
||
|
|
||
|
[Fact(DisplayName = "Delete Region - NotFound case")]
|
||
|
public async Task DeleteSurveyAsync_ShouldReturnStatusCode404()
|
||
|
{
|
||
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||
|
var mockResponse = await MockData.getNotFoundResponse();
|
||
|
|
||
|
mockRegionService.Setup(service => service.DeleteRegionAsync("1")).ReturnsAsync(mockResponse);
|
||
|
|
||
|
var regionProvider = new RegionsController(mockRegionService.Object);
|
||
|
var result = (NotFoundResult)await regionProvider.DeleteRegionAsync("1");
|
||
|
|
||
|
Assert.Equal(404, result.StatusCode);
|
||
|
}
|
||
|
}
|
||
|
}
|