/src/test/java/org/monjo/core/conversion/ComplexSaveTest.java

https://bitbucket.org/rdllopes/monjo · Java · 164 lines · 122 code · 40 blank · 2 comment · 0 complexity · 249e4958a9091877e03f3746e393f0b5 MD5 · raw file

  1. package org.monjo.core.conversion;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNotNull;
  4. import static org.monjo.test.util.MongoDBUtil.getMongoDB;
  5. import java.util.List;
  6. import org.bson.types.ObjectId;
  7. import org.junit.Assert;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.monjo.core.Monjo;
  11. import org.monjo.core.MonjoCursor;
  12. import org.monjo.example.AnotherPojo;
  13. import org.monjo.example.Category;
  14. import org.monjo.example.ComplexPojo;
  15. import org.monjo.example.ListWithin;
  16. import org.monjo.example.PojoWithListInnerObject;
  17. import org.monjo.example.User;
  18. import org.monjo.test.util.MongoDBTest;
  19. import contrib.org.hibernate.cfg.DefaultNamingStrategy;
  20. public class ComplexSaveTest extends MongoDBTest{
  21. @Before
  22. public void setUp() throws Exception {
  23. MonjoConverterFactory.getInstance()
  24. .configure(new DefaultNamingStrategy())
  25. .getDefaultObjectConverter(ListWithin.class);
  26. }
  27. @Test
  28. public void shouldSaveElementWithStringList(){
  29. ListWithin pojo = new ListWithin();
  30. pojo.addName(42);
  31. pojo.addName(43);
  32. Monjo<ObjectId, ListWithin> monjo = new Monjo<ObjectId, ListWithin>(getMongoDB(), ListWithin.class);
  33. ObjectId objectId = monjo.save(pojo);
  34. ListWithin listWithin = monjo.findOne(objectId);
  35. List<String> strings = listWithin.getNames();
  36. Assert.assertTrue(strings.contains("42"));
  37. Assert.assertTrue(strings.contains("43"));
  38. }
  39. @Test
  40. public void shouldSaveElementWithIntegerList(){
  41. ListWithin pojo = new ListWithin();
  42. pojo.addGroup(10);
  43. pojo.addGroup(20);
  44. Monjo<ObjectId, ListWithin> monjo = new Monjo<ObjectId, ListWithin>(getMongoDB(), ListWithin.class);
  45. ObjectId objectId = monjo.save(pojo);
  46. ListWithin listWithin = monjo.findOne(objectId);
  47. List<Integer> integers = listWithin.getGroups();
  48. Assert.assertTrue(integers.contains(10));
  49. Assert.assertTrue(integers.contains(20));
  50. }
  51. @Test
  52. public void shouldNotUseRef() throws Exception {
  53. PojoWithListInnerObject pojo = PojoBuilder.createMegaZordePojo();
  54. Monjo<ObjectId, PojoWithListInnerObject> monjoComplex = new Monjo<ObjectId, PojoWithListInnerObject>(getMongoDB(),
  55. PojoWithListInnerObject.class);
  56. monjoComplex.removeAll();
  57. monjoComplex.insert(pojo);
  58. assertNotNull(pojo.getCategories().get(0).getId());
  59. MonjoCursor<PojoWithListInnerObject> monjoCursor = monjoComplex.find();
  60. PojoWithListInnerObject complex = monjoCursor.toList().get(0);
  61. assertNotNull(complex.getCategories().get(0).getId());
  62. }
  63. @Test
  64. public void shouldAddNewCategory() throws Exception {
  65. PojoWithListInnerObject pojo = PojoBuilder.createMegaZordePojo();
  66. Monjo<ObjectId, PojoWithListInnerObject> monjoComplex = new Monjo<ObjectId, PojoWithListInnerObject>(getMongoDB(),
  67. PojoWithListInnerObject.class);
  68. monjoComplex.removeAll();
  69. monjoComplex.insert(pojo);
  70. Category otherCategory = new Category();
  71. otherCategory.setName("Other Category");
  72. pojo.addCategory(otherCategory);
  73. monjoComplex.update(pojo);
  74. MonjoCursor<PojoWithListInnerObject> monjoCursor = monjoComplex.find();
  75. PojoWithListInnerObject complex = monjoCursor.toList().get(0);
  76. assertEquals(2, complex.getCategories().size());
  77. }
  78. @Test
  79. public void shouldAddNewCategoryButIHaveOnlyAnId() throws Exception {
  80. PojoWithListInnerObject pojo = PojoBuilder.createMegaZordePojo();
  81. Monjo<ObjectId, PojoWithListInnerObject> monjoComplex = new Monjo<ObjectId, PojoWithListInnerObject>(getMongoDB(),
  82. PojoWithListInnerObject.class);
  83. monjoComplex.removeAll();
  84. monjoComplex.insert(pojo);
  85. PojoWithListInnerObject anotherPojo = new PojoWithListInnerObject();
  86. anotherPojo.setId(pojo.getId());
  87. Category otherCategory = new Category();
  88. otherCategory.setName("Other Category");
  89. anotherPojo.addCategory(otherCategory);
  90. monjoComplex.updateWithAddSet(anotherPojo);
  91. MonjoCursor<PojoWithListInnerObject> monjoCursor = monjoComplex.find();
  92. PojoWithListInnerObject complex = monjoCursor.toList().get(0);
  93. assertEquals(2, complex.getCategories().size());
  94. }
  95. @Test
  96. public void shouldNotUseRef2() throws Exception{
  97. User user = new User();
  98. user.setName("NewCategory");
  99. AnotherPojo anotherPojo = PojoBuilder.createAnotherPojo(user);
  100. Monjo<ObjectId, AnotherPojo> monjoComplex = new Monjo<ObjectId, AnotherPojo>(getMongoDB(), AnotherPojo.class);
  101. monjoComplex.removeAll();
  102. monjoComplex.insert(anotherPojo);
  103. MonjoCursor<AnotherPojo> monjoCursor = monjoComplex.find();
  104. AnotherPojo anotherPojo2 = monjoCursor.toList().get(0);
  105. assertEquals("NewCategory", anotherPojo2.getUser().getName());
  106. }
  107. @Test
  108. public void shouldUseRef() throws Exception{
  109. Category category = new Category();
  110. category.setName("NewCategory");
  111. Monjo<ObjectId, Category> monjoCategory = new Monjo<ObjectId, Category>(getMongoDB(), Category.class);
  112. monjoCategory.insert(category);
  113. ComplexPojo complexPojo = PojoBuilder.createComplexPojo(category);
  114. // complexPojo.setCategories(categories);
  115. Monjo<ObjectId, ComplexPojo> monjoComplex = new Monjo<ObjectId, ComplexPojo>(getMongoDB(), ComplexPojo.class);
  116. monjoComplex.removeAll();
  117. monjoCategory.removeAll();
  118. monjoComplex.insert(complexPojo);
  119. MonjoCursor<ComplexPojo> monjoCursor = monjoComplex.find();
  120. ComplexPojo complex = monjoCursor.toList().get(0);
  121. assertEquals(category.getId(), complex.getCategory().getId());
  122. // assertEquals(category.getId(), complex.getCategories().get(0).getId());
  123. }
  124. }