MalariaDataReportFormPresenter extends BaseReportPresenter { public Observable<MalariaProgram> onSaveForm(final ViaReportStatus status, final String sign) { return Observable.create((Observable.OnSubscribe<MalariaProgram>) subscriber -> { try { MalariaProgram malariaProgram = getMalariaProgram(); malariaProgram.setStatus(status); if (status.equals(ViaReportStatus.SUBMITTED)) { malariaProgram.setVerifiedBy(sign); } else { malariaProgram.setCreatedBy(sign); } patientDataService.save(malariaProgram); subscriber.onNext(malariaProgram); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); new LMISException(e, "MalariaData:.onSaveForm").reportToFabric(); } }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()); } MalariaDataReportFormPresenter(); @Override void attachView(BaseView v); Observable<List<ImplementationReportViewModel>> getImplementationViewModelsForCurrentMalariaProgram(final Period period); MalariaProgram getMalariaProgram(); List<ImplementationReportViewModel> regenerateImplementationModels(ImplementationReportViewModel updatedViewModel); Observable<MalariaProgram> onSaveForm(final ViaReportStatus status, final String sign); @Override void deleteDraft(); @Override boolean isDraft(); boolean isSubmittedForApproval(); static final int INVALID_INDEX; }
@Test public void shouldSaveMalariaProgramWithCorrectStatusAndSignWhenStatusIsSubmitted() throws InvocationTargetException, NoSuchMethodException, LMISException, IllegalAccessException { initializeImplementationReportViewModels(); when(patientDataService.save(malariaProgram)).thenReturn(Optional.of(malariaProgram)); MalariaDataReportViewModel malariaDataReportViewModel = anyObject(); when(malariaProgramMapper.map(malariaDataReportViewModel, eq(malariaProgram))).thenReturn(malariaProgram); Observable<MalariaProgram> observable = malariaDataReportFormPresenter.onSaveForm(ViaReportStatus.SUBMITTED, sign); observable.subscribe(malariaProgramTestSubscriber); malariaProgramTestSubscriber.awaitTerminalEvent(); malariaProgramTestSubscriber.assertNoErrors(); MalariaProgram actualMalariaProgram = malariaProgramTestSubscriber.getOnNextEvents().get(0); assertThat(actualMalariaProgram.getStatus(), is(ViaReportStatus.SUBMITTED)); assertThat(actualMalariaProgram.getVerifiedBy(), is(sign)); }
@Test public void shouldSaveMalariaProgramWithCorrectStatusAndSignWhenStatusIsNotSubmitted() throws InvocationTargetException, NoSuchMethodException, LMISException, IllegalAccessException { initializeImplementationReportViewModels(); when(patientDataService.save(malariaProgram)).thenReturn(Optional.of(malariaProgram)); MalariaDataReportViewModel malariaDataReportViewModel = anyObject(); when(malariaProgramMapper.map(malariaDataReportViewModel, eq(malariaProgram))).thenReturn(malariaProgram); Observable<MalariaProgram> observable = malariaDataReportFormPresenter.onSaveForm(ViaReportStatus.MISSING, sign); observable.subscribe(malariaProgramTestSubscriber); malariaProgramTestSubscriber.awaitTerminalEvent(); malariaProgramTestSubscriber.assertNoErrors(); MalariaProgram actualMalariaProgram = malariaProgramTestSubscriber.getOnNextEvents().get(0); assertThat(actualMalariaProgram.getStatus(), is(ViaReportStatus.MISSING)); assertThat(actualMalariaProgram.getCreatedBy(), is(sign)); }
@Test public void shouldThrowAnExceptionWhenOnSaveForm() throws LMISException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { initializeImplementationReportViewModels(); MalariaDataReportViewModel malariaDataReportViewModel = anyObject(); when(malariaProgramMapper.map(malariaDataReportViewModel, eq(malariaProgram))).thenReturn(malariaProgram); doThrow(LMISException.class).when(patientDataService).save(malariaProgram); Observable<MalariaProgram> observable = malariaDataReportFormPresenter.onSaveForm(ViaReportStatus.MISSING, sign); observable.subscribe(malariaProgramTestSubscriber); malariaProgramTestSubscriber.awaitTerminalEvent(); malariaProgramTestSubscriber.assertError(LMISException.class); }