Skip to content

Commit

Permalink
Merge pull request #74 from moreTriangles/import-json
Browse files Browse the repository at this point in the history
Test cases for Import
  • Loading branch information
Jai2501 authored Oct 12, 2021
2 parents 5b26410 + 0542c92 commit d3b6bb4
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
Empty file.
12 changes: 12 additions & 0 deletions src/test/data/ExportImportCommandTest/FolderWithJson/testFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"persons" : [
{
"name": "Amy Bee",
"telegram": "amy_bee",
"phone": "85355255",
"email": "[email protected]",
"address": "123, Jurong West Ave 6, #08-111",
"tagged": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
import seedu.address.model.UserPrefs;

public class ExportCommandTest {
private static final String PATH_FILE_CREATED_FOLDER = "src/test/data/ExportCommandTest/FileDoesNotExist/";
private static final String PATH_FILE_EXISTS_FOLDER = "src/test/data/ExportCommandTest/FileExists/";
private static final String PATH_EMPTY_FOLDER = "src/test/data/ExportImportCommandTest/EmptyFolder/";
private static final String PATH_FOLDER_WITH_JSON = "src/test/data/ExportImportCommandTest/FolderWithJson/";
private static final String TEST_FILE_NAME = "testFile";

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());



@Test
public void equals() {
ExportCommand exportFirstCommand = new ExportCommand("first");
Expand All @@ -50,29 +48,29 @@ public void equals() {
/**
* Export command creates a file when there is no existing file with the given name.
* Test file is deleted after creation.
* Test occurs in src/test/data/ExportCommandTest/FileDoesNotExist.
* Test occurs in src/test/data/ExportImportCommandTest/EmptyFolder.
*/
@Test
public void execute_fileName_fileCreated() {
String expectedMessage = String.format(ExportCommand.MESSAGE_EXPORT_SUCCESS, TEST_FILE_NAME);

ExportCommand exportCommand = new ExportCommand(PATH_FILE_CREATED_FOLDER, TEST_FILE_NAME);
ExportCommand exportCommand = new ExportCommand(PATH_EMPTY_FOLDER, TEST_FILE_NAME);
assertCommandSuccess(exportCommand, model, expectedMessage, expectedModel);
try {
FileUtil.deleteFileIfExists(Path.of(PATH_FILE_CREATED_FOLDER + TEST_FILE_NAME + ".json"));
FileUtil.deleteFileIfExists(Path.of(PATH_EMPTY_FOLDER + TEST_FILE_NAME + ".json"));
} catch (IOException ioe) {
System.out.println("Error deleting testFile.json");
}
}

/**
* Export command fails when there is an existing file with the given name.
* Test occurs in src/test/data/ExportCommandTest/FileExists.
* Test occurs in src/test/data/ExportImportCommandTest/FolderWithJson.
*/
@Test
public void execute_fileName_fileExists() {
String expectedMessage = String.format(ExportCommand.MESSAGE_EXPORT_FAILURE, TEST_FILE_NAME);
ExportCommand exportCommand = new ExportCommand(PATH_FILE_EXISTS_FOLDER, TEST_FILE_NAME);
ExportCommand exportCommand = new ExportCommand(PATH_FOLDER_WITH_JSON, TEST_FILE_NAME);
assertCommandFailure(exportCommand, model, expectedMessage);
}
}
71 changes: 71 additions & 0 deletions src/test/java/seedu/address/logic/commands/ImportCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package seedu.address.logic.commands;

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.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.testutil.PersonBuilder;

public class ImportCommandTest {
private static final String PATH_EMPTY_FOLDER = "src/test/data/ExportImportCommandTest/EmptyFolder/";
private static final String PATH_FOLDER_WITH_JSON = "src/test/data/ExportImportCommandTest/FolderWithJson/";
private static final String TEST_FILE_NAME = "testFile";

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
ImportCommand importFirstCommand = new ImportCommand("first");
ImportCommand importSecondCommand = new ImportCommand("second");

// same object -> returns true
assertTrue(importFirstCommand.equals(importFirstCommand));

// same filename -> returns true
assertTrue(importFirstCommand.equals(new ImportCommand("first")));

// different types -> returns false
assertFalse(importFirstCommand.equals(1));

// null -> returns false
assertFalse(importFirstCommand.equals(null));

// different filename -> returns false
assertFalse(importFirstCommand.equals(importSecondCommand));

}

/**
* Imports the JSON file with the filename provided.
* Test occurs in src/test/data/ExportImportCommandTest/FolderWithJson.
*/
@Test
public void execute_fileName_importSuccess() {
Person newPerson = new PersonBuilder().build();
expectedModel.addPerson(newPerson);
ImportCommand importNewPerson = new ImportCommand(PATH_FOLDER_WITH_JSON, TEST_FILE_NAME);
String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_SUCCESS, TEST_FILE_NAME);
assertCommandSuccess(importNewPerson, model, expectedMessage, expectedModel);
}

/**
* Import command fails when there is no file with the provided file name.
* Test occurs in src/test/data/ExportImportCommandTest/EmptyFolder.
*/
@Test
public void execute_fileName_fileMissing() {
String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_FILE_NOT_FOUND, TEST_FILE_NAME);
ImportCommand importNewPerson = new ImportCommand(PATH_EMPTY_FOLDER, TEST_FILE_NAME);
assertCommandFailure(importNewPerson, model, expectedMessage);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_FILENAME_FRIENDS;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.ImportCommand;

public class ImportCommandParserTest {

private ImportCommandParser parser = new ImportCommandParser();

@Test
public void parse_fileNamePresent_success() {
// file name present
assertParseSuccess(parser, VALID_FILENAME_FRIENDS, new ImportCommand(VALID_FILENAME_FRIENDS));
}

@Test void parse_fileNameMissing_failure() {
// file name missing
assertParseFailure(parser, "", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportCommand.MESSAGE_USAGE));
}
}

0 comments on commit d3b6bb4

Please sign in to comment.