Skip to content

Commit

Permalink
Merge pull request #112 from ZweZeya/branch-zwe-update-test
Browse files Browse the repository at this point in the history
Update tests for storage and parser
  • Loading branch information
Raghava-Chittidi authored Oct 23, 2024
2 parents 1341f5f + 2a7b068 commit ada138a
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Optional<ReadOnlyAddressBook<InternshipApplication>> readAddressBook(Path

Optional<JsonSerializableAddressBook> jsonAddressBook = JsonUtil.readJsonFile(
filePath, JsonSerializableAddressBook.class);
if (!jsonAddressBook.isPresent()) {
if (jsonAddressBook.isEmpty()) {
return Optional.empty();
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/hireme/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private void assertCommandFailureForExceptionFromStorage(IOException e, String e
// Inject LogicManager with an AddressBookStorage that throws the IOException e when saving
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath) {
@Override
public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath)
public void saveAddressBook(ReadOnlyAddressBook<InternshipApplication> addressBook, Path filePath)
throws IOException {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -62,7 +61,7 @@ public void parseCommand_exit() throws Exception {
public void parseCommand_find() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
FindCommand.COMMAND_WORD + " " + String.join(" ", keywords));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void assertPreambleEmpty(ArgumentMultimap argMultimap) {
* and only the last value is returned upon calling {@code ArgumentMultimap#getValue(Prefix)}.
*/
private void assertArgumentPresent(ArgumentMultimap argMultimap, Prefix prefix, String... expectedValues) {

assertTrue(argMultimap.getValue(prefix).isPresent());
// Verify the last value is returned
assertEquals(expectedValues[expectedValues.length - 1], argMultimap.getValue(prefix).get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class DeleteCommandParserTest {

private DeleteCommandParser parser = new DeleteCommandParser();
private final DeleteCommandParser parser = new DeleteCommandParser();

@Test
public void parse_validArgs_returnsDeleteCommand() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class FindCommandParserTest {

private FindCommandParser parser = new FindCommandParser();
private final FindCommandParser parser = new FindCommandParser();

@Test
public void parse_emptyArg_throwsParseException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.hireme.testutil.Assert.assertThrows;
import static seedu.hireme.testutil.TypicalInternshipApplications.AIRBNB;
import static seedu.hireme.testutil.TypicalInternshipApplications.FIGMA;
Expand Down Expand Up @@ -77,12 +78,14 @@ public void readAndSaveAddressBook_allInOrder_success() throws Exception {
original.addItem(GOOGLE);
original.removeItem(FIGMA);
jsonAddressBookStorage.saveAddressBook(original, filePath);
assertTrue(jsonAddressBookStorage.readAddressBook(filePath).isPresent());
readBack = jsonAddressBookStorage.readAddressBook(filePath).get();
assertEquals(original, new AddressBook<InternshipApplication>(readBack));

// Save and read without specifying file path
original.addItem(AIRBNB);
jsonAddressBookStorage.saveAddressBook(original); // file path not specified
assertTrue(jsonAddressBookStorage.readAddressBook().isPresent());
readBack = jsonAddressBookStorage.readAddressBook().get(); // file path not specified
assertEquals(original, new AddressBook<InternshipApplication>(readBack));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.hireme.storage;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.hireme.testutil.Assert.assertThrows;

import java.nio.file.Path;
Expand All @@ -27,6 +28,7 @@ public class JsonSerializableAddressBookTest {

@Test
public void toModelType_typicalInternshipsFile_success() throws Exception {
assertTrue(JsonUtil.readJsonFile(TYPICAL_INTERNSHIPS_FILE, JsonSerializableAddressBook.class).isPresent());
JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_INTERNSHIPS_FILE,
JsonSerializableAddressBook.class).get();
AddressBook<InternshipApplication> addressBookFromFile = dataFromFile.toModelType();
Expand All @@ -37,13 +39,15 @@ public void toModelType_typicalInternshipsFile_success() throws Exception {

@Test
public void toModelType_invalidInternshipFile_throwsIllegalValueException() throws Exception {
assertTrue(JsonUtil.readJsonFile(INVALID_INTERNSHIP_FILE, JsonSerializableAddressBook.class).isPresent());
JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(INVALID_INTERNSHIP_FILE,
JsonSerializableAddressBook.class).get();
assertThrows(IllegalValueException.class, dataFromFile::toModelType);
}

@Test
public void toModelType_duplicateInternship_throwsIllegalValueException() throws Exception {
assertTrue(JsonUtil.readJsonFile(DUPLICATE_INTERNSHIP_FILE, JsonSerializableAddressBook.class).isPresent());
JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(DUPLICATE_INTERNSHIP_FILE,
JsonSerializableAddressBook.class).get();
assertThrows(IllegalValueException.class, JsonSerializableAddressBook.MESSAGE_DUPLICATE_INTERNSHIP_APPLICATION,
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/seedu/hireme/storage/JsonUserPrefsStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.hireme.testutil.Assert.assertThrows;

import java.io.IOException;
Expand Down Expand Up @@ -52,26 +53,30 @@ private Path addToTestDataPathIfNotNull(String userPrefsFileInTestDataFolder) {
@Test
public void readUserPrefs_fileInOrder_successfullyRead() throws DataLoadingException {
UserPrefs expected = getTypicalUserPrefs();
assertTrue(readUserPrefs("TypicalUserPref.json").isPresent());
UserPrefs actual = readUserPrefs("TypicalUserPref.json").get();
assertEquals(expected, actual);
}

@Test
public void readUserPrefs_valuesMissingFromFile_defaultValuesUsed() throws DataLoadingException {
assertTrue(readUserPrefs("EmptyUserPrefs.json").isPresent());
UserPrefs actual = readUserPrefs("EmptyUserPrefs.json").get();
assertEquals(new UserPrefs(), actual);
}

@Test
public void readUserPrefs_extraValuesInFile_extraValuesIgnored() throws DataLoadingException {
UserPrefs expected = getTypicalUserPrefs();
assertTrue(readUserPrefs("ExtraValuesUserPref.json").isPresent());
UserPrefs actual = readUserPrefs("ExtraValuesUserPref.json").get();

assertEquals(expected, actual);
}

private UserPrefs getTypicalUserPrefs() {
UserPrefs userPrefs = new UserPrefs();
userPrefs.setGuiSettings(new GuiSettings(1000, 500, 300, 100));
userPrefs.setHireMeFilePath(Paths.get("hireme.json"));
userPrefs.setHireMeFilePath(Paths.get("data/hireme.json"));
return userPrefs;
}

Expand Down Expand Up @@ -108,6 +113,7 @@ public void saveUserPrefs_allInOrder_success() throws DataLoadingException, IOEx

//Try writing when the file doesn't exist
jsonUserPrefsStorage.saveUserPrefs(original);
assertTrue(jsonUserPrefsStorage.readUserPrefs().isPresent());
UserPrefs readBack = jsonUserPrefsStorage.readUserPrefs().get();
assertEquals(original, readBack);

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/seedu/hireme/storage/StorageManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.hireme.testutil.TypicalInternshipApplications.getTypicalAddressBook;

import java.nio.file.Path;
Expand Down Expand Up @@ -44,6 +45,7 @@ public void prefsReadSave() throws Exception {
UserPrefs original = new UserPrefs();
original.setGuiSettings(new GuiSettings(300, 600, 4, 6));
storageManager.saveUserPrefs(original);
assertTrue(storageManager.readUserPrefs().isPresent());
UserPrefs retrieved = storageManager.readUserPrefs().get();
assertEquals(original, retrieved);
}
Expand All @@ -57,6 +59,7 @@ public void addressBookReadSave() throws Exception {
*/
AddressBook<InternshipApplication> original = getTypicalAddressBook();
storageManager.saveAddressBook(original);
assertTrue(storageManager.readAddressBook().isPresent());
ReadOnlyAddressBook<InternshipApplication> retrieved = storageManager.readAddressBook().get();
assertEquals(original, new AddressBook<InternshipApplication>(retrieved));
}
Expand Down

0 comments on commit ada138a

Please sign in to comment.