Ejemplo n.º 1
0
        public void Controller_AvatarSelectShop_Select_Data_Invalid_StudentID_Null_Should_Fail()
        {
            // Arrange
            var controller = new AvatarSelectController();

            var data = new ShopBuyViewModel
            {
                StudentId = null
            };

            // Act
            var result = (RedirectToRouteResult)controller.Select(data);

            // Reset
            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual("Select", result.RouteValues["action"], TestContext.TestName);
        }
Ejemplo n.º 2
0
        public void Controller_Shop_Factory_Data_Invalid_StudentId_Bogus_Should_Fail()
        {
            // Arrange
            ShopController controller = new ShopController();

            var data = new ShopBuyViewModel
            {
                StudentId = "bogus",
                ItemId    = "itemID"
            };

            // Act
            var result = (RedirectToRouteResult)controller.Factory(data);

            // Reset
            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual("Factory", result.RouteValues["action"], TestContext.TestName);
        }
Ejemplo n.º 3
0
        public void Controller_AvatarSelectShop_Select_Data_Valid_Limited_Should_Pass()
        {
            // Arrange
            var controller = new AvatarSelectController();

            var data = new ShopBuyViewModel
            {
                StudentId = DataSourceBackend.Instance.StudentBackend.GetDefault().Id,
                ItemId    = DataSourceBackend.Instance.AvatarItemBackend.GetFirstAvatarItemId()
            };

            // Get the Student Record and Add some Tokens to it.
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            myStudent.Tokens = 1000;

            // Clear the Student AvatarInventory, so one can be purchanged.
            myStudent.AvatarInventory.Clear();
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            // Get the Item Record and Set the Token Value
            var myInventory = DataSourceBackend.Instance.AvatarItemBackend.Read(data.ItemId);

            myInventory.Tokens        = 10;
            myInventory.IsLimitSupply = true;

            DataSourceBackend.Instance.AvatarItemBackend.Update(myInventory);

            var expect = myStudent.Tokens - myInventory.Tokens;

            // Act
            ViewResult result = controller.Select(data) as ViewResult;

            var myStudent2 = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            // Reset
            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual(expect, myStudent2.Tokens, TestContext.TestName);
        }
Ejemplo n.º 4
0
        public void Controller_AvatarSelectShop_Select_Data_InValid_Tokens_Less_Than_One_Should_Fail()
        {
            // Arrange
            var controller = new AvatarSelectController();

            var data = new ShopBuyViewModel
            {
                StudentId = DataSourceBackend.Instance.StudentBackend.GetDefault().Id,
                ItemId    = DataSourceBackend.Instance.AvatarItemBackend.GetFirstAvatarItemId()
            };

            // Get the Student Record and Add some Tokens to it.
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            myStudent.Tokens = 0;
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            // Get the Item Record and Set the Token Value
            var myInventory = DataSourceBackend.Instance.AvatarItemBackend.Read(data.ItemId);

            myInventory.Tokens = 0;
            DataSourceBackend.Instance.AvatarItemBackend.Update(myInventory);

            // No Purchase, so tokens stay the same
            var expect      = myStudent.Tokens;
            var expectCount = myStudent.AvatarInventory.Count();

            // Act
            var result = (RedirectToRouteResult)controller.Select(data);

            var myStudent2 = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            // Reset
            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual("Select", result.RouteValues["action"], TestContext.TestName);
            Assert.AreEqual(expect, myStudent2.Tokens, TestContext.TestName);
            Assert.AreEqual(expectCount, myStudent2.AvatarInventory.Count(), TestContext.TestName);
        }
Ejemplo n.º 5
0
        public void Controller_Shop_Factory_Data_InValid_Tokens_Not_Enough_Should_Fail()
        {
            // Arrange
            ShopController controller = new ShopController();

            var data = new ShopBuyViewModel
            {
                StudentId = DataSourceBackend.Instance.StudentBackend.GetDefault().Id,
                ItemId    = DataSourceBackend.Instance.FactoryInventoryBackend.GetFirstFactoryInventoryId()
            };

            // Get the Student Record and Add some Tokens to it.
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            myStudent.Tokens = 10;
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            // Get the Item Record and Set the Token Value
            var myInventory = DataSourceBackend.Instance.FactoryInventoryBackend.Read(data.ItemId);

            myInventory.Tokens = 100;
            DataSourceBackend.Instance.FactoryInventoryBackend.Update(myInventory);

            // No Purchase, so tokens stay the same
            var expect      = myStudent.Tokens;
            var expectCount = myStudent.Inventory.Count();

            // Act
            var result = (RedirectToRouteResult)controller.Factory(data);

            var myStudent2 = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual("Factory", result.RouteValues["action"], TestContext.TestName);
            Assert.AreEqual(expect, myStudent2.Tokens, TestContext.TestName);
            Assert.AreEqual(expectCount, myStudent2.Inventory.Count(), TestContext.TestName);
        }
