Search in sources :

Example 46 with FormInstance

use of org.activityinfo.model.form.FormInstance 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));
}
Also used : Maybe(org.activityinfo.promise.Maybe) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Aggregations

FormInstance (org.activityinfo.model.form.FormInstance)46 Test (org.junit.Test)26 ResourceId (org.activityinfo.model.resource.ResourceId)15 FormClass (org.activityinfo.model.form.FormClass)14 FieldValue (org.activityinfo.model.type.FieldValue)10 FormField (org.activityinfo.model.form.FormField)9 RecordRef (org.activityinfo.model.type.RecordRef)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 OnDataSet (org.activityinfo.server.database.OnDataSet)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 FormRecord (org.activityinfo.model.form.FormRecord)4 SerialNumber (org.activityinfo.model.type.SerialNumber)4 Map (java.util.Map)3 FormTree (org.activityinfo.model.formTree.FormTree)3 Maybe (org.activityinfo.promise.Maybe)3 ArrayList (java.util.ArrayList)2