diff --git a/src/test/data/ImportCommandTest/invalid.csv b/src/test/data/ImportCommandTest/invalid.csv new file mode 100644 index 00000000000..75d68c75cc9 --- /dev/null +++ b/src/test/data/ImportCommandTest/invalid.csv @@ -0,0 +1,5 @@ +name,phone,email,gpa,tags,studentNo,previousGrade +Jasmine David,9847283,jasmine_david@u.nus.edu,4.3,deansList;pastTA,A0123486A,D- +Sandeep Kopparthi,8753746,sandeep@u.nus.edu,5.0,pastTA,A0456123A,C- +Lim Boon Kong,9777777,boonkong@u.nus.edu,3.5.2,deansList,A0775848D,C +Mohammed Taufiq bin Rozaini,85535252,taufiqu.nus.edu,4.2,,A0483910A,A+ \ No newline at end of file diff --git a/src/test/data/ImportCommandTest/invalid_format.csv b/src/test/data/ImportCommandTest/invalid_format.csv new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/test/data/ImportCommandTest/missing_fields.csv b/src/test/data/ImportCommandTest/missing_fields.csv new file mode 100644 index 00000000000..ea06b3596b7 --- /dev/null +++ b/src/test/data/ImportCommandTest/missing_fields.csv @@ -0,0 +1,5 @@ +studentNo,name,phone,email,gpaa,previousGrade,tags +A0123486A,Jasmine David,98472983,jasmine_david@u.nus.edu,4.3,B+,deansList;pastTA +A0456123A,Sandeep Kopparthi,86753746,sandeep@u.nus.edu,5.0,B+,pastTA +A0775848D,Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,C,deansList +A0483910A,Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,A+, diff --git a/src/test/data/ImportCommandTest/partial_valid.csv b/src/test/data/ImportCommandTest/partial_valid.csv new file mode 100644 index 00000000000..cf08d44c1ac --- /dev/null +++ b/src/test/data/ImportCommandTest/partial_valid.csv @@ -0,0 +1,5 @@ +name,phone,email,gpa,tags,studentNo,previousGrade +Jasmine David,9847283,jasmine_david@u.nus.edu,4.3,deansList;pastTA,A0123486A,D- +Sandeep Kopparthi,8753746,sandeep@u.nus.edu,5.0,pastTA,A0456123A,C- +Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,deansList,A0775848D,C +Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,,A0483910A,A+ diff --git a/src/test/data/ImportCommandTest/valid.csv b/src/test/data/ImportCommandTest/valid.csv new file mode 100644 index 00000000000..46bf2a6c6e4 --- /dev/null +++ b/src/test/data/ImportCommandTest/valid.csv @@ -0,0 +1,5 @@ +studentNo,name,phone,email,gpa,previousGrade,tags +A0123486A,Jasmine David,98472983,jasmine_david@u.nus.edu,4.3,B+,deansList;pastTA +A0456123A,Sandeep Kopparthi,86753746,sandeep@u.nus.edu,5.0,B+,pastTA +A0775848D,Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,C,deansList +A0483910A,Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,A+, diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java new file mode 100644 index 00000000000..e77d0597043 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -0,0 +1,110 @@ +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.assertCommandFailure; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.attachment.Attachment; + +public class ImportCommandTest { + + private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "ImportCommandTest"); + private static final Path VALID_CSV_FILE = TEST_DATA_FOLDER.resolve("valid.csv"); + private static final Path PARTIAL_VALID_CSV_FILE = TEST_DATA_FOLDER.resolve("partial_valid.csv"); + private static final Path INVALID_CSV_FILE = TEST_DATA_FOLDER.resolve("invalid.csv"); + private static final Path MISSING_FIELDS_CSV_FILE = TEST_DATA_FOLDER.resolve("missing_fields.csv"); + private static final Path INVALID_FORMAT_CSV_FILE = TEST_DATA_FOLDER.resolve("invalid_format.csv"); + + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void execute_validCsv_importSuccessful() throws CommandException { + Attachment validCsv = new Attachment(VALID_CSV_FILE.toString()); + CommandResult commandResult = new ImportCommand(validCsv).execute(model); + + String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_SUCCESS, 4); + + assertEquals(expectedMessage, commandResult.getFeedbackToUser()); + } + + @Test + public void execute_partialValidCsv_importPartiallySuccessful() throws CommandException { + Attachment partialValidCsv = new Attachment(PARTIAL_VALID_CSV_FILE.toString()); + CommandResult commandResult = new ImportCommand(partialValidCsv).execute(model); + + String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_SUCCESS, 2) + " " + + String.format(ImportCommand.MESSAGE_IMPORT_FAILURE, 2, "2, 3"); + + assertEquals(expectedMessage, commandResult.getFeedbackToUser()); + } + + @Test + public void execute_invalidCsv_importFailure() throws CommandException { + Attachment invalidCsv = new Attachment(INVALID_CSV_FILE.toString()); + CommandResult commandResult = new ImportCommand(invalidCsv).execute(model); + + String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_FAILURE, 4, "2, 3, 4, 5"); + + assertEquals(expectedMessage, commandResult.getFeedbackToUser()); + } + + @Test + public void execute_invalidFileFormatCsv_importFailure() throws CommandException { + Attachment invalidFileFormatCsv = new Attachment(INVALID_FORMAT_CSV_FILE.toString()); + ImportCommand importCommand = new ImportCommand(invalidFileFormatCsv); + + String expectedMessage = ImportCommand.MESSAGE_INVALID_FILE_FORMAT; + + assertCommandFailure(importCommand, model, expectedMessage); + } + + @Test + public void execute_missingFieldsCsv_importFailure() throws CommandException { + Attachment missingFieldsCsv = new Attachment(MISSING_FIELDS_CSV_FILE.toString()); + ImportCommand importCommand = new ImportCommand(missingFieldsCsv); + + String expectedMessage = String.format(ImportCommand.MESSAGE_MISSING_FIELDS, "gpa"); + + assertCommandFailure(importCommand, model, expectedMessage); + } + + @Test + public void equals() { + Attachment firstAttachment = new Attachment(VALID_CSV_FILE.toString()); + Attachment secondAttachment = new Attachment(INVALID_CSV_FILE.toString()); + + ImportCommand importFirstCommand = new ImportCommand(firstAttachment); + ImportCommand importSecondCommand = new ImportCommand(secondAttachment); + + assertTrue(importFirstCommand.equals(importFirstCommand)); + + ImportCommand importFirstCommandCopy = new ImportCommand(firstAttachment); + assertTrue(importFirstCommand.equals(importFirstCommandCopy)); + + assertFalse(importFirstCommand.equals(1)); + + assertFalse(importFirstCommand.equals(null)); + + assertFalse(importFirstCommand.equals(importSecondCommand)); + } + + @Test + public void toStringMethod() { + Attachment attachment = new Attachment(VALID_CSV_FILE.toString()); + ImportCommand importCommand = new ImportCommand(attachment); + String expected = ImportCommand.class.getCanonicalName() + "{attachment=" + attachment + "}"; + assertEquals(expected, importCommand.toString()); + } + +}