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
36 changes: 36 additions & 0 deletions hotel-booking-app-tests/Controllers/HotelsContollerTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using HotelBookingApp.Controllers;
using HotelBookingApp.Models.HotelModels;
using HotelBookingApp.Pages;
using HotelBookingApp.Services;
using HotelBookingAppTests.TestUtils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -25,6 +28,39 @@ public HotelControllerTests()
propertyServiceMock = new Mock<IPropertyTypeService>();
}

[Fact]
public async Task Add_WhenValid_ShouldCallServieAndRedirect()
{
var hotelVM = new HotelViewModel
{
Hotel = new Hotel
{
HotelId = 1
}
};
var imageList = new List<IFormFile>();
var controllerContext = ControllerContextProvider.GetDefault();
hotelServiceMock.Setup(s => s.Add(hotelVM.Hotel))
.ReturnsAsync(hotelVM.Hotel);
var controller = new HotelsController(hotelServiceMock.Object,
imageServiceMock.Object, thumbnailServiceMock.Object, propertyServiceMock.Object)
{
ControllerContext = controllerContext
};


var result = await controller.Add(hotelVM, imageList);
var redirectResult = Assert.IsType<RedirectToActionResult>(result);
var redirectId = (int)redirectResult.RouteValues["id"];

Assert.Null(redirectResult.ControllerName);
Assert.Equal(nameof(controller.Hotel), redirectResult.ActionName);
Assert.Equal(controllerContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier), hotelVM.Hotel.ApplicationUserId);
Assert.Equal(hotelVM.Hotel.HotelId, redirectId);
hotelServiceMock.Verify(s => s.Add(hotelVM.Hotel), Times.Once);
imageServiceMock.Verify(s => s.UploadImagesAsync(imageList, hotelVM.Hotel.HotelId), Times.Once);
}

[Fact]
public async Task AddReview_WhenValid_ShouldCallServiceAndRedirect()
{
Expand Down
148 changes: 148 additions & 0 deletions hotel-booking-app-tests/Services/HotelServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -52,5 +53,152 @@ public async Task Add_WhenCalled_ShouldAddAHotel()
Assert.Equal(hotelName, context.Hotels.Find(hotel.HotelId).Name);
}
}

[Fact]
public async Task Delete_WhenCalled_ShouldDeleteAHotel()
{
var id = 0;
using (var context = new ApplicationContext(options))
{
var hotel = context.Add(new Hotel());
await context.SaveChangesAsync();
id = hotel.Entity.HotelId;
}
using (var context = new ApplicationContext(options))
{
var hotelService = new HotelService(context,
imageServiceMock.Object,
thumbnailServiceMock.Object,
localizerMock.Object,
mapper.Object);
var contextLenght = context.Hotels.Count();
var hotel = context.Hotels.SingleOrDefault(h => h.HotelId == id);

await hotelService.Delete(id);

Assert.Equal(contextLenght - 1, context.Hotels.Count());
Assert.False(context.Hotels.Contains(hotel));
}
}

[Fact]
public async Task FindByIdAsync_WhenCalled_ShouldReturnHotelWithID()
{
var id = 0;
var name = "findByID hotel";
var location = new Location();
var pt = new PropertyType();
var rooms = new List<Room>();
rooms.Add(new Room());

using (var context = new ApplicationContext(options))
{
var hotel = context.Add(
new Hotel { Name = name, Location = location, PropertyType = pt, Rooms = rooms }
);
await context.SaveChangesAsync();
id = hotel.Entity.HotelId;

}
using (var context = new ApplicationContext(options))
{
var hotelService = new HotelService(context,
imageServiceMock.Object,
thumbnailServiceMock.Object,
localizerMock.Object,
mapper.Object);
var hotel = context.Hotels.SingleOrDefault(h => h.HotelId == id);

var result = await hotelService.FindByIdAsync(id);

Assert.Equal(hotel, result);
Assert.Equal(name, result.Name);
}
}

[Fact]
public async Task Update_WhenCalled_ShouldUpdateAHotel()
{
var id = 0;
using (var context = new ApplicationContext(options))
{
var pt = context.PropertyTypes.Add(new PropertyType());
var ptId = pt.Entity.PropertyTypeId;

var hotel = context.Add(
new Hotel {PropertyTypeId = ptId, StarRating = 5});
await context.SaveChangesAsync();
id = hotel.Entity.HotelId;
}
using (var context = new ApplicationContext(options))
{
var hotelService = new HotelService(context,
imageServiceMock.Object,
thumbnailServiceMock.Object,
localizerMock.Object,
mapper.Object);
var contextLenght = context.Hotels.Count();
var hotel = context.Hotels.SingleOrDefault(h => h.HotelId == id);
hotel.Name = "not new";

await hotelService.Update(hotel);

Assert.Equal(contextLenght, context.Hotels.Count());
Assert.Equal("not new", hotel.Name);
Assert.True(hotel.StarRating == 5);
}
}

[Fact]
public async Task AddReview_WhenCalled_ShouldAddAReview()
{
using (var context = new ApplicationContext(options))
{
var hotelService = new HotelService(context,
imageServiceMock.Object,
thumbnailServiceMock.Object,
localizerMock.Object,
mapper.Object);
var contextLength = context.Reviews.Count();
var comment = "comment";

var review = await hotelService.AddReviewAsync(
new Review {Comment = comment }
);

Assert.Equal(contextLength + 1, context.Reviews.Count());
Assert.True(context.Reviews.Contains(review));
Assert.Equal(comment, context.Reviews.Find(review.ReviewId).Comment);
}
}

[Fact]
public async Task DeleteReview_WhenCalled_ShouldDeleteAReview()
{
var id = 0;
using (var context = new ApplicationContext(options))
{
var review = context.Add(
new Review { Rating = 5 }
);
await context.SaveChangesAsync();
id = review.Entity.ReviewId;
}
using (var context = new ApplicationContext(options))
{
var hotelService = new HotelService(context,
imageServiceMock.Object,
thumbnailServiceMock.Object,
localizerMock.Object,
mapper.Object);
var contextLenght = context.Reviews.Count();
var review = context.Reviews.SingleOrDefault(h => h.HotelId == id);

await hotelService.DeleteReview(id);

Assert.Equal(contextLenght - 1, context.Reviews.Count());
Assert.False(context.Reviews.Contains(review));
}
}
}
}