FormDataRepository implements FormRepository { @Override public Observable<Boolean> loadForm(String formId, String deviceId) { final DatabaseDataSource dataBaseDataSource = dataSourceFactory.getDataBaseDataSource(); if (TEST_FORM_ID.equals(formId)) { return dataBaseDataSource.installTestForm(); } else { return downloadFormHeader(formId, deviceId); } } @Inject  FormDataRepository(FormHeaderParser formHeaderParser, XmlFormParser xmlParser,
                              RestApi restApi, DataSourceFactory dataSourceFactory, FormIdMapper formIdMapper,
                              S3RestApi s3RestApi, DomainFormMapper domainFormMapper); @Override Observable<Boolean> loadForm(String formId, String deviceId); @Override Observable<Integer> reloadForms(final String deviceId); @Override Observable<Integer> downloadForms(final String deviceId); @Override @NotNull Single<DomainForm> getForm(@NotNull String formId);  }
@Test public void loadFormShouldInstallTestFormIfTestId() { TestObserver observer = new TestObserver<Boolean>(); when(mockDatabaseDataSource.installTestForm()).thenReturn(Observable.just(true)); formDataRepository.loadForm("0", "deviceId").subscribe(observer); verify(mockDatabaseDataSource, times(1)).installTestForm(); observer.assertNoErrors(); }
@Test public void loadFormShouldNotUpdateAlreadyUpdatedForm() { TestObserver observer = new TestObserver<Boolean>(); when(mockDatabaseDataSource.formNeedsUpdate(any(ApiFormHeader.class))) .thenReturn(Observable.just(false)); MockResponse response = new MockResponse().setResponseCode(200) .setBody(",1,cde,abc,cde,6.0,cde,true,33"); mockWebServer.enqueue(response); formDataRepository.loadForm("1", "deviceId").subscribe(observer); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoErrors(); observer.assertValueCount(1); verify(s3RestApi, times(0)).downloadArchive("123456.zip"); verify(mockFileDataSource, times(0)) .extractRemoteArchive(any(ResponseBody.class), anyString()); }
@Test public void loadFormShouldUpdateOutdatedForm() { TestObserver observer = new TestObserver<Boolean>(); when(mockDatabaseDataSource.formNeedsUpdate(any(ApiFormHeader.class))) .thenReturn(Observable.just(true)); when(mockXmlParser.parse(mockInputStream)).thenReturn(mockForm); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody(",1,cde,abc,cde,6.0,cde,true,33")); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("{}")); formDataRepository.loadForm("1", "deviceId").subscribe(observer); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoErrors(); observer.assertValueCount(1); verify(s3RestApi, times(1)).downloadArchive("123456.zip"); verify(mockFileDataSource, times(1)) .extractRemoteArchive(any(ResponseBody.class), anyString()); }