MMIARequisitionPresenter extends BaseRequisitionPresenter { public Observable<Void> addCustomRegimenItem(final Regimen regimen) { return Observable.create((Observable.OnSubscribe<Void>) subscriber -> { try { if (!isRegimeItemExists(regimen)) { RegimenItem regimenItem = createRegimenItem(regimen); regimenItemRepository.create(regimenItem); rnRForm.getRegimenItemListWrapper().add(regimenItem); } } catch (LMISException e) { new LMISException(e, "MMIARequisitionPresenter.addCustomRegimenItem").reportToFabric(); subscriber.onError(e); } subscriber.onCompleted(); }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()); }  @Override void attachView(BaseView baseView); @Override void loadData(final long formId, Date periodEndDate); @Override void updateUIAfterSubmit(); @Override void updateFormUI(); void setViewModels(List<RnrFormItem> formItems,
                              List<RegimenItem> regimenItemList,
                              List<BaseInfoItem> baseInfoItemList,
                              List<RegimenItemThreeLines> regimenItemthreeList,
                              String comments); void setComments(String comments); Observable<Void> addCustomRegimenItem(final Regimen regimen); boolean isRegimeItemExists(Regimen regimen); Observable<Void> deleteRegimeItem(final RegimenItem item); Observable<Void> getSaveFormObservable(final List<RnrFormItem> rnrFormItems,
                                                  final List<RegimenItem> regimenItems,
                                                  final List<BaseInfoItem> baseInfoItems,
                                                  final List<RegimenItemThreeLines> threeLineItems,
                                                  final String comment);  }
@Test public void shouldCreateCustomRegimenItem() throws Exception { List<RegimenItem> regimenItemListWrapper = rnRForm.getRegimenItemListWrapper(); int size = regimenItemListWrapper.size(); Regimen regimen = new Regimen(); TestSubscriber<Void> subscriber = new TestSubscriber<>(); presenter.addCustomRegimenItem(regimen).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); verify(regimenItemRepository).create(any(RegimenItem.class)); assertThat(regimenItemListWrapper.size(), is(size + 1)); }
@Test public void shouldNotCreateCustomRegimenItemWhenExists() throws Exception { List<RegimenItem> regimenItemListWrapper = rnRForm.getRegimenItemListWrapper(); Regimen regimen = new Regimen(); regimen.setId(12); regimen.setType(Regimen.RegimeType.Paediatrics); regimen.setName("test"); RegimenItem regimenItem = new RegimenItem(); regimenItem.setRegimen(regimen); regimenItemListWrapper.add(regimenItem); int size = regimenItemListWrapper.size(); TestSubscriber<Void> subscriber = new TestSubscriber<>(); presenter.addCustomRegimenItem(regimen).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); verify(regimenItemRepository, never()).create(any(RegimenItem.class)); assertThat(regimenItemListWrapper.size(), is(size)); }