Exemple #1
0
        public async Task <IActionResult> GetProductsWithPaging(GetProductsWithPagingRequestModel request)
        {
            IActionResult response = null;

            try
            {
                if (ModelState.IsValid)
                {
                    var product = await this._productMaintenanceApi
                                  .GetProductsWithPagingAsync(request.FirstRecordNumber,
                                                              request.PageSize,
                                                              request.SortField,
                                                              request.SortOrder);

                    response = this.Ok(product);
                }
                else
                {
                    response = BadRequest(ModelState);
                }
            }
            catch (Exception)
            {
                var msg = this._localizer?["ProductPageErrorMsg"]?.Value;
                return(this.StatusCode((int)System.Net.HttpStatusCode.InternalServerError, msg));
            }

            return(response);
        }
        public async Task CompareRecordsTest()
        {
            // Arrange
            IProductRepository mockRepo = new MockProductRepository();

            var mockDownloadTimesApi = Substitute.For <IDownloadTimesApi>();
            var mockProductTypesApi  = Substitute.For <IProductTypesApi>();
            var mockManufacturersApi = Substitute.For <IManufacturersApi>();
            var mockInstallTypesApi  = Substitute.For <IInstallTypesApi>();
            var mockMobileLookupApi  = Substitute.For <IMobileLookupApi>();
            var mockBrandApi         = Substitute.For <IBrandApi>();

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            IStringLocalizer <ProductsController> mockLocalizer = new MockStringLocalizer <ProductsController>();

            IProductApi productApi = new ProductApi(appSettings, mockRepo);
            var         controller = new ProductsController(mockCache,
                                                            productApi,
                                                            mockDownloadTimesApi,
                                                            mockProductTypesApi,
                                                            mockManufacturersApi,
                                                            mockInstallTypesApi,
                                                            mockMobileLookupApi,
                                                            mockBrandApi,
                                                            mockLocalizer);

            var request = new GetProductsWithPagingRequestModel
            {
                FirstRecordNumber = 0,
                PageSize          = 10,
                SortField         = string.Empty,
                SortOrder         = SortOrderEnum.Asc
            };

            var testProducts = await mockRepo.GetAllProductsAsync();

            var firstProduct = new List <ProductModel>(testProducts)[0];

            //// Act
            var actionResult = await controller.GetProductsWithPaging(request);

            //// Assert
            var actualRecord = ((ObjectResult)actionResult).Value;

            Assert.Equal(((List <ProductModel>)actualRecord).Count, 10);
            var testProduct = ((List <ProductModel>)actualRecord)[0];

            Assert.Equal(JsonConvert.SerializeObject(firstProduct), JsonConvert.SerializeObject(testProduct));
        }
        public async Task BadRequestTest()
        {
            // Arrange
            IProductRepository mockRepo = new MockProductRepository();

            var mockDownloadTimesApi = Substitute.For <IDownloadTimesApi>();
            var mockProductTypesApi  = Substitute.For <IProductTypesApi>();
            var mockManufacturersApi = Substitute.For <IManufacturersApi>();
            var mockInstallTypesApi  = Substitute.For <IInstallTypesApi>();
            var mockMobileLookupApi  = Substitute.For <IMobileLookupApi>();
            var mockBrandApi         = Substitute.For <IBrandApi>();

            IDistributedCache   mockCache   = Substitute.For <IDistributedCache>();
            IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >();

            IStringLocalizer <ProductsController> mockLocalizer = new MockStringLocalizer <ProductsController>();

            IProductApi productApi = new ProductApi(appSettings, mockRepo);
            var         controller = new ProductsController(mockCache,
                                                            productApi,
                                                            mockDownloadTimesApi,
                                                            mockProductTypesApi,
                                                            mockManufacturersApi,
                                                            mockInstallTypesApi,
                                                            mockMobileLookupApi,
                                                            mockBrandApi,
                                                            mockLocalizer);

            var request = new GetProductsWithPagingRequestModel
            {
                FirstRecordNumber = -1,
                PageSize          = 0,
                SortField         = string.Empty,
                SortOrder         = SortOrderEnum.Asc
            };

            controller.ModelState.AddModelError("", "Error");


            //// Act
            var actionResult = await controller.GetProductsWithPaging(request);

            var badResponse = actionResult as BadRequestObjectResult;

            //// Assert
            Assert.NotNull(badResponse);
            Assert.Equal(badResponse.StatusCode, (int)System.Net.HttpStatusCode.BadRequest);
        }
        public void FirstRecordNumberValidationFailure()
        {
            var model = new GetProductsWithPagingRequestModel
            {
                FirstRecordNumber = -1,
                PageSize          = 101,
                SortField         = null,
                SortOrder         = SortOrderEnum.Asc
            };
            var context = new ValidationContext(model, null, null);
            var result  = new List <ValidationResult>();

            // Act
            var valid = Validator.TryValidateObject(model, context, result, true);

            Assert.False(valid);
        }