diff --git a/Refactoring/Store.cs b/Refactoring/Store.cs
index d9a7c78..c826e47 100644
--- a/Refactoring/Store.cs
+++ b/Refactoring/Store.cs
@@ -18,22 +18,22 @@ public Store(User user, DataManager dataManager)
this.dataManager = dataManager;
}
- public void Purchase(string productId, int quantity)
+ public void Purchase(string productId, int quantityPurchased)
{
Product product = this.GetProductById(productId);
- if (!UserHasFundsForPurchase(product, quantity))
+ if (!UserHasFundsForPurchase(product, quantityPurchased))
{
throw new InsufficientFundsException();
}
- if (!StoreHasStockForPurchase(product, quantity))
+ if (!StoreHasStockForPurchase(product, quantityPurchased))
{
throw new OutOfStockException();
}
- product.Quantity = product.Quantity - quantity+1;
- user.Balance = user.Balance - product.Price * quantity;
+ product.Quantity -= quantityPurchased;
+ user.Balance = user.Balance - product.Price * quantityPurchased;
dataManager.SaveUser(user);
dataManager.SaveProduct(product);
diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs
new file mode 100644
index 0000000..706c98e
--- /dev/null
+++ b/UnitTestProject/AuthenticatorTests.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using Newtonsoft.Json;
+using NUnit.Framework;
+using Refactoring;
+using System.IO;
+
+namespace UnitTestProject
+{
+ ///
+ /// Summary description for UnitTest1
+ ///
+ [TestFixture]
+ public class AuthenticatorTests
+ {
+ const string TEST_USER_NAME = "Bob";
+ const string TEST_USER_PASSWORD = "pass123";
+ const string TEST_USER_INCORRECT_PASSWORD = "totallynotthepassword";
+ const double ALL_TEST_USERS_BALANCE = 1.00;
+
+ private User createTestUser(string name, string password)
+ {
+ User testUser = new User();
+ testUser.Name = name;
+ testUser.Password = password;
+ testUser.Balance = ALL_TEST_USERS_BALANCE;
+
+ return testUser;
+ }
+
+ private List CreateTestUserList(params string[] userNamesAndPasswords)
+ {
+ var userNameAndPasswordListCountIsOdd = (userNamesAndPasswords.Length % 2 == 1);
+
+ if (userNameAndPasswordListCountIsOdd)
+ {
+ throw new Exception("Test user list needs matching set of usernames and passwords.");
+ }
+
+ var userList = new List();
+
+ for (int i = 0; i < userNamesAndPasswords.Length; i = i + 2)
+ {
+ var userName = userNamesAndPasswords[i];
+ var password = userNamesAndPasswords[i + 1];
+
+ userList.Add(createTestUser(userName, password));
+ }
+
+ return userList;
+ }
+
+ private Authenticator CreateTestAuthenticator()
+ {
+ var userList = CreateTestUserList("Sarah", "12345", TEST_USER_NAME, TEST_USER_PASSWORD, "John", "supersecretpassword", "Dog", "dogpassword2");
+
+ return new Authenticator(userList);
+ }
+
+ [Test]
+ public void Test_AuthenticationWorksWithCorrectPassword()
+ {
+ var authenticator = CreateTestAuthenticator();
+
+ var user = authenticator.Authenticate(TEST_USER_NAME, TEST_USER_PASSWORD);
+
+ Assert.IsNotNull(user);
+ Assert.AreEqual(TEST_USER_NAME, user.Name);
+ Assert.AreEqual(TEST_USER_PASSWORD, user.Password);
+ }
+
+ [Test]
+ public void Test_AuthenticationFailsWithIncorrectPassword()
+ {
+ var authenticator = CreateTestAuthenticator();
+
+ var user = authenticator.Authenticate(TEST_USER_NAME, TEST_USER_INCORRECT_PASSWORD);
+
+ Assert.IsNull(user);
+ }
+
+ [Test]
+ public void Test_AuthenticationFailsWithBlankUserName()
+ {
+ var authenticator = CreateTestAuthenticator();
+
+ var user = authenticator.Authenticate("", TEST_USER_PASSWORD);
+
+ Assert.IsNull(user);
+ }
+ }
+}
diff --git a/UnitTestProject/IntegrationTests.cs b/UnitTestProject/IntegrationTests.cs
index f526b8a..ddb4fb9 100644
--- a/UnitTestProject/IntegrationTests.cs
+++ b/UnitTestProject/IntegrationTests.cs
@@ -10,7 +10,7 @@
namespace UnitTestProject
{
[TestFixture]
- //[Ignore("Disable integration tests")]
+ [Ignore("Disable integration tests")]
public class IntegrationTests
{
private List users;
diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs
index 9f0b866..898237d 100644
--- a/UnitTestProject/StoreTests.cs
+++ b/UnitTestProject/StoreTests.cs
@@ -13,6 +13,8 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
+ const string TEST_PRODUCT_ID = "1";
+
private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
@@ -34,20 +36,23 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}
- [Test]
- public void Test_PurchaseThrowsNoErrorForValidFunds()
+ private Store CreateTestStore(double userBalance, double productPrice, int productQuantity)
{
- //Arrange
- const string TEST_PRODUCT_ID = "1";
-
var users = new List();
- users.Add(createTestUser("Test User", "", 99.99));
+ users.Add(createTestUser("Test User", "", userBalance));
var products = new List();
- products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));
+ products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", productPrice, productQuantity));
var dataManager = new DataManager(users, products);
- var store = new Store(users[0], dataManager);
+
+ return new Store(users[0], dataManager);
+ }
+
+ [Test]
+ public void Test_PurchaseThrowsNoErrorForValidFunds()
+ {
+ var store = CreateTestStore(99.99, 9.99, 10);
//Act
store.Purchase(TEST_PRODUCT_ID, 10);
@@ -60,36 +65,44 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
+ var store = CreateTestStore(99.99, 9.99, 10);
//Act
+ store.Purchase(TEST_PRODUCT_ID, 9);
//Assert
- //(choose the appropriate statement(s))
- //Assert.AreEqual(1, products[0].Quantity);
- //Assert.AreSame(1, products[0].Quantity);
- //Assert.IsTrue(products[0].Quantity == 1);
+ Assert.AreEqual(1, store.GetProductById(TEST_PRODUCT_ID).Quantity);
}
[Test]
- public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
+ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow_OneItem()
{
//Arrange
+ var store = CreateTestStore(1.00, 1.01, 10);
- //Act
-
- //Assert
+ //Act & Assert
+ Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1));
}
[Test]
- public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
+ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow_MultipleItems()
{
//Arrange
+ var store = CreateTestStore(5.00, 1.00, 10);
- //Act
-
- //Assert
+ //Act & Assert
+ Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 6));
}
+ [Test]
+ public void Test_PurchaseThrowsExceptionWhenStoreDoesntHaveEnoughStock()
+ {
+ //Arrange
+ var store = CreateTestStore(99.99, 1.00, 10);
+
+ //Act & Assert
+ Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 11));
+ }
// THE BELOW CODE IS REQUIRED TO PREVENT THE TESTS FROM MODIFYING THE USERS/PRODUCTS ON FILE
// This is not a good unit testing pattern - the unit test dependency on the file system should
@@ -107,7 +120,6 @@ public void Test_Initialize()
originalProducts = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json"));
}
-
[TearDown]
public void Test_Cleanup()
{
diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj
index bcf3847..9ec41da 100644
--- a/UnitTestProject/UnitTestProject.csproj
+++ b/UnitTestProject/UnitTestProject.csproj
@@ -73,6 +73,7 @@
+