Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-T11-2#281 from currynia/branch-ug
Browse files Browse the repository at this point in the history
Update assignment json to handle null values
  • Loading branch information
Incogdino authored Nov 8, 2024
2 parents c820323 + 4470ec4 commit b340f50
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
initialData = new AddressBook();
e.printStackTrace();
}

try {
Expand All @@ -109,7 +108,7 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
predefinedAssignments = readAssignmentOptional
.orElseGet(SampleAssignmentsUtil::getSamplePredefinedAssignments);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
logger.warning("Data file at " + storage.getAssignmentFilePath() + " could not be loaded."
+ " Will be starting with an empty predefined assignments data.");
predefinedAssignments = new PredefinedAssignmentsData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public PredefinedAssignment toModelType() {
public String getName() {
return name;
}

public Float getMaxScore() {
return maxScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class JsonSerializablePredefinedAssignmentData {
private static final String DUPLICATE_PREDEFINED_ASSIGNMENT =
"Predefined assignments list contains duplicate assignment(s).";
private static final String MISSING_VALUES = "Assignment is missing assignmentName and/or maxScore.";
private final List<JsonAdaptedPredefinedAssignment> assignments = new ArrayList<>();

/**
Expand All @@ -40,11 +41,18 @@ public PredefinedAssignmentsData toModelType() throws IllegalValueException {
if (predefinedAssignmentsData.hasAssignment(assignmentName)) {
throw new IllegalValueException(DUPLICATE_PREDEFINED_ASSIGNMENT);
}
if (isFieldNull(jsonAdaptedPredefinedAssignment)) {
throw new IllegalValueException(MISSING_VALUES);
}
predefinedAssignmentsData.addPredefinedAssignment(jsonAdaptedPredefinedAssignment.toModelType());
}
return predefinedAssignmentsData;
}

private boolean isFieldNull(JsonAdaptedPredefinedAssignment assignment) {
return assignment.getName() == null || assignment.getMaxScore() == null;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof JsonSerializablePredefinedAssignmentData other) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/data/AssignmentTest/MissingName.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"assignments" : [
{
"name": "Ex01",
"maxScore": 5
},{
"name": "Ex02",
"maxScore": 5
},{
"name": "Ex03",
"maxScore": 5
},{
"name": "Ex04",
"maxScore": 5
},{
"maxScore": 1
}
]
}
19 changes: 19 additions & 0 deletions src/test/data/AssignmentTest/MissingScore.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"assignments" : [
{
"name": "Ex01",
"maxScore": 5
},{
"name": "Ex02",
"maxScore": 5
},{
"name": "Ex03",
"maxScore": 5
},{
"name": "Ex04",
"maxScore": 5
},{
"name" : "abc"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ void testReadAssignment() {
"AssignmentTest", "PredefinedAssignmentDuplicate.json"));
assertThrows(DataLoadingException.class, test::readAssignment);
}

@Test
void testScoreMissing() {
JsonPredefinedAssignmentDataStorage test = new JsonPredefinedAssignmentDataStorage(
Paths.get(
"src", "test", "data",
"AssignmentTest", "MissingScore.json"));
assertThrows(DataLoadingException.class, test::readAssignment);
}

@Test
void testNameMissing() {
JsonPredefinedAssignmentDataStorage test = new JsonPredefinedAssignmentDataStorage(
Paths.get(
"src", "test", "data",
"AssignmentTest", "MissingName.json"));
assertThrows(DataLoadingException.class, test::readAssignment);
}
}

0 comments on commit b340f50

Please sign in to comment.