Skip to content

Commit

Permalink
Merge pull request #130 from benluiwj/branch-storageTests
Browse files Browse the repository at this point in the history
Storage tests
  • Loading branch information
benluiwj authored Oct 16, 2021
2 parents f6dde24 + 738b043 commit a822b62
Show file tree
Hide file tree
Showing 17 changed files with 437 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ class JsonSerializableAddressBook {
@JsonCreator
public JsonSerializableAddressBook(@JsonProperty("clients") List<JsonAdaptedClient> clients,
@JsonProperty("products") List<JsonAdaptedProduct> products) {
this.clients.addAll(clients);
this.products.addAll(products);
if (clients != null) {
this.clients.addAll(clients);
}
if (products != null) {
this.products.addAll(products);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"products" : [ {
"name": "IPhone 13",
"unitPrice": "1",
"quantity" : "1"
}, {
"name": "IPhone 13",
"unitPrice": "-1",
"quantity" : "1"
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"clients": [ {
"name": "Hans Muster",
"phone": "9482424",
"email": "invalid@email!3e",
"address": "4th street"
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"products" : [
{
"name": "IPhone 13",
"unitPrice": "-1",
"quantity" : "1"
} ]
}
Original file line number Diff line number Diff line change
@@ -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"
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"products" : [
{
"name": "IPhone 13",
"unitPrice": "1",
"quantity" : "1"
}, {
"name": "IPhone 13",
"unitPrice": "1",
"quantity" : "1"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"clients": [ {
"name": "Hans Muster",
"phone": "9482424",
"email": "invalid@email!3e",
"address": "4th street"
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"products" : [
{
"name": "IPhone 13",
"unitPrice": "-1",
"quantity" : "1"
} ]
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
143 changes: 134 additions & 9 deletions src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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() {
Expand Down
Loading

0 comments on commit a822b62

Please sign in to comment.