public void PostAsync_With_Invalid_ModelState_Return_BadRequest()
        {
            //Arrange
            string expectedMessage    = "Invalid Model State";
            int    expectedStatusCode = 400;

            CreateCinemaModel createCinemaModel = new CreateCinemaModel
            {
                Name          = "bioskop",
                CityName      = "grad",
                NumberOfSeats = 13,
                SeatRows      = 13
            };

            _cinemaService = new Mock <ICinemaService>();
            _cityService   = new Mock <ICityService>();
            CinemasController cinemasController = new CinemasController(_cinemaService.Object, _cityService.Object);

            cinemasController.ModelState.AddModelError("key", "Invalid Model State");


            //Act
            var result         = cinemasController.PostAsync(createCinemaModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var resultResponse = (BadRequestObjectResult)result;
            var createdResult  = ((BadRequestObjectResult)result).Value;
            var errorResponse  = ((SerializableError)createdResult).GetValueOrDefault("key");
            var message        = (string[])errorResponse;

            //Assert
            Assert.IsNotNull(resultResponse);
            Assert.AreEqual(expectedMessage, message[0]);
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            Assert.AreEqual(expectedStatusCode, resultResponse.StatusCode);
        }
        public void PostAsync_CreateCinema_Throw_DbException_Cinema()
        {
            //Arrange
            string expectedMessage    = "Inner exception error message.";
            int    expectedStatusCode = 400;

            CreateCinemaModel createCinemaModel = new CreateCinemaModel
            {
                Name          = "Bioskop12345",
                CityName      = "grad",
                NumberOfSeats = 12,
                SeatRows      = 12
            };

            CinemaDomainModel cinemaDomainModel = new CinemaDomainModel
            {
                Id              = 123,
                CityId          = 1423,
                Name            = createCinemaModel.Name,
                AuditoriumsList = new List <AuditoriumDomainModel>()
            };

            CityDomainModel cityDomainModel = new CityDomainModel
            {
                Id          = 123,
                Name        = "grad123",
                CinemasList = new List <CinemaDomainModel>()
            };

            Task <CinemaDomainModel> responseTask  = Task.FromResult(cinemaDomainModel);
            Task <CityDomainModel>   responseTask2 = Task.FromResult(cityDomainModel);
            Exception         exception            = new Exception("Inner exception error message.");
            DbUpdateException dbUpdateException    = new DbUpdateException("Error.", exception);

            _cinemaService = new Mock <ICinemaService>();
            _cityService   = new Mock <ICityService>();

            _cinemaService.Setup(x => x.CreateCinemaAsync(It.IsAny <CinemaDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Throws(dbUpdateException);
            _cityService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            CinemasController cinemasController = new CinemasController(_cinemaService.Object, _cityService.Object);

            //Act
            var result          = cinemasController.PostAsync(createCinemaModel).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 async Task <ActionResult <CinemaDomainModel> > PostAsync([FromBody]   CreateCinemaModel createCinemaModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CityDomainModel cityModel = await _cityService.GetByCityNameAsync(createCinemaModel.CityName);

            if (cityModel == null)
            {
                return(BadRequest(Messages.CITY_NOT_FOUND));
            }

            CinemaDomainModel cinemaDomainModel = new CinemaDomainModel
            {
                Name   = createCinemaModel.Name,
                CityId = cityModel.Id,
            };

            CinemaDomainModel insertedModel;

            try
            {
                insertedModel = await _cinemaService.CreateCinemaAsync(cinemaDomainModel, createCinemaModel.NumberOfSeats, createCinemaModel.SeatRows, createCinemaModel.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.CINEMA_CREATION_ERROR,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            return(Created("//cinemas" + insertedModel.Id, insertedModel));
        }
        public void PostAsync_Create_IsSuccessful_True_Cinema()
        {
            //Arrange
            int expectedStatusCode = 201;

            CreateCinemaModel createCinemaModel = new CreateCinemaModel()
            {
                Name          = "bioskop123",
                CityName      = "grad",
                SeatRows      = 15,
                NumberOfSeats = 11
            };

            CityDomainModel cityDomainModel = new CityDomainModel
            {
                Id          = 123,
                Name        = "grad123",
                CinemasList = new List <CinemaDomainModel>()
            };

            CinemaDomainModel cinemaDomainModel = new CinemaDomainModel
            {
                CityId          = 1234,
                Name            = createCinemaModel.Name,
                AuditoriumsList = new List <AuditoriumDomainModel>()
            };

            Task <CinemaDomainModel> responseTask  = Task.FromResult(cinemaDomainModel);
            Task <CityDomainModel>   responseTask2 = Task.FromResult(cityDomainModel);

            _cinemaService = new Mock <ICinemaService>();
            _cityService   = new Mock <ICityService>();
            _cityService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            _cinemaService.Setup(x => x.CreateCinemaAsync(It.IsAny <CinemaDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask);
            CinemasController cinemasController = new CinemasController(_cinemaService.Object, _cityService.Object);


            //Act
            var result              = cinemasController.PostAsync(createCinemaModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var createdResult       = ((CreatedResult)result).Value;
            var cinemaReturnedModel = (CinemaDomainModel)createdResult;

            //Assert
            Assert.IsNotNull(cinemaReturnedModel);
            Assert.IsInstanceOfType(result, typeof(CreatedResult));
            Assert.AreEqual(expectedStatusCode, ((CreatedResult)result).StatusCode);
        }
Beispiel #5
0
        public async Task <ActionResult> Put(int id, [FromBody] CreateCinemaModel cinemaModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CinemaDomainModel cinemaToUpdate;

            cinemaToUpdate = await _cinemaService.GetCinemaByIdAsync(id);

            if (cinemaToUpdate == null)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = Messages.CINEMA_DOES_NOT_EXIST,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            cinemaToUpdate.Name = cinemaModel.Name;

            CinemaDomainModel cinemaDomainModel;

            try
            {
                cinemaDomainModel = await _cinemaService.UpdateCinema(cinemaToUpdate);
            }
            catch (DbUpdateException e)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = e.InnerException.Message ?? e.Message,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            return(Accepted("cinemas//" + cinemaDomainModel.Id, cinemaDomainModel));
        }
        public void PostAsync_Create_IsSuccessful_False_Return_BadRequest()
        {
            //Arrange
            string expectedMessage    = Messages.CINEMA_CREATION_ERROR;
            int    expectedStatusCode = 400;

            CreateCinemaModel createCinemaModel = new CreateCinemaModel
            {
                Name          = "bioskop",
                CityName      = "grad",
                NumberOfSeats = 13,
                SeatRows      = 13
            };

            CityDomainModel cityDomainModel = new CityDomainModel
            {
                Id          = 123,
                Name        = "grad123",
                CinemasList = new List <CinemaDomainModel>()
            };

            CinemaDomainModel        cinemaDomainModel = null;
            Task <CinemaDomainModel> responseTask      = Task.FromResult(cinemaDomainModel);
            Task <CityDomainModel>   responseTask2     = Task.FromResult(cityDomainModel);

            _cinemaService = new Mock <ICinemaService>();
            _cityService   = new Mock <ICityService>();
            _cityService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            _cinemaService.Setup(x => x.CreateCinemaAsync(It.IsAny <CinemaDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask);
            CinemasController cinemasController = new CinemasController(_cinemaService.Object, _cityService.Object);

            //Act
            var result          = cinemasController.PostAsync(createCinemaModel).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);
        }
Beispiel #7
0
        public async Task <ActionResult> Post([FromBody] CreateCinemaModel cinemaModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CinemaDomainModel domainModel = new CinemaDomainModel
            {
                Name = cinemaModel.Name
            };

            CreateCinemaResultModel createCinemaResultModel;

            try
            {
                createCinemaResultModel = await _cinemaService.AddCinema(domainModel);
            }
            catch (DbUpdateException e)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = e.InnerException.Message ?? e.Message,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };
                return(BadRequest(errorResponse));
            }
            if (!createCinemaResultModel.IsSuccessful)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel()
                {
                    ErrorMessage = createCinemaResultModel.ErrorMessage,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            return(Created("auditoriums//" + createCinemaResultModel.Cinema.Id, createCinemaResultModel));
        }