public void DeleteAsync_DeleteCinema_Failed_Return_BadRequest() { //Arrange string expectedMessage = Messages.THEATRE_DOES_NOT_EXIST; int expectedStatusCode = 400; TheatreDomainModel theatreDomainModel = null; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _theatreService.Setup(x => x.Delete(It.IsAny <int>())).Returns(responseTask); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.DeleteAsync(123).ConfigureAwait(false).GetAwaiter().GetResult().Result; var badObjectResult = ((BadRequestObjectResult)result).Value; var errorResult = (ErrorResponseModel)badObjectResult; //Assert Assert.IsNotNull(errorResult); Assert.AreEqual(expectedMessage, errorResult.ErrorMessage); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode); }
public void DeleteAsync_DeleteTheatre_Failed_Throw_DbException() { //Arrange string expectedMessage = "Inner exception error message."; int expectedStatusCode = 400; TheatreDomainModel theatreDomainModel = new TheatreDomainModel { Id = 123, AddressId = 555, Name = "ime", AuditoriumsList = new List <AuditoriumDomainModel>() }; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); Exception exception = new Exception("Inner exception error message."); DbUpdateException dbUpdateException = new DbUpdateException("Error.", exception); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _theatreService.Setup(x => x.Delete(It.IsAny <int>())).Throws(dbUpdateException); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.DeleteAsync(theatreDomainModel.Id).ConfigureAwait(false).GetAwaiter().GetResult().Result; var badObjectResult = ((BadRequestObjectResult)result).Value; var errorResult = (ErrorResponseModel)badObjectResult; //Assert Assert.IsNotNull(errorResult); Assert.AreEqual(expectedMessage, errorResult.ErrorMessage); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode); }
public void DeleteAsync_DeleteTheatre_IsSuccessful() { //Arrange int theatreDeleteId = 12; int expectedStatusCode = 202; TheatreDomainModel theatreDomainModel = new TheatreDomainModel { Id = theatreDeleteId, AddressId = 1234, Name = "Bioskop", AuditoriumsList = new List <AuditoriumDomainModel>() }; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _theatreService.Setup(x => x.Delete(It.IsAny <int>())).Returns(responseTask); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.DeleteAsync(theatreDeleteId).ConfigureAwait(false).GetAwaiter().GetResult().Result; var objectResult = ((AcceptedResult)result).Value; TheatreDomainModel theatreDomainModelResult = (TheatreDomainModel)objectResult; //Assert Assert.IsNotNull(theatreDomainModel); Assert.IsInstanceOfType(result, typeof(AcceptedResult)); Assert.AreEqual(expectedStatusCode, ((AcceptedResult)result).StatusCode); Assert.AreEqual(theatreDeleteId, theatreDomainModelResult.Id); }
public void PostAsync_CreateTheatre_Throw_DbException_Theatre() { //Arrange string expectedMessage = "Inner exception error message."; int expectedStatusCode = 400; CreateTheatreModel createTheatreModel = new CreateTheatreModel { Name = "Bioskop12345", CityName = "grad", NumberOfSeats = 12, SeatRows = 12, AuditName = "Sala23" }; TheatreDomainModel theatreDomainModel = new TheatreDomainModel { Id = 123, AddressId = 1423, Name = createTheatreModel.Name, AuditoriumsList = new List <AuditoriumDomainModel>() }; AddressDomainModel addressDomainModel = new AddressDomainModel { Id = 123, CityName = "grad123", StreetName = "ulica123" }; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); Task <AddressDomainModel> responseTask2 = Task.FromResult(addressDomainModel); Exception exception = new Exception("Inner exception error message."); DbUpdateException dbUpdateException = new DbUpdateException("Error.", exception); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Throws(dbUpdateException); _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result; var badObjectResult = ((BadRequestObjectResult)result).Value; var errorResult = (ErrorResponseModel)badObjectResult; //Assert Assert.IsNotNull(errorResult); Assert.AreEqual(expectedMessage, errorResult.ErrorMessage); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode); }
public void PostAsync_Create_IsSuccessful_True_Theatre() { //Arrange int expectedStatusCode = 201; CreateTheatreModel createTheatreModel = new CreateTheatreModel() { Name = "bioskop123", CityName = "grad", SeatRows = 15, NumberOfSeats = 11, AuditName = "Sala1" }; AddressDomainModel addressDomainModel = new AddressDomainModel { Id = 123, CityName = "grad123", StreetName = "ulica123" }; TheatreDomainModel theatreDomainModel = new TheatreDomainModel { AddressId = 1234, Name = createTheatreModel.Name, AuditoriumsList = new List <AuditoriumDomainModel>() }; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); Task <AddressDomainModel> responseTask2 = Task.FromResult(addressDomainModel); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2); _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask); TheatresController theatreController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatreController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result; var createdResult = ((CreatedResult)result).Value; var theatreReturnedModel = (TheatreDomainModel)createdResult; //Assert Assert.IsNotNull(theatreReturnedModel); Assert.IsInstanceOfType(result, typeof(CreatedResult)); Assert.AreEqual(expectedStatusCode, ((CreatedResult)result).StatusCode); }
public async Task <ActionResult <TheatreDomainModel> > PostAsync([FromBody] CreateTheatreModel createTheatreModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TheatreDomainModel theatreDomainModel = new TheatreDomainModel { Name = createTheatreModel.Name, CityName = createTheatreModel.CityName, StreetName = createTheatreModel.StreetName }; TheatreDomainModel insertedModel; try { insertedModel = await _theatreService.Create(theatreDomainModel, createTheatreModel.NumberOfSeats, createTheatreModel.SeatRows, createTheatreModel.AuditName); } catch (DbUpdateException e) { ErrorResponseModel errorResponse = new ErrorResponseModel { ErrorMessage = e.InnerException.Message ?? e.Message, StatusCode = System.Net.HttpStatusCode.BadRequest }; return(BadRequest(errorResponse)); } if (insertedModel == null) { ErrorResponseModel errorResponse = new ErrorResponseModel { ErrorMessage = Messages.THEATRE_CREATION_ERROR, StatusCode = System.Net.HttpStatusCode.BadRequest }; return(BadRequest(errorResponse)); } return(Created("theatres//" + insertedModel.Id, insertedModel)); }
public void PostAsync_Create_IsSuccessful_False_Return_BadRequest() { //Arrange string expectedMessage = Messages.THEATRE_CREATION_ERROR; int expectedStatusCode = 400; CreateTheatreModel createTheatreModel = new CreateTheatreModel { Name = "bioskop", CityName = "grad", NumberOfSeats = 13, SeatRows = 13, AuditName = "Sala12" }; AddressDomainModel addressDomainModel = new AddressDomainModel { Id = 123, CityName = "grad123", StreetName = "ulica123" }; TheatreDomainModel theatreDomainModel = null; Task <TheatreDomainModel> responseTask = Task.FromResult(theatreDomainModel); Task <AddressDomainModel> responseTask2 = Task.FromResult(addressDomainModel); _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2); _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result; var badObjectResult = ((BadRequestObjectResult)result).Value; var errorResult = (ErrorResponseModel)badObjectResult; //Assert Assert.IsNotNull(errorResult); Assert.AreEqual(expectedMessage, errorResult.ErrorMessage); Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode); }
public void GetAsync_Return_All_Theatres() { //Arrange List <TheatreDomainModel> theatreDomainModelsList = new List <TheatreDomainModel>(); TheatreDomainModel theatreDomainModel = new TheatreDomainModel { Id = 123, Name = "Teatar1", AddressId = 23, AuditoriumsList = new List <AuditoriumDomainModel>() }; theatreDomainModelsList.Add(theatreDomainModel); IEnumerable <TheatreDomainModel> theatreDomainModels = theatreDomainModelsList; Task <IEnumerable <TheatreDomainModel> > responseTask = Task.FromResult(theatreDomainModels); int expectedResultCount = 1; int expectedStatusCode = 200; _theatreService = new Mock <ITheatreService>(); _addressService = new Mock <IAddressService>(); _theatreService.Setup(x => x.GetAllAsync()).Returns(responseTask); TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object); //Act var result = theatresController.GetAsync().ConfigureAwait(false).GetAwaiter().GetResult().Result; var resultListObjects = ((OkObjectResult)result).Value; var theatreDomainModelResultList = (List <TheatreDomainModel>)resultListObjects; //Assert Assert.IsNotNull(theatreDomainModelResultList); Assert.AreEqual(expectedResultCount, theatreDomainModelResultList.Count); Assert.AreEqual(theatreDomainModel.Id, theatreDomainModelResultList[0].Id); Assert.IsInstanceOfType(result, typeof(OkObjectResult)); Assert.AreEqual(expectedStatusCode, ((OkObjectResult)result).StatusCode); }
public async Task <IEnumerable <TheatreDomainModel> > GetAllAsync() { var data = await _theatreRepository.GetAllAsync(); List <TheatreDomainModel> result = new List <TheatreDomainModel>(); TheatreDomainModel model; foreach (var theatre in data) { model = new TheatreDomainModel { Id = theatre.Id, Name = theatre.Name, AddressId = theatre.AddressId, CityName = theatre.Address.CityName, StreetName = theatre.Address.StreetName, AuditoriumsList = new List <AuditoriumDomainModel>() }; foreach (var auditorium in theatre.Auditoriums) { AuditoriumDomainModel auditoriumModel = new AuditoriumDomainModel { Id = auditorium.Id, TheatreId = theatre.Id, Name = auditorium.Name }; model.AuditoriumsList.Add(auditoriumModel); } result.Add(model); } return(result); }
public async Task <TheatreDomainModel> Create(TheatreDomainModel theatreDomainModel, int numOfSeats, int numOfRows, string auditoriumName) { var theatres = await _theatreRepository.GetAllAsync(); foreach (var theatre in theatres) { if (theatre.Address.CityName == theatreDomainModel.CityName) { if (theatre.Name == theatreDomainModel.Name || theatre.Address.StreetName == theatreDomainModel.StreetName) { return(null); } } } Theatre newTheatre = new Theatre { Name = theatreDomainModel.Name, Address = new Address { CityName = theatreDomainModel.CityName, StreetName = theatreDomainModel.StreetName } }; newTheatre.Auditoriums = new List <Auditorium>(); Auditorium auditorium = new Auditorium { Name = auditoriumName }; auditorium.Seats = new List <Seat>(); for (int j = 1; j <= numOfRows; j++) { for (int k = 1; k <= numOfSeats; k++) { Seat seat = new Seat { Row = j, Number = k }; auditorium.Seats.Add(seat); } } newTheatre.Auditoriums.Add(auditorium); Theatre insertedTheatre = _theatreRepository.Insert(newTheatre); if (insertedTheatre == null) { return(null); } _theatreRepository.Save(); if (insertedTheatre == null) { return(null); } TheatreDomainModel theatreModel = new TheatreDomainModel { Id = insertedTheatre.Id, Name = insertedTheatre.Name, AddressId = insertedTheatre.AddressId, AuditoriumsList = new List <AuditoriumDomainModel>() }; foreach (var auditoriumInserted in insertedTheatre.Auditoriums) { AuditoriumDomainModel modelAuditoroum = new AuditoriumDomainModel { Id = auditoriumInserted.Id, TheatreId = insertedTheatre.Id, Name = auditoriumInserted.Name, SeatsList = new List <SeatDomainModel>() }; foreach (var seat in auditoriumInserted.Seats) { modelAuditoroum.SeatsList.Add(new SeatDomainModel { Id = seat.Id, AuditoriumId = auditoriumInserted.Id, Number = seat.Number, Row = seat.Row }); } theatreModel.AuditoriumsList.Add(modelAuditoroum); } return(theatreModel); }
public async Task <TheatreDomainModel> Delete(int id) { var theatre = await _theatreRepository.GetByIdAsync(id); if (theatre == null) { return(null); } var allAuditoriums = await _auditoriumsRepository.GetAllAsync(); if (allAuditoriums == null) { return(null); } var auditoriumsInTheatre = allAuditoriums.Where(x => x.TheatreId == id); var seats = await _seatsRepository.GetAllAsync(); foreach (var auditorium in auditoriumsInTheatre) { seats = seats.Where(x => x.AuditoriumId == auditorium.Id); foreach (var seat in seats) { if (seat.ReservationSeats.Any()) { return(null); } //dodat await await _seatsRepository.Delete(seat.Id); } //dodat await await _auditoriumsRepository.Delete(auditorium.Id); } TheatreDomainModel theatreModel = new TheatreDomainModel { Id = theatre.Id, Name = theatre.Name, AddressId = theatre.AddressId, AuditoriumsList = theatre.Auditoriums.Select(x => new AuditoriumDomainModel { Id = x.Id, TheatreId = x.TheatreId, Name = x.Name, SeatsList = x.Seats.Select(x => new SeatDomainModel { Id = x.Id, AuditoriumId = x.AuditoriumId, Number = x.Number, Row = x.Row }) .ToList() }) .ToList() }; //dodat await await _theatreRepository.Delete(theatre.Id); _theatreRepository.Save(); return(theatreModel); }