Ejemplo n.º 6
0
        public void Controller_Shop_Factory_Data_Valid_Item_Quantity_Is_Not_Limited_Should_Pass()
        {
            // Arrange
            ShopController controller = new ShopController();

            var data = new ShopBuyViewModel
            {
                StudentId = DataSourceBackend.Instance.StudentBackend.GetDefault().Id,
                ItemId    = DataSourceBackend.Instance.FactoryInventoryBackend.GetFirstFactoryInventoryId()
            };

            // Get the Student Record and Add some Tokens to it.
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            myStudent.Tokens = 1000;
            myStudent.Inventory.Clear();    //Clear inventory
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            // Get the Item Record and Set the Token Value
            var myInventory = DataSourceBackend.Instance.FactoryInventoryBackend.Read(data.ItemId);

            myInventory.Tokens = 10;
            DataSourceBackend.Instance.FactoryInventoryBackend.Update(myInventory);

            var expect        = myStudent.Tokens - myInventory.Tokens;
            var expectOutcome = myStudent.Truck.Outcome + myInventory.Tokens;

            // Act
            ViewResult result = controller.Factory(data) as ViewResult;

            var myStudent2 = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            DataSourceBackend.Instance.Reset();

            // Assert
            Assert.AreEqual(expect, myStudent2.Tokens, TestContext.TestName);
            Assert.AreEqual(expectOutcome, myStudent2.Truck.Outcome, TestContext.TestName);
            Assert.IsNotNull(myStudent2.Truck.BusinessList, TestContext.TestName);
        }
        public ActionResult Select([Bind(Include =
                                             "StudentId," +
                                             "ItemId," +
                                             "")] ShopBuyViewModel data)
        {
            if (!ModelState.IsValid)
            {
                // Send back for edit, with Error Message
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            if (data == null)
            {
                // Send to Error Page
                return(RedirectToAction("Error", "Home"));
            }

            if (string.IsNullOrEmpty(data.StudentId))
            {
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Temp hold the Student Id for the Nav, until the Nav can call for Identity.
            ViewBag.StudentId = data.StudentId;

            if (string.IsNullOrEmpty(data.ItemId))
            {
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Get Student
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            if (myStudent == null)
            {
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Get Item
            var myItem = DataSourceBackend.Instance.AvatarItemBackend.Read(data.ItemId);

            if (myItem == null)
            {
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Check the Student Token amount, If not enough, return error
            if (myStudent.Tokens < myItem.Tokens)
            {
                // Not enough money...
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // check the quantities of item
            if (myItem.Quantities < 1)
            {
                // Not enough quantity...
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Check the Item amount, If not enough, return error
            if (myItem.Tokens < 1)
            {
                // Not enough money...
                // Send back for Edit
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Check to see if the student already has the item.  If so, don't buy again, only 1 item per student
            var ItemAlreadyExists = myStudent.AvatarInventory.Exists(m => m.Id == myItem.Id);

            if (ItemAlreadyExists)
            {
                // Already own it.
                return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
            }

            // Reduce the Student Tokens by Item Price
            myStudent.Tokens -= myItem.Tokens;

            // Time to buy !
            // Check if the item is limited or not
            if (myItem.IsLimitSupply == true)
            {
                // Reduce the quantities of Item
                myItem.Quantities -= 1;
            }

            // Add Item to Student Inventory
            // TODO:  Mike, add inventory to Students...
            myStudent.AvatarInventory.Add(myItem);

            // Update Student
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            return(RedirectToAction("Select", "AvatarSelect", new { id = data.StudentId }));
        }
Ejemplo n.º 8
0
        public ActionResult Buy([Bind(Include =
                                          "StudentId," +
                                          "ItemId," +
                                          "")] ShopBuyViewModel data)
        {
            if (!ModelState.IsValid)
            {
                // Send back for edit, with Error Message
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            if (data == null)
            {
                // Send to Error Page
                return(RedirectToAction("Error", new { route = "Home", action = "Error" }));
            }

            if (string.IsNullOrEmpty(data.StudentId))
            {
                // Send back for Edit
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            if (string.IsNullOrEmpty(data.ItemId))
            {
                // Send back for Edit
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            // Get Student
            var myStudent = DataSourceBackend.Instance.StudentBackend.Read(data.StudentId);

            if (myStudent == null)
            {
                // Send back for Edit
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            // Get Item
            var myItem = DataSourceBackend.Instance.ShopInventoryBackend.Read(data.ItemId);

            if (myItem == null)
            {
                // Send back for Edit
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            // Check the Student Token amount, If not enough, return error
            if (myStudent.Tokens < myItem.Tokens)
            {
                // Not enough money...
                // Send back for Edit
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            // Check to see if the student already has the item.  If so, don't buy again, only 1 item per student
            var ItemAlreadyExists = myStudent.Inventory.Exists(m => m.Id == myItem.Id);

            if (ItemAlreadyExists)
            {
                // Already own it.
                return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
            }

            // Time to buy !

            // Reduce the Student Tokens by Item Price
            myStudent.Tokens -= myItem.Tokens;

            // Add Item to Student Inventory
            // TODO:  Mike, add inventory to Students...
            myStudent.Inventory.Add(myItem);

            // Update Student
            DataSourceBackend.Instance.StudentBackend.Update(myStudent);

            return(RedirectToAction("Buy", "Shop", new { id = data.StudentId }));
        }