PageRenderTime 81ms CodeModel.GetById 30ms 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
  1. package whiteboard.persistence.mongodb;
  2. import com.google.gson.Gson;
  3. import com.google.gson.JsonElement;
  4. import com.mongodb.DB;
  5. import com.mongodb.Mongo;
  6. import org.codehaus.jackson.JsonNode;
  7. import org.codehaus.jackson.map.ObjectMapper;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import whiteboard.colors.CyclingHtmlColors;
  12. import whiteboard.persistence.WhiteboardDetail;
  13. import static junit.framework.Assert.assertEquals;
  14. import static junit.framework.Assert.assertNotNull;
  15. import static junit.framework.Assert.assertNull;
  16. /**
  17. * @author A. Y. Kereena Davidsen <yani.kereena@gmail.com>
  18. */
  19. public class MongoDBPersistenceTest {
  20. DB database;
  21. MongoDBPersistence testing;
  22. @After
  23. public void tearDown() throws Exception {
  24. this.database.dropDatabase();
  25. }
  26. @Before
  27. public void setUp() throws Exception {
  28. Mongo mongo = new Mongo();
  29. this.database = mongo.getDB("whiteboard-testing");
  30. testing = new MongoDBPersistence(this.database, new CyclingHtmlColors());
  31. }
  32. @Test
  33. public void testCreate() throws Exception {
  34. assertEquals(0, database.getCollection("whiteboards").count());
  35. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  36. assertNotNull(detail);
  37. assertEquals("aaaa", detail.owner);
  38. assertEquals("ttttt", detail.title);
  39. assertEquals("dddddddddd", detail.description);
  40. assertNotNull(detail.boardID);
  41. assertEquals(1, database.getCollection("whiteboards").count());
  42. }
  43. @Test
  44. public void testCreate2() throws Exception {
  45. assertEquals(0, database.getCollection("whiteboards").count());
  46. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  47. WhiteboardDetail detail2 = testing.create("aaaa", "ttttt", "dddddddddd");
  48. assertEquals(detail.boardID, detail2.boardID);
  49. assertEquals(1, database.getCollection("whiteboards").count());
  50. }
  51. @Test
  52. public void testFindByBoardID() throws Exception {
  53. assertEquals(0, database.getCollection("whiteboards").count());
  54. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  55. assertNotNull(detail);
  56. assertNotNull(detail.boardID);
  57. assertEquals(1, database.getCollection("whiteboards").count());
  58. WhiteboardDetail success = testing.findByBoardID(detail.boardID);
  59. assertNotNull(success);
  60. assertEquals("aaaa", success.owner);
  61. assertEquals("ttttt", success.title);
  62. assertEquals("dddddddddd", success.description);
  63. assertEquals(success.boardID, detail.boardID);
  64. WhiteboardDetail failure = testing.findByBoardID(detail.boardID + "morestuff");
  65. assertNull(failure);
  66. }
  67. @Test
  68. public void testAddUser() throws Exception {
  69. assertEquals(0, database.getCollection("whiteboards").count());
  70. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  71. assertNotNull(detail);
  72. assertNotNull(detail.boardID);
  73. assertEquals(0, detail.users.size());
  74. assertEquals(1, database.getCollection("whiteboards").count());
  75. WhiteboardDetail updated = testing.addUser(detail.boardID, "kkkkkkk");
  76. assertNotNull(updated);
  77. assertEquals(1, updated.users.size());
  78. assertEquals("kkkkkkk", updated.users.keySet().iterator().next());
  79. assertNotNull(updated.users.get("kkkkkkk"));
  80. assertEquals(1, database.getCollection("whiteboards").count());
  81. WhiteboardDetail found = testing.findByBoardID(detail.boardID);
  82. assertNotNull(found);
  83. assertEquals(1, found.users.size());
  84. assertEquals("kkkkkkk", updated.users.keySet().iterator().next());
  85. assertNotNull(updated.users.get("kkkkkkk"));
  86. }
  87. @Test
  88. public void testAddDrawingItem() throws Exception {
  89. JsonNode json = new ObjectMapper().readValue("[\"a\",\"b\"]", JsonNode.class);
  90. WhiteboardDetail.DrawingItem item = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee", json);
  91. assertEquals(0, database.getCollection("whiteboards").count());
  92. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  93. assertNotNull(detail);
  94. assertNotNull(detail.boardID);
  95. assertEquals(0, detail.items.size());
  96. assertEquals(1, database.getCollection("whiteboards").count());
  97. WhiteboardDetail updated = testing.addDrawingItem(detail.boardID, item);
  98. assertNotNull(updated);
  99. assertEquals(1, updated.items.size());
  100. WhiteboardDetail.DrawingItem di = updated.items.get(0);
  101. assertEquals("kkkkkkk", di.username);
  102. assertEquals("ee", di.elementID);
  103. assertNotNull(di.elementData);
  104. assertEquals(1, database.getCollection("whiteboards").count());
  105. WhiteboardDetail found = testing.findByBoardID(detail.boardID);
  106. assertNotNull(found);
  107. assertEquals(1, updated.items.size());
  108. di = updated.items.get(0);
  109. assertEquals("kkkkkkk", di.username);
  110. assertEquals("ee", di.elementID);
  111. assertNotNull(di.elementData);
  112. System.out.println("elementData loaded = " + di.elementData);
  113. }
  114. @Test
  115. public void testRemoveDrawingItem() throws Exception {
  116. JsonNode json = new ObjectMapper().readValue("[\"a\",\"b\"]", JsonNode.class);
  117. WhiteboardDetail.DrawingItem item = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee1", json);
  118. WhiteboardDetail.DrawingItem item2 = new WhiteboardDetail.DrawingItem("kkkkkkk", "ee2", json);
  119. assertEquals(0, database.getCollection("whiteboards").count());
  120. WhiteboardDetail detail = testing.create("aaaa", "ttttt", "dddddddddd");
  121. assertNotNull(detail);
  122. assertNotNull(detail.boardID);
  123. assertEquals(0, detail.items.size());
  124. assertEquals(1, database.getCollection("whiteboards").count());
  125. testing.addDrawingItem(detail.boardID, item);
  126. testing.addDrawingItem(detail.boardID, item2);
  127. WhiteboardDetail found = testing.findByBoardID(detail.boardID);
  128. assertEquals(2, found.items.size());
  129. testing.removeDrawingItem(detail.boardID, "ee1");
  130. found = testing.findByBoardID(detail.boardID);
  131. assertEquals(1, found.items.size());
  132. assertEquals("ee2", found.items.get(0).elementID);
  133. }
  134. }