using DamageAssesment.Api.Locations.Controllers; using DamageAssesment.Api.Locations.Interfaces; using Microsoft.AspNetCore.Mvc; using Moq; using Xunit; namespace DamageAssesment.Api.Locations.Test { public class LocationsServiceTest { //Test for locations [Fact(DisplayName = "Get Locations - Ok case")] public async Task GetLocationsAsync_ShouldReturnStatusCode200() { var mockLocationService = new Mock(); var mockResponse = await MockData.getOkResponseLocation(); mockLocationService.Setup(service => service.GetLocationsAsync()).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (OkObjectResult)await locationProvider.GetLocationsAsync(); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Get Locations - NoContent Case")] public async Task GetLocationsAsync_ShouldReturnStatusCode204() { var mockLocationService = new Mock(); var mockResponse = await MockData.getNotFoundResponseLocation(); mockLocationService.Setup(service => service.GetLocationsAsync()).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (NoContentResult)await locationProvider.GetLocationsAsync(); Assert.Equal(204, result.StatusCode); } [Fact(DisplayName = "Get Locations by Id- Ok case")] public async Task GetLocationsByIdAsync_ShouldReturnStatusCode200() { var mockLocationService = new Mock(); var mockResponse = await MockData.getOkResponseLocation(1); mockLocationService.Setup(service => service.GetLocationByIdAsync(1)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (OkObjectResult)await locationProvider.GetLocationByIdAsync(1); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Get Locations By Id - NoFound Case")] public async Task GetLocationsByIdAsync_ShouldReturnStatusCode404() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocationNotFoundResponse(); mockLocationService.Setup(service => service.GetLocationByIdAsync(1)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (NotFoundResult)await locationProvider.GetLocationByIdAsync(1); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Post Location - Ok case")] public async Task PostLocationAsync_ShouldReturnStatusCode200() { var mockLocationService = new Mock(); var mockResponse = await MockData.getOkResponseLocation(1); var mockInputLocation = new Models.Location { Id = 1, RegionId = 1, Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" }; mockLocationService.Setup(service => service.PostLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (OkObjectResult)await locationProvider.CreateLocation(mockInputLocation); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Post Location - BadRequest case")] public async Task PostLocationAsync_ShouldReturnStatusCode400() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocationNotFoundResponse(); var mockInputLocation = new Models.Location { Id = 1, RegionId = 1, Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" }; mockLocationService.Setup(service => service.PostLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (BadRequestObjectResult)await locationProvider.CreateLocation(mockInputLocation); Assert.Equal(400, result.StatusCode); } [Fact(DisplayName = "Put Location - Ok case")] public async Task PutLocationAsync_ShouldReturnStatusCode200() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocation(true, "update success"); var mockInputLocation = new Models.Location { Id = 1, LocationCode ="Loc1", RegionId = 1, Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" }; mockLocationService.Setup(service => service.UpdateLocationAsync(1,mockInputLocation)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (OkObjectResult)await locationProvider.UpdateLocation(1,mockInputLocation); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Put Location - NotFound case")] public async Task PutLocationAsync_ShouldReturnStatusCode404() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocation(false, null); var mockInputLocation = new Models.Location { Id = 1, RegionId = 1, Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" }; mockLocationService.Setup(service => service.UpdateLocationAsync(1,mockInputLocation)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (NotFoundResult)await locationProvider.UpdateLocation(1,mockInputLocation); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Delete Location - Ok case")] public async Task DeleteLocationAsync_ShouldReturnStatusCode200() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocation(true, "delete success"); mockLocationService.Setup(service => service.DeleteLocationAsync(1)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (OkObjectResult)await locationProvider.DeleteLocation(1); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Delete Location - NotFound case")] public async Task DeleteLocationAsync_ShouldReturnStatusCode404() { var mockLocationService = new Mock(); var mockResponse = await MockData.getLocation(false, null); mockLocationService.Setup(service => service.DeleteLocationAsync(1)).ReturnsAsync(mockResponse); var locationProvider = new LocationsController(mockLocationService.Object); var result = (NotFoundResult)await locationProvider.DeleteLocation(1); Assert.Equal(404, result.StatusCode); } //Tests for regions [Fact(DisplayName = "Get Regions - Ok case")] public async Task GetRegionsAsync_ShouldReturnStatusCode200() { var mockRegionService = new Mock(); 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(); 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(); 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(); var mockResponse = await MockData.getNotFoundResponse(); mockRegionService.Setup(service => service.GetRegionByIdAsync(99999)).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 PostRegionAsync_ShouldReturnStatusCode200() { var mockRegionService = new Mock(); 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 PostRegionAsync_ShouldReturnStatusCode400() { var mockRegionService = new Mock(); 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(); var mockResponse = await MockData.getOkResponse(1); var mockInputRegion = await MockData.getInputRegionData(); mockRegionService.Setup(service => service.PutRegionAsync(1,mockInputRegion)).ReturnsAsync(mockResponse); var regionProvider = new RegionsController(mockRegionService.Object); var result = (OkObjectResult)await regionProvider.PutRegionAsync(1,mockInputRegion); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Put Region - NotFound case")] public async Task PutRegionAsync_ShouldReturnStatusCode404() { var mockRegionService = new Mock(); var mockResponse = await MockData.getNotFoundResponse(); var mockInputRegion = await MockData.getInputRegionData(); mockRegionService.Setup(service => service.PutRegionAsync(1, mockInputRegion)).ReturnsAsync(mockResponse); var regionProvider = new RegionsController(mockRegionService.Object); var result = (NotFoundObjectResult)await regionProvider.PutRegionAsync(1,mockInputRegion); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Put Region - BadRequest case")] public async Task PutRegionyAsync_ShouldReturnStatusCode400() { var mockRegionService = new Mock(); var mockResponse = await MockData.getBadRequestResponse(); var mockInputRegion = await MockData.getInputRegionData(); mockRegionService.Setup(service => service.PutRegionAsync(1, mockInputRegion)).ReturnsAsync(mockResponse); var regionProvider = new RegionsController(mockRegionService.Object); var result = (BadRequestObjectResult)await regionProvider.PutRegionAsync(1, mockInputRegion); Assert.Equal(400, result.StatusCode); } [Fact(DisplayName = "Delete Region - Ok case")] public async Task DeleteRegionAsync_ShouldReturnStatusCode200() { var mockRegionService = new Mock(); 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 DeleteRegionAsync_ShouldReturnStatusCode404() { var mockRegionService = new Mock(); 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); } } }