### Question:
MongoDbPortalPreferenceRepository implements PortalPreferenceRepository { @Override public PortalPreference save(PortalPreference item) { PortalPreference fromDb = getByKey(item.getKey()); MongoDbPortalPreference converted = converter.convert(item, PortalPreference.class); if(fromDb != null) { converted.setId(((MongoDbPortalPreference)fromDb).getId()); } template.save(converted, PREFERENCE_COLLECTION); converter.hydrate(converted, PortalPreference.class); return converted; }  @Override List<PortalPreference> getAll(); @Override List<PortalPreference> getLimitedList(int offset, int pageSize); @Override int getCountAll(); @Override PortalPreference getByKey(String key); @Override Class<? extends PortalPreference> getType(); @Override PortalPreference get(String id); @Override PortalPreference save(PortalPreference item); @Override void delete(PortalPreference item); void setTemplate(MongoOperations template); void setConverter(HydratingConverterFactory converter); static final Class<PortalPreferenceImpl> CLASS; }### Answer:
@Test public void save_Valid(){ PortalPreference item = new PortalPreferenceImpl(); item.setKey("123"); PortalPreference fromDb = new MongoDbPortalPreference(); ((MongoDbPortalPreference)fromDb).setId("123"); PortalPreference converted = new MongoDbPortalPreference(); expect(converter.convert(item, PortalPreference.class)).andReturn(converted); expect(template.findOne(query(where("key").is("123")), preferenceRepository.CLASS, CollectionNames.PREFERENCE_COLLECTION)).andReturn((MongoDbPortalPreference)fromDb); template.save(converted, CollectionNames.PREFERENCE_COLLECTION); expectLastCall(); converter.hydrate(converted, PortalPreference.class); expectLastCall(); replay(template, converter); PortalPreference result = preferenceRepository.save(item); assertNotNull(((MongoDbPortalPreference) converted).getId()); assertThat(result, is(sameInstance(converted))); }
@Test public void save_Null(){ PortalPreference item = new PortalPreferenceImpl(); item.setKey("123"); PortalPreference converted = new MongoDbPortalPreference(); expect(template.findOne(query(where("key").is("123")), preferenceRepository.CLASS, CollectionNames.PREFERENCE_COLLECTION)).andReturn(null); expect(converter.convert(item, PortalPreference.class)).andReturn(converted); template.save(converted, PREFERENCE_COLLECTION); expectLastCall(); converter.hydrate(converted, PortalPreference.class); expectLastCall(); replay(template, converter); PortalPreference result = preferenceRepository.save(item); assertThat(result, is(sameInstance(converted))); }