Skip to content

Commit

Permalink
Merge pull request #291 from yugan01/check-prefix-validity
Browse files Browse the repository at this point in the history
Check prefix validity
  • Loading branch information
DINESH1201 authored Nov 7, 2024
2 parents 0eeddd5 + 27d4da4 commit 5b4ba3b
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC;
Expand Down Expand Up @@ -29,6 +30,9 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
checkPrefixPresentAndValidPrefix(args, AddCommand.MESSAGE_USAGE, PREFIX_NAME, PREFIX_NRIC, PREFIX_SEX,
PREFIX_BIRTHDATE, PREFIX_PHONE);

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_NRIC, PREFIX_SEX,
PREFIX_BIRTHDATE, PREFIX_PHONE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ALLERGY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDATE;
Expand Down Expand Up @@ -47,7 +48,14 @@ public class AddFCommandParser implements Parser<AddFCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddFCommand parse(String args) throws ParseException {

assert args != null : "Argument cannot be null";

checkPrefixPresentAndValidPrefix(args, AddFCommand.MESSAGE_USAGE, PREFIX_NAME, PREFIX_NRIC, PREFIX_SEX,
PREFIX_BIRTHDATE, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_ADDRESS, PREFIX_ALLERGY,
PREFIX_BLOODTYPE, PREFIX_EXISTINGCONDITION, PREFIX_NOTE, PREFIX_NOKNAME, PREFIX_NOKPHONE,
PREFIX_HEALTHRISK);

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_NRIC, PREFIX_SEX,
PREFIX_BIRTHDATE, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_ADDRESS, PREFIX_ALLERGY,
PREFIX_BLOODTYPE, PREFIX_EXISTINGCONDITION, PREFIX_NOTE, PREFIX_NOKNAME, PREFIX_NOKPHONE,
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.logic.parser.exceptions.ParseException;



/**
* Tokenizes arguments string of the form: {@code preamble <prefix>value <prefix>value ...}<br>
Expand All @@ -28,6 +35,74 @@ public static ArgumentMultimap tokenize(String argsString, Prefix... prefixes) {
return extractArguments(argsString, positions);
}

/**
* Checks an arguments string whether the prefixes provided are valid and there is at least a prefix present.
*
* @param argsString Arguments string of the form: {@code preamble <prefix>value <prefix>value ...}
* @param prefixes Prefixes to tokenize the arguments string with
*/
public static void checkPrefixPresentAndValidPrefix(String argsString, String messageUsage, Prefix... prefixes)
throws ParseException {
if (argsString.trim().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, messageUsage));
}
String[] splitArgs = argsString.split("\\|");

checkPrefixesPresent(splitArgs, messageUsage);
checkValidPrefix(splitArgs, prefixes);

}

/**
* Checks an arguments string whether there is at least a prefix present.
*
* @param splitArgs Array of String split by "|"
* @param messageUsage String to be returned if no valid prefixes are found
*/
public static void checkPrefixesPresent(String[] splitArgs, String messageUsage) throws ParseException {
if (splitArgs.length == 1) {
throw new ParseException("No valid prefixes found \n" + messageUsage);
}
}

/**
* Checks an arguments string whether the prefixes provided are valid.
*
* @param splitArgs Array of String split by "|"
* @param prefixes Prefixes to tokenize the arguments string with
*/
public static void checkValidPrefix(String[] splitArgs, Prefix... prefixes) throws ParseException {
List<String> prefixesPresent = Arrays.stream(splitArgs, 0, splitArgs.length - 1)
.map(ArgumentTokenizer::getPrefix).toList();

for (String prefixString : prefixesPresent) {
checkEachPrefix(prefixString, prefixes);
}
}

public static String getPrefix(String arg) {
String[] splitSingleArg = arg.split(" ");
return splitSingleArg[splitSingleArg.length - 1];
}

/**
* Checks an arguments string whether a single prefix provided are valid.
*
* @param prefixString String representation of a prefix provided by user
* @param prefixes Prefixes to tokenize the arguments string with
*/
public static void checkEachPrefix(String prefixString, Prefix... prefixes) throws ParseException {

Stream<String> prefixStrings = Arrays.stream(prefixes).map(Prefix::getPrefix)
.map(prefix -> prefix.substring(0, prefix.length() - 1));

boolean isValid = prefixStrings.anyMatch(prefixString::equals);

if (!isValid) {
throw new ParseException("Prefix " + prefixString + " is invalid");

Check warning on line 102 in src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java#L102

Added line #L102 was not covered by tests
}
}

