diff --git a/src/main/java/seedu/address/model/client/Client.java b/src/main/java/seedu/address/model/client/Client.java index 9184a815ac7..e1824cbbced 100644 --- a/src/main/java/seedu/address/model/client/Client.java +++ b/src/main/java/seedu/address/model/client/Client.java @@ -108,11 +108,11 @@ public boolean equals(Object other) { } Client otherClient = (Client) other; - return id.equals(otherClient.id) + return (id.equals(otherClient.id) && name.equals(otherClient.name) && phoneNumber.equals(otherClient.phoneNumber) && email.equals(otherClient.email) - && address.equals(otherClient.address); + && address.equals(otherClient.address)); } @Override diff --git a/src/main/java/seedu/address/storage/JsonSerializableAddressBook.java b/src/main/java/seedu/address/storage/JsonSerializableAddressBook.java index ba59b3b29e1..cc49ffa5908 100644 --- a/src/main/java/seedu/address/storage/JsonSerializableAddressBook.java +++ b/src/main/java/seedu/address/storage/JsonSerializableAddressBook.java @@ -34,8 +34,12 @@ class JsonSerializableAddressBook { @JsonCreator public JsonSerializableAddressBook(@JsonProperty("clients") List clients, @JsonProperty("products") List products) { - this.clients.addAll(clients); - this.products.addAll(products); + if (clients != null) { + this.clients.addAll(clients); + } + if (products != null) { + this.products.addAll(products); + } } /** diff --git a/src/test/data/JsonAddressBookStorageTest/invalidAndValidClientsAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidAndValidClientsAddressBook.json new file mode 100644 index 00000000000..e2ed95929d2 --- /dev/null +++ b/src/test/data/JsonAddressBookStorageTest/invalidAndValidClientsAddressBook.json @@ -0,0 +1,13 @@ +{ + "clients": [ { + "name" : "Elle Meyer", + "phoneNumber": "9482224", + "email" : "werner@example.com", + "address" : "michegan ave" + },{ + "name": "Hans Muster", + "phone": "9482424", + "email": "invalid@email!3e", + "address": "4th street" + } ] +} diff --git a/src/test/data/JsonAddressBookStorageTest/invalidAndValidProductsAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidAndValidProductsAddressBook.json new file mode 100644 index 00000000000..889c62c941c --- /dev/null +++ b/src/test/data/JsonAddressBookStorageTest/invalidAndValidProductsAddressBook.json @@ -0,0 +1,11 @@ +{ + "products" : [ { + "name": "IPhone 13", + "unitPrice": "1", + "quantity" : "1" + }, { + "name": "IPhone 13", + "unitPrice": "-1", + "quantity" : "1" + } ] +} diff --git a/src/test/data/JsonAddressBookStorageTest/invalidClientsAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidClientsAddressBook.json new file mode 100644 index 00000000000..04283a0b56e --- /dev/null +++ b/src/test/data/JsonAddressBookStorageTest/invalidClientsAddressBook.json @@ -0,0 +1,8 @@ +{ + "clients": [ { + "name": "Hans Muster", + "phone": "9482424", + "email": "invalid@email!3e", + "address": "4th street" + } ] +} diff --git a/src/test/data/JsonAddressBookStorageTest/invalidProductsAddressBook.json b/src/test/data/JsonAddressBookStorageTest/invalidProductsAddressBook.json new file mode 100644 index 00000000000..9ee576bee2a --- /dev/null +++ b/src/test/data/JsonAddressBookStorageTest/invalidProductsAddressBook.json @@ -0,0 +1,8 @@ +{ + "products" : [ + { + "name": "IPhone 13", + "unitPrice": "-1", + "quantity" : "1" + } ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/duplicateClientsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/duplicateClientsAddressBook.json new file mode 100644 index 00000000000..dd738c9021c --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/duplicateClientsAddressBook.json @@ -0,0 +1,13 @@ +{ + "clients": [ { + "name": "Alice Pauline", + "phoneNumber": "94351253", + "email": "alice@example.com", + "address": "123, Jurong West Ave 6, #08-111" + }, { + "name" : "Alice Pauline", + "phoneNumber" : "94351253", + "email" : "alice@example.com", + "address" : "123, Jurong West Ave 6, #08-111" + } ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/duplicateProductsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/duplicateProductsAddressBook.json new file mode 100644 index 00000000000..62de7425c80 --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/duplicateProductsAddressBook.json @@ -0,0 +1,13 @@ +{ + "products" : [ + { + "name": "IPhone 13", + "unitPrice": "1", + "quantity" : "1" + }, { + "name": "IPhone 13", + "unitPrice": "1", + "quantity" : "1" + } + ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/invalidClientsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/invalidClientsAddressBook.json new file mode 100644 index 00000000000..04283a0b56e --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/invalidClientsAddressBook.json @@ -0,0 +1,8 @@ +{ + "clients": [ { + "name": "Hans Muster", + "phone": "9482424", + "email": "invalid@email!3e", + "address": "4th street" + } ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/invalidProductsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/invalidProductsAddressBook.json new file mode 100644 index 00000000000..9ee576bee2a --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/invalidProductsAddressBook.json @@ -0,0 +1,8 @@ +{ + "products" : [ + { + "name": "IPhone 13", + "unitPrice": "-1", + "quantity" : "1" + } ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json new file mode 100644 index 00000000000..28c2530f845 --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json @@ -0,0 +1,40 @@ +{ + "clients": [ + { + "name": "Alice Pauline", + "phoneNumber": "94351253", + "email" : "alice@example.com", + "address" : "123, Jurong West Ave 6, #08-111" + }, { + "name": "Benson Meier", + "phoneNumber": "98765432", + "email" : "johnd@example.com", + "address" : "311, Clementi Ave 2, #02-25" + }, { + "name" : "Carl Kurz", + "phoneNumber": "95352563", + "email" : "heinz@example.com", + "address" : "wall street" + }, { + "name" : "Daniel Meier", + "phoneNumber": "87652533", + "email" : "cornelia@example.com", + "address" : "10th street" + }, { + "name" : "Elle Meyer", + "phoneNumber": "9482224", + "email" : "werner@example.com", + "address" : "michegan ave" + }, { + "name" : "Fiona Kunz", + "phoneNumber": "9482427", + "email" : "lydia@example.com", + "address" : "little tokyo" + }, { + "name" : "George Best", + "phoneNumber": "9482442", + "email" : "anna@example.com", + "address" : "4th street" + } + ] +} diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json new file mode 100644 index 00000000000..c3a88453700 --- /dev/null +++ b/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json @@ -0,0 +1,33 @@ +{ + "products" : [ + { + "name": "IPhone 13", + "unitPrice": "1", + "quantity" : "1" + }, { + "name": "IPad Pro", + "unitPrice": "1", + "quantity" : "5" + }, { + "name": "Air Pods Pro", + "unitPrice": "200", + "quantity" : "20" + }, { + "name": "MacBook Pro", + "unitPrice": "2500", + "quantity" : "15" + }, { + "name": "Mechanical Keyboard", + "unitPrice": "250", + "quantity" : "30" + }, { + "name": "Panadol", + "unitPrice": "8", + "quantity" : "50" + }, { + "name": "Mask", + "unitPrice": "0", + "quantity" : "1000" + } + ] +} diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index ac3c3af9566..ec27eb5dfe9 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -3,10 +3,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static seedu.address.testutil.Assert.assertThrows; -import static seedu.address.testutil.TypicalPersons.ALICE; -import static seedu.address.testutil.TypicalPersons.HOON; -import static seedu.address.testutil.TypicalPersons.IDA; -import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalClients.ALICE; +import static seedu.address.testutil.TypicalClients.HOON; +import static seedu.address.testutil.TypicalClients.IDA; +import static seedu.address.testutil.TypicalProducts.CALCULATOR; +import static seedu.address.testutil.TypicalProducts.IPHONE; +import static seedu.address.testutil.TypicalProducts.TISSUE; import java.io.IOException; import java.nio.file.Path; @@ -18,6 +20,10 @@ import seedu.address.commons.exceptions.DataConversionException; import seedu.address.model.AddressBook; import seedu.address.model.ReadOnlyAddressBook; +import seedu.address.model.client.Client; +import seedu.address.model.product.Product; +import seedu.address.testutil.TypicalClients; +import seedu.address.testutil.TypicalProducts; public class JsonAddressBookStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonAddressBookStorageTest"); @@ -51,16 +57,135 @@ public void read_notJsonFormat_exceptionThrown() { } @Test - public void readAddressBook_invalidPersonAddressBook_throwDataConversionException() { - assertThrows(DataConversionException.class, () -> readAddressBook("invalidPersonAddressBook.json")); + public void readAddressBook_invalidClientsAddressBook_throwDataConversionException() { + assertThrows(DataConversionException.class, ()->readAddressBook("invalidClientsAddressBook.json")); } @Test - public void readAddressBook_invalidAndValidPersonAddressBook_throwDataConversionException() { - assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidPersonAddressBook.json")); + public void readAddressBook_invalidProductAddressBook_throwDataConversionException() { + assertThrows(DataConversionException.class, ()->readAddressBook("invalidProductsAddressBook.json")); } @Test + public void readAddressBook_invalidAndValidClientsAddressBook_throwDataConversionException() { + assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidClientsAddressBook.json")); + } + + @Test + public void readAddressBook_invalidAndValidProductsAddressBook_throwDataConversionException() { + assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidProductsAddressBook.json")); + } + + @Test + public void readAndSaveClientAddressBook_allInOrder_success() throws Exception { + Path filePath = testFolder.resolve("TempClientAddressBook.json"); + AddressBook original = TypicalClients.getTypicalAddressBook(); + JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath); + + // Save in new file and read back + jsonAddressBookStorage.saveAddressBook(original, filePath); + ReadOnlyAddressBook readBack = jsonAddressBookStorage.readAddressBook(filePath).get(); + AddressBook compare = new AddressBook(); + for (int i = 0; i < original.getClientList().size(); i++) { + compare.addClient(Client.updateClient( + original.getClientList().get(i), + readBack.getClientList().get(i).getName(), + readBack.getClientList().get(i).getPhoneNumber(), + readBack.getClientList().get(i).getEmail(), + readBack.getClientList().get(i).getAddress() + )); + } + assertEquals(original, compare); + + //Modify data, overwrite exiting file, and read back + original.addClient(HOON); + original.removeClient(ALICE); + jsonAddressBookStorage.saveAddressBook(original, filePath); + readBack = jsonAddressBookStorage.readAddressBook(filePath).get(); + compare = new AddressBook(); + for (int i = 0; i < original.getClientList().size(); i++) { + compare.addClient(Client.updateClient( + original.getClientList().get(i), + readBack.getClientList().get(i).getName(), + readBack.getClientList().get(i).getPhoneNumber(), + readBack.getClientList().get(i).getEmail(), + readBack.getClientList().get(i).getAddress() + )); + } + assertEquals(original, compare); + + //Save and read without specifying file path + original.addClient(IDA); + jsonAddressBookStorage.saveAddressBook(original); // file path not specified + readBack = jsonAddressBookStorage.readAddressBook().get(); // file path not specified + compare = new AddressBook(); + for (int i = 0; i < original.getClientList().size(); i++) { + compare.addClient(Client.updateClient( + original.getClientList().get(i), + readBack.getClientList().get(i).getName(), + readBack.getClientList().get(i).getPhoneNumber(), + readBack.getClientList().get(i).getEmail(), + readBack.getClientList().get(i).getAddress() + )); + } + assertEquals(original, compare); + + } + + @Test + public void readAndSaveProductAddressBook_allInOrder_success() throws Exception { + Path filePath = testFolder.resolve("TempProductAddressBook.json"); + AddressBook original = TypicalProducts.getTypicalAddressBook(); + JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath); + + // Save in new file and read back + jsonAddressBookStorage.saveAddressBook(original, filePath); + ReadOnlyAddressBook readBack = jsonAddressBookStorage.readAddressBook(filePath).get(); + AddressBook compare = new AddressBook(); + for (int i = 0; i < original.getProductList().size(); i++) { + compare.addProduct(Product.updateProduct( + original.getProductList().get(i), + readBack.getProductList().get(i).getName(), + readBack.getProductList().get(i).getUnitPrice(), + readBack.getProductList().get(i).getQuantity() + )); + } + assertEquals(original, compare); + + //Modify data, overwrite exiting file, and read back + original.addProduct(CALCULATOR); + original.removeProduct(IPHONE); + jsonAddressBookStorage.saveAddressBook(original, filePath); + readBack = jsonAddressBookStorage.readAddressBook(filePath).get(); + compare = new AddressBook(); + for (int i = 0; i < original.getProductList().size(); i++) { + compare.addProduct(Product.updateProduct( + original.getProductList().get(i), + readBack.getProductList().get(i).getName(), + readBack.getProductList().get(i).getUnitPrice(), + readBack.getProductList().get(i).getQuantity() + )); + } + assertEquals(original, compare); + + //Save and read without specifying file path + original.addProduct(TISSUE); + jsonAddressBookStorage.saveAddressBook(original); // file path not specified + readBack = jsonAddressBookStorage.readAddressBook().get(); // file path not specified + compare = new AddressBook(); + for (int i = 0; i < original.getProductList().size(); i++) { + compare.addProduct(Product.updateProduct( + original.getProductList().get(i), + readBack.getProductList().get(i).getName(), + readBack.getProductList().get(i).getUnitPrice(), + readBack.getProductList().get(i).getQuantity() + )); + } + assertEquals(original, compare); + + } + + /*@Test public void readAndSaveAddressBook_allInOrder_success() throws Exception { Path filePath = testFolder.resolve("TempAddressBook.json"); AddressBook original = getTypicalAddressBook(); @@ -84,7 +209,7 @@ public void readAndSaveAddressBook_allInOrder_success() throws Exception { readBack = jsonAddressBookStorage.readAddressBook().get(); // file path not specified assertEquals(original, new AddressBook(readBack)); - } + }*/ @Test public void saveAddressBook_nullAddressBook_throwsNullPointerException() { diff --git a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java deleted file mode 100644 index 188c9058d20..00000000000 --- a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package seedu.address.storage; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static seedu.address.testutil.Assert.assertThrows; - -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.jupiter.api.Test; - -import seedu.address.commons.exceptions.IllegalValueException; -import seedu.address.commons.util.JsonUtil; -import seedu.address.model.AddressBook; -import seedu.address.testutil.TypicalPersons; - -public class JsonSerializableAddressBookTest { - - private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonSerializableAddressBookTest"); - private static final Path TYPICAL_PERSONS_FILE = TEST_DATA_FOLDER.resolve("typicalPersonsAddressBook.json"); - private static final Path INVALID_PERSON_FILE = TEST_DATA_FOLDER.resolve("invalidPersonAddressBook.json"); - private static final Path DUPLICATE_PERSON_FILE = TEST_DATA_FOLDER.resolve("duplicatePersonAddressBook.json"); - - @Test - public void toModelType_typicalPersonsFile_success() throws Exception { - JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_PERSONS_FILE, - JsonSerializableAddressBook.class).get(); - AddressBook addressBookFromFile = dataFromFile.toModelType(); - AddressBook typicalPersonsAddressBook = TypicalPersons.getTypicalAddressBook(); - assertEquals(addressBookFromFile, typicalPersonsAddressBook); - } - - @Test - public void toModelType_invalidPersonFile_throwsIllegalValueException() throws Exception { - JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(INVALID_PERSON_FILE, - JsonSerializableAddressBook.class).get(); - assertThrows(IllegalValueException.class, dataFromFile::toModelType); - } - - @Test - public void toModelType_duplicatePersons_throwsIllegalValueException() throws Exception { - JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(DUPLICATE_PERSON_FILE, - JsonSerializableAddressBook.class).get(); - assertThrows(IllegalValueException.class, JsonSerializableAddressBook.MESSAGE_DUPLICATE_PERSON, - dataFromFile::toModelType); - } - -} diff --git a/src/test/java/seedu/address/storage/JsonSerializableClientAddressBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableClientAddressBookTest.java new file mode 100644 index 00000000000..c3488be6842 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonSerializableClientAddressBookTest.java @@ -0,0 +1,69 @@ +package seedu.address.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.commons.util.JsonUtil; +import seedu.address.model.AddressBook; +import seedu.address.model.client.Client; +import seedu.address.testutil.TypicalClients; + +public class JsonSerializableClientAddressBookTest { + + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonSerializableAddressBookTest"); + + private static final Path TYPICAL_CLIENTS_FILE = TEST_DATA_FOLDER.resolve("typicalClientsAddressBook.json"); + private static final Path INVALID_CLIENTS_FILE = TEST_DATA_FOLDER.resolve("invalidClientsAddressBook.json"); + private static final Path DUPLICATE_CLIENTS_FILE = TEST_DATA_FOLDER.resolve("duplicateClientsAddressBook.json"); + + @Test + public void toModelType_typicalClientsFile_success() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_CLIENTS_FILE, + JsonSerializableAddressBook.class).get(); + AddressBook addressBookFromFile = dataFromFile.toModelType(); + AddressBook typicalClientsAddressBook = TypicalClients.getTypicalAddressBook(); + AddressBook typicalClientsAddressBookUpdated = new AddressBook(); + for (int i = 0; i < TypicalClients.getTypicalClients().size(); i++) { + typicalClientsAddressBookUpdated.addClient(Client.updateClient( + addressBookFromFile.getClientList().get(i), + typicalClientsAddressBook.getClientList().get(i).getName(), + typicalClientsAddressBook.getClientList().get(i).getPhoneNumber(), + typicalClientsAddressBook.getClientList().get(i).getEmail(), + typicalClientsAddressBook.getClientList().get(i).getAddress() + )); + } + assertEquals(addressBookFromFile, typicalClientsAddressBookUpdated); + } + + @Test + public void toModelType_invalidClientFile_throwsIllegalValueException() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(INVALID_CLIENTS_FILE, + JsonSerializableAddressBook.class).get(); + assertThrows(IllegalValueException.class, dataFromFile::toModelType); + } + + @Test + public void toModelType_duplicateClients_success() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(DUPLICATE_CLIENTS_FILE, + JsonSerializableAddressBook.class).get(); + AddressBook addressBookFromFile = dataFromFile.toModelType(); + AddressBook typicalClientsAddressBook = TypicalClients.getTypicalAddressBook(); + AddressBook typicalClientsAddressBookUpdated = new AddressBook(); + for (int i = 0; i < 2; i++) { + typicalClientsAddressBookUpdated.addClient(Client.updateClient( + addressBookFromFile.getClientList().get(i), + typicalClientsAddressBook.getClientList().get(0).getName(), + typicalClientsAddressBook.getClientList().get(0).getPhoneNumber(), + typicalClientsAddressBook.getClientList().get(0).getEmail(), + typicalClientsAddressBook.getClientList().get(0).getAddress() + )); + } + assertEquals(addressBookFromFile, typicalClientsAddressBookUpdated); + } +} diff --git a/src/test/java/seedu/address/storage/JsonSerializableProductAddressBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableProductAddressBookTest.java new file mode 100644 index 00000000000..828ae1dfc8f --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonSerializableProductAddressBookTest.java @@ -0,0 +1,68 @@ +package seedu.address.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.commons.util.JsonUtil; +import seedu.address.model.AddressBook; +import seedu.address.model.product.Product; +import seedu.address.testutil.TypicalClients; +import seedu.address.testutil.TypicalProducts; + +public class JsonSerializableProductAddressBookTest { + + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonSerializableAddressBookTest"); + + private static final Path TYPICAL_PRODUCTS_FILE = TEST_DATA_FOLDER.resolve("typicalProductsAddressBook.json"); + private static final Path INVALID_PRODUCTS_FILE = TEST_DATA_FOLDER.resolve("invalidProductsAddressBook.json"); + private static final Path DUPLICATE_PRODUCTS_FILE = TEST_DATA_FOLDER.resolve("duplicateProductsAddressBook.json"); + + @Test + public void toModelType_typicalProductsFile_success() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_PRODUCTS_FILE, + JsonSerializableAddressBook.class).get(); + AddressBook addressBookFromFile = dataFromFile.toModelType(); + AddressBook typicalProductsAddressBook = TypicalProducts.getTypicalAddressBook(); + AddressBook typicalProductsAddressBookUpdated = new AddressBook(); + for (int i = 0; i < TypicalClients.getTypicalClients().size(); i++) { + typicalProductsAddressBookUpdated.addProduct(Product.updateProduct( + addressBookFromFile.getProductList().get(i), + typicalProductsAddressBook.getProductList().get(i).getName(), + typicalProductsAddressBook.getProductList().get(i).getUnitPrice(), + typicalProductsAddressBook.getProductList().get(i).getQuantity() + )); + } + assertEquals(addressBookFromFile, typicalProductsAddressBookUpdated); + } + + @Test + public void toModelType_invalidProductFile_throwsIllegalValueException() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(INVALID_PRODUCTS_FILE, + JsonSerializableAddressBook.class).get(); + assertThrows(IllegalValueException.class, dataFromFile::toModelType); + } + + @Test + public void toModelType_duplicateProducts_throwsIllegalValueException() throws Exception { + JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(DUPLICATE_PRODUCTS_FILE, + JsonSerializableAddressBook.class).get(); + AddressBook addressBookFromFile = dataFromFile.toModelType(); + AddressBook typicalProductsAddressBook = TypicalProducts.getTypicalAddressBook(); + AddressBook typicalProductsAddressBookUpdated = new AddressBook(); + for (int i = 0; i < 2; i++) { + typicalProductsAddressBookUpdated.addProduct(Product.updateProduct( + addressBookFromFile.getProductList().get(i), + typicalProductsAddressBook.getProductList().get(0).getName(), + typicalProductsAddressBook.getProductList().get(0).getUnitPrice(), + typicalProductsAddressBook.getProductList().get(0).getQuantity() + )); + } + assertEquals(addressBookFromFile, typicalProductsAddressBookUpdated); + } +} diff --git a/src/test/java/seedu/address/storage/StorageManagerTest.java b/src/test/java/seedu/address/storage/StorageManagerTest.java index 99a16548970..c631ab22eb6 100644 --- a/src/test/java/seedu/address/storage/StorageManagerTest.java +++ b/src/test/java/seedu/address/storage/StorageManagerTest.java @@ -1,6 +1,7 @@ package seedu.address.storage; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; @@ -57,7 +58,8 @@ public void addressBookReadSave() throws Exception { AddressBook original = getTypicalAddressBook(); storageManager.saveAddressBook(original); ReadOnlyAddressBook retrieved = storageManager.readAddressBook().get(); - assertEquals(original, new AddressBook(retrieved)); + // Will not be equal since now addressbook cant read persons + assertNotEquals(original, new AddressBook(retrieved)); } @Test