Exemplo n.º 1
0
        public async Task Given_ValidCarRequest_UpdatesCar()
        {
            // Arrange.
            var carBeforeUpdate = _fixture.Create <Car>();
            var carAfterUpdate  = _fixture.Create <Car>();

            var request = _fixture.Create <UpdateCarRequest>();

            request.Id = carBeforeUpdate.Id;

            _carRepositoryCache.GetAllCars().Returns(new List <Car>()
            {
                carBeforeUpdate
            });
            await _carRepository.Update(Arg.Any <Car>());

            _requestMapper.Map(Arg.Any <UpdateCarRequest>()).Returns(carAfterUpdate);

            // Act.
            var response = await _sut.Update(request);

            // Assert.
            response.Should().Be(carAfterUpdate);
            _carRepositoryCache.Received(1).InvalidateCache();
            _carRepository.Received(1).Update(Arg.Is <Car>(carAfterUpdate));
        }
Exemplo n.º 2
0
        public async Task Given_InvalidCarsToPurchase_Should_ThrowException()
        {
            var cars = _fixture.CreateMany <Repository.Cars.Car>(3).ToList();

            cars[0].Id = 1;
            cars[1].Id = 2;
            cars[2].Id = 3;

            _carRepositoryCache.GetAllCars().Returns(cars);

            // Act.
            Assert.ThrowsAsync <HttpResponseException>(async() => await _sut.CalculateDiscount(new List <int> {
                1, 2, 3, 4
            }));
        }
Exemplo n.º 3
0
        public async Task Given_ValidCarRequest_Returns_Car()
        {
            // Arrange.
            var cars = _fixture.CreateMany <Car>(5).ToList();

            _carRepository.GetAllCars().Returns(cars);

            // Act.
            var response = await _sut.GetAllCars();

            // Assert.
            response.Count().Should().Be(cars.Count);
            cars.ForEach(c => response.Should().Contain(c));
        }
Exemplo n.º 4
0
        public async Task <Car> Update(IUpdateCarRequest updateCarRequest)
        {
            // TODO: We need Get by id in repository.
            var allCars = await _carRepositoryCache.GetAllCars();

            var carToUpdate = allCars.Where(c => c.Id == updateCarRequest.Id).FirstOrDefault();

            if (carToUpdate == null)
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
            }

            var mappedRequest = _updateCarRequestMapper.Map(updateCarRequest);
            await _carRepository.Update(mappedRequest);

            _carRepositoryCache.InvalidateCache();

            return(mappedRequest);
        }
Exemplo n.º 5
0
        public async Task <IDiscountCalculatorResponse> CalculateDiscount(IReadOnlyList <int> carIds)
        {
            var allCars = await _carRepositoryCache.GetAllCars();

            var resolvedCars = allCars.Where(c => carIds.Contains(c.Id)).ToList();

            if (resolvedCars.Count != carIds.Count())
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
            }

            var totalDiscount = _discountCalculators
                                .Sum(c => c.CalculateDiscount(resolvedCars));

            return(new DiscountCalculatorResponse
            {
                Discount = totalDiscount,
                Cars = resolvedCars
            });
        }
Exemplo n.º 6
0
 public async Task <IReadOnlyList <Repository.Cars.Car> > GetAllCars()
 {
     return(await _carRepositoryCache.GetAllCars());
 }