Skip to content

Commit

Permalink
Add unit tests for Json adapted classes in Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
zekone committed Oct 20, 2023
1 parent 0d6175f commit c61c358
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.format.settings.url": "eclipse-formatter.xml"
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/event/EventLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class EventLocation {
private EventLocation() {
this.location = "unknown";
}

private EventLocation(String location) {
this.location = location;
}

/**
* Construct an {@code EventLocation} object from {@code String}
*
* @param location The location in String
* @return The {@code EventLocation} object
*/
Expand All @@ -24,6 +26,7 @@ public static EventLocation fromString(String location) {

/**
* Get the String representation of this {@code EventLocation} object
*
* @return The String representation of this {@code EventLocation} object
*/
@Override
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.event.Event;
import seedu.address.model.event.EventInformation;
import seedu.address.model.event.EventLocation;

/**
* Jackson-friendly version of {@link Event}.
Expand Down Expand Up @@ -68,11 +70,13 @@ public Event toModelType() throws IllegalValueException {
}

if (location == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "location"));
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, EventLocation.class.getSimpleName()));
}

if (information == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "information"));
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, EventInformation.class.getSimpleName()));
}

return new Event(name, start, end, location, information);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedNote.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.note.Note;
import seedu.address.model.note.NoteContent;
import seedu.address.model.note.NoteTitle;

/**
* Jackson-friendly version of {@link Note}.
Expand Down Expand Up @@ -43,11 +45,13 @@ public JsonAdaptedNote(Note source) {
public Note toModelType() throws IllegalValueException {

if (title == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "title"));
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, NoteTitle.class.getSimpleName()));
}

if (content == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "body"));
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, NoteContent.class.getSimpleName()));
}

return new Note(title, content);
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/seedu/address/storage/JsonAdaptedEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.address.storage;

import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.storage.JsonAdaptedEvent.MISSING_FIELD_MESSAGE_FORMAT;

import org.junit.jupiter.api.Test;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.event.EventInformation;
import seedu.address.model.event.EventLocation;

public class JsonAdaptedEventTest {
private static final String VALID_NAME = "Meeting with professor";
private static final String VALID_START_TIME = "12:00";
private static final String VALID_END_TIME = "01:00";
private static final String VALID_LOCATION = "COM 1 Basement";
private static final String VALID_INFORMATION = "Discuss the project implementation with the professor";

@Test
public void toModelType_nullName_throwsIllegalValueException() {
JsonAdaptedEvent event = new JsonAdaptedEvent(null, VALID_START_TIME, VALID_END_TIME, VALID_LOCATION,
VALID_INFORMATION);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, "name");
assertThrows(IllegalValueException.class, expectedMessage, event::toModelType);
}

@Test
public void toModelType_nullStartTime_throwsIllegalValueException() {
JsonAdaptedEvent event = new JsonAdaptedEvent(VALID_NAME, null, VALID_END_TIME, VALID_LOCATION,
VALID_INFORMATION);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, "start");
assertThrows(IllegalValueException.class, expectedMessage, event::toModelType);
}

@Test
public void toModelType_nullEndTime_throwsIllegalValueException() {
JsonAdaptedEvent event = new JsonAdaptedEvent(VALID_NAME, VALID_START_TIME, null, VALID_LOCATION,
VALID_INFORMATION);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, "end");
assertThrows(IllegalValueException.class, expectedMessage, event::toModelType);
}

@Test
public void toModelType_nullLocation_throwsIllegalValueException() {
JsonAdaptedEvent event = new JsonAdaptedEvent(VALID_NAME, VALID_START_TIME, VALID_END_TIME, null,
VALID_INFORMATION);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, EventLocation.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, event::toModelType);
}

@Test
public void toModelType_nullInformation_throwsIllegalValueException() {
JsonAdaptedEvent event = new JsonAdaptedEvent(VALID_NAME, VALID_START_TIME, VALID_END_TIME, VALID_LOCATION,
null);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, EventInformation.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, event::toModelType);
}
}
37 changes: 37 additions & 0 deletions src/test/java/seedu/address/storage/JsonAdaptedNoteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package seedu.address.storage;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.storage.JsonAdaptedNote.MISSING_FIELD_MESSAGE_FORMAT;
import static seedu.address.testutil.TypicalNotes.NOTE_A;

import org.junit.jupiter.api.Test;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.note.NoteContent;
import seedu.address.model.note.NoteTitle;

public class JsonAdaptedNoteTest {
private static final String VALID_TITLE = "Hello World!";
private static final String VALID_CONTENT = "CS2103T is the best module I've taken.";

@Test
public void toModelType_validNoteDetails_returnsNote() throws Exception {
JsonAdaptedNote note = new JsonAdaptedNote(NOTE_A);
assertEquals(NOTE_A, note.toModelType());
}

@Test
public void toModelType_nullTitle_throwsIllegalValueException() {
JsonAdaptedNote note = new JsonAdaptedNote(null, VALID_CONTENT);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, NoteTitle.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, note::toModelType);
}

@Test
public void toModelType_nullContent_throwsIllegalValueException() {
JsonAdaptedNote note = new JsonAdaptedNote(VALID_TITLE, null);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, NoteContent.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, note::toModelType);
}
}

0 comments on commit c61c358

Please sign in to comment.