From 59fdafab4cfd22fe2ef53fc6e7ea9532c1c3af49 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 11:10:34 +0800 Subject: [PATCH 1/6] Add tagCategory property This is so that the JsonSerializableCampusConnect is able to convert the Tag to a JsonAdaptedTag that stores the category of the tag --- .../seedu/address/model/tag/TagCategory.java | 26 +++++++++++++++++++ .../seedu/address/storage/JsonAdaptedTag.java | 26 ++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/model/tag/TagCategory.java b/src/main/java/seedu/address/model/tag/TagCategory.java index 6b89b0d150d..da12b3358d3 100644 --- a/src/main/java/seedu/address/model/tag/TagCategory.java +++ b/src/main/java/seedu/address/model/tag/TagCategory.java @@ -16,6 +16,32 @@ public enum TagCategory { this.colorCode = colorCode; } + /** + * Returns TagCategory from a String. + * @return TagCategory represented by the String. + */ + public static TagCategory fromString(String category) { + switch (category) { + case "acads": + return ACADEMICS; + + case "activity": + return ACTIVITIES; + + case "networking": + return NETWORKING; + + case "mentor": + return MENTORSHIP; + + case "general": + return GENERAL; + + default: + return GENERAL; // sufficient since no other classes should call this method. + } + } + /** * Returns the colour code of the {@code TagCategory}. * @return String containing the hex code for the colour. diff --git a/src/main/java/seedu/address/storage/JsonAdaptedTag.java b/src/main/java/seedu/address/storage/JsonAdaptedTag.java index 0df22bdb754..4269e03e4a0 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedTag.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedTag.java @@ -1,10 +1,12 @@ package seedu.address.storage; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.model.tag.Tag; +import seedu.address.model.tag.TagCategory; /** * Jackson-friendly version of {@link Tag}. @@ -12,13 +14,16 @@ class JsonAdaptedTag { private final String tagName; + private final String tagCategory; /** - * Constructs a {@code JsonAdaptedTag} with the given {@code tagName}. + * Constructs a {@code JsonAdaptedTag} with the given {@code tagName} and {@code tagCategory}. */ @JsonCreator - public JsonAdaptedTag(String tagName) { + public JsonAdaptedTag(@JsonProperty("tagName") String tagName, + @JsonProperty("tagCategory") String tagCategory) { this.tagName = tagName; + this.tagCategory = tagCategory; } /** @@ -26,12 +31,18 @@ public JsonAdaptedTag(String tagName) { */ public JsonAdaptedTag(Tag source) { tagName = source.tagName; + tagCategory = source.getTagCategory().toString(); } - @JsonValue - public String getTagName() { - return tagName; - } +// @JsonValue +// public String getTagName() { +// return tagName; +// } +// +// @JsonValue +// public String getTagCategory() { +// return tagCategory; +// } /** * Converts this Jackson-friendly adapted tag object into the model's {@code Tag} object. @@ -42,7 +53,8 @@ public Tag toModelType() throws IllegalValueException { if (!Tag.isValidTagName(tagName)) { throw new IllegalValueException(Tag.MESSAGE_CONSTRAINTS); } - return new Tag(tagName); + TagCategory tagCat = TagCategory.fromString(tagCategory); // convert to TagCategory enum + return new Tag(tagName, tagCat); } } From 79f9b1a5d91d170bdbeee5bb88429df96a42a2b2 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 11:36:11 +0800 Subject: [PATCH 2/6] Fix unit test cases for Json functionality --- .../seedu/address/model/tag/TagCategory.java | 7 ++-- .../address/model/util/SampleDataUtil.java | 34 ++++++++++++++++--- .../seedu/address/storage/JsonAdaptedTag.java | 11 ------ .../storage/JsonAdaptedPersonTest.java | 18 ++++++++-- .../seedu/address/testutil/PersonBuilder.java | 11 ++++++ 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/main/java/seedu/address/model/tag/TagCategory.java b/src/main/java/seedu/address/model/tag/TagCategory.java index da12b3358d3..1fcb2360a8d 100644 --- a/src/main/java/seedu/address/model/tag/TagCategory.java +++ b/src/main/java/seedu/address/model/tag/TagCategory.java @@ -1,5 +1,7 @@ package seedu.address.model.tag; +import seedu.address.commons.exceptions.IllegalValueException; + /** * Provides categories to sort Tags into, as well as colour codes for display in the UI. */ @@ -10,6 +12,7 @@ public enum TagCategory { NETWORKING("#32CD32"), // Lime Green MENTORSHIP("#FF69B4"); // Hot Pink + private static final String INVALID_CATEGORY = "Error! %s is not a valid category!"; private final String colorCode; TagCategory(String colorCode) { @@ -20,7 +23,7 @@ public enum TagCategory { * Returns TagCategory from a String. * @return TagCategory represented by the String. */ - public static TagCategory fromString(String category) { + public static TagCategory fromString(String category) throws IllegalValueException { switch (category) { case "acads": return ACADEMICS; @@ -38,7 +41,7 @@ public static TagCategory fromString(String category) { return GENERAL; default: - return GENERAL; // sufficient since no other classes should call this method. + throw new IllegalValueException(String.format(INVALID_CATEGORY, category)); } } diff --git a/src/main/java/seedu/address/model/util/SampleDataUtil.java b/src/main/java/seedu/address/model/util/SampleDataUtil.java index 5840229f49b..137f931a01a 100644 --- a/src/main/java/seedu/address/model/util/SampleDataUtil.java +++ b/src/main/java/seedu/address/model/util/SampleDataUtil.java @@ -1,16 +1,18 @@ package seedu.address.model.util; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; -//import seedu.address.model.CampusConnect; -//import seedu.address.model.ReadOnlyCampusConnect; +import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.model.person.Email; import seedu.address.model.person.Name; import seedu.address.model.person.Person; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; +import seedu.address.model.tag.TagCategory; /** * Contains utility methods for populating {@code CampusConnect} with sample data. @@ -44,11 +46,33 @@ public static ReadOnlyCampusConnect getSampleCampusConnect() { }*/ /** - * Returns a tag set containing the list of strings given. + * Returns a tag set containing the list of name strings given. + * @param names Varargs array containing the names of the tags. + * @return Set containing the tags. */ - public static Set getTagSet(String... strings) { - return Arrays.stream(strings) + public static Set getTagSet(String... names) { + return Arrays.stream(names) .map(Tag::new) .collect(Collectors.toSet()); } + + /** + * Returns a tag set containing tags with the names and categories specified. + * @param names Array of names for the tags. + * @param categories Array of categories to be attached to the respective name. + * @exception IllegalValueException thrown when a tag category is not valid. + * @return Set containing the tags. + */ + public static Set getTagSet(String[] names, String[] categories) throws IllegalValueException { + List resultTags = new ArrayList<>(); + TagCategory tagCat; + + // assume both arrays same length, since this is a helper for test functions + // so no need to test for inconsistent length + for (int i = 0; i < names.length; i++) { + tagCat = TagCategory.fromString(categories[i]); + resultTags.add(new Tag(names[i], tagCat)); + } + return resultTags.stream().collect(Collectors.toSet()); + } } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedTag.java b/src/main/java/seedu/address/storage/JsonAdaptedTag.java index 4269e03e4a0..f7649d3b40c 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedTag.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedTag.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonValue; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.model.tag.Tag; @@ -34,16 +33,6 @@ public JsonAdaptedTag(Tag source) { tagCategory = source.getTagCategory().toString(); } -// @JsonValue -// public String getTagName() { -// return tagName; -// } -// -// @JsonValue -// public String getTagCategory() { -// return tagCategory; -// } - /** * Converts this Jackson-friendly adapted tag object into the model's {@code Tag} object. * diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index cd3e64e7bba..9ca525c7a20 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -20,11 +20,14 @@ public class JsonAdaptedPersonTest { private static final String INVALID_NAME = "R@chel"; private static final String INVALID_PHONE = "+651234"; private static final String INVALID_EMAIL = "example.com"; - private static final String INVALID_TAG = "#friend"; + private static final String INVALID_TAG_NAME = "#friend"; + private static final String INVALID_TAG_CATEGORY = "hehe"; private static final String VALID_NAME = BENSON.getName().toString(); private static final String VALID_PHONE = BENSON.getPhone().toString(); private static final String VALID_EMAIL = BENSON.getEmail().toString(); + private static final String VALID_TAG_NAME = "colleague"; + private static final String VALID_TAG_CATEGORY = "acads"; private static final List VALID_TAGS = BENSON.getTags().stream() .map(JsonAdaptedTag::new) .collect(Collectors.toList()); @@ -81,9 +84,18 @@ public void toModelType_nullEmail_throwsIllegalValueException() { } @Test - public void toModelType_invalidTags_throwsIllegalValueException() { + public void toModelType_invalidTagName_throwsIllegalValueException() { List invalidTags = new ArrayList<>(VALID_TAGS); - invalidTags.add(new JsonAdaptedTag(INVALID_TAG)); + invalidTags.add(new JsonAdaptedTag(INVALID_TAG_NAME, VALID_TAG_CATEGORY)); + JsonAdaptedPerson person = + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, invalidTags); + assertThrows(IllegalValueException.class, person::toModelType); + } + + @Test + public void toModelType_invalidTagCategory_throwsIllegalValueException() { + List invalidTags = new ArrayList<>(VALID_TAGS); + invalidTags.add(new JsonAdaptedTag(VALID_TAG_NAME, INVALID_TAG_CATEGORY)); JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, invalidTags); assertThrows(IllegalValueException.class, person::toModelType); diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index 6c55ad599f3..2a86bfb67c7 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -3,6 +3,7 @@ import java.util.HashSet; import java.util.Set; +import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.model.person.Email; import seedu.address.model.person.Name; import seedu.address.model.person.Person; @@ -60,6 +61,16 @@ public PersonBuilder withTags(String ... tags) { return this; } + /** + * Parses the {@code tagNames} and the respective {@tagCategories} into a {@code Set} and + * set it to the {@code Person} that we are building. + * @throws IllegalValueException thrown when a tag category is invalid + */ + public PersonBuilder withTags(String[] tags, String[] categories) throws IllegalValueException { + this.tags = SampleDataUtil.getTagSet(tags, categories); + return this; + } + /** * Sets the {@code Phone} of the {@code Person} that we are building. */ From 7bc32fee518579e602fdb4030b8854b353c5b2c2 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 11:49:35 +0800 Subject: [PATCH 3/6] Fix more unit tests IMPORTANT: the TagCategory enums must have a string representation identical to the name of the enums themselves, otherwise the JsonAdaptedTag will not work due to how Tag(name) initializes the Tag --- src/main/java/seedu/address/model/tag/TagCategory.java | 10 +++++----- .../java/seedu/address/model/util/SampleDataUtil.java | 4 ++-- .../java/seedu/address/storage/JsonAdaptedTag.java | 8 ++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/model/tag/TagCategory.java b/src/main/java/seedu/address/model/tag/TagCategory.java index 1fcb2360a8d..d48e7cf36ff 100644 --- a/src/main/java/seedu/address/model/tag/TagCategory.java +++ b/src/main/java/seedu/address/model/tag/TagCategory.java @@ -25,19 +25,19 @@ public enum TagCategory { */ public static TagCategory fromString(String category) throws IllegalValueException { switch (category) { - case "acads": + case "ACADEMICS": return ACADEMICS; - case "activity": + case "ACTIVITIES": return ACTIVITIES; - case "networking": + case "NETWORKING": return NETWORKING; - case "mentor": + case "MENTORSHIP": return MENTORSHIP; - case "general": + case "GENERAL": return GENERAL; default: diff --git a/src/main/java/seedu/address/model/util/SampleDataUtil.java b/src/main/java/seedu/address/model/util/SampleDataUtil.java index 137f931a01a..4164b44cde1 100644 --- a/src/main/java/seedu/address/model/util/SampleDataUtil.java +++ b/src/main/java/seedu/address/model/util/SampleDataUtil.java @@ -59,9 +59,9 @@ public static Set getTagSet(String... names) { /** * Returns a tag set containing tags with the names and categories specified. * @param names Array of names for the tags. - * @param categories Array of categories to be attached to the respective name. - * @exception IllegalValueException thrown when a tag category is not valid. + * @param categories Array of categories to be attached to the respective names. * @return Set containing the tags. + * @throws IllegalValueException thrown when a tag category is not valid. */ public static Set getTagSet(String[] names, String[] categories) throws IllegalValueException { List resultTags = new ArrayList<>(); diff --git a/src/main/java/seedu/address/storage/JsonAdaptedTag.java b/src/main/java/seedu/address/storage/JsonAdaptedTag.java index f7649d3b40c..d2bd5fd4a47 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedTag.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedTag.java @@ -25,6 +25,14 @@ public JsonAdaptedTag(@JsonProperty("tagName") String tagName, this.tagCategory = tagCategory; } + /** + * Overloaded constructor for default tag creation. + */ + @JsonCreator + public JsonAdaptedTag(String tagName) { + this(tagName, "GENERAL"); + } + /** * Converts a given {@code Tag} into this class for Jackson use. */ From bebf5d4d3e2017097d364ce977d43617ebdf2955 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 12:07:42 +0800 Subject: [PATCH 4/6] Unit tests for TagCategory So codecov can get off my ass --- .../address/model/tag/TagCategoryTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/seedu/address/model/tag/TagCategoryTest.java diff --git a/src/test/java/seedu/address/model/tag/TagCategoryTest.java b/src/test/java/seedu/address/model/tag/TagCategoryTest.java new file mode 100644 index 00000000000..338e9381a58 --- /dev/null +++ b/src/test/java/seedu/address/model/tag/TagCategoryTest.java @@ -0,0 +1,33 @@ +package seedu.address.model.tag; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.exceptions.IllegalValueException; + +public class TagCategoryTest { + + @Test + public void fromString_validCategories_returnsSuccess() throws IllegalValueException { + // all valid inputs + assertEquals(TagCategory.GENERAL, TagCategory.fromString("GENERAL")); + assertEquals(TagCategory.ACADEMICS, TagCategory.fromString("ACADEMICS")); + assertEquals(TagCategory.NETWORKING, TagCategory.fromString("NETWORKING")); + assertEquals(TagCategory.MENTORSHIP, TagCategory.fromString("MENTORSHIP")); + assertEquals(TagCategory.ACTIVITIES, TagCategory.fromString("ACTIVITIES")); + } + + @Test + public void fromString_invalidCategories_throwsIllegalValueException() { + // completely invalid inputs + assertThrows(IllegalValueException.class, () -> TagCategory.fromString("HEHE")); + assertThrows(IllegalValueException.class, () -> TagCategory.fromString("ACACACACAC")); + + // wrong case + assertThrows(IllegalValueException.class, () -> TagCategory.fromString("Academics")); + assertThrows(IllegalValueException.class, () -> TagCategory.fromString("GeNeRaL")); + assertThrows(IllegalValueException.class, () -> TagCategory.fromString("aCtIViTieS")); + } +} From 1d24f26f406cbd5ab9190d2473e7612f4791a65b Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 22:43:49 +0800 Subject: [PATCH 5/6] Change user guide --- docs/UserGuide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index e90b3802500..4d7bc8348d6 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -166,7 +166,7 @@ Adds the specified person's tag. Format: `addtag INDEX t/KEYWORD [t/MORE_TAGS]` -* Deletes the tag with the specified name `KEYWORD` of the person at the specified `INDEX`. +* Add the tags with the specified name `KEYWORD` of the person at the specified `INDEX`. * The index refers to the index number shown in the displayed person list. * The index **must be a positive integer** 1, 2, 3, …​ * Multiple tags can be added at a time. From 5876e8992c46fea46f4c1ebe2aaef37405e58a8e Mon Sep 17 00:00:00 2001 From: KrashKart Date: Mon, 4 Nov 2024 22:44:34 +0800 Subject: [PATCH 6/6] Change grammar --- docs/UserGuide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 4d7bc8348d6..0a651740852 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -166,7 +166,7 @@ Adds the specified person's tag. Format: `addtag INDEX t/KEYWORD [t/MORE_TAGS]` -* Add the tags with the specified name `KEYWORD` of the person at the specified `INDEX`. +* Adds the tags with the specified name `KEYWORD` of the person at the specified `INDEX`. * The index refers to the index number shown in the displayed person list. * The index **must be a positive integer** 1, 2, 3, …​ * Multiple tags can be added at a time.