Skip to content

Commit

Permalink
Merge 65a100c into 603b82d
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandroRenzi committed Nov 28, 2016
2 parents 603b82d + 65a100c commit 70bcd5c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
Expand Down Expand Up @@ -33,11 +34,19 @@ public void updateDB(String id, String name) {
}

public boolean exists(String id) {
if (takeStudentsById(id) != null) {
return true;
}
return false;
}

public Student takeStudentsById(String id) {
return null;
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("id", id);
DBObject toFind = students.findOne(searchQuery);
return toFind != null ?
new Student((String) toFind.get("id"), (String) toFind.get("name")) :
null;
}

public void add(Student student) {
Expand Down
Expand Up @@ -37,13 +37,34 @@ public void testGetAllStudentsEmpty() {

@Test
public void testGetAllStudentsNotEmpty(){
BasicDBObject document = new BasicDBObject();
document.put("id", "1");
document.put("name", "first");
students.insert(document);
addStudent("1", "first");
assertEquals(1, database.getAllStudentsList().size());
assertEquals("1", database.getAllStudentsList().get(0).getId());
assertEquals("first", database.getAllStudentsList().get(0).getName());
}

private void addStudent(String id, String name) {
BasicDBObject document = new BasicDBObject();
document.put("id", id);
document.put("name", name);
students.insert(document);
}

@Test
public void testGetStudentByIdNotFound(){
addStudent("1", "first");
assertNull(database.takeStudentsById("2"));
}

@Test
public void testGetStudentByIdFound(){
addStudent("1", "first");
Student found = database.takeStudentsById("1");
assertNotNull(found);
assertEquals("1", found.getId());
assertEquals("first", found.getName());
}


}

0 comments on commit 70bcd5c

Please sign in to comment.