use of org.activityinfo.model.resource.RecordTransaction in project activityinfo by bedatadriven.
the class FormStoreTest method newRecordOffline.
@Test
public void newRecordOffline() {
TestSetup setup = new TestSetup();
Survey survey = setup.getSurveyForm();
// Synchronize the survey form
setup.setConnected(true);
setup.getFormStore().setFormOffline(survey.getFormId(), true);
setup.runScheduled();
// Go offline...
setup.setConnected(false);
// Monitor the pending queue status
Connection<PendingStatus> pendingStatus = setup.connect(setup.getOfflineStore().getPendingStatus());
assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
// Create a new survey record
FormInstance newRecordTyped = survey.getGenerator().get();
RecordTransaction tx = RecordTransaction.builder().create(newRecordTyped).build();
// Update a record...
Promise<Void> updateResult = setup.getFormStore().updateRecords(tx);
assertThat(updateResult.getState(), equalTo(Promise.State.FULFILLED));
// Now query offline...
Connection<Maybe<FormRecord>> recordView = setup.connect(setup.getFormStore().getRecord(newRecordTyped.getRef()));
// It should be listed as a pending change...
assertThat(pendingStatus.assertLoaded().getCount(), equalTo(1));
Maybe<FormRecord> record = recordView.assertLoaded();
assertThat(record.getState(), equalTo(Maybe.State.VISIBLE));
assertThat(record.get().getRecordId(), equalTo(newRecordTyped.getId().asString()));
// Finally go online and ensure that results are sent to the server
setup.setConnected(true);
setup.getOfflineStore().syncChanges();
// Our queue should be empty again
assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
}
Aggregations