### Question:
MongoDbPageLayoutRepository implements PageLayoutRepository { @Override public PageLayout save(PageLayout item) { MongoDbPageLayout toSave = (MongoDbPageLayout)getByPageLayoutCode(item.getCode()); if(toSave == null) { toSave = new MongoDbPageLayout(); } update(item, toSave); template.save(toSave, PAGE_LAYOUT_COLLECTION); return toSave; }  @Override PageLayout getByPageLayoutCode(String codename); @Override List<PageLayout> getAll(); @Override List<PageLayout> getLimitedList(int offset, int limit); @Override int getCountAll(); @Override List<PageLayout> getAllUserSelectable(); @Override Class<? extends PageLayout> getType(); @Override PageLayout get(String id); @Override PageLayout save(PageLayout item); @Override void delete(PageLayout item); void setTemplate(MongoOperations template); static final Class<MongoDbPageLayout> CLASS; }### Answer:
@Test public void save_Valid() { PageLayout item1 = new MongoDbPageLayout(); item1.setCode("blah1"); expect(template.findOne(new Query(where("code").is(item1.getCode())), pageLayoutRepository.CLASS, CollectionNames.PAGE_LAYOUT_COLLECTION)).andReturn(null); template.save(isA(MongoDbPageLayout.class), eq(CollectionNames.PAGE_LAYOUT_COLLECTION)); expectLastCall(); replay(template); PageLayout saved = pageLayoutRepository.save(item1); assertThat(saved, is(instanceOf(MongoDbPageLayout.class))); assertThat(saved.getCode(), is(sameInstance(item1.getCode()))); assertThat(saved.getNumberOfRegions(), is(sameInstance(item1.getNumberOfRegions()))); assertThat(saved.getRenderSequence(), is(sameInstance(item1.getRenderSequence()))); assertThat(saved.isUserSelectable(), is(sameInstance(item1.isUserSelectable()))); }
@Test public void save_Null(){ PageLayout item1 = new MongoDbPageLayout(); item1.setCode("blah1"); item1.setNumberOfRegions((long)123); item1.setRenderSequence((long)432); item1.setUserSelectable(true); MongoDbPageLayout toSave = new MongoDbPageLayout(); expect(template.findOne(new Query(where("code").is(item1.getCode())), pageLayoutRepository.CLASS, CollectionNames.PAGE_LAYOUT_COLLECTION)).andReturn(toSave); template.save(isA(MongoDbPageLayout.class), eq(CollectionNames.PAGE_LAYOUT_COLLECTION)); expectLastCall(); replay(template); PageLayout saved = pageLayoutRepository.save(item1); assertThat(saved.getCode(), is(sameInstance(item1.getCode()))); assertThat(saved.getNumberOfRegions(), is(sameInstance(item1.getNumberOfRegions()))); assertThat(saved.getRenderSequence(), is(sameInstance(item1.getRenderSequence()))); assertThat(saved.isUserSelectable(), is(sameInstance(item1.isUserSelectable()))); }