PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/whiteboard/persistence/mongodb/MongoDBPersistenceTest.java

https://bitbucket.org/kereena/whiteboard
Java | 199 lines | 131 code | 65 blank | 3 comment | 0 complexity | b8cd6d63e08177a02b7b0c5560b431da MD5 | raw file
  1package whiteboard.persistence.mongodb;
  2
  3import com.google.gson.Gson;
  4import com.google.gson.JsonElement;
  5import com.mongodb.DB;
  6import com.mongodb.Mongo;
  7import org.codehaus.jackson.JsonNode;
  8import org.codehaus.jackson.map.ObjectMapper;
  9import org.junit.After;
 10import org.junit.Before;
 11import org.junit.Test;
 12import whiteboard.colors.CyclingHtmlColors;
 13import whiteboard.persistence.WhiteboardDetail;
 14
 15import static junit.framework.Assert.assertEquals;
 16import static junit.framework.Assert.assertNotNull;
 17import static junit.framework.Assert.assertNull;
 18
 19/**
 20 * @author A. Y. Kereena Davidsen <yani.kereena@gmail.com>
 21 */
 22public class MongoDBPersistenceTest {
 23
 24    DB database;
 25
 26    MongoDBPersistence testing;
 27
 28    @After
 29    public void tearDown() throws Exception {
 30        this.database.dropDatabase();
 31    }
 32
 33    @Before
 34    public void setUp() throws Exception {
 35        Mongo mongo = new Mongo();
 36        this.database = mongo.getDB("whiteboard-testing");
 37        testing = new MongoDBPersistence(this.database, new CyclingHtmlColors());
 38    }
 39
 40    @Test
 41    public void testCreate() throws Exception {
 42
 43        assertEquals(0, database.getCollection("whiteboards").count());
 44
 45        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
 46
 47        assertNotNull(detail);
 48
 49        assertEquals("aaaa", detail.owner);
 50        assertEquals("ttttt", detail.title);
 51        assertEquals("dddddddddd", detail.description);
 52
 53        assertNotNull(detail.boardID);
 54
 55        assertEquals(1, database.getCollection("whiteboards").count());
 56
 57    }
 58
 59    @Test
 60    public void testCreate2() throws Exception {
 61
 62        assertEquals(0, database.getCollection("whiteboards").count());
 63
 64        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
 65
 66        WhiteboardDetail detail2 = testing.create("aaaa", "ttttt", "dddddddddd");
 67
 68        assertEquals(detail.boardID, detail2.boardID);
 69
 70        assertEquals(1, database.getCollection("whiteboards").count());
 71
 72    }
 73
 74
 75    @Test
 76    public void testFindByBoardID() throws Exception {
 77
 78        assertEquals(0, database.getCollection("whiteboards").count());
 79
 80        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
 81
 82        assertNotNull(detail);
 83        assertNotNull(detail.boardID);
 84        assertEquals(1, database.getCollection("whiteboards").count());
 85
 86        WhiteboardDetail success = testing.findByBoardID(detail.boardID);
 87        assertNotNull(success);
 88        assertEquals("aaaa", success.owner);
 89        assertEquals("ttttt", success.title);
 90        assertEquals("dddddddddd", success.description);
 91        assertEquals(success.boardID, detail.boardID);
 92
 93        WhiteboardDetail failure = testing.findByBoardID(detail.boardID + "morestuff");
 94        assertNull(failure);
 95
 96    }
 97
 98    @Test
 99    public void testAddUser() throws Exception {
100
101        assertEquals(0, database.getCollection("whiteboards").count());
102
103        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
104
105        assertNotNull(detail);
106        assertNotNull(detail.boardID);
107        assertEquals(0, detail.users.size());
108        assertEquals(1, database.getCollection("whiteboards").count());
109
110        WhiteboardDetail updated = testing.addUser(detail.boardID, "kkkkkkk");
111
112        assertNotNull(updated);
113        assertEquals(1, updated.users.size());
114        assertEquals("kkkkkkk", updated.users.keySet().iterator().next());
115        assertNotNull(updated.users.get("kkkkkkk"));
116
117        assertEquals(1, database.getCollection("whiteboards").count());
118
119        WhiteboardDetail found = testing.findByBoardID(detail.boardID);
120        assertNotNull(found);
121        assertEquals(1, found.users.size());
122        assertEquals("kkkkkkk", updated.users.keySet().iterator().next());
123        assertNotNull(updated.users.get("kkkkkkk"));
124    }
125
126    @Test
127    public void testAddDrawingItem() throws Exception {
128
129        JsonNode json = new ObjectMapper().readValue("[\"a\",\"b\"]", JsonNode.class);
130
131        WhiteboardDetail.DrawingItem item = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee", json);
132
133        assertEquals(0, database.getCollection("whiteboards").count());
134
135        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
136
137        assertNotNull(detail);
138        assertNotNull(detail.boardID);
139        assertEquals(0, detail.items.size());
140        assertEquals(1, database.getCollection("whiteboards").count());
141
142        WhiteboardDetail updated = testing.addDrawingItem(detail.boardID, item);
143
144        assertNotNull(updated);
145        assertEquals(1, updated.items.size());
146        WhiteboardDetail.DrawingItem di = updated.items.get(0);
147
148        assertEquals("kkkkkkk", di.username);
149        assertEquals("ee", di.elementID);
150        assertNotNull(di.elementData);
151
152        assertEquals(1, database.getCollection("whiteboards").count());
153
154        WhiteboardDetail found = testing.findByBoardID(detail.boardID);
155        assertNotNull(found);
156        assertEquals(1, updated.items.size());
157        di = updated.items.get(0);
158
159        assertEquals("kkkkkkk", di.username);
160        assertEquals("ee", di.elementID);
161        assertNotNull(di.elementData);
162
163        System.out.println("elementData loaded = " + di.elementData);
164    }
165
166    @Test
167    public void testRemoveDrawingItem() throws Exception {
168
169        JsonNode json = new ObjectMapper().readValue("[\"a\",\"b\"]", JsonNode.class);
170
171        WhiteboardDetail.DrawingItem item = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee1", json);
172        WhiteboardDetail.DrawingItem item2 = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee2", json);
173
174        assertEquals(0, database.getCollection("whiteboards").count());
175
176        WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
177
178        assertNotNull(detail);
179        assertNotNull(detail.boardID);
180        assertEquals(0, detail.items.size());
181        assertEquals(1, database.getCollection("whiteboards").count());
182
183        testing.addDrawingItem(detail.boardID, item);
184        testing.addDrawingItem(detail.boardID, item2);
185
186        WhiteboardDetail found = testing.findByBoardID(detail.boardID);
187
188        assertEquals(2, found.items.size());
189
190        testing.removeDrawingItem(detail.boardID, "ee1");
191
192        found = testing.findByBoardID(detail.boardID);
193        assertEquals(1, found.items.size());
194
195        assertEquals("ee2", found.items.get(0).elementID);
196
197
198    }
199}