public void Create_WhenPostItem_ThenReturnedTheSameItemAndDeletingIt()
        {
            // Arrange
            var item = Initializer.AeroplaneTypeFaker.Generate();

            var testDto = new AeroplaneTypeDto
            {
                Id       = item.Id,
                Carrying = item.Carrying,
                Model    = item.Model,
                Places   = item.Places
            };

            HttpResponseMessage creatingResponse;
            var jsonRequest = JsonConvert.SerializeObject(testDto);
            var content     = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            // Act
            using (var client = new HttpClient())
            {
                creatingResponse = client.PostAsync($"{serverName}/api/AeroplaneTypes/", content).Result;
            }

            string jsonResponse = creatingResponse.Content.ReadAsStringAsync().Result;
            var    resultItem   = JsonConvert.DeserializeObject <AeroplaneTypeDto>(jsonResponse);

            // Assert
            Assert.IsNotNull(resultItem);
            Assert.AreEqual(creatingResponse.StatusCode, HttpStatusCode.OK);
        }
        public AeroplaneTypeDto Update(Guid id, AeroplaneTypeDto aeroplaneTypeDto)
        {
            var aeroplaneType = mapper.Map <AeroplaneTypeDto, AeroplaneType>(aeroplaneTypeDto);

            aeroplaneType.Id = id;

            db.AeroplaneTypeRepository.Update(aeroplaneType);

            return(mapper.Map <AeroplaneType, AeroplaneTypeDto>(aeroplaneType));
        }
        public AeroplaneTypeDto Create(AeroplaneTypeDto aeroplaneTypeDto)
        {
            var aeroplaneType = mapper.Map <AeroplaneTypeDto, AeroplaneType>(aeroplaneTypeDto);

            aeroplaneType.Id = Guid.NewGuid();

            db.AeroplaneTypeRepository.Create(aeroplaneType);

            return(mapper.Map <AeroplaneType, AeroplaneTypeDto>(aeroplaneType));
        }
        public void Create_WhenDtoIsEmpty_ThenThrowValidExeption()
        {
            // Arrange
            var dto = new AeroplaneTypeDto()
            {
            };

            var service = new AeroplaneTypeService(unitOfWorkFake, mapper, validator);

            // Act

            // Assert
            Assert.Throws <ValidationException>(() => service.Create(dto));
        }
Example #5
0
        public AeroplaneTypeDto Update(Guid id, AeroplaneTypeDto aeroplaneTypeDto)
        {
            var aeroplaneType = mapper.Map <AeroplaneTypeDto, AeroplaneType>(aeroplaneTypeDto);

            aeroplaneType.Id = id;

            var validationResult = validator.Validate(aeroplaneType);

            if (validationResult.IsValid)
            {
                db.AeroplaneTypeRepository.Update(aeroplaneType);
                db.SaveChanges();
            }
            else
            {
                throw new ValidationException(validationResult.Errors);
            }

            return(mapper.Map <AeroplaneType, AeroplaneTypeDto>(aeroplaneType));
        }
        public IActionResult Put(Guid id, [FromBody] AeroplaneTypeDto aeroplaneTypeDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Type = "ValidationError", ErrorMessage = "Required fields is empty" }));
            }

            AeroplaneTypeDto resultDto;

            try
            {
                resultDto = aeroplaneTypeService.Update(id, aeroplaneTypeDto);
            }
            catch (Exception ex)
            {
                return(BadRequest(new { ErrorType = ex.GetType().Name, ex.Message }));
            }

            return(Ok(resultDto));
        }
        public void Create_WhenDtoIsPassed_ThenReturnedTheSameWithCreatedId()
        {
            // Arrange
            var dto = new AeroplaneTypeDto()
            {
                Model    = "Boeing-747",
                Carrying = 100000,
                Places   = 200
            };

            var service = new AeroplaneTypeService(unitOfWorkFake, mapper, alwaysValidValidator);

            // Act
            var returnedDto = service.Create(dto);

            // Assert
            Assert.True(returnedDto.Id != default(Guid));
            Assert.AreEqual(dto.Model, returnedDto.Model);
            Assert.AreEqual(dto.Carrying, returnedDto.Carrying);
            Assert.AreEqual(dto.Places, returnedDto.Places);
        }
        public void Update_WhenUpdateItemWithUknownId_ThenReturnedBadRequest()
        {
            // Arrange
            var updateDto = new AeroplaneTypeDto
            {
                Carrying = 100000,
                Model    = "NewModelName",
                Places   = 50
            };

            // Act
            HttpResponseMessage updatingResponse;
            var jsonUpdatingRequest = JsonConvert.SerializeObject(updateDto);
            var putContext          = new StringContent(jsonUpdatingRequest, Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                updatingResponse = client.PutAsync($"{serverName}/api/AeroplaneTypes/{Guid.NewGuid()}", putContext).Result;
            }

            // Assert
            Assert.AreEqual(updatingResponse.StatusCode, HttpStatusCode.BadRequest);
        }
        public void Setup()
        {
            serverName = "http://localhost:57338";

            var builder = new DbContextOptionsBuilder <AirportContext>()
                          .UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=AirportDb(Bilyk);Trusted_Connection=True;");

            addedItem = Initializer.AeroplaneTypeFaker.Generate();

            testDto = new AeroplaneTypeDto
            {
                Id       = addedItem.Id,
                Carrying = addedItem.Carrying,
                Model    = addedItem.Model,
                Places   = addedItem.Places
            };

            db = new AirportContext(builder.Options);

            // Adding test item to db
            db.AeroplaneTypes.Add(addedItem);
            db.SaveChanges();
        }
Example #10
0
        public void PUT_WhenPuttNewItem_ThenServiceReturnOkAndThisObject()
        {
            //Arrange
            var AeroplaneType = new AeroplaneTypeDto
            {
                Id       = Guid.NewGuid(),
                Model    = "Model",
                Carrying = 100000,
                Places   = 50
            };

            var fakeService = A.Fake <IAeroplaneTypeService>();

            A.CallTo(() => fakeService.Update(AeroplaneType.Id, AeroplaneType)).Returns(AeroplaneType);

            var controller = new AeroplaneTypesController(fakeService);

            //Act
            var response = controller.Post(AeroplaneType) as ObjectResult;

            //Assert
            Assert.AreEqual((int)HttpStatusCode.OK, response.StatusCode);
            Assert.IsInstanceOf(typeof(AeroplaneTypeDto), response.Value);
        }