/**
* Finds all zero-based prefix positions in the given arguments string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATETIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HEALTHSERVICE;

Expand Down Expand Up @@ -31,6 +32,10 @@ public BookApptCommand parse(String args) throws ParseException {
requireNonNull(args);

Check warning on line 32 in src/main/java/seedu/address/logic/parser/BookApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/BookApptCommandParser.java#L32

Added line #L32 was not covered by tests
assert args != null : "Arguments cannot be null";
logger.info("Parsing BookApptCommand");

Check warning on line 34 in src/main/java/seedu/address/logic/parser/BookApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/BookApptCommandParser.java#L34

Added line #L34 was not covered by tests

checkPrefixPresentAndValidPrefix(args, BookApptCommand.MESSAGE_USAGE,

Check warning on line 36 in src/main/java/seedu/address/logic/parser/BookApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/BookApptCommandParser.java#L36

Added line #L36 was not covered by tests
PREFIX_DATETIME, PREFIX_HEALTHSERVICE);

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DATETIME,

Check warning on line 39 in src/main/java/seedu/address/logic/parser/BookApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/BookApptCommandParser.java#L39

Added line #L39 was not covered by tests
PREFIX_HEALTHSERVICE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATETIME;

import java.time.LocalDateTime;
Expand All @@ -28,6 +29,9 @@ public DeleteApptCommand parse(String args) throws ParseException {
requireNonNull(args);
assert args != null : "Arguments cannot be null";
logger.info("Parsing DeleteApptCommand");

Check warning on line 31 in src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java#L31

Added line #L31 was not covered by tests

checkPrefixPresentAndValidPrefix(args, DeleteApptCommand.MESSAGE_USAGE, PREFIX_DATETIME);

Check warning on line 33 in src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java#L33

Added line #L33 was not covered by tests

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DATETIME);

Check warning on line 35 in src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/DeleteApptCommandParser.java#L35

Added line #L35 was not covered by tests

if (!arePrefixesPresent(argMultimap, PREFIX_DATETIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ALLERGY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDATE;
Expand Down Expand Up @@ -41,6 +42,12 @@ public class EditCommandParser implements Parser<EditCommand> {
public EditCommand parse(String args) throws ParseException {
logger.info("Attempting to parse EditCommand arguments.");
requireNonNull(args);

checkPrefixPresentAndValidPrefix(args, EditCommand.MESSAGE_USAGE, PREFIX_NAME, PREFIX_NRIC, PREFIX_BIRTHDATE,
PREFIX_SEX, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_BLOODTYPE, PREFIX_NOKNAME,
PREFIX_NOKPHONE, PREFIX_ALLERGY, PREFIX_REMOVEALLERGY, PREFIX_HEALTHRISK, PREFIX_EXISTINGCONDITION,
PREFIX_NOTE);

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_NRIC, PREFIX_BIRTHDATE, PREFIX_SEX,
PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_BLOODTYPE, PREFIX_NOKNAME, PREFIX_NOKPHONE,
Expand All @@ -52,6 +59,7 @@ public EditCommand parse(String args) throws ParseException {
logger.warning("No NRIC provided in input.");
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_NRIC_EMPTY));
}

try {
assert !argMultimap.getPreamble().isEmpty();
nric = ParserUtil.parseNric(argMultimap.getPreamble());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.ArgumentTokenizer.checkPrefixPresentAndValidPrefix;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ENDDATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HEALTHSERVICE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STARTDATE;
Expand All @@ -25,6 +26,10 @@ public class FilterCommandParser implements Parser<FilterCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public FilterCommand parse(String args) throws ParseException {

checkPrefixPresentAndValidPrefix(args, FilterCommand.MESSAGE_USAGE,
PREFIX_STARTDATE, PREFIX_ENDDATE, PREFIX_HEALTHSERVICE);

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STARTDATE, PREFIX_ENDDATE, PREFIX_HEALTHSERVICE);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/patient/BloodType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* {@link #isValidBloodType(String)}
*/
public class BloodType {
public static final String MESSAGE_CONSTRAINTS = "Blood type must be either A/B/AB/O followed by the Rhesus factor";
public static final String MESSAGE_CONSTRAINTS = "Blood type must be either A/B/AB/O followed by the"
+ " Rhesus factor (+/-)";
public static final String VALIDATION_REGEX = "^(A|B|AB|O)[+-]$";

public final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void parse_repeatedValue_failure() {
@Test
public void parse_compulsoryFieldMissing_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE);
String expectedMessageForNoPrefix = String.format("No valid prefixes found \n" + AddCommand.MESSAGE_USAGE);

// missing name prefix
assertParseFailure(parser, VALID_NAME_BOB + NRIC_DESC_BOB + SEX_DESC_BOB + BIRTHDATE_DESC_BOB
Expand All @@ -160,7 +161,7 @@ public void parse_compulsoryFieldMissing_failure() {

// all prefixes missing
assertParseFailure(parser, VALID_NAME_BOB + VALID_NRIC_BOB + VALID_SEX_BOB + VALID_BIRTHDATE_BOB
+ VALID_PHONE_BOB, expectedMessage);
+ VALID_PHONE_BOB, expectedMessageForNoPrefix);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public void parse_repeatedNonAllergyValue_failure() {
@Test
public void parse_compulsoryFieldMissing_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddFCommand.MESSAGE_USAGE);
String expectedMessageForNoPrefix = String.format("No valid prefixes found \n" + AddFCommand.MESSAGE_USAGE);

// missing name prefix
assertParseFailure(parser, VALID_NAME_BOB + NRIC_DESC_BOB + SEX_DESC_BOB + BIRTHDATE_DESC_BOB
Expand All @@ -268,7 +269,7 @@ public void parse_compulsoryFieldMissing_failure() {

// all prefixes missing
assertParseFailure(parser, VALID_NAME_BOB + VALID_NRIC_BOB + VALID_SEX_BOB + VALID_BIRTHDATE_BOB
+ VALID_PHONE_BOB, expectedMessage);
+ VALID_PHONE_BOB, expectedMessageForNoPrefix);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SEX_AMY;
import static seedu.address.logic.commands.EditCommand.MESSAGE_NRIC_EMPTY;
import static seedu.address.logic.commands.EditCommand.MESSAGE_USAGE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BLOODTYPE;
Expand Down Expand Up @@ -95,10 +96,15 @@ public void parse_missingParts_failure() {
assertParseFailure(parser, NAME_DESC_AMY + SEX_DESC_AMY + BIRTHDATE_DESC_AMY, MESSAGE_EMPTY_NRIC);

// no field specified
assertParseFailure(parser, VALID_NRIC_AMY, EditCommand.MESSAGE_NOT_EDITED);
String expectedMessageForNoFields = "No valid prefixes found \n" + MESSAGE_USAGE;
assertParseFailure(parser, VALID_NRIC_AMY, expectedMessageForNoFields);

// no NRIC and no field specified
assertParseFailure(parser, "", MESSAGE_EMPTY_NRIC);
String expectedMessageForNoNricAndNoFields = String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE);
assertParseFailure(parser, "", expectedMessageForNoNricAndNoFields);

// field specified but no NRIC
assertParseFailure(parser, NRIC_DESC_AMY, MESSAGE_EMPTY_NRIC);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class FilterCommandParserTest {

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FilterCommand.MESSAGE_USAGE));
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE);
assertParseFailure(parser, " ", expectedMessage);
}

@Test
Expand Down Expand Up @@ -95,13 +95,13 @@ public void parse_compulsoryFieldMissing_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE);

// missing end date prefix with start date present
assertParseFailure(parser, " sd/2000-10-10", expectedMessage);
assertParseFailure(parser, validStartdate, expectedMessage);

// missing end date prefix with health service present
assertParseFailure(parser, " h/Blood Test", expectedMessage);
assertParseFailure(parser, validHealthService, expectedMessage);

// missing end date prefix with start date and health service present
assertParseFailure(parser, " sd/2000-10-10 h/Blood Test", expectedMessage);
assertParseFailure(parser, validStartdate + validHealthService, expectedMessage);

}

Expand Down

0 comments on commit 5b4ba3b

Please sign in to comment.