### Question:
EntityWriteConverter implements Converter<Entity, DBObject> { @Override public DBObject convert(Entity e) { MongoEntity me; if (e instanceof MongoEntity) { me = (MongoEntity) e; } else { me = new MongoEntity(e.getType(), e.getEntityId(), e.getBody(), e.getMetaData(), e.getCalculatedValues(), e.getAggregates()); } if (encrypt != null) { me.encrypt(encrypt); } return me.toDBObject(uuidGeneratorStrategy, naturalKeyExtractor); }  @Override DBObject convert(Entity e);  }### Answer:
@Test public void testEntityConvert() throws NoNaturalKeysDefinedException { Entity e = Mockito.mock(Entity.class); HashMap<String, Object> body = new HashMap<String, Object>(); body.put("field1", "field1"); body.put("field2", "field2"); HashMap<String, Object> meta = new HashMap<String, Object>(); meta.put("meta1", "field1"); meta.put("meta2", "field2"); NaturalKeyDescriptor desc = new NaturalKeyDescriptor(); Mockito.when(e.getType()).thenReturn("collection"); Mockito.when(e.getBody()).thenReturn(body); Mockito.when(e.getMetaData()).thenReturn(meta); Mockito.when(naturalKeyExtractor.getNaturalKeyDescriptor(Mockito.any(Entity.class))).thenReturn(desc); Mockito.when(uuidGeneratorStrategy.generateId(desc)).thenReturn("uid"); DBObject d = converter.convert(e); Assert.assertNotNull("DBObject should not be null", d); assertSame(body, (Map<?, ?>) d.get("body")); assertSame(meta, (Map<?, ?>) d.get("metaData")); Assert.assertEquals(1, encryptCalls.size()); Assert.assertEquals(2, encryptCalls.get(0).length); Assert.assertEquals(e.getType(), encryptCalls.get(0)[0]); Assert.assertEquals(e.getBody(), encryptCalls.get(0)[1]); }
@Test public void testMockMongoEntityConvert() { MongoEntity e = Mockito.mock(MongoEntity.class); DBObject result = Mockito.mock(DBObject.class); Mockito.when(e.toDBObject(uuidGeneratorStrategy, naturalKeyExtractor)).thenReturn(result); DBObject d = converter.convert(e); Assert.assertNotNull(d); Assert.assertEquals(result, d); Mockito.verify(e, Mockito.times(1)).encrypt(encryptor); Mockito.verify(e, Mockito.times(1)).toDBObject(uuidGeneratorStrategy, naturalKeyExtractor); }
@Test public void testMongoEntityConvert() { HashMap<String, Object> body = new HashMap<String, Object>(); body.put("field1", "field1"); body.put("field2", "field2"); HashMap<String, Object> meta = new HashMap<String, Object>(); meta.put("meta1", "field1"); meta.put("meta2", "field2"); MongoEntity e = new MongoEntity("collection", UUID.randomUUID().toString(), body, meta); DBObject d = converter.convert(e); Assert.assertNotNull(d); assertSame(body, (Map<?, ?>) d.get("body")); assertSame(meta, (Map<?, ?>) d.get("metaData")); Assert.assertEquals(1, encryptCalls.size()); Assert.assertEquals(2, encryptCalls.get(0).length); Assert.assertEquals(e.getType(), encryptCalls.get(0)[0]); Assert.assertEquals(e.getBody(), encryptCalls.get(0)[1]); }