### Question:
UtilMongoDB { public static <T> DBObject getBsonFromObject(T t) throws IllegalAccessException, InstantiationException, InvocationTargetException { Field[] fields = AnnotationUtils.filterDeepFields(t.getClass()); DBObject bson = new BasicDBObject(); for (Field field : fields) { Method method = Utils.findGetter(field.getName(), t.getClass()); Object object = method.invoke(t); if (object != null) { if (Collection.class.isAssignableFrom(field.getType())) { Collection c = (Collection) object; Iterator iterator = c.iterator(); List innerBsonList = new ArrayList<>(); while (iterator.hasNext()) { innerBsonList.add(getBsonFromObject(iterator.next())); } bson.put(AnnotationUtils.deepFieldName(field), innerBsonList); } else if (IDeepType.class.isAssignableFrom(field.getType())) { bson.put(AnnotationUtils.deepFieldName(field), getBsonFromObject((IDeepType) object)); } else { bson.put(AnnotationUtils.deepFieldName(field), object); } } } return bson; } private  UtilMongoDB(); static T getObjectFromBson(Class<T> classEntity, BSONObject bsonObject); static DBObject getBsonFromObject(T t); static Object getId(T t); static Cells getCellFromBson(BSONObject bsonObject, String tableName); static DBObject getDBObjectFromCell(Cells cells); static final String MONGO_DEFAULT_ID; }### Answer:
@Test public void testGetBsonFromObject() throws UnknownHostException, NoSuchFieldException, IllegalAccessException, InvocationTargetException, InstantiationException { MetadataEntity metadataEntity = new MetadataEntity(); metadataEntity.setAuthor(AUTHOR); metadataEntity.setTitle(TITLE); metadataEntity.setSource(SOURCE); List<CantoEntity> CantoEntities = new ArrayList<>(); CantoEntity cantoI = new CantoEntity(); cantoI.setNumber(CANTO_I); cantoI.setText(TEXT_I); CantoEntities.add(cantoI); CantoEntity cantoII = new CantoEntity(); cantoII.setNumber(CANTO_II); cantoII.setText(TEXT_II); CantoEntities.add(cantoII); BookEntity bookEntity = new BookEntity(); bookEntity.setCantoEntities(CantoEntities); bookEntity.setMetadataEntity(metadataEntity); BSONObject bson = UtilMongoDB.getBsonFromObject(bookEntity); BSONObject metadataFromBson = (BSONObject) bson.get("metadata"); assertEquals(metadataFromBson.get("author"), AUTHOR); assertEquals(metadataFromBson.get("title"), TITLE); assertEquals(metadataFromBson.get("source"), SOURCE); List<BSONObject> cantoEntityListFromBson = (List<BSONObject>) bson.get("cantos"); assertEquals(cantoEntityListFromBson.get(0).get("canto"), CANTO_I); assertEquals(cantoEntityListFromBson.get(0).get("text"), TEXT_I); assertEquals(cantoEntityListFromBson.get(1).get("canto"), CANTO_II); assertEquals(cantoEntityListFromBson.get(1).get("text"), TEXT_II); }