public IActionResult GetCustomers(CustomersParametersModel parameters)
        {
            if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter"));
            }

            if (parameters.Page < Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters"));
            }

            try
            {
                var allCustomers = _customerApiService.GetCustomersDtos(parameters.CreatedAtMin,
                                                                        parameters.CreatedAtMax,
                                                                        parameters.Limit,
                                                                        parameters.Page,
                                                                        parameters.SinceId);

                var customersRootObject = new CustomersRootObject()
                {
                    Customers = allCustomers
                };

                var json = JsonFieldsSerializer.Serialize(customersRootObject, parameters.Fields);

                return(new RawJsonActionResult(json));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemple #2
0
        public void WhenFieldsParametersPassed_ShouldCallTheSerializerWithTheSameFields()
        {
            var parameters = new CustomersParametersModel()
            {
                Fields = "id,email"
            };

            //Arange
            var autoMocker = new RhinoAutoMocker <CustomersController>();

            //Act
            autoMocker.ClassUnderTest.GetCustomers(parameters);

            //Assert
            autoMocker.Get <IJsonFieldsSerializer>().AssertWasCalled(
                x => x.Serialize(Arg <CustomersRootObject> .Is.Anything, Arg <string> .Is.Equal(parameters.Fields)));
        }
Exemple #3
0
        public void WhenNoCustomersExist_ShouldCallTheSerializerWithNoCustomers()
        {
            var returnedCustomersDtoCollection = new List <CustomerDto>();

            var parameters = new CustomersParametersModel();

            //Arange
            var autoMocker = new RhinoAutoMocker <CustomersController>();

            autoMocker.Get <ICustomerApiService>().Stub(x => x.GetCustomersDtos()).Return(returnedCustomersDtoCollection);

            //Act
            autoMocker.ClassUnderTest.GetCustomers(parameters);

            //Assert
            autoMocker.Get <IJsonFieldsSerializer>().AssertWasCalled(
                x => x.Serialize(Arg <CustomersRootObject> .Matches(r => r.Customers.Count == returnedCustomersDtoCollection.Count),
                                 Arg <string> .Is.Equal(parameters.Fields)));
        }
Exemple #4
0
        public void WhenInvalidPageParameterPassed_ShouldReturnBadRequest(int invalidPage)
        {
            var parameters = new CustomersParametersModel()
            {
                Page = invalidPage
            };

            //Arange
            var autoMocker = new RhinoAutoMocker <CustomersController>();

            autoMocker.Get <IJsonFieldsSerializer>().Stub(x => x.Serialize(Arg <CustomersRootObject> .Is.Anything, Arg <string> .Is.Anything))
            .IgnoreArguments()
            .Return(string.Empty);

            //Act
            IHttpActionResult result = autoMocker.ClassUnderTest.GetCustomers(parameters);

            //Assert
            var statusCode = result.ExecuteAsync(new CancellationToken()).Result.StatusCode;

            Assert.AreEqual(HttpStatusCode.BadRequest, statusCode);
        }
Exemple #5
0
        public async Task <IActionResult> GetCustomers(CustomersParametersModel parameters)
        {
            if (parameters.Limit < Constants.Configurations.MinLimit || parameters.Limit > Constants.Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter"));
            }

            if (parameters.Page < Constants.Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters"));
            }

            var allCustomers = await _customerApiService.GetCustomersDtosAsync(parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Limit, parameters.Page, parameters.SinceId);

            var customersRootObject = new CustomersRootObject
            {
                Customers = allCustomers
            };

            var json = JsonFieldsSerializer.Serialize(customersRootObject, parameters.Fields);

            return(new RawJsonActionResult(json));
        }
Exemple #6
0
        public void WhenSomeValidParametersPassed_ShouldCallTheServiceWithTheSameParameters()
        {
            var parameters = new CustomersParametersModel()
            {
                SinceId      = Configurations.DefaultSinceId + 1, // some different than default since id
                CreatedAtMin = DateTime.Now,
                CreatedAtMax = DateTime.Now,
                Page         = Configurations.DefaultPageValue + 1, // some different than default page
                Limit        = Configurations.MinLimit + 1          // some different than default limit
            };

            //Arange
            var autoMocker = new RhinoAutoMocker <CustomersController>();

            //Act
            autoMocker.ClassUnderTest.GetCustomers(parameters);

            //Assert
            autoMocker.Get <ICustomerApiService>().AssertWasCalled(x => x.GetCustomersDtos(parameters.CreatedAtMin,
                                                                                           parameters.CreatedAtMax,
                                                                                           parameters.Limit,
                                                                                           parameters.Page,
                                                                                           parameters.SinceId));
        }