Skip to content

Commit

Permalink
Merge pull request #31 from LorenzoBettini/Issue27
Browse files Browse the repository at this point in the history
Issue27
  • Loading branch information
LorenzoBettini committed Nov 28, 2016
2 parents 7a27a91 + 1c2fc97 commit 603b82d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Expand Up @@ -10,4 +10,7 @@ public interface Database {

public boolean exists(String id);

public Student takeStudentsById(String id);}
public void add(Student student);

public Student takeStudentsById(String id);
}
Expand Up @@ -40,4 +40,9 @@ public Student takeStudentsById(String id) {
return null;
}

public void add(Student student) {
// TODO Auto-generated method stub

}

}
Expand Up @@ -21,6 +21,14 @@ public List<Student> getAllStudents() {
return database.getAllStudentsList();
}

public boolean addToDB(Student student) {
if(database.exists(student.getId())){
return false;
}
database.add(student);
return true;
}

public Student getStudentById(String id) {
return database.takeStudentsById(id);
}
Expand Down
Expand Up @@ -74,15 +74,41 @@ public void testGetStudentByIdWithBadIndex() {
when(database.takeStudentsById("0000")).thenReturn(null);
schoolController.getAllStudents();
}

@Test
public void testAddStudentBase(){
Student student = factoryStudent("1", "Pippo");
when(database.exists(anyString())).thenReturn(false);
assertTrue(schoolController.addToDB(student));
verify(database, times(1)).add(student);
verify(database, times(1)).add(student);

}

@Test
public void testAddStudentWhenIsDuplicate(){
Student student = factoryStudent("1", "Pippo");
when(database.exists(anyString())).thenReturn(true);
assertFalse(schoolController.addToDB(student));
verify(database, times(1)).exists(student.getId());
verify(database, times(0)).add(student);

}


@Test
public void testGetStudentByIdWithCorrectedIndex() {
Student student = new Student();
student.setId("0000");
student.setName("matteo");
Student student = factoryStudent("0000", "matteo");
when(database.takeStudentsById("0000")).thenReturn(student);

Student result = schoolController.getStudentById("0000");
assertSame(student, result);
}

private Student factoryStudent(String id, String name) {
Student student = new Student();
student.setId(id);
student.setName(name);
return student;
}
}

0 comments on commit 603b82d

Please sign in to comment.