From 787b3c4c7041dccf9d149f10e95437804917352b Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 13:14:08 +0800 Subject: [PATCH 01/20] Add UniversityCatalogue test --- .../model/UniversityCatalogueTest.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/test/java/seedu/address/model/UniversityCatalogueTest.java diff --git a/src/test/java/seedu/address/model/UniversityCatalogueTest.java b/src/test/java/seedu/address/model/UniversityCatalogueTest.java new file mode 100644 index 00000000000..ad62b2349f6 --- /dev/null +++ b/src/test/java/seedu/address/model/UniversityCatalogueTest.java @@ -0,0 +1,91 @@ +package seedu.address.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalObjects.NTU; +import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import seedu.address.model.university.University; +import seedu.address.model.university.exceptions.DuplicateUniversityException; +import seedu.address.testutil.UniversityBuilder; + +public class UniversityCatalogueTest { + private final UniversityCatalogue universityCatalogue = new UniversityCatalogue(); + + @Test + public void constructor() { + assertEquals(Collections.emptyList(), universityCatalogue.getUniversityList()); + } + + @Test + public void resetData_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> universityCatalogue.resetData(null)); + } + + @Test + public void resetData_withValidReadOnlyUniversityCatalogue_replacesData() { + UniversityCatalogue newData = getTypicalUniversityCatalogue(); + universityCatalogue.resetData(newData); + assertEquals(newData, universityCatalogue); + } + + @Test + public void resetData_withDuplicateUniversity_throwsDuplicateUniversityException() { + University editedNtu = + new UniversityBuilder(NTU).build(); + List newUniversities = Arrays.asList(NTU, editedNtu); + UniversityCatalogueTest.UniversityCatalogueStub newData = + new UniversityCatalogueTest.UniversityCatalogueStub(newUniversities); + + assertThrows(DuplicateUniversityException.class, () -> universityCatalogue.resetData(newData)); + } + @Test + public void hasUniversity_nullUniversity_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> universityCatalogue.hasUniversity(null)); + } + + @Test + public void hasUniversity_universityNotInUniversityCatalogue_returnsFalse() { + assertFalse(universityCatalogue.hasUniversity(NTU)); + } + + @Test + public void hasUniversity_universityInUniversityCatalogue_returnsTrue() { + universityCatalogue.addUniversity(NTU); + assertTrue(universityCatalogue.hasUniversity(NTU)); + } + + @Test + public void toStringMethod() { + String expected = UniversityCatalogue.class.getCanonicalName() + "{university=" + + universityCatalogue.getUniversityList() + "}"; + assertEquals(expected, universityCatalogue.toString()); + } + + /** + * A stub ReadOnlyUniversityCatalogue whose university list can violate interface constraints. + */ + private static class UniversityCatalogueStub implements ReadOnlyUniversityCatalogue { + private final ObservableList universities = FXCollections.observableArrayList(); + + UniversityCatalogueStub(Collection universityCollection) { + universities.setAll(universityCollection); + } + + @Override + public ObservableList getUniversityList() { + return universities; + } + } +} From 956057367eb0c86134ff0e060e361cc2d62be787 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 13:31:51 +0800 Subject: [PATCH 02/20] Add JsonAdaptedUniversity test --- .../storage/JsonAdaptedUniversity.java | 8 ++--- .../storage/JsonAdaptedUniversityTest.java | 35 +++++++++++++++++++ .../address/testutil/TypicalObjects.java | 1 + 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/test/java/seedu/address/storage/JsonAdaptedUniversityTest.java diff --git a/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java b/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java index 05fb7cdd961..d54918f1259 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java @@ -4,13 +4,11 @@ import com.fasterxml.jackson.annotation.JsonProperty; import seedu.address.commons.exceptions.IllegalValueException; -import seedu.address.model.localcourse.LocalCourse; -import seedu.address.model.localcourse.LocalName; import seedu.address.model.university.University; import seedu.address.model.university.UniversityName; /** - * Jackson-friendly version of {@link LocalCourse}. + * Jackson-friendly version of {@link University}. */ class JsonAdaptedUniversity { @@ -20,7 +18,7 @@ class JsonAdaptedUniversity { /** - * Constructs a {@code JsonAdaptedUniversity} with the given localCourse details. + * Constructs a {@code JsonAdaptedUniversity} with the given university details. */ @JsonCreator public JsonAdaptedUniversity(@JsonProperty("university") String universityName) { @@ -46,7 +44,7 @@ public University toModelType() throws IllegalValueException { UniversityName.class.getSimpleName())); } if (!UniversityName.isValidUniversityName(universityName)) { - throw new IllegalValueException(LocalName.MESSAGE_CONSTRAINTS); + throw new IllegalValueException(UniversityName.MESSAGE_CONSTRAINTS); } final UniversityName modelUniversityName = new UniversityName(universityName); diff --git a/src/test/java/seedu/address/storage/JsonAdaptedUniversityTest.java b/src/test/java/seedu/address/storage/JsonAdaptedUniversityTest.java new file mode 100644 index 00000000000..e2917454068 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonAdaptedUniversityTest.java @@ -0,0 +1,35 @@ +package seedu.address.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.storage.JsonAdaptedUniversity.MISSING_FIELD_MESSAGE_FORMAT; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalObjects.INVALID_UNIVERSITY_NAME; +import static seedu.address.testutil.TypicalObjects.NTU; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.model.university.UniversityName; + +public class JsonAdaptedUniversityTest { + @Test + public void toModelType_validUniversityName_returnsUniversity() throws Exception { + JsonAdaptedUniversity university = new JsonAdaptedUniversity(NTU); + assertEquals(NTU, university.toModelType()); + } + + @Test + public void toModelType_invalidUniversityName_throwsIllegalValueException() { + JsonAdaptedUniversity university = + new JsonAdaptedUniversity(INVALID_UNIVERSITY_NAME); + String expectedMessage = UniversityName.MESSAGE_CONSTRAINTS; + assertThrows(IllegalValueException.class, expectedMessage, university::toModelType); + } + + @Test + public void toModelType_nullUniversity_throwsIllegalValueException() { + JsonAdaptedUniversity university = new JsonAdaptedUniversity((String) null); + String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, UniversityName.class.getSimpleName()); + assertThrows(IllegalValueException.class, expectedMessage, university::toModelType); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index 716a3fa2d50..0af1ba1027e 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -42,6 +42,7 @@ public class TypicalObjects { public static final String INVALID_PARTNER_COURSE_CODE = "$H23Y1"; public static final String INVALID_PARTNER_COURSE_NAME = " "; public static final String EDGE_CASE_VALID_UNIVERSITY_NAME = " School"; + public static final String INVALID_UNIVERSITY_NAME = " "; public static final LocalCourse CS2040S = new LocalCourseBuilder().withLocalCode("CS2040S") .withLocalName("Data Structures & Algorithms").build(); From 6db39670494f5a5c96892869b2cd168830583a6d Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 14:06:22 +0800 Subject: [PATCH 03/20] Add json catalogue tests for partner course and university --- .../address/model/UniversityCatalogue.java | 3 + ...invalidAndValidPartnerCourseCatalogue.json | 14 ++ .../invalidPartnerCourseCatalogue.json | 9 ++ .../notJsonFormatPartnerCourseCatalogue.json | 1 + .../invalidAndValidUniversityCatalogue.json | 7 + .../invalidUniversityCatalogue.json | 5 + .../notJsonFormatUniversityCatalogue.json | 1 + .../JsonLocalCourseCatalogueStorageTest.java | 2 +- .../JsonPartnerCourseCatalogueTest.java | 130 ++++++++++++++++++ .../JsonUniversityCatalogueStorageTest.java | 129 +++++++++++++++++ .../address/testutil/TypicalObjects.java | 6 + 11 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json create mode 100644 src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json create mode 100644 src/test/data/JsonPartnerCourseCatalogueStorageTest/notJsonFormatPartnerCourseCatalogue.json create mode 100644 src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json create mode 100644 src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json create mode 100644 src/test/data/JsonUniversityCatalogueStorageTest/notJsonFormatUniversityCatalogue.json create mode 100644 src/test/java/seedu/address/storage/JsonPartnerCourseCatalogueTest.java create mode 100644 src/test/java/seedu/address/storage/JsonUniversityCatalogueStorageTest.java diff --git a/src/main/java/seedu/address/model/UniversityCatalogue.java b/src/main/java/seedu/address/model/UniversityCatalogue.java index ee5b492a32c..16df0cec1eb 100644 --- a/src/main/java/seedu/address/model/UniversityCatalogue.java +++ b/src/main/java/seedu/address/model/UniversityCatalogue.java @@ -44,6 +44,9 @@ public boolean hasUniversity(University universityQuery) { public void addUniversity(University uN) { universities.add(uN); } + public void removeUniversity(University uN) { + universities.remove(uN); + } @Override public String toString() { diff --git a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json new file mode 100644 index 00000000000..356dbf29d34 --- /dev/null +++ b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json @@ -0,0 +1,14 @@ +{ + "partnerCourses" : [ + { + "universityName" : "SomeUni", + "partnerCode" : "COMP1000", + "partnerName" : "Introduction to Programming" + }, + { + "universityName" : " ", + "partnerCode" : "BAHBAHSHEEP!", + "partnerName" : " " + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json new file mode 100644 index 00000000000..be07f5a9840 --- /dev/null +++ b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json @@ -0,0 +1,9 @@ +{ + "partnerCourses": [ + { + "universityName" : " ", + "partnerCode" : "BAHBAHSHEEP!", + "partnerName" : " " + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonPartnerCourseCatalogueStorageTest/notJsonFormatPartnerCourseCatalogue.json b/src/test/data/JsonPartnerCourseCatalogueStorageTest/notJsonFormatPartnerCourseCatalogue.json new file mode 100644 index 00000000000..452ae4a6afe --- /dev/null +++ b/src/test/data/JsonPartnerCourseCatalogueStorageTest/notJsonFormatPartnerCourseCatalogue.json @@ -0,0 +1 @@ +hi! not fomatted correctly~ diff --git a/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json b/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json new file mode 100644 index 00000000000..efab01fe5ee --- /dev/null +++ b/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json @@ -0,0 +1,7 @@ +{ + "universities" : [ { + "universityName" : "Nanyang Technological University" + }, { + "universityName" : " " + } ] +} \ No newline at end of file diff --git a/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json b/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json new file mode 100644 index 00000000000..3b0448b9f49 --- /dev/null +++ b/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json @@ -0,0 +1,5 @@ +{ + "universities" : [ { + "universityName" : " " + } ] +} \ No newline at end of file diff --git a/src/test/data/JsonUniversityCatalogueStorageTest/notJsonFormatUniversityCatalogue.json b/src/test/data/JsonUniversityCatalogueStorageTest/notJsonFormatUniversityCatalogue.json new file mode 100644 index 00000000000..c47cc64a636 --- /dev/null +++ b/src/test/data/JsonUniversityCatalogueStorageTest/notJsonFormatUniversityCatalogue.json @@ -0,0 +1 @@ +not in correct json format! diff --git a/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java b/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java index a98beeca870..790fdfcb58e 100644 --- a/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java @@ -69,7 +69,7 @@ public void readLocalCourseCatalogue_invalidLocalCourseCatalogue_throwDataLoadin } @Test - public void readLocalCourseCatalogue_invalidAndValidPersonLocalCourseCatalogue_throwDataLoadingException() { + public void readLocalCourseCatalogue_invalidAndValidLocalCourseCatalogue_throwDataLoadingException() { assertThrows(DataLoadingException.class, () -> readLocalCourseCatalogue("invalidAndValidLocalCourseCatalogue.json")); } diff --git a/src/test/java/seedu/address/storage/JsonPartnerCourseCatalogueTest.java b/src/test/java/seedu/address/storage/JsonPartnerCourseCatalogueTest.java new file mode 100644 index 00000000000..42504084bf9 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonPartnerCourseCatalogueTest.java @@ -0,0 +1,130 @@ +package seedu.address.storage; + +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.TypicalObjects.COMP2000; +import static seedu.address.testutil.TypicalObjects.COMP3000; +import static seedu.address.testutil.TypicalObjects.COMP4000; +import static seedu.address.testutil.TypicalObjects.getTypicalPartnerCourseCatalogue; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import seedu.address.commons.exceptions.DataLoadingException; +import seedu.address.model.PartnerCourseCatalogue; +import seedu.address.model.ReadOnlyPartnerCourseCatalogue; + +public class JsonPartnerCourseCatalogueTest { + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", + "JsonPartnerCourseCatalogueStorageTest"); + + @TempDir + public Path testFolder; + + @Test + public void readPartnerCourseCatalogue_nullFilePath_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> readPartnerCourseCatalogue(null)); + } + + /** + * filePath would be the relative path to file in the TEST_DATA_FOLDER + */ + private Optional readPartnerCourseCatalogue(String filePath) throws Exception { + return new JsonPartnerCourseCatalogueStorage(Paths.get(filePath)) + .readPartnerCourseCatalogue(addToTestDataPathIfNotNull(filePath)); + } + + private Path addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) { + return prefsFileInTestDataFolder != null + ? TEST_DATA_FOLDER.resolve(prefsFileInTestDataFolder) + : null; + } + + @Test + public void read_missingFile_emptyResult() throws Exception { + assertFalse(readPartnerCourseCatalogue("NonExistentFile.json").isPresent()); + } + + @Test + public void read_notJsonFormat_exceptionThrown() { + assertThrows(DataLoadingException.class, () -> + readPartnerCourseCatalogue("notJsonFormatPartnerCourseCatalogue.json")); + } + + // DataLoadingException is thrown upon catching IllegalValueException, which can happen for + // a number of reasons: + // - Duplicate PartnerCourse + // - Missing PartnerCode / PartnerName + // - Invalid PartnerCode / PartnerName + @Test + public void readPartnerCourseCatalogue_invalidPartnerCourseCatalogue_throwDataLoadingException() { + assertThrows(DataLoadingException.class, () -> + readPartnerCourseCatalogue("invalidPartnerCourseCatalogue.json")); + } + + @Test + public void readPartnerCourseCatalogue_invalidAndValidPartnerCourseCatalogue_throwDataLoadingException() { + assertThrows(DataLoadingException.class, () -> + readPartnerCourseCatalogue("invalidAndValidPartnerCourseCatalogue.json")); + } + + @Test + public void readAndSavePartnerCourseCatalogue_allInOrder_success() throws Exception { + // In TempDir + Path filePath = testFolder.resolve("TempPartnerCourseCatalogue.json"); + + PartnerCourseCatalogue original = getTypicalPartnerCourseCatalogue(); + JsonPartnerCourseCatalogueStorage jsonPartnerCourseCatalogueStorage = + new JsonPartnerCourseCatalogueStorage(filePath); + + // Save in new file and read back + jsonPartnerCourseCatalogueStorage.savePartnerCourseCatalogue(original, filePath); + ReadOnlyPartnerCourseCatalogue readBack = + jsonPartnerCourseCatalogueStorage.readPartnerCourseCatalogue(filePath).get(); + assertEquals(original, new PartnerCourseCatalogue(readBack)); + + // Modify data, overwrite exiting file, and read back + original.addPartnerCourse(COMP3000); + original.removePartnerCourse(COMP2000); + jsonPartnerCourseCatalogueStorage.savePartnerCourseCatalogue(original, filePath); + readBack = jsonPartnerCourseCatalogueStorage.readPartnerCourseCatalogue(filePath).get(); + assertEquals(original, new PartnerCourseCatalogue(readBack)); + + // Save and read without specifying file path + original.addPartnerCourse(COMP4000); + jsonPartnerCourseCatalogueStorage.savePartnerCourseCatalogue(original); // file path not specified + readBack = jsonPartnerCourseCatalogueStorage.readPartnerCourseCatalogue().get(); // file path not specified + assertEquals(original, new PartnerCourseCatalogue(readBack)); + + } + + @Test + public void savePartnerCourseCatalogue_nullPartnerCourseCatalogue_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + savePartnerCourseCatalogue(null, "SomeFile.json")); + } + + /** + * Saves {@code partnerCourseCatalogue} at the specified {@code filePath}. + */ + private void savePartnerCourseCatalogue(ReadOnlyPartnerCourseCatalogue partnerCourseCatalogue, String filePath) { + try { + new JsonPartnerCourseCatalogueStorage(Paths.get(filePath)) + .savePartnerCourseCatalogue(partnerCourseCatalogue, addToTestDataPathIfNotNull(filePath)); + } catch (IOException ioe) { + throw new AssertionError("There should not be an error writing to the file.", ioe); + } + } + + @Test + public void savePartnerCourseCatalogue_nullFilePath_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + savePartnerCourseCatalogue(new PartnerCourseCatalogue(), null)); + } +} diff --git a/src/test/java/seedu/address/storage/JsonUniversityCatalogueStorageTest.java b/src/test/java/seedu/address/storage/JsonUniversityCatalogueStorageTest.java new file mode 100644 index 00000000000..e35c4e12b00 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonUniversityCatalogueStorageTest.java @@ -0,0 +1,129 @@ +package seedu.address.storage; + +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.TypicalObjects.SNU; +import static seedu.address.testutil.TypicalObjects.STANFORD; +import static seedu.address.testutil.TypicalObjects.WATERLOO; +import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import seedu.address.commons.exceptions.DataLoadingException; +import seedu.address.model.ReadOnlyUniversityCatalogue; +import seedu.address.model.UniversityCatalogue; + +public class JsonUniversityCatalogueStorageTest { + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", + "JsonUniversityCatalogueStorageTest"); + + @TempDir + public Path testFolder; + + @Test + public void readUniversityCatalogue_nullFilePath_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> readUniversityCatalogue(null)); + } + + /** + * filePath would be the relative path to file in the TEST_DATA_FOLDER + */ + private Optional readUniversityCatalogue(String filePath) throws Exception { + return new JsonUniversityCatalogueStorage(Paths.get(filePath)) + .readUniversityCatalogue(addToTestDataPathIfNotNull(filePath)); + } + + private Path addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) { + return prefsFileInTestDataFolder != null + ? TEST_DATA_FOLDER.resolve(prefsFileInTestDataFolder) + : null; + } + + @Test + public void read_missingFile_emptyResult() throws Exception { + assertFalse(readUniversityCatalogue("NonExistentFile.json").isPresent()); + } + + @Test + public void read_notJsonFormat_exceptionThrown() { + assertThrows(DataLoadingException.class, () -> + readUniversityCatalogue("notJsonFormatUniversityCatalogue.json")); + } + + // DataLoadingException is thrown upon catching IllegalValueException, which can happen for + // a number of reasons: + // - Duplicate University + // - Missing UniversityName + // - Invalid UniversityName + @Test + public void readUniversityCatalogue_invalidUniversityCatalogue_throwDataLoadingException() { + assertThrows(DataLoadingException.class, () -> + readUniversityCatalogue("invalidUniversityCatalogue.json")); + } + + @Test + public void readUniversityCatalogue_invalidAndValidUniversityCatalogue_throwDataLoadingException() { + assertThrows(DataLoadingException.class, () -> + readUniversityCatalogue("invalidAndValidUniversityCatalogue.json")); + } + + @Test + public void readAndSaveUniversityCatalogue_allInOrder_success() throws Exception { + // In TempDir + Path filePath = testFolder.resolve("TempUniversityCatalogue.json"); + + UniversityCatalogue original = getTypicalUniversityCatalogue(); + JsonUniversityCatalogueStorage jsonUniversityCatalogueStorage = new JsonUniversityCatalogueStorage(filePath); + + // Save in new file and read back + jsonUniversityCatalogueStorage.saveUniversityCatalogue(original, filePath); + ReadOnlyUniversityCatalogue readBack = + jsonUniversityCatalogueStorage.readUniversityCatalogue(filePath).get(); + assertEquals(original, new UniversityCatalogue(readBack)); + + // Modify data, overwrite exiting file, and read back + original.addUniversity(WATERLOO); + original.removeUniversity(STANFORD); + jsonUniversityCatalogueStorage.saveUniversityCatalogue(original, filePath); + readBack = jsonUniversityCatalogueStorage.readUniversityCatalogue(filePath).get(); + assertEquals(original, new UniversityCatalogue(readBack)); + + // Save and read without specifying file path + original.addUniversity(SNU); + jsonUniversityCatalogueStorage.saveUniversityCatalogue(original); // file path not specified + readBack = jsonUniversityCatalogueStorage.readUniversityCatalogue().get(); // file path not specified + assertEquals(original, new UniversityCatalogue(readBack)); + + } + + @Test + public void saveUniversityCatalogue_nullUniversityCatalogue_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + saveUniversityCatalogue(null, "SomeFile.json")); + } + + /** + * Saves {@code universityCatalogue} at the specified {@code filePath}. + */ + private void saveUniversityCatalogue(ReadOnlyUniversityCatalogue universityCatalogue, String filePath) { + try { + new JsonUniversityCatalogueStorage(Paths.get(filePath)) + .saveUniversityCatalogue(universityCatalogue, addToTestDataPathIfNotNull(filePath)); + } catch (IOException ioe) { + throw new AssertionError("There should not be an error writing to the file.", ioe); + } + } + + @Test + public void saveUniversityCatalogue_nullFilePath_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> + saveUniversityCatalogue(new UniversityCatalogue(), null)); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index 0af1ba1027e..d7390a9c1e0 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -79,10 +79,16 @@ public class TypicalObjects { .withPartnerUniversity(new UniversityName("University of Zurich")) .withPartnerCode("COMP3000").withPartnerName("Introduction to Networks").build(); + public static final PartnerCourse COMP4000 = new PartnerCourseBuilder() + .withPartnerUniversity(new UniversityName("Seoul National University")) + .withPartnerCode("COMP4000").withPartnerName("Introduction to Game Development").build(); + + // University public static final University NTU = new University(new UniversityName("Nanyang Technological University")); public static final University STANFORD = new University(new UniversityName("Stanford University")); public static final University WATERLOO = new University(new UniversityName("University of Waterloo")); + public static final University SNU = new University(new UniversityName("Seoul National University")); // Note public static final Note NOTE1 = new Note(new Content("Nanyang Technological University"), new Tag("university")); From aecb43c1bece9938bff202947ea66de44340d805 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 14:33:07 +0800 Subject: [PATCH 04/20] Add json serializable catalogue tests for partnercourse and university --- .../duplicatePartnerCourseInCatalogue.json | 14 ++++++ .../invalidPartnerCourseInCatalogue.json | 9 ++++ .../typicalPartnerCourseCatalogue.json | 11 ++++ .../duplicateUniversityInCatalogue.json | 10 ++++ .../invalidUniversityInCatalogue.json | 7 +++ .../typicalUniversityCatalogue.json | 7 +++ ...nSerializableLocalCourseCatalogueTest.java | 2 +- ...erializablePartnerCourseCatalogueTest.java | 50 +++++++++++++++++++ ...onSerializableUniversityCatalogueTest.java | 49 ++++++++++++++++++ 9 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json create mode 100644 src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json create mode 100644 src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json create mode 100644 src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json create mode 100644 src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json create mode 100644 src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json create mode 100644 src/test/java/seedu/address/storage/JsonSerializablePartnerCourseCatalogueTest.java create mode 100644 src/test/java/seedu/address/storage/JsonSerializableUniversityCatalogueTest.java diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json new file mode 100644 index 00000000000..2a9970a4bb7 --- /dev/null +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json @@ -0,0 +1,14 @@ +{ + "partnerCourses" : [ + { + "universityName" : "University of Leeds", + "partnerCode" : "COMP2000", + "partnerName" : "Introduction to Databases" + }, + { + "universityName" : "University of Leeds", + "partnerCode" : "COMP2000", + "partnerName" : "Introduction to Databases" + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json new file mode 100644 index 00000000000..be07f5a9840 --- /dev/null +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json @@ -0,0 +1,9 @@ +{ + "partnerCourses": [ + { + "universityName" : " ", + "partnerCode" : "BAHBAHSHEEP!", + "partnerName" : " " + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json new file mode 100644 index 00000000000..02de17248c5 --- /dev/null +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json @@ -0,0 +1,11 @@ +{ + "partnerCourses" : [ { + "universityName" : "University of Edinburgh", + "partnerCode" : "COMP1000", + "partnerName" : "Introduction to Programming" + }, { + "universityName" : "University of Leeds", + "partnerCode" : "COMP2000", + "partnerName" : "Introduction to Databases" + } ] +} \ No newline at end of file diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json new file mode 100644 index 00000000000..109267c9c63 --- /dev/null +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json @@ -0,0 +1,10 @@ +{ + "universities" : [ + { + "universityName" : "Nanyang Technological University" + }, + { + "universityName" : "Nanyang Technological University" + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json new file mode 100644 index 00000000000..3b11bc97393 --- /dev/null +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json @@ -0,0 +1,7 @@ +{ + "universities" : [ + { + "universityName" : " " + } + ] +} \ No newline at end of file diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json new file mode 100644 index 00000000000..aae3c1aebb6 --- /dev/null +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json @@ -0,0 +1,7 @@ +{ + "universities" : [ { + "universityName" : "Nanyang Technological University" + }, { + "universityName" : "Stanford University" + } ] +} \ No newline at end of file diff --git a/src/test/java/seedu/address/storage/JsonSerializableLocalCourseCatalogueTest.java b/src/test/java/seedu/address/storage/JsonSerializableLocalCourseCatalogueTest.java index 5d6196baf07..3119e457e8f 100644 --- a/src/test/java/seedu/address/storage/JsonSerializableLocalCourseCatalogueTest.java +++ b/src/test/java/seedu/address/storage/JsonSerializableLocalCourseCatalogueTest.java @@ -34,7 +34,7 @@ public void toModelType_typicalLocalCoursesFile_success() throws Exception { } @Test - public void toModelType_invalidLocalCourseCatalogueFile_throwsIllegalValueException() throws Exception { + public void toModelType_invalidLocalCoursesCatalogueFile_throwsIllegalValueException() throws Exception { JsonSerializableLocalCourseCatalogue dataFromFile = JsonUtil.readJsonFile(INVALID_LOCAL_COURSES_FILE, JsonSerializableLocalCourseCatalogue.class).get(); assertThrows(IllegalValueException.class, dataFromFile::toModelType); diff --git a/src/test/java/seedu/address/storage/JsonSerializablePartnerCourseCatalogueTest.java b/src/test/java/seedu/address/storage/JsonSerializablePartnerCourseCatalogueTest.java new file mode 100644 index 00000000000..1e414aba812 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonSerializablePartnerCourseCatalogueTest.java @@ -0,0 +1,50 @@ +package seedu.address.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalObjects.getTypicalPartnerCourseCatalogue; + +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.PartnerCourseCatalogue; + +public class JsonSerializablePartnerCourseCatalogueTest { + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", + "JsonSerializablePartnerCourseCatalogueTest"); + private static final Path TYPICAL_PARTNER_COURSES_FILE = + TEST_DATA_FOLDER.resolve("typicalPartnerCourseCatalogue.json"); + private static final Path INVALID_PARTNER_COURSES_FILE = TEST_DATA_FOLDER.resolve( + "invalidPartnerCourseInCatalogue.json"); + private static final Path DUPLICATE_PARTNER_COURSES_FILE = TEST_DATA_FOLDER.resolve( + "duplicatePartnerCourseInCatalogue.json"); + + @Test + public void toModelType_typicalPartnerCoursesFile_success() throws Exception { + JsonSerializablePartnerCourseCatalogue dataFromFile = JsonUtil.readJsonFile(TYPICAL_PARTNER_COURSES_FILE, + JsonSerializablePartnerCourseCatalogue.class).get(); + PartnerCourseCatalogue partnerCourseCatalogueFromFile = dataFromFile.toModelType(); + PartnerCourseCatalogue typicalPartnerCourseCatalogue = getTypicalPartnerCourseCatalogue(); + assertEquals(partnerCourseCatalogueFromFile, typicalPartnerCourseCatalogue); + } + + @Test + public void toModelType_invalidPartnerCoursesCatalogueFile_throwsIllegalValueException() throws Exception { + JsonSerializablePartnerCourseCatalogue dataFromFile = JsonUtil.readJsonFile(INVALID_PARTNER_COURSES_FILE, + JsonSerializablePartnerCourseCatalogue.class).get(); + assertThrows(IllegalValueException.class, dataFromFile::toModelType); + } + + @Test + public void toModelType_duplicatePartnerCourses_throwsIllegalValueException() throws Exception { + JsonSerializablePartnerCourseCatalogue dataFromFile = JsonUtil.readJsonFile(DUPLICATE_PARTNER_COURSES_FILE, + JsonSerializablePartnerCourseCatalogue.class).get(); + assertThrows(IllegalValueException.class, + JsonSerializablePartnerCourseCatalogue.MESSAGE_DUPLICATE_PARTNER_COURSE, + dataFromFile::toModelType); + } +} diff --git a/src/test/java/seedu/address/storage/JsonSerializableUniversityCatalogueTest.java b/src/test/java/seedu/address/storage/JsonSerializableUniversityCatalogueTest.java new file mode 100644 index 00000000000..7bbf1ae4851 --- /dev/null +++ b/src/test/java/seedu/address/storage/JsonSerializableUniversityCatalogueTest.java @@ -0,0 +1,49 @@ +package seedu.address.storage; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; + +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.UniversityCatalogue; + +public class JsonSerializableUniversityCatalogueTest { + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", + "JsonSerializableUniversityCatalogueTest"); + private static final Path TYPICAL_UNIVERSITIES_FILE = + TEST_DATA_FOLDER.resolve("typicalUniversityCatalogue.json"); + private static final Path INVALID_UNIVERSITIES_FILE = TEST_DATA_FOLDER.resolve( + "invalidUniversityInCatalogue.json"); + private static final Path DUPLICATE_UNIVERSITIES_FILE = TEST_DATA_FOLDER.resolve( + "duplicateUniversityInCatalogue.json"); + + @Test + public void toModelType_typicalUniversityFile_success() throws Exception { + JsonSerializableUniversityCatalogue dataFromFile = JsonUtil.readJsonFile(TYPICAL_UNIVERSITIES_FILE, + JsonSerializableUniversityCatalogue.class).get(); + UniversityCatalogue universityCatalogueFromFile = dataFromFile.toModelType(); + UniversityCatalogue typicalUniversityCatalogue = getTypicalUniversityCatalogue(); + assertEquals(universityCatalogueFromFile, typicalUniversityCatalogue); + } + + @Test + public void toModelType_invalidUniversitiesCatalogueFile_throwsIllegalValueException() throws Exception { + JsonSerializableUniversityCatalogue dataFromFile = JsonUtil.readJsonFile(INVALID_UNIVERSITIES_FILE, + JsonSerializableUniversityCatalogue.class).get(); + assertThrows(IllegalValueException.class, dataFromFile::toModelType); + } + + @Test + public void toModelType_duplicateUniversities_throwsIllegalValueException() throws Exception { + JsonSerializableUniversityCatalogue dataFromFile = JsonUtil.readJsonFile(DUPLICATE_UNIVERSITIES_FILE, + JsonSerializableUniversityCatalogue.class).get(); + assertThrows(IllegalValueException.class, JsonSerializableUniversityCatalogue.MESSAGE_DUPLICATE_UNIVERSITY, + dataFromFile::toModelType); + } +} From 2025c55b481c9354a4cefd4657e6b6981de97e54 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 14:40:42 +0800 Subject: [PATCH 05/20] Add partnercourse and university related tests in StoreManagerTest --- .../address/storage/StorageManagerTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/test/java/seedu/address/storage/StorageManagerTest.java b/src/test/java/seedu/address/storage/StorageManagerTest.java index 03624ae0f63..a25713e6a98 100644 --- a/src/test/java/seedu/address/storage/StorageManagerTest.java +++ b/src/test/java/seedu/address/storage/StorageManagerTest.java @@ -3,6 +3,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static seedu.address.testutil.TypicalObjects.getTypicalLocalCourseCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalPartnerCourseCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import java.nio.file.Path; @@ -14,8 +16,12 @@ import seedu.address.commons.core.GuiSettings; import seedu.address.model.AddressBook; import seedu.address.model.LocalCourseCatalogue; +import seedu.address.model.PartnerCourseCatalogue; import seedu.address.model.ReadOnlyAddressBook; import seedu.address.model.ReadOnlyLocalCourseCatalogue; +import seedu.address.model.ReadOnlyPartnerCourseCatalogue; +import seedu.address.model.ReadOnlyUniversityCatalogue; +import seedu.address.model.UniversityCatalogue; import seedu.address.model.UserPrefs; public class StorageManagerTest { @@ -94,4 +100,38 @@ public void getLocalCourseCatalogueFilePath() { assertNotNull(storageManager.getLocalCourseCatalogueFilePath()); } + @Test + public void partnerCourseCatalogueReadSave() throws Exception { + /* + * Note: This is an integration test that verifies the StorageManager is properly wired to the + * {@link JsonPartnerCourseCatalogueStorage} class. + */ + PartnerCourseCatalogue original = getTypicalPartnerCourseCatalogue(); + storageManager.savePartnerCourseCatalogue(original); + ReadOnlyPartnerCourseCatalogue retrieved = storageManager.readPartnerCourseCatalogue().get(); + assertEquals(original, new PartnerCourseCatalogue(retrieved)); + } + + @Test + public void getPartnerCourseCatalogueFilePath() { + assertNotNull(storageManager.getPartnerCourseCatalogueFilePath()); + } + + @Test + public void universityCatalogueReadSave() throws Exception { + /* + * Note: This is an integration test that verifies the StorageManager is properly wired to the + * {@link JsonUniversityCatalogueStorage} class. + */ + UniversityCatalogue original = getTypicalUniversityCatalogue(); + storageManager.saveUniversityCatalogue(original); + ReadOnlyUniversityCatalogue retrieved = storageManager.readUniversityCatalogue().get(); + assertEquals(original, new UniversityCatalogue(retrieved)); + } + + @Test + public void getUniversityCatalogueFilePath() { + assertNotNull(storageManager.getUniversityCatalogueFilePath()); + } + } From 687fb1077f62835810ee9700960a16ef81cbb298 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 14:59:13 +0800 Subject: [PATCH 06/20] Add partnercourse and university related tests in ParserUtilTest and SeplendidLogicManagerTest --- .../logic/SeplendidLogicManagerTest.java | 10 +++ .../address/logic/parser/ParserUtilTest.java | 82 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/test/java/seedu/address/logic/SeplendidLogicManagerTest.java b/src/test/java/seedu/address/logic/SeplendidLogicManagerTest.java index b48a166f4d3..e3a83ccae0e 100644 --- a/src/test/java/seedu/address/logic/SeplendidLogicManagerTest.java +++ b/src/test/java/seedu/address/logic/SeplendidLogicManagerTest.java @@ -105,6 +105,16 @@ public void getFilteredLocalCourseList_modifyList_throwsUnsupportedOperationExce assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredLocalCourseCatalogue().remove(0)); } + @Test + public void getFilteredPartnerCourseList_modifyList_throwsUnsupportedOperationException() { + assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredPartnerCourseCatalogue().remove(0)); + } + + @Test + public void getFilteredUniversityList_modifyList_throwsUnsupportedOperationException() { + assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredUniversityCatalogue().remove(0)); + } + /** * Executes the command and confirms that * - no exceptions are thrown
diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 2ec79e4cd66..365e453e60d 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -9,8 +9,14 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_OBJECT; import static seedu.address.testutil.TypicalObjects.INVALID_LOCAL_COURSE_CODE; import static seedu.address.testutil.TypicalObjects.INVALID_LOCAL_COURSE_NAME; +import static seedu.address.testutil.TypicalObjects.INVALID_PARTNER_COURSE_CODE; +import static seedu.address.testutil.TypicalObjects.INVALID_PARTNER_COURSE_NAME; +import static seedu.address.testutil.TypicalObjects.INVALID_UNIVERSITY_NAME; import static seedu.address.testutil.TypicalObjects.TYPICAL_LOCAL_COURSE_CODE; import static seedu.address.testutil.TypicalObjects.TYPICAL_LOCAL_COURSE_NAME; +import static seedu.address.testutil.TypicalObjects.TYPICAL_PARTNER_COURSE_CODE; +import static seedu.address.testutil.TypicalObjects.TYPICAL_PARTNER_COURSE_NAME; +import static seedu.address.testutil.TypicalObjects.TYPICAL_UNIVERSITY_NAME; import java.util.Arrays; import java.util.Collections; @@ -22,11 +28,14 @@ import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.localcourse.LocalCode; import seedu.address.model.localcourse.LocalName; +import seedu.address.model.partnercourse.PartnerCode; +import seedu.address.model.partnercourse.PartnerName; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; +import seedu.address.model.university.UniversityName; public class ParserUtilTest { private static final String INVALID_NAME = "R@chel"; @@ -202,6 +211,8 @@ public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception { assertEquals(expectedTagSet, actualTagSet); } + //localcourse + @Test public void parseLocalCode_null_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> ParserUtil.parseLocalCode((String) null)); @@ -248,6 +259,77 @@ public void parseLocalName_validValueWithWhitespace_returnsTrimmedLocalName() th assertEquals(expectedLocalName, ParserUtil.parseLocalName(localNameWithWhitespace)); } + //partner course + + @Test + public void parseUniversityName_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseUniversityName((String) null)); + } + + @Test + public void parseUniversityName_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseUniversityName(INVALID_UNIVERSITY_NAME)); + } + + @Test + public void parseUniversityName_validValueWithoutWhitespace_returnsUniversityName() throws Exception { + UniversityName expectedUniversityName = new UniversityName(TYPICAL_UNIVERSITY_NAME); + assertEquals(expectedUniversityName, ParserUtil.parseUniversityName(TYPICAL_UNIVERSITY_NAME)); + } + + @Test + public void parseUniversityName_validValueWithWhitespace_returnsTrimmedUniversityName() throws Exception { + String universityNameWithWhitespace = WHITESPACE + TYPICAL_UNIVERSITY_NAME + WHITESPACE; + UniversityName expectedUniversityName = new UniversityName(TYPICAL_UNIVERSITY_NAME); + assertEquals(expectedUniversityName, ParserUtil.parseUniversityName(universityNameWithWhitespace)); + } + + @Test + public void parsePartnerCode_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parsePartnerCode((String) null)); + } + + @Test + public void parsePartnerCode_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parsePartnerCode(INVALID_PARTNER_COURSE_CODE)); + } + + @Test + public void parsePartnerCode_validValueWithoutWhitespace_returnsPartnerCode() throws Exception { + PartnerCode expectedPartnerCode = new PartnerCode(TYPICAL_PARTNER_COURSE_CODE); + assertEquals(expectedPartnerCode, ParserUtil.parsePartnerCode(TYPICAL_PARTNER_COURSE_CODE)); + } + + @Test + public void parsePartnerCode_validValueWithWhitespace_returnsTrimmedPartnerCode() throws Exception { + String partnerCodeWithWhitespace = WHITESPACE + TYPICAL_PARTNER_COURSE_CODE + WHITESPACE; + PartnerCode expectedPartnerCode = new PartnerCode(TYPICAL_PARTNER_COURSE_CODE); + assertEquals(expectedPartnerCode, ParserUtil.parsePartnerCode(partnerCodeWithWhitespace)); + } + + @Test + public void parsePartnerName_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parsePartnerName((String) null)); + } + + @Test + public void parsePartnerName_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parsePartnerName(INVALID_PARTNER_COURSE_NAME)); + } + + @Test + public void parsePartnerName_validValueWithoutWhitespace_returnsPartnerName() throws Exception { + PartnerName expectedPartnerName = new PartnerName(TYPICAL_PARTNER_COURSE_NAME); + assertEquals(expectedPartnerName, ParserUtil.parsePartnerName(TYPICAL_PARTNER_COURSE_NAME)); + } + + @Test + public void parsePartnerName_validValueWithWhitespace_returnsTrimmedPartnerName() throws Exception { + String partnerNameWithWhitespace = WHITESPACE + TYPICAL_PARTNER_COURSE_NAME + WHITESPACE; + PartnerName expectedPartnerName = new PartnerName(TYPICAL_PARTNER_COURSE_NAME); + assertEquals(expectedPartnerName, ParserUtil.parsePartnerName(partnerNameWithWhitespace)); + } + @Test public void areValuesEnclosedAndNonEmpty_emptyArgs_returnsFalse() throws Exception { assertFalse(areValuesEnclosedAndNonEmpty("")); From e32771881d41efa7dd20f57ce25acd1f247b7730 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 16:02:16 +0800 Subject: [PATCH 07/20] Add partnercourse and university related tests in SeplendidParserTest Fix toString() for university in partnercourse --- .../address/logic/parser/ParserUtil.java | 2 + .../parser/PartnerCourseAddCommandParser.java | 7 ++-- .../model/partnercourse/PartnerCourse.java | 2 +- .../address/model/university/University.java | 5 +++ .../logic/parser/SeplendidParserTest.java | 37 ++++++++++++++++++- .../address/testutil/PartnerCourseUtil.java | 1 + 6 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index d6a9cb72c94..c365e0c558e 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -20,6 +20,7 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; +import seedu.address.model.university.University; import seedu.address.model.university.UniversityName; /** @@ -202,6 +203,7 @@ public static UniversityName parseUniversityName(String universityName) throws P } return new UniversityName(universityName); } + /** * Parses a {@code String content}. * Leading and trailing whitespaces will be trimmed. diff --git a/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java b/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java index f4759d7ab9f..be4227a9bb2 100644 --- a/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java @@ -11,6 +11,7 @@ import seedu.address.model.partnercourse.PartnerCourse; import seedu.address.model.partnercourse.PartnerName; import seedu.address.model.university.University; +import seedu.address.model.university.UniversityName; /** * Parses input arguments and creates a new PartnerCourseCommand object. @@ -42,12 +43,12 @@ public PartnerCourseAddCommand parse(String args) throws ParseException { } - University university = new University(ParserUtil.parseUniversityName(parameterToArgMap - .getValue(PARAMETER_UNIVERSITY).get())); + UniversityName universityName = ParserUtil.parseUniversityName(parameterToArgMap + .getValue(PARAMETER_UNIVERSITY).get()); PartnerCode partnerCode = ParserUtil.parsePartnerCode(parameterToArgMap.getValue(PARAMETER_PARTNERCODE).get()); PartnerName partnerName = ParserUtil.parsePartnerName(parameterToArgMap.getValue(PARAMETER_PARTNERNAME).get()); - PartnerCourse partnerCourse = new PartnerCourse(university, partnerCode, partnerName); + PartnerCourse partnerCourse = new PartnerCourse(new University(universityName), partnerCode, partnerName); return new PartnerCourseAddCommand(partnerCourse); } diff --git a/src/main/java/seedu/address/model/partnercourse/PartnerCourse.java b/src/main/java/seedu/address/model/partnercourse/PartnerCourse.java index 536766e5373..cd06bf7eaa5 100644 --- a/src/main/java/seedu/address/model/partnercourse/PartnerCourse.java +++ b/src/main/java/seedu/address/model/partnercourse/PartnerCourse.java @@ -82,7 +82,7 @@ public int hashCode() { @Override public String toString() { return new ToStringBuilder(this) - .add("partnerUniversity", partnerUniversity) + .add("partnerUniversity", partnerUniversity.getUniversityName().toString()) .add("partnerCode", partnerCode) .add("partnerName", partnerName) .toString(); diff --git a/src/main/java/seedu/address/model/university/University.java b/src/main/java/seedu/address/model/university/University.java index 07237d30bd0..f4f4434d498 100644 --- a/src/main/java/seedu/address/model/university/University.java +++ b/src/main/java/seedu/address/model/university/University.java @@ -52,4 +52,9 @@ public boolean equals(Object other) { University otherUniversity = (University) other; return universityName.equals(otherUniversity.universityName); } + + @Override + public String toString() { + return this.getUniversityName().toString(); + } } diff --git a/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java b/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java index 829b6f9860f..10ec4652956 100644 --- a/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java +++ b/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java @@ -10,10 +10,15 @@ import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.LocalCourseAddCommand; import seedu.address.logic.commands.LocalCourseDeleteCommand; +import seedu.address.logic.commands.PartnerCourseAddCommand; +import seedu.address.logic.commands.PartnerCourseDeleteCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.localcourse.LocalCourse; +import seedu.address.model.partnercourse.PartnerCourse; import seedu.address.testutil.LocalCourseBuilder; import seedu.address.testutil.LocalCourseUtil; +import seedu.address.testutil.PartnerCourseBuilder; +import seedu.address.testutil.PartnerCourseUtil; public class SeplendidParserTest { @@ -34,12 +39,30 @@ public void parseCommand_addLocalCourse() throws Exception { @Test public void parseCommand_deleteLocalCourse() throws Exception { LocalCourse localCourse = new LocalCourseBuilder().build(); - // This narrow typecast is safe as LocalCourseAddCommand is a known subtype of Command + // This narrow typecast is safe as LocalCourseDeleteCommand is a known subtype of Command LocalCourseDeleteCommand command = (LocalCourseDeleteCommand) parser .parseCommand(LocalCourseUtil.getLocalCourseDeleteCommandFrom(localCourse)); assertEquals(new LocalCourseDeleteCommand(localCourse.getLocalCode()), command); } + @Test + public void parseCommand_addPartnerCourse() throws Exception { + PartnerCourse partnerCourse = new PartnerCourseBuilder().build(); + // This narrow typecast is safe as PartnerCourseAddCommand is a known subtype of Command + PartnerCourseAddCommand command = (PartnerCourseAddCommand) parser + .parseCommand(PartnerCourseUtil.getPartnerCourseAddCommandFrom(partnerCourse)); + assertEquals(new PartnerCourseAddCommand(partnerCourse), command); + } + + @Test + public void parseCommand_deletePartnerCourse() throws Exception { + PartnerCourse partnerCourse = new PartnerCourseBuilder().build(); + // This narrow typecast is safe as PartnerCourseDeleteCommand is a known subtype of Command + PartnerCourseDeleteCommand command = (PartnerCourseDeleteCommand) parser + .parseCommand(PartnerCourseUtil.getPartnerCourseDeleteCommandFrom(partnerCourse)); + assertEquals(new PartnerCourseDeleteCommand(partnerCourse.getPartnerCode()), command); + } + // @Test // public void parseCommand_help() throws Exception { @@ -68,4 +91,16 @@ public void parseCommand_unknownLocalCourseCommand_throwsParseException() { -> parser.parseCommand("localcourse eject args")); } + @Test + public void parseCommand_unknownPartnerCourseCommand_throwsParseException() { + assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () + -> parser.parseCommand("partnercourse eject args")); + } + + @Test + public void parseCommand_unknownUniversityCommand_throwsParseException() { + assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () + -> parser.parseCommand("university eject args")); + } + } diff --git a/src/test/java/seedu/address/testutil/PartnerCourseUtil.java b/src/test/java/seedu/address/testutil/PartnerCourseUtil.java index 24c53cf8fcf..0a6cbba4f0f 100644 --- a/src/test/java/seedu/address/testutil/PartnerCourseUtil.java +++ b/src/test/java/seedu/address/testutil/PartnerCourseUtil.java @@ -24,6 +24,7 @@ public static String getPartnerCourseAddCommandFrom(PartnerCourse partnerCourse) */ public static String getPartnerCourseArgumentsForAddCommand(PartnerCourse partnerCourse) { StringBuilder sb = new StringBuilder("["); + sb.append(partnerCourse.getPartnerUniversity()).append("] ["); sb.append(partnerCourse.getPartnerCode()).append("] ["); sb.append(partnerCourse.getPartnerName()).append("]"); return sb.toString(); From 7ddf01ce99dfe8a86dbc23eed808ada0506a2b30 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Sun, 22 Oct 2023 16:09:08 +0800 Subject: [PATCH 08/20] Fix checkstyle --- src/main/java/seedu/address/logic/parser/ParserUtil.java | 1 - .../invalidAndValidPartnerCourseCatalogue.json | 2 +- .../invalidPartnerCourseCatalogue.json | 2 +- .../duplicatePartnerCourseInCatalogue.json | 2 +- .../invalidPartnerCourseInCatalogue.json | 2 +- .../typicalPartnerCourseCatalogue.json | 2 +- .../duplicateUniversityInCatalogue.json | 2 +- .../invalidUniversityInCatalogue.json | 2 +- .../typicalUniversityCatalogue.json | 2 +- .../invalidAndValidUniversityCatalogue.json | 2 +- .../invalidUniversityCatalogue.json | 2 +- 11 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index c365e0c558e..5632cfeeacd 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -20,7 +20,6 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; -import seedu.address.model.university.University; import seedu.address.model.university.UniversityName; /** diff --git a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json index 356dbf29d34..914256772e2 100644 --- a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json +++ b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidAndValidPartnerCourseCatalogue.json @@ -11,4 +11,4 @@ "partnerName" : " " } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json index be07f5a9840..51c8a26c6f7 100644 --- a/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json +++ b/src/test/data/JsonPartnerCourseCatalogueStorageTest/invalidPartnerCourseCatalogue.json @@ -6,4 +6,4 @@ "partnerName" : " " } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json index 2a9970a4bb7..1f0470eb93a 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json @@ -11,4 +11,4 @@ "partnerName" : "Introduction to Databases" } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json index be07f5a9840..51c8a26c6f7 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json @@ -6,4 +6,4 @@ "partnerName" : " " } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json index 02de17248c5..a2b93ea3245 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json @@ -8,4 +8,4 @@ "partnerCode" : "COMP2000", "partnerName" : "Introduction to Databases" } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json index 109267c9c63..c94990d07f7 100644 --- a/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/duplicateUniversityInCatalogue.json @@ -7,4 +7,4 @@ "universityName" : "Nanyang Technological University" } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json index 3b11bc97393..56f7a5a9c99 100644 --- a/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/invalidUniversityInCatalogue.json @@ -4,4 +4,4 @@ "universityName" : " " } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json b/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json index aae3c1aebb6..527308e536e 100644 --- a/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json +++ b/src/test/data/JsonSerializableUniversityCatalogueTest/typicalUniversityCatalogue.json @@ -4,4 +4,4 @@ }, { "universityName" : "Stanford University" } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json b/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json index efab01fe5ee..7eeadcfe83f 100644 --- a/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json +++ b/src/test/data/JsonUniversityCatalogueStorageTest/invalidAndValidUniversityCatalogue.json @@ -4,4 +4,4 @@ }, { "universityName" : " " } ] -} \ No newline at end of file +} diff --git a/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json b/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json index 3b0448b9f49..42c31a8fc31 100644 --- a/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json +++ b/src/test/data/JsonUniversityCatalogueStorageTest/invalidUniversityCatalogue.json @@ -2,4 +2,4 @@ "universities" : [ { "universityName" : " " } ] -} \ No newline at end of file +} From 20573d4d7eca111510a44989268f10db0f64d730 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Mon, 23 Oct 2023 11:08:38 +0800 Subject: [PATCH 09/20] Fix checkstyle --- .../seedu/address/model/partnercourse/PartnerCourseTest.java | 2 +- .../model/partnercourse/UniquePartnerCourseListTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/seedu/address/model/partnercourse/PartnerCourseTest.java b/src/test/java/seedu/address/model/partnercourse/PartnerCourseTest.java index 4a4a0c3cd71..82c01088cff 100644 --- a/src/test/java/seedu/address/model/partnercourse/PartnerCourseTest.java +++ b/src/test/java/seedu/address/model/partnercourse/PartnerCourseTest.java @@ -13,7 +13,7 @@ public class PartnerCourseTest { @Test - public void isSameLocalCourse() { + public void isSamePartnerCourse() { // same object -> returns true assertTrue(COMP1000.isSamePartnerCourse(COMP1000)); diff --git a/src/test/java/seedu/address/model/partnercourse/UniquePartnerCourseListTest.java b/src/test/java/seedu/address/model/partnercourse/UniquePartnerCourseListTest.java index 2061a1e19e1..6f8c91430b9 100644 --- a/src/test/java/seedu/address/model/partnercourse/UniquePartnerCourseListTest.java +++ b/src/test/java/seedu/address/model/partnercourse/UniquePartnerCourseListTest.java @@ -132,7 +132,7 @@ public void remove_existingPartnerCourse_removesPartnerCourse() { assertEquals(expectedUniquePartnerCourseList, uniquePartnerCourseList); } - // Tests setLocalCourses + // Tests setPartnerCourses @Test public void setPartnerCourses_nullUniquePartnerCourseList_throwsNullPointerException() { From 2a333c87ddf118191f5872f2a78be23ea08850a4 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Tue, 24 Oct 2023 14:14:31 +0800 Subject: [PATCH 10/20] Update ParserUtil --- .../address/logic/parser/ParserUtil.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 5632cfeeacd..1b5a8ffd69f 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -11,6 +11,7 @@ import seedu.address.commons.util.StringUtil; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.localcourse.LocalCode; +import seedu.address.model.localcourse.LocalCourseAttribute; import seedu.address.model.localcourse.LocalName; import seedu.address.model.notes.Content; import seedu.address.model.partnercourse.PartnerCode; @@ -271,4 +272,33 @@ public static boolean areValuesEnclosedAndNonEmpty(String args) { public static boolean areArgumentsPresent(SeplendidArgumentMap argumentMap, SeplendidParameter... parameters) { return Stream.of(parameters).allMatch(parameter -> argumentMap.getValue(parameter).isPresent()); } + + /** + * Parses a {@code String attribute}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code attribute} is invalid. + */ + public static LocalCourseAttribute parseLocalCourseAttribute(String attribute) throws ParseException { + requireNonNull(attribute); + String attributeLowerCase = attribute.toLowerCase(); + String resultAttribute = attributeLowerCase; + switch(attributeLowerCase) { + case("localcode"): + resultAttribute = "LOCALCODE"; + break; + case("localname"): + resultAttribute = "LOCALNAME"; + break; + default: + break; + } + + if (!LocalCourseAttribute.isValidAttribute(resultAttribute)) { + throw new ParseException(LocalCourseAttribute.MESSAGE_CONSTRAINTS); + } + + return LocalCourseAttribute.valueOf(resultAttribute); + + } } From a9653780b0ba13ec5d939e233eaf4ee15a718e19 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Tue, 24 Oct 2023 14:17:22 +0800 Subject: [PATCH 11/20] Add sort command for local course --- .../commands/LocalCourseSortCommand.java | 75 +++++++++++++++++++ .../seedu/address/logic/parser/CliSyntax.java | 1 + .../parser/LocalCourseAddCommandParser.java | 4 +- .../parser/LocalCourseSortCommandParser.java | 57 ++++++++++++++ .../address/logic/parser/SeplendidParser.java | 3 + .../seedu/address/model/SeplendidModel.java | 8 +- .../address/model/SeplendidModelManager.java | 14 ++++ .../localcourse/LocalCourseAttribute.java | 27 +++++++ .../LocalCourseComparatorByLocalCode.java | 28 +++++++ .../LocalCourseComparatorByLocalName.java | 24 ++++++ .../commands/LocalCourseAddCommandTest.java | 11 +++ .../LocalCourseDeleteCommandTest.java | 11 +++ .../commands/PartnerCourseAddCommandTest.java | 11 +++ .../PartnerCourseDeleteCommandTest.java | 11 +++ .../LocalCourseComparatorByLocalCodeTest.java | 19 +++++ .../LocalCourseComparatorByLocalNameTest.java | 20 +++++ 16 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java create mode 100644 src/main/java/seedu/address/model/localcourse/LocalCourseAttribute.java create mode 100644 src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java create mode 100644 src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java create mode 100644 src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCodeTest.java create mode 100644 src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalNameTest.java diff --git a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java new file mode 100644 index 00000000000..2b352f2daa3 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java @@ -0,0 +1,75 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import java.util.Comparator; + +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.SeplendidModel; +import seedu.address.model.localcourse.LocalCourse; +import seedu.address.seplendidui.UiUtil; + +/** + * Sorts local course list. + */ +public class LocalCourseSortCommand extends LocalCourseCommand { + public static final String LOCAL_COURSE_SORT_MESSAGE_USAGE = COMMAND_WORD + + " sort: Sorts all local courses by attributes"; + public static final String ACTION_WORD = "sort"; + + public static final String MESSAGE_SUCCESS = "Sorted all local courses"; + + private final Comparator comparator; + + /** + * Creates a LocalCourseSortCommand to sort the local course list. + * @param comparator Comparator used for sorting + */ + public LocalCourseSortCommand(Comparator comparator) { + this.comparator = comparator; + } + + /** + * TBD: This stub is to be removed after morphing is complete. + * + * @param model {@code Model} which the command should operate on. + * @return Nothing. + * @throws CommandException Always. + */ + @Override + public CommandResult execute(Model model) throws CommandException { + throw new CommandException("TBD: this is a stub and should be removed after morph."); + } + + @Override + public CommandResult execute(SeplendidModel seplendidModel) throws CommandException { + requireNonNull(seplendidModel); + seplendidModel.updatedSortedLocalList(comparator); + return new CommandResult(MESSAGE_SUCCESS, + UiUtil.ListViewModel.LOCAL_COURSE_LIST); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof LocalCourseSortCommand)) { + return false; + } + + LocalCourseSortCommand otherLocalCourseSortCommand = (LocalCourseSortCommand) other; + return comparator.equals(otherLocalCourseSortCommand.comparator); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("localCourseAttributeToSort", comparator) + .toString(); + } +} diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 3296fcff272..0da981edeb0 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -19,6 +19,7 @@ public class CliSyntax { public static final SeplendidParameter PARAMETER_PARTNERCODE = new SeplendidParameter("partnercode"); public static final SeplendidParameter PARAMETER_PARTNERNAME = new SeplendidParameter("partnername"); public static final SeplendidParameter PARAMETER_UNIVERSITY = new SeplendidParameter("university"); + public static final SeplendidParameter PARAMETER_ATTRIBUTE = new SeplendidParameter("attribute"); public static final SeplendidParameter PARAMETER_CONTENT = new SeplendidParameter("content"); public static final SeplendidParameter PARAMETER_TAGS = new SeplendidParameter("tags"); diff --git a/src/main/java/seedu/address/logic/parser/LocalCourseAddCommandParser.java b/src/main/java/seedu/address/logic/parser/LocalCourseAddCommandParser.java index 0afb44ebf91..58549a422e9 100644 --- a/src/main/java/seedu/address/logic/parser/LocalCourseAddCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/LocalCourseAddCommandParser.java @@ -12,13 +12,13 @@ import seedu.address.model.localcourse.LocalName; /** - * Parses input arguments and creates a new LocalCourse object. + * Parses input arguments and creates a new LocalCourseAddCommand object. */ public class LocalCourseAddCommandParser implements Parser { /** * Parses the given {@code String} of arguments in the context of the LocalCourseAddCommand - * and returns an LocalCourseAddCommand object for execution. + * and returns a LocalCourseAddCommand object for execution. * * @throws ParseException if the user input does not conform the expected format */ diff --git a/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java b/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java new file mode 100644 index 00000000000..800f43f16a0 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java @@ -0,0 +1,57 @@ +package seedu.address.logic.parser; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PARAMETER_ATTRIBUTE; +import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty; + +import java.util.Comparator; + +import seedu.address.logic.commands.LocalCourseSortCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.localcourse.LocalCourse; +import seedu.address.model.localcourse.LocalCourseAttribute; +import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalCode; +import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalName; + +/** + * Parses input arguments and creates a new LocalCourseSortCommand object. + */ +public class LocalCourseSortCommandParser implements Parser { + /** + * Pqrses the given {@code String} of arguments in the context of the LocalCourseSortCommand + * and returns a LocalCourseSortCommand object for execution. + * + * @throws ParseException if the user input does not conform the expected format. + */ + public LocalCourseSortCommand parse(String args) throws ParseException { + if (!areValuesEnclosedAndNonEmpty(args)) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + LocalCourseSortCommand.LOCAL_COURSE_SORT_MESSAGE_USAGE)); + } + + SeplendidArgumentMap parameterToArgMap = + SeplendidArgumentTokenizer.tokenize(args, PARAMETER_ATTRIBUTE); + + if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_ATTRIBUTE)) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + LocalCourseSortCommand.LOCAL_COURSE_SORT_MESSAGE_USAGE)); + } + + Comparator localCourseComparator = + parseLocalCourseComparator(parameterToArgMap.getValue(PARAMETER_ATTRIBUTE).get()); + + return new LocalCourseSortCommand(localCourseComparator); + } + + private Comparator parseLocalCourseComparator(String args) throws ParseException { + LocalCourseAttribute localCourseAttribute = ParserUtil.parseLocalCourseAttribute(args); + switch(localCourseAttribute) { + case LOCALCODE: + return new LocalCourseComparatorByLocalCode(); + case LOCALNAME: + return new LocalCourseComparatorByLocalName(); + default: + throw new ParseException(LocalCourseAttribute.MESSAGE_CONSTRAINTS); + } + } +} diff --git a/src/main/java/seedu/address/logic/parser/SeplendidParser.java b/src/main/java/seedu/address/logic/parser/SeplendidParser.java index 7e1d3a1ff45..39e61241fea 100644 --- a/src/main/java/seedu/address/logic/parser/SeplendidParser.java +++ b/src/main/java/seedu/address/logic/parser/SeplendidParser.java @@ -14,6 +14,7 @@ import seedu.address.logic.commands.LocalCourseCommand; import seedu.address.logic.commands.LocalCourseDeleteCommand; import seedu.address.logic.commands.LocalCourseListCommand; +import seedu.address.logic.commands.LocalCourseSortCommand; import seedu.address.logic.commands.NoteAddCommand; import seedu.address.logic.commands.NoteCommand; import seedu.address.logic.commands.PartnerCourseAddCommand; @@ -128,6 +129,8 @@ private LocalCourseCommand getLocalCourseCommandWithArg(String userInput, String return new LocalCourseAddCommandParser().parse(arguments); case LocalCourseDeleteCommand.ACTION_WORD: return new LocalCourseDeleteCommandParser().parse(arguments); + case LocalCourseSortCommand.ACTION_WORD: + return new LocalCourseSortCommandParser().parse(arguments); default: logger.finer("This user input caused a ParseException: " + userInput); throw new ParseException(MESSAGE_UNKNOWN_COMMAND); diff --git a/src/main/java/seedu/address/model/SeplendidModel.java b/src/main/java/seedu/address/model/SeplendidModel.java index d90ad2f2793..cd1f66cbe3a 100644 --- a/src/main/java/seedu/address/model/SeplendidModel.java +++ b/src/main/java/seedu/address/model/SeplendidModel.java @@ -1,6 +1,7 @@ package seedu.address.model; import java.nio.file.Path; +import java.util.Comparator; import java.util.Optional; import java.util.function.Predicate; @@ -102,7 +103,11 @@ public interface SeplendidModel { */ void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCourse); - //=========== FilteredLocalCourseList Accessors ============================================================= + //=========== Filtered/SortedLocalCourseList Accessors ============================================================= + + ObservableList getSortedLocalCourseList(); + + void updatedSortedLocalList(Comparator localCourseComparator); /** * Returns an unmodifiable view of the filtered local course list @@ -110,7 +115,6 @@ public interface SeplendidModel { ObservableList getFilteredLocalCourseList(); - /** * Updates the filter of the filtered person list to filter by the given {@code predicate}. * diff --git a/src/main/java/seedu/address/model/SeplendidModelManager.java b/src/main/java/seedu/address/model/SeplendidModelManager.java index b4c826dad38..8b252b9e4c4 100644 --- a/src/main/java/seedu/address/model/SeplendidModelManager.java +++ b/src/main/java/seedu/address/model/SeplendidModelManager.java @@ -4,12 +4,14 @@ import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import java.nio.file.Path; +import java.util.Comparator; import java.util.Optional; import java.util.function.Predicate; import java.util.logging.Logger; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; +import javafx.collections.transformation.SortedList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.SeplendidLogsCenter; import seedu.address.model.localcourse.LocalCode; @@ -29,6 +31,7 @@ public class SeplendidModelManager implements SeplendidModel { private final LocalCourseCatalogue localCourseCatalogue; private final UserPrefs userPrefs; private final FilteredList filteredLocalCourseCatalogue; + private final SortedList sortedLocalCourseCatalogue; private final UniversityCatalogue universityCatalogue; private final FilteredList filteredUniversityCatalogue; @@ -55,6 +58,7 @@ public SeplendidModelManager(ReadOnlyLocalCourseCatalogue localCourseCatalogue, this.localCourseCatalogue = new LocalCourseCatalogue(localCourseCatalogue); this.userPrefs = new UserPrefs(userPrefs); filteredLocalCourseCatalogue = new FilteredList<>(this.localCourseCatalogue.getLocalCourseList()); + sortedLocalCourseCatalogue = new SortedList<>(this.localCourseCatalogue.getLocalCourseList()); this.partnerCourseCatalogue = new PartnerCourseCatalogue(partnerCourseCatalogue); filteredPartnerCourseCatalogue = new FilteredList<>(this.partnerCourseCatalogue.getPartnerCourseList()); this.universityCatalogue = new UniversityCatalogue(universityCatalogue); @@ -168,6 +172,16 @@ public void setLocalCourse(LocalCourse target, LocalCourse editedLocalCourse) { localCourseCatalogue.setLocalCourse(target, editedLocalCourse); } + @Override + public ObservableList getSortedLocalCourseList() { + return sortedLocalCourseCatalogue; + } + + @Override + public void updatedSortedLocalList(Comparator localCourseComparator) { + sortedLocalCourseCatalogue.setComparator(localCourseComparator); + } + //=========== FilteredLocalCourseList Accessors ============================================================= /** diff --git a/src/main/java/seedu/address/model/localcourse/LocalCourseAttribute.java b/src/main/java/seedu/address/model/localcourse/LocalCourseAttribute.java new file mode 100644 index 00000000000..ba2790835a7 --- /dev/null +++ b/src/main/java/seedu/address/model/localcourse/LocalCourseAttribute.java @@ -0,0 +1,27 @@ +package seedu.address.model.localcourse; + +/** + * Represents the attribute which is related to local course. + */ +public enum LocalCourseAttribute { + LOCALCODE, + LOCALNAME; + + public static final String MESSAGE_CONSTRAINTS = + "There are only 2 attributes: localcode and localname."; + + /** + * Returns true if the given attribute is valid. + * @param test String to be tested + * @return true if attribute is valid. + */ + public static boolean isValidAttribute(String test) { + switch(test) { + case ("LOCALCODE"): + case ("LOCALNAME"): + return true; + default: + return false; + } + } +} diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java new file mode 100644 index 00000000000..4974686de92 --- /dev/null +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java @@ -0,0 +1,28 @@ +package seedu.address.model.localcourse.comparator; + +import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; + +import java.util.Comparator; + +import seedu.address.model.localcourse.LocalCode; +import seedu.address.model.localcourse.LocalCourse; + +/** + * Comparator class to compare two LocalCourses by LocalCode. + */ +public class LocalCourseComparatorByLocalCode implements Comparator { + + @Override + public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { + requireAllNonNull(localCourse, otherLocalCourse); + LocalCode localCode = localCourse.getLocalCode(); + LocalCode otherLocalCode = otherLocalCourse.getLocalCode(); + int diff = localCode.getValue().compareToIgnoreCase(otherLocalCode.getValue()); + return Integer.compare(diff, 0); + } + + @Override + public String toString() { + return "LocalCode"; + } +} diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java new file mode 100644 index 00000000000..bd9d8110764 --- /dev/null +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java @@ -0,0 +1,24 @@ +package seedu.address.model.localcourse.comparator; + +import java.util.Comparator; + +import seedu.address.model.localcourse.LocalCourse; +import seedu.address.model.localcourse.LocalName; + +/** + * Comparator class to compare two LocalCourses by LocalName. + */ +public class LocalCourseComparatorByLocalName implements Comparator { + @Override + public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { + LocalName localName = localCourse.getLocalName(); + LocalName otherLocalName = otherLocalCourse.getLocalName(); + int diff = localName.getValue().compareToIgnoreCase(otherLocalName.getValue()); + return Integer.compare(diff, 0); + } + + @Override + public String toString() { + return "LocalName"; + } +} diff --git a/src/test/java/seedu/address/logic/commands/LocalCourseAddCommandTest.java b/src/test/java/seedu/address/logic/commands/LocalCourseAddCommandTest.java index 47bfcd62279..18523481125 100644 --- a/src/test/java/seedu/address/logic/commands/LocalCourseAddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/LocalCourseAddCommandTest.java @@ -10,6 +10,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.Optional; import java.util.function.Predicate; @@ -167,6 +168,16 @@ public void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCours throw new AssertionError("This method should not be called."); } + @Override + public ObservableList getSortedLocalCourseList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updatedSortedLocalList(Comparator localCourseComparator) { + throw new AssertionError("This method should not be called."); + } + @Override public ObservableList getFilteredLocalCourseList() { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/commands/LocalCourseDeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/LocalCourseDeleteCommandTest.java index 484bf9bb7a2..5996c1ed5b0 100644 --- a/src/test/java/seedu/address/logic/commands/LocalCourseDeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/LocalCourseDeleteCommandTest.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.function.Predicate; @@ -170,6 +171,16 @@ public void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCours throw new AssertionError("This method should not be called."); } + @Override + public ObservableList getSortedLocalCourseList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updatedSortedLocalList(Comparator localCourseComparator) { + throw new AssertionError("This method should not be called."); + } + @Override public ObservableList getFilteredLocalCourseList() { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/commands/PartnerCourseAddCommandTest.java b/src/test/java/seedu/address/logic/commands/PartnerCourseAddCommandTest.java index 018d9a15add..4b5457e3d14 100644 --- a/src/test/java/seedu/address/logic/commands/PartnerCourseAddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/PartnerCourseAddCommandTest.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.Optional; import java.util.function.Predicate; @@ -160,6 +161,16 @@ public void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCours throw new AssertionError("This method should not be called."); } + @Override + public ObservableList getSortedLocalCourseList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updatedSortedLocalList(Comparator localCourseComparator) { + throw new AssertionError("This method should not be called."); + } + @Override public ObservableList getFilteredLocalCourseList() { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/commands/PartnerCourseDeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/PartnerCourseDeleteCommandTest.java index 0a8b7205ef3..086e341b5be 100644 --- a/src/test/java/seedu/address/logic/commands/PartnerCourseDeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/PartnerCourseDeleteCommandTest.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.function.Predicate; @@ -163,6 +164,16 @@ public void setLocalCourse(LocalCourse localCourse, LocalCourse editedLocalCours throw new AssertionError("This method should not be called."); } + @Override + public ObservableList getSortedLocalCourseList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updatedSortedLocalList(Comparator localCourseComparator) { + throw new AssertionError("This method should not be called."); + } + @Override public ObservableList getFilteredLocalCourseList() { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCodeTest.java b/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCodeTest.java new file mode 100644 index 00000000000..28af2ed93ce --- /dev/null +++ b/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCodeTest.java @@ -0,0 +1,19 @@ +package seedu.address.model.localcourse.comparator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.TypicalObjects.CS2030S; +import static seedu.address.testutil.TypicalObjects.MA2001; + +import org.junit.jupiter.api.Test; + + +public class LocalCourseComparatorByLocalCodeTest { + private LocalCourseComparatorByLocalCode localCourseComparatorByLocalCode = new LocalCourseComparatorByLocalCode(); + + @Test + public void compare() { + assertEquals(-1, localCourseComparatorByLocalCode.compare(CS2030S, MA2001)); + assertEquals(0, localCourseComparatorByLocalCode.compare(CS2030S, CS2030S)); + assertEquals(1, localCourseComparatorByLocalCode.compare(MA2001, CS2030S)); + } +} diff --git a/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalNameTest.java b/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalNameTest.java new file mode 100644 index 00000000000..a02bfe04804 --- /dev/null +++ b/src/test/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalNameTest.java @@ -0,0 +1,20 @@ +package seedu.address.model.localcourse.comparator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.TypicalObjects.CS2030S; +import static seedu.address.testutil.TypicalObjects.MA2001; + +import org.junit.jupiter.api.Test; + +public class LocalCourseComparatorByLocalNameTest { + private LocalCourseComparatorByLocalName localCourseComparatorByLocalName = new LocalCourseComparatorByLocalName(); + + @Test + public void compare() { + //MA2001 LocalName = Linear Algebra + //CS2030S LocalName = Programming Methodology II + assertEquals(-1, localCourseComparatorByLocalName.compare(MA2001, CS2030S)); + assertEquals(0, localCourseComparatorByLocalName.compare(CS2030S, CS2030S)); + assertEquals(1, localCourseComparatorByLocalName.compare(CS2030S, MA2001)); + } +} From 732d602aabd744dbe2a39dd454654a581b8d6c2a Mon Sep 17 00:00:00 2001 From: dlathyun Date: Tue, 24 Oct 2023 14:43:38 +0800 Subject: [PATCH 12/20] Add basic test for LocalCourseSort command --- .../commands/LocalCourseSortCommandTest.java | 108 ++++++++++++++++++ .../address/testutil/TypicalObjects.java | 3 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java diff --git a/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java b/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java new file mode 100644 index 00000000000..8c8e042f5ab --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java @@ -0,0 +1,108 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.assertSeplendidCommandSuccess; +import static seedu.address.testutil.TypicalObjects.CS1231S; +import static seedu.address.testutil.TypicalObjects.CS2040S; +import static seedu.address.testutil.TypicalObjects.CS3230; +import static seedu.address.testutil.TypicalObjects.MA2001; +import static seedu.address.testutil.TypicalObjects.getTypicalLocalCourseCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalNoteCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalPartnerCourseCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import seedu.address.model.SeplendidModel; +import seedu.address.model.SeplendidModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.localcourse.LocalCourse; +import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalCode; +import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalName; + +public class LocalCourseSortCommandTest { + private SeplendidModel model; + private SeplendidModel expectedModel; + + @BeforeEach + public void setUp() { + model = new SeplendidModelManager(getTypicalLocalCourseCatalogue(), new UserPrefs(), + getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue()); + expectedModel = new SeplendidModelManager(model.getLocalCourseCatalogue(), new UserPrefs(), + getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue()); + + } + + @Test + public void execute_sortByLocalCode_success() { + LocalCourseComparatorByLocalCode comparator = new LocalCourseComparatorByLocalCode(); + LocalCourseSortCommand localCourseSortCommand = new LocalCourseSortCommand(comparator); + expectedModel.updatedSortedLocalList(comparator); + + assertSeplendidCommandSuccess(localCourseSortCommand, model, + LocalCourseSortCommand.MESSAGE_SUCCESS, + expectedModel); + + List sortedList = new ArrayList<>(); + sortedList.add(CS1231S); + sortedList.add(CS2040S); + sortedList.add(CS3230); + sortedList.add(MA2001); + + assertEquals(sortedList, model.getSortedLocalCourseList()); + } + + @Test + public void execute_sortByLocalName_success() { + LocalCourseComparatorByLocalName comparator = new LocalCourseComparatorByLocalName(); + LocalCourseSortCommand localCourseSortCommand = new LocalCourseSortCommand(comparator); + expectedModel.updatedSortedLocalList(comparator); + + assertSeplendidCommandSuccess(localCourseSortCommand, model, + LocalCourseSortCommand.MESSAGE_SUCCESS, + expectedModel); + + List sortedList = new ArrayList<>(); + sortedList.add(CS2040S); + sortedList.add(CS3230); + sortedList.add(CS1231S); + sortedList.add(MA2001); + + assertEquals(sortedList, model.getSortedLocalCourseList()); + } + + @Test + public void equals() { + Comparator localCourseComparatorByLocalCode = new LocalCourseComparatorByLocalCode(); + Comparator localCourseComparatorByLocalName = new LocalCourseComparatorByLocalName(); + + LocalCourseSortCommand localCourseSortCommandByLocalCode = + new LocalCourseSortCommand(localCourseComparatorByLocalCode); + LocalCourseSortCommand localCourseSortCommandByLocalName = + new LocalCourseSortCommand(localCourseComparatorByLocalName); + + // same object -> returns true + assertTrue(localCourseSortCommandByLocalCode.equals(localCourseSortCommandByLocalCode)); + + // same values -> returns true + LocalCourseSortCommand localCourseSortCommandByLocalCodeCopy = + new LocalCourseSortCommand(localCourseComparatorByLocalCode); + assertTrue(localCourseSortCommandByLocalCode.equals(localCourseSortCommandByLocalCodeCopy)); + + // different types -> returns false + assertFalse(localCourseSortCommandByLocalCode.equals(1)); + + // null -> returns false + assertFalse(localCourseSortCommandByLocalCode.equals(null)); + + // different task -> returns false + assertFalse(localCourseSortCommandByLocalCode.equals(localCourseSortCommandByLocalName)); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index d7390a9c1e0..85974f8b731 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -142,7 +142,8 @@ public static NoteCatalogue getTypicalNoteCatalogue() { } public static List getTypicalLocalCourses() { - return new ArrayList<>(Arrays.asList(CS2040S, CS3230)); + + return new ArrayList<>(Arrays.asList(CS2040S, MA2001, CS3230, CS1231S)); } public static List getTypicalPartnerCourses() { From 946231499c7f594b594d94cbd6a8520ad99aff3b Mon Sep 17 00:00:00 2001 From: dlathyun Date: Tue, 24 Oct 2023 15:11:28 +0800 Subject: [PATCH 13/20] Fix tests --- .../logic/parser/PartnerCourseAddCommandParser.java | 1 - .../seedu/address/model/university/University.java | 5 ----- .../seedu/address/storage/JsonAdaptedUniversity.java | 1 - .../typicalLocalCourseCatalogue.json | 10 ++++++++++ .../duplicatePartnerCourseInCatalogue.json | 6 ++++-- .../invalidPartnerCourseInCatalogue.json | 3 ++- .../typicalPartnerCourseCatalogue.json | 6 ++++-- .../storage/JsonLocalCourseCatalogueStorageTest.java | 4 ++-- .../seedu/address/testutil/PartnerCourseBuilder.java | 2 +- .../java/seedu/address/testutil/PartnerCourseUtil.java | 2 +- .../java/seedu/address/testutil/TypicalObjects.java | 7 ++++++- 11 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java b/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java index 485fe5015a9..40e177519de 100644 --- a/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/PartnerCourseAddCommandParser.java @@ -13,7 +13,6 @@ import seedu.address.model.partnercourse.PartnerName; import seedu.address.model.partnercourse.PartnerUnit; import seedu.address.model.university.University; -import seedu.address.model.university.UniversityName; /** * Parses input arguments and creates a new PartnerCourseCommand object. diff --git a/src/main/java/seedu/address/model/university/University.java b/src/main/java/seedu/address/model/university/University.java index cee3c4034ed..5e8839b2ed5 100644 --- a/src/main/java/seedu/address/model/university/University.java +++ b/src/main/java/seedu/address/model/university/University.java @@ -57,9 +57,4 @@ public boolean equals(Object other) { University otherUniversity = (University) other; return universityName.equals(otherUniversity.universityName); } - - @Override - public String toString() { - return this.getUniversityName().toString(); - } } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java b/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java index c7f1c07cd3a..75f5f610468 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedUniversity.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import seedu.address.commons.exceptions.IllegalValueException; -import seedu.address.model.localcourse.LocalCourse; import seedu.address.model.university.University; import seedu.address.model.university.UniversityName; diff --git a/src/test/data/JsonSerializableLocalCourseCatalogueTest/typicalLocalCourseCatalogue.json b/src/test/data/JsonSerializableLocalCourseCatalogueTest/typicalLocalCourseCatalogue.json index 7cc0b2d6c9f..5c55f461a09 100644 --- a/src/test/data/JsonSerializableLocalCourseCatalogueTest/typicalLocalCourseCatalogue.json +++ b/src/test/data/JsonSerializableLocalCourseCatalogueTest/typicalLocalCourseCatalogue.json @@ -6,10 +6,20 @@ "localName": "Data Structures & Algorithms", "localUnit": 4.0 }, + { + "localCode": "MA2001", + "localName": "Linear Algebra", + "localUnit": 4.0 + }, { "localCode": "CS3230", "localName": "Design & Analysis of Algorithms", "localUnit": 4.0 + }, + { + "localCode": "CS1231S", + "localName": "Discrete Structures", + "localUnit": 4.0 } ] } diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json index 1f0470eb93a..898bd237e61 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/duplicatePartnerCourseInCatalogue.json @@ -3,12 +3,14 @@ { "universityName" : "University of Leeds", "partnerCode" : "COMP2000", - "partnerName" : "Introduction to Databases" + "partnerName" : "Introduction to Databases", + "partnerUnit": 5.0 }, { "universityName" : "University of Leeds", "partnerCode" : "COMP2000", - "partnerName" : "Introduction to Databases" + "partnerName" : "Introduction to Databases", + "partnerUnit": 5.0 } ] } diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json index 51c8a26c6f7..6d9323cdd95 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/invalidPartnerCourseInCatalogue.json @@ -3,7 +3,8 @@ { "universityName" : " ", "partnerCode" : "BAHBAHSHEEP!", - "partnerName" : " " + "partnerName" : " ", + "partnerUnit": 5.0 } ] } diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json index a2b93ea3245..46c18743ee8 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json @@ -2,10 +2,12 @@ "partnerCourses" : [ { "universityName" : "University of Edinburgh", "partnerCode" : "COMP1000", - "partnerName" : "Introduction to Programming" + "partnerName" : "Introduction to Programming", + "partnerUnit": 5.0 }, { "universityName" : "University of Leeds", "partnerCode" : "COMP2000", - "partnerName" : "Introduction to Databases" + "partnerName" : "Introduction to Databases", + "partnerUnit": 5.0 } ] } diff --git a/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java b/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java index 790fdfcb58e..7cf61fd8b40 100644 --- a/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonLocalCourseCatalogueStorageTest.java @@ -3,9 +3,9 @@ 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.TypicalObjects.CS1231S; import static seedu.address.testutil.TypicalObjects.CS2030S; import static seedu.address.testutil.TypicalObjects.CS3230; +import static seedu.address.testutil.TypicalObjects.MA1521; import static seedu.address.testutil.TypicalObjects.getTypicalLocalCourseCatalogue; import java.io.IOException; @@ -89,7 +89,7 @@ public void readAndSaveLocalCourseCatalogue_allInOrder_success() throws Exceptio assertEquals(original, new LocalCourseCatalogue(readBack)); // Modify data, overwrite exiting file, and read back - original.addLocalCourse(CS1231S); + original.addLocalCourse(MA1521); original.removeLocalCourse(CS3230); jsonLocalCourseCatalogueStorage.saveLocalCourseCatalogue(original, filePath); readBack = jsonLocalCourseCatalogueStorage.readLocalCourseCatalogue(filePath).get(); diff --git a/src/test/java/seedu/address/testutil/PartnerCourseBuilder.java b/src/test/java/seedu/address/testutil/PartnerCourseBuilder.java index f8747eb338d..49ebdad1d21 100644 --- a/src/test/java/seedu/address/testutil/PartnerCourseBuilder.java +++ b/src/test/java/seedu/address/testutil/PartnerCourseBuilder.java @@ -36,9 +36,9 @@ public PartnerCourseBuilder() { * Initializes the PartnerCourseBuilder with the data of {@code partnerCourseToCopy}. */ public PartnerCourseBuilder(PartnerCourse partnerCourseToCopy) { + partnerUniversity = partnerCourseToCopy.getPartnerUniversity(); partnerCode = partnerCourseToCopy.getPartnerCode(); partnerName = partnerCourseToCopy.getPartnerName(); - partnerUniversity = partnerCourseToCopy.getPartnerUniversity(); partnerUnit = partnerCourseToCopy.getPartnerUnit(); } diff --git a/src/test/java/seedu/address/testutil/PartnerCourseUtil.java b/src/test/java/seedu/address/testutil/PartnerCourseUtil.java index 53a2691939f..92d85a9a1e2 100644 --- a/src/test/java/seedu/address/testutil/PartnerCourseUtil.java +++ b/src/test/java/seedu/address/testutil/PartnerCourseUtil.java @@ -23,9 +23,9 @@ public static String getPartnerCourseAddCommandFrom(PartnerCourse partnerCourse) */ public static String getPartnerCourseArgumentsForAddCommand(PartnerCourse partnerCourse) { return String.format("[%s] [%s] [%s] [%s]", + partnerCourse.getPartnerUniversity(), partnerCourse.getPartnerCode(), partnerCourse.getPartnerName(), - partnerCourse.getPartnerUniversity(), partnerCourse.getPartnerUnit()); } diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index 9cb1eaa146a..e308e801ea4 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -48,7 +48,6 @@ public class TypicalObjects { public static final String INVALID_PARTNER_COURSE_CODE = "$H23Y1"; public static final String INVALID_PARTNER_COURSE_NAME = " "; - public static final String EDGE_CASE_VALID_UNIVERSITY_NAME = " School"; public static final String INVALID_UNIVERSITY_NAME = " "; public static final Double INVALID_PARTNER_COURSE_UNIT = -1.0; @@ -78,6 +77,12 @@ public class TypicalObjects { .withLocalName("Linear Algebra") .withLocalUnit(4.0) .build(); + + public static final LocalCourse MA1521 = + new LocalCourseBuilder().withLocalCode("MA1521") + .withLocalName("Calculus in Computing") + .withLocalUnit(4.0) + .build(); public static final LocalCourse TYPICAL_LOCAL_COURSE = new LocalCourseBuilder().withLocalCode(TYPICAL_LOCAL_COURSE_CODE) .withLocalName(TYPICAL_LOCAL_COURSE_NAME) From eb2fe7b4064857d0a8ed0c1a916808eb136d76bd Mon Sep 17 00:00:00 2001 From: dlathyun Date: Tue, 24 Oct 2023 15:47:49 +0800 Subject: [PATCH 14/20] Edit LocalCourseSortCommandTest and typicalPartnerCourseCatalogue.json --- .../typicalPartnerCourseCatalogue.json | 10 ++++++++++ .../logic/commands/LocalCourseSortCommandTest.java | 7 +++++-- .../java/seedu/address/testutil/TypicalObjects.java | 6 +++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json index 46c18743ee8..ae3ff52c977 100644 --- a/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json +++ b/src/test/data/JsonSerializablePartnerCourseCatalogueTest/typicalPartnerCourseCatalogue.json @@ -9,5 +9,15 @@ "partnerCode" : "COMP2000", "partnerName" : "Introduction to Databases", "partnerUnit": 5.0 + }, { + "universityName" : "Nanyang Technological University", + "partnerCode" : "S0402SC", + "partnerName" : "Algorithms", + "partnerUnit": 3.0 + }, { + "universityName" : "Stanford University", + "partnerCode" : "STAN3230", + "partnerName" : "Design & Analysis of Algorithms", + "partnerUnit": 3.0 } ] } diff --git a/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java b/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java index 8c8e042f5ab..150f64ef715 100644 --- a/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/LocalCourseSortCommandTest.java @@ -9,6 +9,7 @@ import static seedu.address.testutil.TypicalObjects.CS3230; import static seedu.address.testutil.TypicalObjects.MA2001; import static seedu.address.testutil.TypicalObjects.getTypicalLocalCourseCatalogue; +import static seedu.address.testutil.TypicalObjects.getTypicalMappingCatalogue; import static seedu.address.testutil.TypicalObjects.getTypicalNoteCatalogue; import static seedu.address.testutil.TypicalObjects.getTypicalPartnerCourseCatalogue; import static seedu.address.testutil.TypicalObjects.getTypicalUniversityCatalogue; @@ -34,9 +35,11 @@ public class LocalCourseSortCommandTest { @BeforeEach public void setUp() { model = new SeplendidModelManager(getTypicalLocalCourseCatalogue(), new UserPrefs(), - getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue()); + getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue(), + getTypicalMappingCatalogue()); expectedModel = new SeplendidModelManager(model.getLocalCourseCatalogue(), new UserPrefs(), - getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue()); + getTypicalPartnerCourseCatalogue(), getTypicalUniversityCatalogue(), getTypicalNoteCatalogue(), + getTypicalMappingCatalogue()); } diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index 800d7cc44aa..a8c2dedc86f 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -131,10 +131,10 @@ public class TypicalObjects { public static final PartnerCourse S0402SC = new PartnerCourseBuilder() .withPartnerUniversity(new UniversityName("Nanyang Technological University")) - .withPartnerCode("S0402SC").withPartnerName("Algorithms").build(); + .withPartnerCode("S0402SC").withPartnerName("Algorithms").withPartnerUnit(3.0).build(); public static final PartnerCourse STAN3230 = new PartnerCourseBuilder() - .withPartnerUniversity(new UniversityName("Stanford University")) - .withPartnerCode("STAN3230").withPartnerName("Design & Analysis of Algorithms").build(); + .withPartnerUniversity(new UniversityName("Stanford University")).withPartnerCode("STAN3230") + .withPartnerName("Design & Analysis of Algorithms").withPartnerUnit(3.0).build(); public static final PartnerCourse COMP4000 = new PartnerCourseBuilder() .withPartnerUniversity(new UniversityName("Seoul National University")) From c2002e5166ac5a43e297e46be3e82bbfe6bd5cf9 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Wed, 25 Oct 2023 01:23:53 +0800 Subject: [PATCH 15/20] Add tests for LocalCourse sort command --- .../commands/LocalCourseSortCommand.java | 2 +- .../LocalCourseComparatorByLocalCode.java | 2 +- .../LocalCourseComparatorByLocalName.java | 2 +- .../JsonSerializableUniversityCatalogue.java | 7 +- .../storage/UniversityCatalogueStorage.java | 2 +- .../LocalCourseSortCommandParserTest.java | 78 +++++++++++++++++++ .../logic/parser/SeplendidParserTest.java | 13 ++++ .../localcourse/LocalCourseAttributeTest.java | 26 +++++++ .../address/testutil/LocalCourseUtil.java | 14 ++++ .../address/testutil/TypicalObjects.java | 1 + 10 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 src/test/java/seedu/address/logic/parser/LocalCourseSortCommandParserTest.java create mode 100644 src/test/java/seedu/address/model/localcourse/LocalCourseAttributeTest.java diff --git a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java index 2b352f2daa3..78a0c7d895d 100644 --- a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java +++ b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java @@ -63,7 +63,7 @@ public boolean equals(Object other) { } LocalCourseSortCommand otherLocalCourseSortCommand = (LocalCourseSortCommand) other; - return comparator.equals(otherLocalCourseSortCommand.comparator); + return comparator.toString().equals(otherLocalCourseSortCommand.comparator.toString()); } @Override diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java index 4974686de92..0d1121f1307 100644 --- a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java @@ -23,6 +23,6 @@ public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { @Override public String toString() { - return "LocalCode"; + return "localcode"; } } diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java index bd9d8110764..637f41e40ba 100644 --- a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java @@ -19,6 +19,6 @@ public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { @Override public String toString() { - return "LocalName"; + return "localname"; } } diff --git a/src/main/java/seedu/address/storage/JsonSerializableUniversityCatalogue.java b/src/main/java/seedu/address/storage/JsonSerializableUniversityCatalogue.java index 23d06641ca4..0de17c2478a 100644 --- a/src/main/java/seedu/address/storage/JsonSerializableUniversityCatalogue.java +++ b/src/main/java/seedu/address/storage/JsonSerializableUniversityCatalogue.java @@ -25,7 +25,7 @@ class JsonSerializableUniversityCatalogue { private final List universities = new ArrayList<>(); /** - * Constructs a {@code JsonSerializableLocalCourseCatalogue} with the given localCourses. + * Constructs a {@code JsonSerializableUniversityCourseCatalogue} with the given universities. */ @JsonCreator public JsonSerializableUniversityCatalogue(@JsonProperty("universities") List @@ -34,9 +34,10 @@ public JsonSerializableUniversityCatalogue(@JsonProperty("universities") List expectedComparator = new LocalCourseComparatorByLocalCode(); + + // add unnecessary whitespace + assertParseSuccess(parser, UNNCESSARY_WHITESPACE + + LocalCourseCommand.COMMAND_WORD + + UNNCESSARY_WHITESPACE + + LocalCourseSortCommand.ACTION_WORD + + UNNCESSARY_WHITESPACE + + getSquareBracketWrappedArgument("localcode") + + UNNCESSARY_WHITESPACE, + new LocalCourseSortCommand(expectedComparator)); + } + + @Test + void parse_argumentNotClosedOrEmpty_failure() { + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, + LocalCourseSortCommand.LOCAL_COURSE_SORT_MESSAGE_USAGE); + + // missing open bracket + assertParseFailure(parser, commandActionWord + + LocalCourseUtil.getLocalCourseArgumentsForSortCommand(new LocalCourseComparatorByLocalCode()) + .substring(1), expectedMessage); + + // empty argument + assertParseFailure(parser, commandActionWord + + getSquareBracketWrappedArgument(""), expectedMessage); + } + + @Test + public void parse_invalidValue_failure() { + // invalid attribute + assertParseFailure(parser, commandActionWord + + getSquareBracketWrappedArgument(INVALID_LOCAL_COURSE_ATTRIBUTE), + LocalCourseAttribute.MESSAGE_CONSTRAINTS); + } + + @Test + public void parse_validArguments_success() { + assertParseSuccess(parser, commandActionWord + + getSquareBracketWrappedArgument(LocalCourseAttribute.LOCALCODE.toString()), + new LocalCourseSortCommand(new LocalCourseComparatorByLocalCode())); + + assertParseSuccess(parser, commandActionWord + + getSquareBracketWrappedArgument(LocalCourseAttribute.LOCALNAME.toString()), + new LocalCourseSortCommand(new LocalCourseComparatorByLocalName())); + + } +} diff --git a/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java b/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java index 10ec4652956..778641601fb 100644 --- a/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java +++ b/src/test/java/seedu/address/logic/parser/SeplendidParserTest.java @@ -5,15 +5,19 @@ import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND; import static seedu.address.testutil.Assert.assertThrows; +import java.util.Comparator; + import org.junit.jupiter.api.Test; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.LocalCourseAddCommand; import seedu.address.logic.commands.LocalCourseDeleteCommand; +import seedu.address.logic.commands.LocalCourseSortCommand; import seedu.address.logic.commands.PartnerCourseAddCommand; import seedu.address.logic.commands.PartnerCourseDeleteCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.localcourse.LocalCourse; +import seedu.address.model.localcourse.comparator.LocalCourseComparatorByLocalCode; import seedu.address.model.partnercourse.PartnerCourse; import seedu.address.testutil.LocalCourseBuilder; import seedu.address.testutil.LocalCourseUtil; @@ -45,6 +49,15 @@ public void parseCommand_deleteLocalCourse() throws Exception { assertEquals(new LocalCourseDeleteCommand(localCourse.getLocalCode()), command); } + @Test + public void parseCommand_sortLocalCourse() throws Exception { + Comparator localCourseComparator = new LocalCourseComparatorByLocalCode(); + + LocalCourseSortCommand command = (LocalCourseSortCommand) parser + .parseCommand(LocalCourseUtil.getLocalCourseSortCommandFrom(localCourseComparator)); + assertEquals(new LocalCourseSortCommand(localCourseComparator), command); + } + @Test public void parseCommand_addPartnerCourse() throws Exception { PartnerCourse partnerCourse = new PartnerCourseBuilder().build(); diff --git a/src/test/java/seedu/address/model/localcourse/LocalCourseAttributeTest.java b/src/test/java/seedu/address/model/localcourse/LocalCourseAttributeTest.java new file mode 100644 index 00000000000..b9a90dd2cb0 --- /dev/null +++ b/src/test/java/seedu/address/model/localcourse/LocalCourseAttributeTest.java @@ -0,0 +1,26 @@ +package seedu.address.model.localcourse; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +public class LocalCourseAttributeTest { + + @Test + public void isValidLocalCourseAttribute() { + // null String + assertThrows(NullPointerException.class, () -> LocalCourseAttribute.isValidAttribute((String) null)); + + // invalid localCourseAttribute + assertFalse(LocalCourseAttribute.isValidAttribute("")); // empty string + assertFalse(LocalCourseAttribute.isValidAttribute(" ")); // spaces only + assertFalse(LocalCourseAttribute.isValidAttribute("LOCAL")); + assertFalse(LocalCourseAttribute.isValidAttribute("localcode")); + + // valid localCourseAttribute + assertTrue(LocalCourseAttribute.isValidAttribute("LOCALCODE")); + assertTrue(LocalCourseAttribute.isValidAttribute("LOCALNAME")); + } +} diff --git a/src/test/java/seedu/address/testutil/LocalCourseUtil.java b/src/test/java/seedu/address/testutil/LocalCourseUtil.java index 4ed6aebecee..5bc733cfb3a 100644 --- a/src/test/java/seedu/address/testutil/LocalCourseUtil.java +++ b/src/test/java/seedu/address/testutil/LocalCourseUtil.java @@ -1,7 +1,10 @@ package seedu.address.testutil; +import java.util.Comparator; + import seedu.address.logic.commands.LocalCourseAddCommand; import seedu.address.logic.commands.LocalCourseDeleteCommand; +import seedu.address.logic.commands.LocalCourseSortCommand; import seedu.address.model.localcourse.LocalCourse; /** @@ -46,6 +49,17 @@ public static String getLocalCourseArgumentsForDeleteCommand(LocalCourse localCo return String.format("[%s]", localCourse.getLocalCode()); } + public static String getLocalCourseSortCommandFrom(Comparator localCourseComparator) { + return String.format("%s %s %s", + LocalCourseSortCommand.COMMAND_WORD, + LocalCourseSortCommand.ACTION_WORD, + getLocalCourseArgumentsForSortCommand(localCourseComparator)); + } + + public static String getLocalCourseArgumentsForSortCommand(Comparator comparator) { + return String.format("[%s]", comparator.toString()); + } + // TBD: modify for the purpose of seplendid // /** diff --git a/src/test/java/seedu/address/testutil/TypicalObjects.java b/src/test/java/seedu/address/testutil/TypicalObjects.java index a8c2dedc86f..1ec86eaf523 100644 --- a/src/test/java/seedu/address/testutil/TypicalObjects.java +++ b/src/test/java/seedu/address/testutil/TypicalObjects.java @@ -48,6 +48,7 @@ public class TypicalObjects { public static final String INVALID_LOCAL_COURSE_CODE = "$HOW2BECOMERICH"; public static final String INVALID_LOCAL_COURSE_NAME = " "; public static final Double INVALID_LOCAL_COURSE_UNIT = -1.0; + public static final String INVALID_LOCAL_COURSE_ATTRIBUTE = "partnercode"; public static final String INVALID_PARTNER_COURSE_CODE = "$H23Y1"; public static final String INVALID_PARTNER_COURSE_NAME = " "; From 9b1788c2663e15e45c93f1353e8681d0cfce4b0b Mon Sep 17 00:00:00 2001 From: dlathyun Date: Wed, 25 Oct 2023 01:42:57 +0800 Subject: [PATCH 16/20] Add Ui support for localcourse sort command --- src/main/java/seedu/address/logic/SeplendidLogic.java | 2 ++ src/main/java/seedu/address/logic/SeplendidLogicManager.java | 5 +++++ .../seedu/address/logic/commands/LocalCourseSortCommand.java | 4 ++-- src/main/java/seedu/address/seplendidui/MainWindow.java | 3 +++ src/main/java/seedu/address/seplendidui/UiUtil.java | 4 +++- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/SeplendidLogic.java b/src/main/java/seedu/address/logic/SeplendidLogic.java index 5ae6954e173..00131e04456 100644 --- a/src/main/java/seedu/address/logic/SeplendidLogic.java +++ b/src/main/java/seedu/address/logic/SeplendidLogic.java @@ -63,6 +63,8 @@ public interface SeplendidLogic { */ Path getLocalCourseCatalogueFilePath(); + ObservableList getSortedLocalCourseCatalogue(); + //=========== PartnerCourseCatalouge ============================================================================ ReadOnlyPartnerCourseCatalogue getPartnerCourseCatalogue(); diff --git a/src/main/java/seedu/address/logic/SeplendidLogicManager.java b/src/main/java/seedu/address/logic/SeplendidLogicManager.java index e9cc02602c8..55d71296518 100644 --- a/src/main/java/seedu/address/logic/SeplendidLogicManager.java +++ b/src/main/java/seedu/address/logic/SeplendidLogicManager.java @@ -107,6 +107,11 @@ public Path getLocalCourseCatalogueFilePath() { return model.getLocalCourseCatalogueFilePath(); } + @Override + public ObservableList getSortedLocalCourseCatalogue() { + return model.getSortedLocalCourseList(); + } + //=========== PartnerCourseCatalouge ============================================================================ @Override public ReadOnlyPartnerCourseCatalogue getPartnerCourseCatalogue() { diff --git a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java index 78a0c7d895d..5f6a9e5c029 100644 --- a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java +++ b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java @@ -16,7 +16,7 @@ */ public class LocalCourseSortCommand extends LocalCourseCommand { public static final String LOCAL_COURSE_SORT_MESSAGE_USAGE = COMMAND_WORD - + " sort: Sorts all local courses by attributes"; + + " sort: Sorts all local courses by attributes - localcode & localname"; public static final String ACTION_WORD = "sort"; public static final String MESSAGE_SUCCESS = "Sorted all local courses"; @@ -48,7 +48,7 @@ public CommandResult execute(SeplendidModel seplendidModel) throws CommandExcept requireNonNull(seplendidModel); seplendidModel.updatedSortedLocalList(comparator); return new CommandResult(MESSAGE_SUCCESS, - UiUtil.ListViewModel.LOCAL_COURSE_LIST); + UiUtil.ListViewModel.LOCAL_COURSE_SORT); } @Override diff --git a/src/main/java/seedu/address/seplendidui/MainWindow.java b/src/main/java/seedu/address/seplendidui/MainWindow.java index 8fce0e48755..9d821f49039 100644 --- a/src/main/java/seedu/address/seplendidui/MainWindow.java +++ b/src/main/java/seedu/address/seplendidui/MainWindow.java @@ -146,6 +146,9 @@ private CommandResult executeCommand(String commandText) throws CommandException case MAPPING_LIST: itemListPanel.setDisplayList(seplendidLogic.getFilteredMappingCatalogue()); break; + case LOCAL_COURSE_SORT: + itemListPanel.setDisplayList(seplendidLogic.getSortedLocalCourseCatalogue()); + break; default: // do nothing } diff --git a/src/main/java/seedu/address/seplendidui/UiUtil.java b/src/main/java/seedu/address/seplendidui/UiUtil.java index 3836d9862bf..8542376d5ae 100644 --- a/src/main/java/seedu/address/seplendidui/UiUtil.java +++ b/src/main/java/seedu/address/seplendidui/UiUtil.java @@ -13,7 +13,9 @@ public enum ListViewModel { PARTNER_COURSE_LIST, UNIVERSITY_LIST, NOTE_LIST, - MAPPING_LIST; + MAPPING_LIST, + + LOCAL_COURSE_SORT; @Override public String toString() { From a01a568d92cedd598d3e1bb048702315d3172564 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Wed, 25 Oct 2023 01:53:50 +0800 Subject: [PATCH 17/20] Fix checkstyle --- src/main/java/seedu/address/model/UniversityCatalogue.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/model/UniversityCatalogue.java b/src/main/java/seedu/address/model/UniversityCatalogue.java index dc898df7ccf..c83622a07e9 100644 --- a/src/main/java/seedu/address/model/UniversityCatalogue.java +++ b/src/main/java/seedu/address/model/UniversityCatalogue.java @@ -12,8 +12,8 @@ import seedu.address.model.university.UniversityName; /** - * Wraps universities data at the catalogue level - * Duplicates are not allowed (by .isSameUniversities comparison) + * Wraps universities data at the catalogue level. + * Duplicates are not allowed (by .isSameUniversities comparison). */ public class UniversityCatalogue implements ReadOnlyUniversityCatalogue { @@ -28,7 +28,7 @@ public UniversityCatalogue() { } /** - * Creates an UniversityCatalogue using the Universities in the {@code toBeCopied} + * Creates an UniversityCatalogue using the Universities in the {@code toBeCopied}. */ public UniversityCatalogue(ReadOnlyUniversityCatalogue toBeCopied) { this(); From 32b3b65defce22d333d4bf2aa676c098284d0ef0 Mon Sep 17 00:00:00 2001 From: dlathyun <122247324+dlathyun@users.noreply.github.com> Date: Wed, 25 Oct 2023 01:55:21 +0800 Subject: [PATCH 18/20] Fix checkstyle --- src/main/java/seedu/address/model/UniversityCatalogue.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/seedu/address/model/UniversityCatalogue.java b/src/main/java/seedu/address/model/UniversityCatalogue.java index e79e4511b15..60e9e5e7139 100644 --- a/src/main/java/seedu/address/model/UniversityCatalogue.java +++ b/src/main/java/seedu/address/model/UniversityCatalogue.java @@ -117,3 +117,5 @@ public int hashCode() { return universities.hashCode(); } } + + From a3c94f1421aeee77f1e8b3982a727bc667ff4af5 Mon Sep 17 00:00:00 2001 From: dlathyun Date: Wed, 25 Oct 2023 02:02:10 +0800 Subject: [PATCH 19/20] Fix checkstyle --- .../java/seedu/address/model/UniversityCatalogue.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/model/UniversityCatalogue.java b/src/main/java/seedu/address/model/UniversityCatalogue.java index ba28db15197..47dd5da9ecf 100644 --- a/src/main/java/seedu/address/model/UniversityCatalogue.java +++ b/src/main/java/seedu/address/model/UniversityCatalogue.java @@ -54,10 +54,14 @@ public Optional getUniversityIfExists(UniversityName universityNameQ return universities.getUniversityIfExists(universityNameQuery); } + /** + * Adds university to university list. + * @param uN university to add. + */ public void addUniversity(University uN) { universities.add(uN); } - + public void removeUniversity(University uN) { universities.remove(uN); } @@ -117,5 +121,3 @@ public int hashCode() { return universities.hashCode(); } } - - From 15770d9751a52374c89815130c1ee3815e987b1e Mon Sep 17 00:00:00 2001 From: dlathyun Date: Thu, 26 Oct 2023 23:15:48 +0800 Subject: [PATCH 20/20] Edit equals() method in localcourse comparators --- .../logic/commands/LocalCourseSortCommand.java | 2 +- .../parser/LocalCourseSortCommandParser.java | 2 +- .../address/model/SeplendidModelManager.java | 2 +- .../LocalCourseComparatorByLocalCode.java | 15 +++++++++++++++ .../LocalCourseComparatorByLocalName.java | 15 +++++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java index 5f6a9e5c029..90dd9a3359f 100644 --- a/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java +++ b/src/main/java/seedu/address/logic/commands/LocalCourseSortCommand.java @@ -63,7 +63,7 @@ public boolean equals(Object other) { } LocalCourseSortCommand otherLocalCourseSortCommand = (LocalCourseSortCommand) other; - return comparator.toString().equals(otherLocalCourseSortCommand.comparator.toString()); + return comparator.equals(otherLocalCourseSortCommand.comparator); } @Override diff --git a/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java b/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java index 800f43f16a0..d2b58950396 100644 --- a/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/LocalCourseSortCommandParser.java @@ -18,7 +18,7 @@ */ public class LocalCourseSortCommandParser implements Parser { /** - * Pqrses the given {@code String} of arguments in the context of the LocalCourseSortCommand + * Parses the given {@code String} of arguments in the context of the LocalCourseSortCommand * and returns a LocalCourseSortCommand object for execution. * * @throws ParseException if the user input does not conform the expected format. diff --git a/src/main/java/seedu/address/model/SeplendidModelManager.java b/src/main/java/seedu/address/model/SeplendidModelManager.java index b57b37f25a6..b7b5d66fe98 100644 --- a/src/main/java/seedu/address/model/SeplendidModelManager.java +++ b/src/main/java/seedu/address/model/SeplendidModelManager.java @@ -86,7 +86,7 @@ public SeplendidModelManager(ReadOnlyLocalCourseCatalogue localCourseCatalogue, } /** - * Contructs Seplendid Model Manager. + * Constructs Seplendid Model Manager. */ public SeplendidModelManager() { this(new LocalCourseCatalogue(), new UserPrefs(), new PartnerCourseCatalogue(), new UniversityCatalogue(), diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java index 0d1121f1307..c5545092c32 100644 --- a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalCode.java @@ -25,4 +25,19 @@ public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { public String toString() { return "localcode"; } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + if (!(other instanceof LocalCourseComparatorByLocalCode)) { + return false; + } + + LocalCourseComparatorByLocalCode otherLocalCourseComparatorByLocalCode = + (LocalCourseComparatorByLocalCode) other; + return this.toString().equals(otherLocalCourseComparatorByLocalCode.toString()); + } } diff --git a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java index 637f41e40ba..e8255d97232 100644 --- a/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java +++ b/src/main/java/seedu/address/model/localcourse/comparator/LocalCourseComparatorByLocalName.java @@ -21,4 +21,19 @@ public int compare(LocalCourse localCourse, LocalCourse otherLocalCourse) { public String toString() { return "localname"; } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + if (!(other instanceof LocalCourseComparatorByLocalName)) { + return false; + } + + LocalCourseComparatorByLocalName otherLocalCourseComparatorByLocalName = + (LocalCourseComparatorByLocalName) other; + return this.toString().equals(otherLocalCourseComparatorByLocalName.toString()); + } }