public void CanDoCreatePostback()
        {
            //ACT
            var commodity = new Commodity{Name = "Rice", CommodityCode = null, CommodityTypeID = 1, ParentID = 1, CommodityID = 1};
            var result = _commodityController.Create(commodity);
            Assert.IsNotNull(result);
            var viewResult = result as ViewResult;
            var model = viewResult.Model;
            var jsonResult = result as JsonResult;

            //ASSERT
            Assert.IsNotNull(jsonResult);
            dynamic data = jsonResult.Data;
            Assert.AreEqual(true, data.success);
            Assert.IsInstanceOf<Commodity>(model);
        }
        public void Commodity_Delete_Confirmed_Faliure_Test()
        {
            IUnitOfWork repository = this.MockCommodityRepository;
            CommodityController target = new CommodityController(repository);
            Commodity commodity = new Commodity
            {
                Name = "Gebse",
                LongName = "",
                CommodityTypeID = 1,
                ParentID = null
            };
            int id = 1; //delete the wheat commodity

            int commodityPreCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(5, commodityPreCount);
            JsonResult expected = new JsonResult();
            ActionResult actual;

            //Act
            actual = target.DeleteConfirmed(id);
            ViewResult result = actual as ViewResult;

            //the number of commodities should decrease by 1
            int commodityPostCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(commodityPreCount, commodityPostCount);

            Assert.AreEqual(result.ViewBag.ERROR,true);
            Assert.IsNotNull(result.ViewBag.ERROR_MSG);
        }
        public void Commodity_Create_Post_For_Valid_Model_Test()
        {
            IUnitOfWork repository = this.MockCommodityRepository;
            CommodityController target = new CommodityController(repository);
            Commodity commodity = new Commodity
                                        {
                                            Name = "Gebse",
                                            LongName = "",
                                            CommodityTypeID = 1,
                                            ParentID = 1
                                        };

            int commodityPreCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(5, commodityPreCount);
            JsonResult expected = new JsonResult();
            ActionResult actual;

            //Act
            actual = target.Create(commodity);
            JsonResult result = actual as JsonResult;
            expected.Data = new { success = true };

            //the number of commodities should increase by 1
            int commodityPostCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(commodityPreCount + 1, commodityPostCount);
            Assert.AreEqual(expected.Data.ToString() , result.Data.ToString());
        }
        public void Commodity_Create_Post_For_InValid_Model_Test()
        {
            IUnitOfWork repository = this.MockCommodityRepository;
            CommodityController target = new CommodityController(repository);
            Commodity commodity = new Commodity
            {
                LongName = "",
                CommodityTypeID = 1,
                ParentID = 1
            };

            //SetModelError(target);

            int commodityPreCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(5, commodityPreCount); // Verify the expected Number pre-insert
            PartialViewResult expected = new PartialViewResult();
            ActionResult actual;

            //Act
            actual = target.Create(commodity);

            PartialViewResult result = actual as PartialViewResult;
            Assert.IsNotNull(result);
            //no increase in the number of commodities
            int commodityPostCount = this.MockCommodityRepository.Commodity.GetAll().Count;
            Assert.AreEqual(commodityPreCount, commodityPostCount);
        }
 public void Commodity1Test()
 {
     Commodity target = new Commodity(); // TODO: Initialize to an appropriate value
     EntityCollection<Commodity> expected = null; // TODO: Initialize to an appropriate value
     EntityCollection<Commodity> actual;
     target.Commodity1 = expected;
     actual = target.Commodity1;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
        public void CanDoEditPostBack()
        {
            //ACT
            var commodity = new Commodity
                {CommodityID = 1, Name = "Cereal", CommodityCode = "CER", CommodityTypeID = 1, ParentID = null};
            var result = _commodityController.Edit(commodity);
            var jsonResult = result as JsonResult;

            //ASSERT
            Assert.IsNotNull(jsonResult);
            dynamic data = jsonResult.Data;
            Assert.AreEqual(true, data.success);
        }
 public void NameTest()
 {
     Commodity target = new Commodity(); // TODO: Initialize to an appropriate value
     string expected = string.Empty; // TODO: Initialize to an appropriate value
     string actual;
     target.Name = expected;
     actual = target.Name;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
        public ActionResult Create(Commodity commodity)
        {
            if(!_commodityService.IsCodeValid(commodity.CommodityID,commodity.CommodityCode))
            {
                ModelState.AddModelError("CommodityCode",@"Commodity Code should be unique.");
            }
            if (!_commodityService.IsNameValid(commodity.CommodityID, commodity.Name))
            {
                ModelState.AddModelError("Name", @"Commodity Name should be unique.");
            }

            if (ModelState.IsValid)
            {
                _commodityService.AddCommodity(commodity);
                return Json(new { success = true });
            }

            Create(commodity.ParentID != null ? 0 : 1, commodity.ParentID);
            return PartialView(commodity);
        }
        public void GetCommodityByNameTest()
        {
            string name = "Green Wheat";
            Commodity actual = this.MockCommodityRepository.GetCommodityByName(name); // TODO: Initialize to an appropriate value
            Commodity expected = new Commodity { CommodityID = 6, Name = "Green Wheat", LongName = "", CommodityTypeID = 1, ParentID = 1 };

            Assert.IsInstanceOfType(actual,typeof(Commodity));
            Assert.AreEqual(actual.GetType(), expected.GetType());
            Assert.AreEqual(actual.CommodityID, expected.CommodityID);
            Assert.AreEqual(actual.Name, expected.Name);
            Assert.AreEqual(actual.CommodityTypeID, expected.CommodityTypeID);
            Assert.AreEqual(actual.ParentID, expected.ParentID);
        }
 public void GiftCertificateDetailsTest()
 {
     Commodity target = new Commodity(); // TODO: Initialize to an appropriate value
     EntityCollection<GiftCertificateDetail> expected = null; // TODO: Initialize to an appropriate value
     EntityCollection<GiftCertificateDetail> actual;
     target.GiftCertificateDetails = expected;
     actual = target.GiftCertificateDetails;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
 public void CommodityTypeIDTest()
 {
     Commodity target = new Commodity(); // TODO: Initialize to an appropriate value
     int expected = 0; // TODO: Initialize to an appropriate value
     int actual;
     target.CommodityTypeID = expected;
     actual = target.CommodityTypeID;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
 public void CommodityConstructorTest()
 {
     Commodity target = new Commodity();
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
        public ActionResult Edit(Commodity commodity)
        {
            // TODO: move this to a shared helper function.
            if (!_commodityService.IsCodeValid(commodity.CommodityID, commodity.CommodityCode))
            {
                ModelState.AddModelError("CommodityCode", @"Commodity Code should be unique.");
            }
            if (!_commodityService.IsNameValid(commodity.CommodityID, commodity.Name))
            {
                ModelState.AddModelError("Name", @"Commodity Name should be unique.");
            }

            if (ModelState.IsValid)
            {
                _commodityService.EditCommodity(commodity);
                return Json(new { success = true });
            }
            Edit(commodity.CommodityID);
            return PartialView(commodity);
        }
        public void Commodity_Edit_Post_InValid_Model_Test()
        {
            IUnitOfWork repository = this.MockCommodityRepository;
            CommodityController target = new CommodityController(repository);
            Commodity commodity = new Commodity
            {
                CommodityID = 1,
                Name = "aja",
                LongName = "",
                CommodityTypeID = 1,
                ParentID = 1
            };

            //SetModelError(target);

            PartialViewResult expected = new PartialViewResult();
            ActionResult actual;

            //Act
            actual = target.Edit(commodity);

            PartialViewResult result = actual as PartialViewResult;
            Assert.IsNotNull(result);
        }
 public void ParentIDTest()
 {
     Commodity target = new Commodity(); // TODO: Initialize to an appropriate value
     Nullable<int> expected = new Nullable<int>(); // TODO: Initialize to an appropriate value
     Nullable<int> actual;
     target.ParentID = expected;
     actual = target.ParentID;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
        public void Commodity_Edit_Post_Valid_Model_Test()
        {
            IUnitOfWork repository = this.MockCommodityRepository;
            CommodityController target = new CommodityController(repository);
            Commodity commodity = new Commodity
            {
                  CommodityID = 1,
                  Name = "Wheat",
                  LongName = "",
                  CommodityTypeID = 1,
                  ParentID = null
            };

            //SetModelError(target);

            JsonResult expected = new JsonResult();
            ActionResult actual;

            //Act
            actual = target.Edit(commodity);

            JsonResult result = actual as JsonResult;
            expected.Data = new { success = true };

               Assert.AreEqual(expected.Data.ToString(), result.Data.ToString());
        }
        /**
         * <type> indicates the type of commodity to be (parent/child)</type>
         * <Parent> An Optional Nullable value param for preseting the sub Commoditites to be created</Praent>
         */
        //
        // GET: /Commodity/Create
        public ActionResult Create(int type, int? parent)
        {
            //TODO  validation check @ the post server side if the user mischively sent a non-null parent
            //this check should also be done for the editing part

            if (0 == type)
            {
                ViewBag.ParentID = new SelectList(_commodityService.GetAllParents(), "CommodityID", "Name", parent);

                //drop down boxes don't remove thses cos i used them to set a hidden value

                var firstOrDefault = _commodityService.GetAllParents().FirstOrDefault(p => p.CommodityID == parent);

                if (firstOrDefault != null){
                    ViewBag.CommodityTypeID = new SelectList(_commodityTypeService.GetAllCommodityType(), "CommodityTypeID", "Name",
                                                             firstOrDefault.CommodityTypeID);
                }
                else
                {
                   //TODO null value validation can be set here later
                    ViewBag.CommodityTypeID = new SelectList(_commodityTypeService.GetAllCommodityType(), "CommodityTypeID", "Name");
                }

                //disabled text boxes (elias worte this part i think)
                ViewBag.isParent = true;

                if (parent != null)
                {
                    var commodity = _commodityService.FindById(parent.Value);
                    if (commodity.CommodityType != null){
                        ViewBag.CommodityType = commodity.CommodityType.Name;
                        ViewBag.ParentCommodity = commodity.Name;
                        ViewBag.SelectedCommodityID = commodity.CommodityID;
                    }
                    else
                    {
                        ViewBag.CommodityType = "";
                        ViewBag.ParentCommodity = "";
                    }
                }

            }

            else
            {
                ViewBag.CommodityTypeID = new SelectList(_commodityService.GetAllCommodity(), "CommodityTypeID", "Name");
                ViewBag.isParent = false;
            }

            var partialCommodity = new Commodity();

            var partialParent = _commodityService.GetAllParents().FirstOrDefault(p => p.CommodityID == parent);

            if (partialParent != null)
                {
                    partialCommodity.CommodityTypeID = partialParent.CommodityTypeID;
                }

            //TODO either ways it will be null, but we can make this assignment to null/and move it in side else after testing
            //to be sure for preventing hierarchy(tree)
            partialCommodity.ParentID = parent;

            return PartialView(partialCommodity);
        }