From 6f0846d557af0faf9f16dca3b05c234350c90f0a Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 16 Oct 2021 11:58:39 +0800 Subject: [PATCH 1/5] Initial test commit --- .../typicalClientsAddressBook.json | 40 +++++++++++++++++++ .../JsonSerializableAddressBookTest.java | 5 ++- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json new file mode 100644 index 00000000000..2985a1d5b80 --- /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" + } + ] +} \ No newline at end of file diff --git a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java index 188c9058d20..ea36be8442c 100644 --- a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java +++ b/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java @@ -20,7 +20,8 @@ public class JsonSerializableAddressBookTest { 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 + + /*@Test public void toModelType_typicalPersonsFile_success() throws Exception { JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_PERSONS_FILE, JsonSerializableAddressBook.class).get(); @@ -43,5 +44,5 @@ public void toModelType_duplicatePersons_throwsIllegalValueException() throws Ex assertThrows(IllegalValueException.class, JsonSerializableAddressBook.MESSAGE_DUPLICATE_PERSON, dataFromFile::toModelType); } - +*/ } From a6ca38853e290da4bd270d0b9f4a3df38fcbdaa8 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 16 Oct 2021 14:24:50 +0800 Subject: [PATCH 2/5] Update serializable test cases for client and product --- .../seedu/address/model/client/Client.java | 4 +- .../storage/JsonSerializableAddressBook.java | 8 ++- .../duplicateClientsAddressBook.json | 13 ++++ .../duplicateProductsAddressBook.json | 13 ++++ .../invalidClientsAddressBook.json | 8 +++ .../invalidProductsAddressBook.json | 8 +++ .../typicalProductsAddressBook.json | 33 +++++++++ .../storage/JsonAddressBookStorageTest.java | 12 ++-- .../JsonSerializableAddressBookTest.java | 48 ------------- ...JsonSerializableClientAddressBookTest.java | 69 +++++++++++++++++++ ...sonSerializableProductAddressBookTest.java | 68 ++++++++++++++++++ .../address/storage/StorageManagerTest.java | 4 +- 12 files changed, 229 insertions(+), 59 deletions(-) create mode 100644 src/test/data/JsonSerializableAddressBookTest/duplicateClientsAddressBook.json create mode 100644 src/test/data/JsonSerializableAddressBookTest/duplicateProductsAddressBook.json create mode 100644 src/test/data/JsonSerializableAddressBookTest/invalidClientsAddressBook.json create mode 100644 src/test/data/JsonSerializableAddressBookTest/invalidProductsAddressBook.json create mode 100644 src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json delete mode 100644 src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java create mode 100644 src/test/java/seedu/address/storage/JsonSerializableClientAddressBookTest.java create mode 100644 src/test/java/seedu/address/storage/JsonSerializableProductAddressBookTest.java 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/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/typicalProductsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json new file mode 100644 index 00000000000..6e079de9f5b --- /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" + } + ] +} \ No newline at end of file diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index ac3c3af9566..8fd341f8f88 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -50,17 +50,17 @@ public void read_notJsonFormat_exceptionThrown() { assertThrows(DataConversionException.class, () -> readAddressBook("notJsonFormatAddressBook.json")); } - @Test + /*@Test public void readAddressBook_invalidPersonAddressBook_throwDataConversionException() { assertThrows(DataConversionException.class, () -> readAddressBook("invalidPersonAddressBook.json")); - } + }*/ - @Test + /*@Test public void readAddressBook_invalidAndValidPersonAddressBook_throwDataConversionException() { assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidPersonAddressBook.json")); - } + }*/ - @Test + /*@Test public void readAndSaveAddressBook_allInOrder_success() throws Exception { Path filePath = testFolder.resolve("TempAddressBook.json"); AddressBook original = getTypicalAddressBook(); @@ -84,7 +84,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 ea36be8442c..00000000000 --- a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java +++ /dev/null @@ -1,48 +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 From aaa8afab1e1dfa76221419e0bbb3ef4b778ce820 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 16 Oct 2021 14:48:30 +0800 Subject: [PATCH 3/5] Add storage tests for product and clients --- .../invalidAndValidClientsAddressBook.json | 13 +++++++ .../invalidAndValidProductsAddressBook.json | 11 ++++++ .../invalidClientsAddressBook.json | 8 +++++ .../invalidProductsAddressBook.json | 8 +++++ .../storage/JsonAddressBookStorageTest.java | 35 +++++++++++++------ 5 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src/test/data/JsonAddressBookStorageTest/invalidAndValidClientsAddressBook.json create mode 100644 src/test/data/JsonAddressBookStorageTest/invalidAndValidProductsAddressBook.json create mode 100644 src/test/data/JsonAddressBookStorageTest/invalidClientsAddressBook.json create mode 100644 src/test/data/JsonAddressBookStorageTest/invalidProductsAddressBook.json 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/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index 8fd341f8f88..25993c92f30 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -1,12 +1,12 @@ package seedu.address.storage; -import static org.junit.jupiter.api.Assertions.assertEquals; +//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.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.TypicalPersons.getTypicalAddressBook;*/ import java.io.IOException; import java.nio.file.Path; @@ -18,6 +18,9 @@ import seedu.address.commons.exceptions.DataConversionException; import seedu.address.model.AddressBook; import seedu.address.model.ReadOnlyAddressBook; +//import seedu.address.testutil.TypicalClients; + +//import javax.xml.crypto.Data; public class JsonAddressBookStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonAddressBookStorageTest"); @@ -50,15 +53,25 @@ public void read_notJsonFormat_exceptionThrown() { assertThrows(DataConversionException.class, () -> readAddressBook("notJsonFormatAddressBook.json")); } - /*@Test - public void readAddressBook_invalidPersonAddressBook_throwDataConversionException() { - assertThrows(DataConversionException.class, () -> readAddressBook("invalidPersonAddressBook.json")); - }*/ + @Test + public void readAddressBook_invalidClientsAddressBook_throwDataConversionException() { + assertThrows(DataConversionException.class, ()->readAddressBook("invalidClientsAddressBook.json")); + } - /*@Test - public void readAddressBook_invalidAndValidPersonAddressBook_throwDataConversionException() { - assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidPersonAddressBook.json")); - }*/ + @Test + 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 readAndSaveAddressBook_allInOrder_success() throws Exception { From 7aac409d3df22bb37941c7a8fbffbb124cc34348 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 16 Oct 2021 14:50:24 +0800 Subject: [PATCH 4/5] Fix EOF error --- .../typicalClientsAddressBook.json | 2 +- .../typicalProductsAddressBook.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json index 2985a1d5b80..28c2530f845 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalClientsAddressBook.json @@ -37,4 +37,4 @@ "address" : "4th street" } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json index 6e079de9f5b..c3a88453700 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalProductsAddressBook.json @@ -30,4 +30,4 @@ "quantity" : "1000" } ] -} \ No newline at end of file +} From 738b0436147183dff3f080f0c2b016c459c659dc Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 16 Oct 2021 16:03:00 +0800 Subject: [PATCH 5/5] Add storage address book tests for client and product --- .../storage/JsonAddressBookStorageTest.java | 128 ++++++++++++++++-- 1 file changed, 120 insertions(+), 8 deletions(-) diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index 25993c92f30..ec27eb5dfe9 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -1,12 +1,14 @@ package seedu.address.storage; -//import static org.junit.jupiter.api.Assertions.assertEquals; +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,9 +20,10 @@ import seedu.address.commons.exceptions.DataConversionException; import seedu.address.model.AddressBook; import seedu.address.model.ReadOnlyAddressBook; -//import seedu.address.testutil.TypicalClients; - -//import javax.xml.crypto.Data; +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"); @@ -73,6 +76,115 @@ public void readAddressBook_invalidAndValidProductsAddressBook_throwDataConversi 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");