Esempio n. 1
0
        public IActionResult PostCase([FromBody] CaseCategoryCreateModel model)
        {
            Console.Write(model);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CaseCategoryDto caseCategoryDto = new CaseCategoryDto()
            {
                CaseCategoryName = model.CaseCategoryName,
                CustomerId       = model.CustomerId,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            try
            {
                var caseCategory = adminRepository.CreateCaseCategory(caseCategoryDto);
                if (caseCategory != null)
                {
                    return(Ok(caseCategory));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(NoContent());
        }
        public void AddCaseCategoryTest()
        {
            var mockRepo   = new Mock <IAdminManipulation>();
            var controller = new AdminController(mockRepo.Object);

            // Empty Model state
            controller.ModelState.AddModelError("error", "some error");

            var result = controller.PostCase(new CaseCategoryCreateModel());

            Assert.IsType <BadRequestObjectResult>(result);

            //Empty Model
            controller.ModelState.AddModelError("error", "some error");
            result = controller.PostCase(null);
            Assert.IsType <BadRequestObjectResult>(result);

            //Bad Case Model

            NSI.REST.Models.CaseCategoryCreateModel caseCategory = new CaseCategoryCreateModel()
            {
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
            };

            result = controller.PostCase(caseCategory);
            Assert.IsType <BadRequestObjectResult>(result);

            //Bad Validation

            caseCategory.CaseCategoryName = "----------!!!";
            caseCategory.CustomerId       = 1;
            result = controller.PostCase(caseCategory);
            Assert.IsType <BadRequestObjectResult>(result);
        }
        public void AddCaseOk()
        {
            NSI.REST.Models.CaseCategoryCreateModel caseCategory = new CaseCategoryCreateModel()
            {
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var caseCategory1 = new CaseCategoryDto
            {
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var mockRepo = new Mock <IAdminRepository>();

            mockRepo.Setup(x => x.CreateCaseCategory(It.IsAny <CaseCategoryDto>())).Returns(caseCategory1);
            var adminManipulation = new AdminManipulation(mockRepo.Object);
            var controller        = new AdminController(adminManipulation);
            var result            = controller.PostCase(caseCategory);

            Assert.IsType <OkObjectResult>(result);
        }
        public void UpdateCaseCategoryTest()
        {
            NSI.REST.Models.CaseCategoryCreateModel caseCategoryPost = new CaseCategoryCreateModel()
            {
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            NSI.REST.Models.CaseCategoryEditModel caseCategoryPut = new CaseCategoryEditModel()
            {
                CaseCategoryId   = 10,
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var caseCategory = new CaseCategoryDto()
            {
                CaseCategoryId   = 10,
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var mockRepo = new Mock <IAdminManipulation>();

            mockRepo.Setup(x => x.CreateCaseCategory(It.IsAny <CaseCategoryDto>())).Returns(caseCategory);
            var controller = new AdminController(mockRepo.Object);

            controller.PostCase(caseCategoryPost);

            //No content Result

            caseCategory.CaseCategoryName = "NewName";
            var result = controller.PutCase(1, caseCategoryPut);

            Assert.IsType <NoContentResult>(result);

            //Bad Case Model
            controller.ModelState.AddModelError("error", "some error");

            result = controller.PutCase(30, model: null);
            Assert.IsType <BadRequestObjectResult>(result);
        }
        public void DeleteCaseCategoryTest()
        {
            NSI.REST.Models.CaseCategoryCreateModel caseCategory1 = new CaseCategoryCreateModel()
            {
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var caseCategory = new CaseCategoryDto()
            {
                CaseCategoryId   = 10,
                CaseCategoryName = "firstCase",
                IsDeleted        = false,
                CustomerId       = 1,
                DateCreated      = DateTime.Now,
                DateModified     = DateTime.Now
            };

            var mockRepo   = new Mock <IAdminManipulation>();
            var controller = new AdminController(mockRepo.Object);

            controller.ModelState.AddModelError("error", "some error");

            // Invalid Model
            var result = controller.DeleteCase(10000);

            Assert.IsType <NoContentResult>(result);

            //OK
            mockRepo.Setup(x => x.CreateCaseCategory(It.IsAny <CaseCategoryDto>())).Returns(caseCategory);
            mockRepo.Setup(x => x.DeleteCaseCategoryById(It.IsAny <int>())).Returns(true);
            controller.PostCase(caseCategory1);

            result = controller.DeleteCase(10);
            Assert.IsType <OkObjectResult>(result);
        }