### Question:
ApplicationDataStore extends CommonDatastore<ApplicationItem, Application> implements DatastoreHasAdd<ApplicationItem>,
DatastoreHasUpdate<ApplicationItem>,
DatastoreHasGet<ApplicationItem>,DatastoreHasSearch<ApplicationItem>, DatastoreHasDelete { @Override public ApplicationItem add(final ApplicationItem item) { try { final Page homePageDef = pageAPI.getPageByName(CUSTOMPAGE_HOME); final ApplicationCreator creator = converter.toApplicationCreator(item); final Application application = applicationAPI.createApplication(creator); final ApplicationPage appHomePage = applicationAPI.createApplicationPage(application.getId(), homePageDef.getId(), "home"); applicationAPI.setApplicationHomePage(application.getId(), appHomePage.getId()); return converter.toApplicationItem(application); }catch (final Exception e) { throw new APIException(e); } } ApplicationDataStore(final APISession engineSession, final ApplicationAPI applicationAPI, final PageAPI pageAPI, final ApplicationItemConverter converter); @Override void delete(final List<APIID> ids); @Override ApplicationItem get(final APIID id); @Override ApplicationItem add(final ApplicationItem item); @Override ApplicationItem update(final APIID id, final Map<String, String> attributes); @Override ItemSearchResult<ApplicationItem> search(final int page, final int resultsByPage, final String search, final String orders,
            final Map<String, String> filters);  }### Answer:
@Test public void should_return_application_created_by_ApplicationAPI_converted_to_ApplicationItem_on_add() throws Exception { final ApplicationCreator creator = new ApplicationCreator("app", "My application", "1.0"); ApplicationItem app = new ApplicationItem(); given(converter.toApplicationCreator(app)).willReturn(creator); final ApplicationImpl application = new ApplicationImpl("app", "1.0", "app desc", 2L, 3L); given(applicationAPI.createApplication(creator)).willReturn(application); final ApplicationItem item = new ApplicationItem(); given(converter.toApplicationItem(application)).willReturn(item); given(pageAPI.getPageByName("custompage_home")).willReturn(homePage); given(homePage.getId()).willReturn(1L); given(applicationAPI.createApplicationPage(application.getId(), 1, "home")).willReturn(applicationPage); final ApplicationItem createdItem = dataStore.add(new ApplicationItem()); assertThat(createdItem).isEqualTo(item); }
@Test public void should_create_default_home_page_on_add() throws Exception { final ApplicationCreator creator = new ApplicationCreator("app", "My application", "1.0"); ApplicationItem item = new ApplicationItem(); given(converter.toApplicationCreator(item)).willReturn(creator); final ApplicationImpl application = new ApplicationImpl("app", "1.0", "app desc", 2L, 3L); application.setId(1); given(applicationAPI.createApplication(creator)).willReturn(application); final ApplicationItem convertedAppItem = new ApplicationItem(); given(converter.toApplicationItem(application)).willReturn(convertedAppItem); given(pageAPI.getPageByName("custompage_home")).willReturn(homePage); given(homePage.getId()).willReturn(1L); given(applicationAPI.createApplicationPage(application.getId(), 1L, "home")).willReturn(applicationPage); given(applicationPage.getId()).willReturn(3L); dataStore.add(new ApplicationItem()); verify(applicationAPI, times(1)).setApplicationHomePage(application.getId(), 3); }
@Test(expected = APIException.class) public void should_throw_APIException_when_ApplicationAPI_throws_an_exception_on_add() throws Exception { given(applicationAPI.createApplication(any(ApplicationCreator.class))).willThrow(new CreationException("")); dataStore.add(new ApplicationItem()); }