/
DbMongoTest.java
70 lines (54 loc) · 1.66 KB
/
DbMongoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.examples.exercise;
import static org.junit.Assert.*;
import java.net.UnknownHostException;
import org.junit.Before;
import org.junit.Test;
import com.github.fakemongo.Fongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
public class DbMongoTest {
private DbMongo database;
private DBCollection students;
@Before
public void setUp() throws UnknownHostException{
Fongo fongo = new Fongo("exercise server");
MongoClient mongoClient = fongo.getMongo();
//check and drop some trash
DB db = mongoClient.getDB("schoolExcercise");
db.getCollection("student").drop();
database = new DbMongo(mongoClient);
students = db.getCollection("student");
}
@Test
public void testGetAllStudentsEmpty() {
assertTrue(database.getAllStudentsList().isEmpty());
}
@Test
public void testGetAllStudentsNotEmpty(){
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());
}
}