From 4e5791f73d8c55889f8dee2a87fab060693f4269 Mon Sep 17 00:00:00 2001 From: haozhen Date: Thu, 7 Nov 2024 01:59:40 +0800 Subject: [PATCH 1/3] Add JUNIT tests Added tests for updateListingsAfterClientEdit() and some Listing methods. --- .../seedu/address/model/listing/Listing.java | 10 ++++ .../seedu/address/model/ListingsTest.java | 52 +++++++++++++++++++ .../seedu/address/model/ModelManagerTest.java | 50 ++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/src/main/java/seedu/address/model/listing/Listing.java b/src/main/java/seedu/address/model/listing/Listing.java index 607253288a8..ef8c2367638 100644 --- a/src/main/java/seedu/address/model/listing/Listing.java +++ b/src/main/java/seedu/address/model/listing/Listing.java @@ -117,6 +117,16 @@ public void replaceBuyer(Person buyerToRemove, Person buyerToAdd) { } } + /** + * Checks if this listing has the specified buyer. + * + * @param buyer The buyer to check. + * @return True if the listing has the buyer, false otherwise. + */ + public boolean hasBuyer(Person buyer) { + return buyers.contains(buyer); + } + /** * Returns a new Listing with a different seller * @param sellerToAdd The new buyer to add to the listing. diff --git a/src/test/java/seedu/address/model/ListingsTest.java b/src/test/java/seedu/address/model/ListingsTest.java index 3f0b4107e3e..133bc14ff44 100644 --- a/src/test/java/seedu/address/model/ListingsTest.java +++ b/src/test/java/seedu/address/model/ListingsTest.java @@ -2,12 +2,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import seedu.address.model.listing.Listing; +import seedu.address.model.person.Person; import seedu.address.testutil.ListingBuilder; +import seedu.address.testutil.TypicalListings; +import seedu.address.testutil.TypicalPersons; public class ListingsTest { @@ -46,5 +50,53 @@ public void hashCodeTest() { differentListings.addListing(new ListingBuilder().withName("Different Listing").build()); assertFalse(listings.hashCode() == differentListings.hashCode()); } + + @Test + public void replaceBuyer_existingBuyer_replacesWithNewBuyer() { + Listing listing = new Listing(TypicalListings.PASIR_RIS); // Assume PASIR_RIS has DANIEL and GEORGE as buyers + Person buyerToRemove = TypicalPersons.DANIEL; + Person buyerToAdd = TypicalPersons.HOON; + + // Verify initial state + assertTrue(listing.getBuyers().contains(buyerToRemove), "The listing should contain DANIEL as a buyer initially."); + + // Replace the buyer + listing.replaceBuyer(buyerToRemove, buyerToAdd); + + // Assert that the old buyer is removed and the new buyer is added + assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should no longer contain DANIEL."); + assertTrue(listing.getBuyers().contains(buyerToAdd), "The listing should now contain HOON as a buyer."); + } + + @Test + public void replaceBuyer_nonExistingBuyer_noChange() { + Listing listing = new Listing(TypicalListings.PASIR_RIS); + Person buyerToRemove = TypicalPersons.AMY; // Assume AMY is not in the buyer list + Person buyerToAdd = TypicalPersons.HOON; + + assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should not contain AMY initially."); + + listing.replaceBuyer(buyerToRemove, buyerToAdd); + + assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should still not contain AMY."); + assertFalse(listing.getBuyers().contains(buyerToAdd), "The listing should not contain HOON since AMY was not in the list."); + } + + @Test + public void modifyListingWithSeller_newSeller_returnsModifiedListing() { + Listing originalListing = new Listing(TypicalListings.PASIR_RIS); + Person newSeller = TypicalPersons.BOB; + + Listing modifiedListing = originalListing.modifyListingWithSeller(newSeller); + + assertEquals(newSeller, modifiedListing.getSeller(), "The modified listing should have BOB as the seller."); + + assertNotEquals(newSeller, originalListing.getSeller(), "The original listing should still have ALICE as the seller."); + assertEquals(originalListing.getBuyers(), modifiedListing.getBuyers(), "The buyers list should be unchanged."); + assertEquals(originalListing.getAddress(), modifiedListing.getAddress(), "The address should be unchanged."); + assertEquals(originalListing.getName(), modifiedListing.getName(), "The name should be unchanged."); + } + + } diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index 5022559bf00..e691f269a23 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -17,11 +17,16 @@ import org.junit.jupiter.api.Test; import seedu.address.commons.core.GuiSettings; +import seedu.address.model.listing.Listing; import seedu.address.model.listing.exceptions.ListingNotFoundException; import seedu.address.model.person.Name; import seedu.address.model.person.NameContainsKeywordsPredicate; +import seedu.address.model.person.Person; import seedu.address.model.person.exceptions.PersonNotFoundException; import seedu.address.testutil.AddressBookBuilder; +import seedu.address.testutil.PersonBuilder; +import seedu.address.testutil.TypicalListings; +import seedu.address.testutil.TypicalPersons; public class ModelManagerTest { @@ -267,4 +272,49 @@ public void equals() { differentListings.addListing(PASIR_RIS); assertFalse(modelManager.equals(new ModelManager(addressBook, userPrefs, differentListings))); } + + @Test + public void updateListingsAfterClientEdit_buyerUpdatedInListings() { + ModelManager modelManager = new ModelManager(); + modelManager.addListing(TypicalListings.PASIR_RIS); + + Person originalBuyer = TypicalPersons.DANIEL; + Person updatedBuyer = new PersonBuilder(originalBuyer).withName("Updated Daniel").buildBuyer(); + + modelManager.updateListingsAfterClientEdit(originalBuyer, updatedBuyer); + + Listing listing = modelManager.getListings().getListingList().get(0); + assertTrue( + listing.hasBuyer(updatedBuyer), + "The listing should have the updated buyer." + ); + assertFalse( + listing.hasBuyer(originalBuyer), + "The listing should no longer have the original buyer." + ); + } + + @Test + public void updateListingsAfterClientEdit_sellerListingsUpdated() { + modelManager.addListing(TypicalListings.PASIR_RIS); + Person originalSeller = TypicalPersons.ALICE; + Person updatedSeller = TypicalPersons.BOB; + + assertTrue(modelManager.hasListing(TypicalListings.PASIR_RIS), + "The listing should initially exist."); + assertEquals(originalSeller, modelManager.getListings().getListingList() + .get(0).getSeller(), + "The seller should initially be ALICE."); + + modelManager.updateListingsAfterClientEdit(originalSeller, updatedSeller); + + Listing updatedListing = modelManager.getListings().getListingList().get(0); + assertEquals(updatedSeller, updatedListing.getSeller(), + "The listing should have the updated seller (BOB)."); + + assertFalse(modelManager.getListings().getListingList().stream() + .anyMatch(listing -> listing.getSeller().equals(originalSeller)), + "There should be no listings with the original seller (ALICE) after the update."); + } + } From 90fd648a23a7f25d1ef385e0e9065bf0995fc135 Mon Sep 17 00:00:00 2001 From: haozhen Date: Thu, 7 Nov 2024 02:09:36 +0800 Subject: [PATCH 2/3] Fix checkstyle Fixed checkstyle errors. --- .../seedu/address/model/ListingsTest.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/test/java/seedu/address/model/ListingsTest.java b/src/test/java/seedu/address/model/ListingsTest.java index 133bc14ff44..89034e73d30 100644 --- a/src/test/java/seedu/address/model/ListingsTest.java +++ b/src/test/java/seedu/address/model/ListingsTest.java @@ -53,33 +53,36 @@ public void hashCodeTest() { @Test public void replaceBuyer_existingBuyer_replacesWithNewBuyer() { - Listing listing = new Listing(TypicalListings.PASIR_RIS); // Assume PASIR_RIS has DANIEL and GEORGE as buyers + Listing listing = new Listing(TypicalListings.PASIR_RIS); Person buyerToRemove = TypicalPersons.DANIEL; Person buyerToAdd = TypicalPersons.HOON; - // Verify initial state - assertTrue(listing.getBuyers().contains(buyerToRemove), "The listing should contain DANIEL as a buyer initially."); + assertTrue(listing.getBuyers().contains(buyerToRemove), + "The listing should contain DANIEL as a buyer initially."); - // Replace the buyer listing.replaceBuyer(buyerToRemove, buyerToAdd); - // Assert that the old buyer is removed and the new buyer is added - assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should no longer contain DANIEL."); - assertTrue(listing.getBuyers().contains(buyerToAdd), "The listing should now contain HOON as a buyer."); + assertFalse(listing.getBuyers().contains(buyerToRemove), + "The listing should no longer contain DANIEL."); + assertTrue(listing.getBuyers().contains(buyerToAdd), + "The listing should now contain HOON as a buyer."); } @Test public void replaceBuyer_nonExistingBuyer_noChange() { Listing listing = new Listing(TypicalListings.PASIR_RIS); - Person buyerToRemove = TypicalPersons.AMY; // Assume AMY is not in the buyer list + Person buyerToRemove = TypicalPersons.AMY; Person buyerToAdd = TypicalPersons.HOON; - assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should not contain AMY initially."); + assertFalse(listing.getBuyers().contains(buyerToRemove), + "The listing should not contain AMY initially."); listing.replaceBuyer(buyerToRemove, buyerToAdd); - assertFalse(listing.getBuyers().contains(buyerToRemove), "The listing should still not contain AMY."); - assertFalse(listing.getBuyers().contains(buyerToAdd), "The listing should not contain HOON since AMY was not in the list."); + assertFalse(listing.getBuyers().contains(buyerToRemove), + "The listing should still not contain AMY."); + assertFalse(listing.getBuyers().contains(buyerToAdd), + "The listing should not contain HOON since AMY was not in the list."); } @Test @@ -91,7 +94,8 @@ public void modifyListingWithSeller_newSeller_returnsModifiedListing() { assertEquals(newSeller, modifiedListing.getSeller(), "The modified listing should have BOB as the seller."); - assertNotEquals(newSeller, originalListing.getSeller(), "The original listing should still have ALICE as the seller."); + assertNotEquals(newSeller, originalListing.getSeller(), + "The original listing should still have ALICE as the seller."); assertEquals(originalListing.getBuyers(), modifiedListing.getBuyers(), "The buyers list should be unchanged."); assertEquals(originalListing.getAddress(), modifiedListing.getAddress(), "The address should be unchanged."); assertEquals(originalListing.getName(), modifiedListing.getName(), "The name should be unchanged."); From 38ece50010750a4749cb09bc1c771bd7756454d0 Mon Sep 17 00:00:00 2001 From: haozhen Date: Thu, 7 Nov 2024 02:20:40 +0800 Subject: [PATCH 3/3] Add JUnit tests Added JUnit test for AddListingCommandTest. --- .../AddListingCommandTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java index 82009fcce1a..8787e990592 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java @@ -340,4 +340,29 @@ public Person getPersonByName(Name name) { .orElse(null); } } + + @Test + public void execute_buyerNotInList_throwsCommandException() { + AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), + new HashSet<>(List.of(DANIEL.getName()))); + ModelStub modelStub = new ModelStubAcceptingListingAdded(); + modelStub.addPerson(ALICE); + assertThrows(CommandException.class, + Messages.MESSAGE_INVALID_PERSON_INPUT, () -> addListingCommand.execute(modelStub)); + } + + @Test + public void execute_buyerNotOfTypeBuyer_throwsCommandException() { + AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), + new HashSet<>(List.of(BENSON.getName()))); + ModelStub modelStub = new ModelStubAcceptingListingAdded(); + modelStub.addPerson(ALICE); + modelStub.addPerson(BENSON); + + assertThrows(CommandException.class, + AddListingCommand.MESSAGE_NOT_BUYER, () -> addListingCommand.execute(modelStub)); + } + }