From 0a771cd286be9be2d22653384b0644c00e090db2 Mon Sep 17 00:00:00 2001 From: muhdbhz Date: Wed, 30 Oct 2024 12:45:31 +0800 Subject: [PATCH] Add ShowListingsCommandTest and improve tests Add ShowListingsCommandTest Improve robustness of ListCommandTest --- .../logic/commands/CommandTestUtil.java | 32 ++++++++ .../logic/commands/ListCommandTest.java | 39 +++++++++ .../commands/ShowListingsCommandTest.java | 79 +++++++++++++++++++ .../address/testutil/TypicalIndexes.java | 3 + .../address/testutil/TypicalListings.java | 7 ++ 5 files changed, 160 insertions(+) create mode 100644 src/test/java/seedu/address/logic/commands/ShowListingsCommandTest.java diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 05a81b01c34..398aeb1682e 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -22,6 +22,8 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.AddressBook; import seedu.address.model.Model; +import seedu.address.model.listing.Listing; +import seedu.address.model.listing.ListingContainsKeywordsPredicate; import seedu.address.model.person.Name; import seedu.address.model.person.NameContainsKeywordsPredicate; import seedu.address.model.person.Person; @@ -114,6 +116,9 @@ public static void assertCommandSuccess(Command command, Model actualModel, Stri if (command instanceof ListCommand) { expectedCommandResult = new CommandResult(expectedMessage, false, false, false, true); + } else if (command instanceof ShowListingsCommand) { + expectedCommandResult = new CommandResult(expectedMessage, false, + false, true, false); } else { expectedCommandResult = new CommandResult(expectedMessage); } @@ -162,4 +167,31 @@ public static void showPersonWithName(Model model, Name targetName) { assertEquals(1, model.getFilteredPersonList().size()); } + /** + * Updates {@code model}'s filtered list to show only the listing at the given {@code targetIndex} in the + * {@code model}'s address book. + */ + public static void showListingAtIndex(Model model, Index targetIndex) { + assertTrue(targetIndex.getZeroBased() < model.getFilteredListingList().size()); + + Listing listing = model.getFilteredListingList().get(targetIndex.getZeroBased()); + final String[] splitName = listing.getName().fullName.split("\\s+"); + model.updateFilteredListingList(new ListingContainsKeywordsPredicate(Arrays.asList(splitName[0]))); + + assertEquals(1, model.getFilteredListingList().size()); + } + + /** + * Updates {@code model}'s filtered list to show only the listing with the given {@code targetName} in the + * {@code model}'s address book. + */ + public static void showListingWithName(Model model, Name targetName) { + Listing listing = model.getListingByName(targetName); + assertTrue(model.hasListing(listing)); + final String[] splitName = listing.getName().fullName.split("\\s+"); + model.updateFilteredListingList(new ListingContainsKeywordsPredicate(Arrays.asList(splitName[0]))); + + assertEquals(1, model.getFilteredListingList().size()); + } + } diff --git a/src/test/java/seedu/address/logic/commands/ListCommandTest.java b/src/test/java/seedu/address/logic/commands/ListCommandTest.java index 6db2ec3874a..b335a092df5 100644 --- a/src/test/java/seedu/address/logic/commands/ListCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ListCommandTest.java @@ -1,13 +1,19 @@ package seedu.address.logic.commands; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; +import static seedu.address.logic.commands.CommandTestUtil.showPersonWithName; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalPersons.getTypicalNames; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import seedu.address.model.AddressBook; import seedu.address.model.Listings; import seedu.address.model.Model; import seedu.address.model.ModelManager; @@ -36,5 +42,38 @@ public void execute_listIsNotFiltered_showsSameList() { public void execute_listIsFiltered_showsEverything() { showPersonAtIndex(model, INDEX_FIRST_PERSON); assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonAtIndex(model, INDEX_SECOND_PERSON); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonAtIndex(model, INDEX_THIRD_PERSON); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(0)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(1)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(2)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(3)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(4)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(5)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + + showPersonWithName(model, getTypicalNames().get(6)); + assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + } + + @Test + public void execute_emptyList_throwsCommandException() { + model = new ModelManager(new AddressBook(), new UserPrefs(), new Listings()); + assertCommandFailure(new ListCommand(), model, ListCommand.MESSAGE_NO_CLIENT_IN_LIST); } } diff --git a/src/test/java/seedu/address/logic/commands/ShowListingsCommandTest.java b/src/test/java/seedu/address/logic/commands/ShowListingsCommandTest.java new file mode 100644 index 00000000000..40b65d5f491 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/ShowListingsCommandTest.java @@ -0,0 +1,79 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CommandTestUtil.showListingAtIndex; +import static seedu.address.logic.commands.CommandTestUtil.showListingWithName; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_LISTING; +import static seedu.address.testutil.TypicalListings.getTypicalListings; +import static seedu.address.testutil.TypicalListings.getTypicalNames; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import seedu.address.model.Listings; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; + +/** + * Contains integration tests (interaction with the Model) and unit tests for ShowListingsCommand. + */ +public class ShowListingsCommandTest { + + private Model model; + private Model expectedModel; + + @BeforeEach + public void setUp() { + model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); + expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs(), model.getListings()); + } + + @Test + public void execute_listIsNotFiltered_showsSameList() { + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + } + + @Test + public void execute_listIsFiltered_showsEverything() { + showListingAtIndex(model, INDEX_FIRST_LISTING); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingAtIndex(model, INDEX_SECOND_LISTING); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingAtIndex(model, INDEX_THIRD_LISTING); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(0)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(1)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(2)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(3)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(4)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(5)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + + showListingWithName(model, getTypicalNames().get(6)); + assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel); + } + + @Test + public void execute_emptyListings_throwsCommandException() { + model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new Listings()); + assertCommandFailure(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_NO_LISTINGS_IN_LIST); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalIndexes.java b/src/test/java/seedu/address/testutil/TypicalIndexes.java index 1e613937657..f621aebd0e1 100644 --- a/src/test/java/seedu/address/testutil/TypicalIndexes.java +++ b/src/test/java/seedu/address/testutil/TypicalIndexes.java @@ -9,4 +9,7 @@ public class TypicalIndexes { public static final Index INDEX_FIRST_PERSON = Index.fromOneBased(1); public static final Index INDEX_SECOND_PERSON = Index.fromOneBased(2); public static final Index INDEX_THIRD_PERSON = Index.fromOneBased(3); + public static final Index INDEX_FIRST_LISTING = Index.fromOneBased(1); + public static final Index INDEX_SECOND_LISTING = Index.fromOneBased(2); + public static final Index INDEX_THIRD_LISTING = Index.fromOneBased(3); } diff --git a/src/test/java/seedu/address/testutil/TypicalListings.java b/src/test/java/seedu/address/testutil/TypicalListings.java index 3e376925e02..6d6ad0c5c8d 100644 --- a/src/test/java/seedu/address/testutil/TypicalListings.java +++ b/src/test/java/seedu/address/testutil/TypicalListings.java @@ -8,6 +8,7 @@ import seedu.address.model.Listings; import seedu.address.model.listing.Listing; import seedu.address.model.listing.Region; +import seedu.address.model.person.Name; /** * A utility class containing a list of {@code Listing} objects to be used in tests. @@ -80,6 +81,12 @@ public static Listings getTypicalListings() { return listings; } + public static List getTypicalNames() { + return new ArrayList<>(Arrays.asList(PASIR_RIS.getName(), TAMPINES.getName(), KENT_RIDGE.getName(), + BUONA_VISTA.getName(), SENGKANG.getName(), PUNGGOL.getName(), + SENTOSA.getName())); + } + public static List getTypicalPropertyListing() { return new ArrayList<>(Arrays.asList(PASIR_RIS, TAMPINES, KENT_RIDGE, BUONA_VISTA, SENGKANG, PUNGGOL, SENTOSA));