@@ -0,0 +1,3228 @@
+package org.hisp.dhis.android.core.common;%0A%0Aimport android.database.Cursor;%0Aimport android.support.test.runner.AndroidJUnit4;%0A%0Aimport com.google.common.truth.Truth;%0A%0Aimport org.hisp.dhis.android.core.D2;%0Aimport org.hisp.dhis.android.core.data.database.AbsStoreTestCase;%0Aimport org.hisp.dhis.android.core.data.file.AssetsFileReader;%0Aimport org.hisp.dhis.android.core.data.server.Dhis2MockServer;%0Aimport org.hisp.dhis.android.core.user.UserCredentialsModel;%0Aimport org.hisp.dhis.android.core.user.UserCredentialsStoreImpl;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0A%0Aimport java.io.IOException;%0Aimport java.util.concurrent.Callable;%0A%0Aimport static org.hisp.dhis.android.core.data.database.CursorAssert.assertThatCursor;%0A%0A@RunWith(AndroidJUnit4.class)%0Apublic class ForeignKeyCleanerShould extends AbsStoreTestCase %7B%0A%0A    private Dhis2MockServer dhis2MockServer;%0A    private D2 d2;%0A    private final String%5B%5D PROJECTION = %7B%0A            UserCredentialsModel.Columns.UID,%0A            UserCredentialsModel.Columns.USER%0A    %7D;%0A%0A    @Override%0A    @Before%0A    public void setUp() throws IOException %7B%0A        super.setUp();%0A%0A        dhis2MockServer = new Dhis2MockServer(new AssetsFileReader());%0A%0A        d2 = D2Factory.create(dhis2MockServer.getBaseEndpoint(), databaseAdapter());%0A    %7D%0A%0A    @Override%0A    @After%0A    public void tearDown() throws IOException %7B%0A        super.tearDown();%0A%0A        dhis2MockServer.shutdown();%0A    %7D%0A%0A    @Test%0A    public void remove_rows_that_produce_foreign_key_errors() throws Exception %7B%0A        final D2CallExecutor executor = new D2CallExecutor();%0A%0A        executor.executeD2CallTransactionally(d2.databaseAdapter(), new Callable%3CVoid%3E() %7B%0A            @Override%0A            public Void call() throws D2CallException %7B%0A                givenAMetadataInDatabase();%0A                new UserCredentialsStoreImpl(d2.databaseAdapter()).insert(%22user_credential_uid1%22, null,%0A                        null, null, null, null, null, %22no_user_uid%22);%0A                assertThatCursorHasRowCount(2);%0A                new ForeignKeyCleaner(d2.databaseAdapter()).cleanForeignKeyErrors();%0A                return null;%0A            %7D%0A        %7D);%0A%0A        Cursor cursor = getCursor();%0A        assertThatCursorHasRowCount(1);%0A        cursor.moveToFirst();%0A%0A        int uidColumnIndex = cursor.getColumnIndex(UserCredentialsModel.Columns.UID);%0A        Truth.assertThat(cursor.getString(uidColumnIndex)).isEqualTo(%22M0fCOxtkURr%22);%0A        int userColumnIndex = cursor.getColumnIndex(UserCredentialsModel.Columns.USER);%0A        Truth.assertThat(cursor.getString(userColumnIndex)).isEqualTo(%22DXyJmlo9rge%22);%0A%0A        assertThatCursor(cursor).isExhausted();%0A    %7D%0A%0A    private void givenAMetadataInDatabase() %7B%0A        try %7B%0A            dhis2MockServer.enqueueMetadataResponses();%0A            d2.syncMetaData().call();%0A        %7D catch (Exception ignore) %7B%0A        %7D%0A    %7D%0A%0A    private Cursor getCursor() %7B%0A        return database().query(UserCredentialsModel.TABLE, PROJECTION, null, null,%0A                null, null, null);%0A    %7D%0A%0A    private void assertThatCursorHasRowCount(int rowCount) %7B%0A        Truth.assertThat(getCursor().getCount()).isEqualTo(rowCount);%0A    %7D%0A%7D

add ForeignKeyCleanerShould