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

https://github.com/equake/monjo · Java · 163 lines · 128 code · 35 blank · 0 comment · 0 complexity · 2f2f45d6b424a0d79fd9c225eb754a15 MD5 · raw file

  1. package org.monjo.core.conversion;
  2. import static org.hamcrest.Matchers.equalTo;
  3. import static org.hamcrest.Matchers.is;
  4. import static org.hamcrest.Matchers.nullValue;
  5. import static org.junit.Assert.assertThat;
  6. import static org.mockito.Mockito.never;
  7. import static org.mockito.Mockito.spy;
  8. import static org.mockito.Mockito.verify;
  9. import static org.monjo.test.util.HamcrestPatch.classEqualTo;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.monjo.example.SimplePOJO;
  15. import org.monjo.example.SimplePOJOWithStringId;
  16. import org.monjo.test.util.MongoDBTest;
  17. import com.mongodb.BasicDBObject;
  18. import com.mongodb.DBObject;
  19. import contrib.org.hibernate.cfg.DefaultNamingStrategy;
  20. public class DefaultDocumentToObjectConverterTest extends MongoDBTest {
  21. @Before
  22. public void setUp() throws Exception {
  23. MongoDBTest.getMonjoCollection().drop();
  24. }
  25. @Test(expected = IllegalStateException.class)
  26. public void shouldThrowIllegalStateExceptionIfToMethodIsCalledWithouthCallingFromMethodFirst() throws Exception {
  27. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  28. converter.setNamingStrategy(new DefaultNamingStrategy());
  29. converter.to();
  30. }
  31. @Test
  32. public void shouldConvertASimpleDocumentWithStringFieldsToAJavaObject() throws IllegalArgumentException, Exception {
  33. DBObject document = new BasicDBObject();
  34. document.put("aField", "aFieldValue");
  35. document.put("anotherField", "anotherFieldValue");
  36. saveToMongo(document);
  37. DBObject docFromMongo = getFromMongo(document.get("_id"));
  38. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  39. converter.setNamingStrategy(new DefaultNamingStrategy());
  40. SimplePOJO convertedObject = converter.from(docFromMongo).to();
  41. assertThat(convertedObject.getaField(), is(equalTo("aFieldValue")));
  42. assertThat(convertedObject.getAnotherField(), is(equalTo("anotherFieldValue")));
  43. }
  44. @Test
  45. public void shouldOnlyConvertFieldIfTheDocumentContainsAMatchingField() throws IllegalArgumentException, Exception {
  46. DBObject document = new BasicDBObject();
  47. document.put("aField", "aFieldValue");
  48. saveToMongo(document);
  49. DBObject doc = spy(document);
  50. DBObject docFromMongo = getFromMongo(document.get("_id"));
  51. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  52. converter.setNamingStrategy(new DefaultNamingStrategy());
  53. SimplePOJO convertedObject = converter.from(docFromMongo).to();
  54. assertThat(convertedObject.getaField(), is(equalTo("aFieldValue")));
  55. assertThat(convertedObject.getAnotherField(), is(nullValue()));
  56. verify(doc, never()).get("anotherField");
  57. }
  58. @Test
  59. public void shouldConvertNumericValues() throws IllegalArgumentException, Exception {
  60. DBObject document = new BasicDBObject();
  61. document.put("anIntegerField", 42);
  62. document.put("aLongField", 43L);
  63. document.put("aDoubleField", 44.0);
  64. document.put("aFloatField", 45.0f);
  65. saveToMongo(document);
  66. DBObject docFromMongo = getFromMongo(document.get("_id"));
  67. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  68. converter.setNamingStrategy(new DefaultNamingStrategy());
  69. SimplePOJO convertedObject = converter.from(docFromMongo).to();
  70. assertThat(convertedObject.getAnIntegerField(), is(equalTo(42)));
  71. assertThat(convertedObject.getaLongField(), is(equalTo(43L)));
  72. assertThat(convertedObject.getaDoubleField(), is(equalTo(44.0)));
  73. }
  74. @Test
  75. public void shouldConvertMapObject() {
  76. HashMap<String, String> map = new HashMap<String, String>();
  77. map.put("key1", "value1");
  78. map.put("key2", "value2");
  79. DBObject document = new BasicDBObject();
  80. document.put("anIntegerField", 42);
  81. document.put("aLongField", 43L);
  82. document.put("aDoubleField", 44.0);
  83. document.put("aFloatField", 45.0f);
  84. document.put("aMapField", map);
  85. saveToMongo(document);
  86. DBObject docFromMongo = getFromMongo(document.get("_id"));
  87. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  88. converter.setNamingStrategy(new DefaultNamingStrategy());
  89. SimplePOJO convertedObject = converter.from(docFromMongo).to();
  90. assertThat(convertedObject.getaMapField(), is(equalTo(map)));
  91. }
  92. @Test
  93. public void shouldPopulateIdWithMongosGeneratedIdValue() throws IllegalArgumentException, Exception {
  94. DBObject document = new BasicDBObject();
  95. saveToMongo(document);
  96. Object documentId = document.get("_id");
  97. DBObject docFromMongo = getFromMongo(documentId);
  98. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  99. converter.setNamingStrategy(new DefaultNamingStrategy());
  100. SimplePOJO convertedObject = converter.from(docFromMongo).to();
  101. assertThat(convertedObject.getId(), is(equalTo(documentId)));
  102. }
  103. @Test
  104. public void shouldPopulateStringId() throws IllegalArgumentException, Exception {
  105. DBObject document = new BasicDBObject();
  106. document.put("_id", "abcd1234");
  107. saveToMongo(document);
  108. Object documentId = document.get("_id");
  109. DBObject docFromMongo = getFromMongo(documentId);
  110. DocumentToObjectConverter<SimplePOJOWithStringId> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJOWithStringId.class);
  111. converter.setNamingStrategy(new DefaultNamingStrategy());
  112. SimplePOJOWithStringId convertedObject = converter.from(docFromMongo).to();
  113. Class<?> idClass = convertedObject.getId().getClass();
  114. assertThat(idClass, classEqualTo(String.class));
  115. assertThat(convertedObject.getId(), is(equalTo(documentId)));
  116. }
  117. @Test
  118. public void shouldNotPopulateTransientFields() throws IllegalArgumentException, Exception {
  119. DBObject document = new BasicDBObject();
  120. document.put("aTransientField", "transient");
  121. saveToMongo(document);
  122. DBObject docFromMongo = getFromMongo(document.get("_id"));
  123. DocumentToObjectConverter<SimplePOJO> converter = MonjoConverterFactory.getInstance().getDefaultDocumentConverter(SimplePOJO.class);
  124. converter.setNamingStrategy(new DefaultNamingStrategy());
  125. SimplePOJO simplePOJO = converter.from(docFromMongo).to();
  126. assertThat(simplePOJO.getaTransientField(), is(nullValue()));
  127. }
  128. }