Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit efd32d97 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

clean UploadFileOperationTest

- Add Context as class's field
- Fix typos for if/else, (, {
- Add final keyword when possible
parent b7cc5176
Loading
Loading
Loading
Loading
Loading
+62 −59
Original line number Diff line number Diff line
@@ -2,11 +2,13 @@ package foundation.e.drive.operations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.accounts.AccountManager;
import android.content.Context;
import android.os.Build;

import com.owncloud.android.lib.common.OwnCloudClient;
@@ -42,20 +44,21 @@ import foundation.e.drive.utils.CommonUtils;
public class UploadFileOperationTest {

    private List<SyncedFileState> syncedFileStates= new ArrayList<>();
    private OwnCloudClient client;
    private AccountManager accountManager;

    private final OwnCloudClient client;
    private final AccountManager accountManager;
    private final Context context;
    private long userFreeQuota;

    public UploadFileOperationTest() {
        context = RuntimeEnvironment.getApplication();
        accountManager = AccountManager.get(context);
        ShadowLog.stream = System.out;
        TestUtils.loadServerCredentials();
        accountManager = AccountManager.get(RuntimeEnvironment.getApplication());
        TestUtils.prepareValidAccount(accountManager);
        ShadowLog.stream = System.out;
        client = CommonUtils.getOwnCloudClient(CommonUtils.getAccount(TestUtils.TEST_ACCOUNT_NAME, TestUtils.TEST_ACCOUNT_TYPE, accountManager), RuntimeEnvironment.application);
        client = CommonUtils.getOwnCloudClient(CommonUtils.getAccount(TestUtils.TEST_ACCOUNT_NAME, TestUtils.TEST_ACCOUNT_TYPE, accountManager), context);

        try {
            TestUtils.testConnection(client, RuntimeEnvironment.application);
            TestUtils.testConnection(client, context);
        }catch(Exception e) {
            System.out.println("test connection failed: "+e.getMessage());
        }
@@ -74,17 +77,15 @@ public class UploadFileOperationTest {

    /**
     * Prepare content of database for test
     * Insert three test folder: small, medium, large
     */
    private void prepareDB() {
        //Insert three test folder: small, medium, large

        DbHelper.insertSyncedFolder(createSyncedFolder("small"), context);
        DbHelper.insertSyncedFolder(createSyncedFolder("medium"), context);
        DbHelper.insertSyncedFolder(createSyncedFolder("large"), context);

        DbHelper.insertSyncedFolder(createSyncedFolder("small"), RuntimeEnvironment.application);
        DbHelper.insertSyncedFolder(createSyncedFolder("medium"), RuntimeEnvironment.application);
        DbHelper.insertSyncedFolder(createSyncedFolder("large"), RuntimeEnvironment.application);

        //Insert at least one file for each folder
        assertEquals("There isn't three folder in DB as expected", 3, DbHelper.getSyncedFolderList(RuntimeEnvironment.application, true).size());
        //assertion for debug purpose
        assertEquals("There isn't three folder in DB as expected", 3, DbHelper.getSyncedFolderList(context, true).size());
    }

    /**
@@ -93,14 +94,16 @@ public class UploadFileOperationTest {
     * @return SyncedFolder instance
     */
    private SyncedFolder createSyncedFolder(String name) {
        return new SyncedFolder(name, TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH+name+"/", TestUtils.TEST_REMOTE_ROOT_FOLDER_PATH+name+"/", true, true, true, true);
        return new SyncedFolder(name,
                TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH+name+"/",
                TestUtils.TEST_REMOTE_ROOT_FOLDER_PATH+name+"/",
                true, true, true, true);
    }

    /**
     * Create local file to use for upload test
     * Create a local small file to use for upload test
     */
    private void createSmallFile() {

        final String smallDummyFilePath = TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH+"small/dummy.txt";
        try {
            TestUtils.createFile(smallDummyFilePath, 2);
@@ -109,7 +112,7 @@ public class UploadFileOperationTest {
        }

        final SyncedFileState sfs = new SyncedFileState(-1, "dummy.txt", TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH+"small/dummy.txt", TestUtils.TEST_REMOTE_ROOT_FOLDER_PATH+"small/dummy.txt",  "", 0l, 0, true, 3);
        sfs.setId(DbHelper.manageSyncedFileStateDB(sfs, "INSERT", RuntimeEnvironment.application));
        sfs.setId(DbHelper.manageSyncedFileStateDB(sfs, "INSERT", context));

        syncedFileStates.add(sfs);
    }
@@ -124,21 +127,21 @@ public class UploadFileOperationTest {
        if (!syncedFileStates.isEmpty()) {
            final SyncedFileState sfs = syncedFileStates.get(0);
            if (sfs != null) {
                RemoveFileOperation removeRemoteFileOp = new RemoveFileOperation(sfs);
                final RemoveFileOperation removeRemoteFileOp = new RemoveFileOperation(sfs);
                removeRemoteFileOp.execute(client);
            }
        }

        final String smallDummyFilePath = TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH+"small/dummy.txt";
        File smallFile = new File(smallDummyFilePath);
        final File smallFile = new File(smallDummyFilePath);
        if (smallFile.exists()) {
            return smallFile.delete();
        } else return true;
    }

    private long getUserRemoteFreeQuota() {
        GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation();
        RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client);
        final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation();
        final RemoteOperationResult ocsResult = getRemoteUserInfoOperation.execute(client);

        if (ocsResult.isSuccess() && ocsResult.getData() != null) {
            UserInfo userInfo = (UserInfo) ocsResult.getData().get(0);
@@ -159,14 +162,13 @@ public class UploadFileOperationTest {
        removeSmallFile(); //clean the environnement
        createSmallFile(); //preparation

        final SyncedFileState sfs_fromDB = DbHelper.loadSyncedFile(RuntimeEnvironment.application, syncedFileStates.get(0).getLocalPath(), true);
        final SyncedFileState sfs_fromDB = DbHelper.loadSyncedFile(context, syncedFileStates.get(0).getLocalPath(), true);
        assertTrue("SyncedFileState loaded from DB must have an empty Etag", sfs_fromDB.getLastETAG().isEmpty());


        boolean checkEtag = false;
        UploadFileOperation testOperation = new UploadFileOperation(syncedFileStates.get(0), RuntimeEnvironment.application);
        UploadFileOperation testOperation = new UploadFileOperation(syncedFileStates.get(0), context);

        RemoteOperationResult result = testOperation.execute(client);
        final RemoteOperationResult result = testOperation.execute(client);
        String errorMsg = "The upload failed:\n http code: "+result.getHttpCode()
                +"\n, is success ?"+result.isSuccess()
                +"\n, log msg: "+result.getLogMessage()
@@ -176,7 +178,7 @@ public class UploadFileOperationTest {
        }
        assertTrue( errorMsg, result.isSuccess());

        final SyncedFileState sfs_fromDBAfterUpload = DbHelper.loadSyncedFile(RuntimeEnvironment.application, syncedFileStates.get(0).getLocalPath(), true);
        final SyncedFileState sfs_fromDBAfterUpload = DbHelper.loadSyncedFile(context, syncedFileStates.get(0).getLocalPath(), true);
        assertFalse("After upload, the database must store the etag of the syncedFileState. But here it is empty", sfs_fromDBAfterUpload.getLastETAG().isEmpty());
    }

@@ -194,7 +196,7 @@ public class UploadFileOperationTest {
        SyncedFileState syncedFileState = null;
        //Test fails at the moment because of UploadFileOperation's constructor not checking for syncedFileState is null)
        // check https://gitlab.e.foundation/e/apps/eDrive/-/issues/120
        UploadFileOperation testOperation = new UploadFileOperation(syncedFileState, RuntimeEnvironment.application);
        final UploadFileOperation testOperation = new UploadFileOperation(syncedFileState, context);

        RemoteOperationResult result = testOperation.execute(client);
        assertEquals("Expected result code was FORBIDDEN but got: "+result.getCode().name(), RemoteOperationResult.ResultCode.FORBIDDEN, result.getCode());
@@ -210,16 +212,16 @@ public class UploadFileOperationTest {
        removeSmallFile(); //clean the environnement
        createSmallFile(); //preparation

        final SyncedFileState sfs_fromDB = DbHelper.loadSyncedFile(RuntimeEnvironment.application, syncedFileStates.get(0).getLocalPath(), true);
        final SyncedFileState sfs_fromDB = DbHelper.loadSyncedFile(context, syncedFileStates.get(0).getLocalPath(), true);
        assertTrue("SyncedFileState loaded from DB must have an empty Etag", sfs_fromDB.getLastETAG().isEmpty());

        boolean checkEtag = false;
        UploadFileOperation testOperation = new UploadFileOperation(syncedFileStates.get(0), RuntimeEnvironment.application);
        UploadFileOperation testOperation = new UploadFileOperation(syncedFileStates.get(0), context);

        File smallFile = new File(sfs_fromDB.getLocalPath());
        final File smallFile = new File(sfs_fromDB.getLocalPath());
        assertTrue("Local file deletion return false instead of true", smallFile.delete());

        RemoteOperationResult result = testOperation.execute(client);
        final RemoteOperationResult result = testOperation.execute(client);
        assertEquals("Expected result code was FORBIDDEN but got: "+result.getCode().name(), RemoteOperationResult.ResultCode.FORBIDDEN, result.getCode());
    }

@@ -232,26 +234,28 @@ public class UploadFileOperationTest {
        //long freeQuota = getUserRemoteFreeQuota();
        assertFalse("Reading remote free quota fails"+userFreeQuota, -1 == userFreeQuota);
        //We don't care of parameter of UploadFileOperation's constructor
        RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), RuntimeEnvironment.application)
        final RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), context)
                .checkAvailableSpace(client, (userFreeQuota+1));
        assertEquals("Quota check ("+ userFreeQuota+"vs"+(userFreeQuota+1)+") failed", RemoteOperationResult.ResultCode.QUOTA_EXCEEDED, actualResult.getCode());
    }

    /**
     * Assert that uploading a file which size is exactly the amount of free quota isn't allowed
     *
     */
    @Test
    public void fileSizeEqualToFreeQuota_shouldnotBeAllowed(){
    public void fileSizeEqualToFreeQuota_shouldBeAllowed() {
        //I don't know why but it always fail for this test.
        //long freeQuota = getUserRemoteFreeQuota();
        assertFalse("Reading remote free quota fails"+userFreeQuota, -1 == userFreeQuota);

        RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), RuntimeEnvironment.application)
        final RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), context)
                .checkAvailableSpace(client, userFreeQuota);
        assertEquals("Quota check  ("+ userFreeQuota+" vs "+userFreeQuota+") failed",
        assertNotEquals("Quota check is Quota Exceeded ("+ userFreeQuota+" vs "+userFreeQuota+")",
                RemoteOperationResult.ResultCode.QUOTA_EXCEEDED,
                actualResult.getCode());
        assertEquals("Quota Check is not OK",
                RemoteOperationResult.ResultCode.OK,
                actualResult.getCode());
    }


@@ -264,11 +268,10 @@ public class UploadFileOperationTest {
        //long freeQuota = getUserRemoteFreeQuota();
        assertFalse("Reading remote free quota fails "+userFreeQuota, -1 == userFreeQuota);

        RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), RuntimeEnvironment.application)
        final RemoteOperationResult actualResult = new UploadFileOperation(Mockito.mock(SyncedFileState.class), context)
                .checkAvailableSpace(client, (userFreeQuota-1));
        assertEquals("Quota check ("+ userFreeQuota+" vs "+(userFreeQuota-1)+") failed",
                RemoteOperationResult.ResultCode.OK,
                actualResult.getCode());

    }
}
 No newline at end of file