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

Commit 96741b10 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

start to implement new method to detect changement on server with CI process (not over)

parent e05a46f5
Loading
Loading
Loading
Loading
+254 −40
Original line number Diff line number Diff line
@@ -7,11 +7,14 @@
 */
package io.eelo.drive.Test;

import com.owncloud.android.lib.resources.files.model.RemoteFile;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;


/**
@@ -20,50 +23,261 @@ import java.util.List;

public class ObserverServiceUnitTest {

    public File[] mockCachedFile(){
        File[] mockFile= new File[5];
        for(int i = 0; i < 5; ++i){
            mockFile[i] = new File("/emulated/storage/0/android/data/toto"+i+".jpg");

    List<RemoteFile> previousRemoteFileList;
    List<RemoteFile> newRemoteFileList;
    List<String> pathListToIgnore;


    @Before
    public void init(){
        this.previousRemoteFileList = getRemoteOldList();
        Assert.assertNotNull("Old list is null", previousRemoteFileList);
        Assert.assertFalse("Old list is empty", previousRemoteFileList.isEmpty());


        this.newRemoteFileList = getRemoteNewList();
        Assert.assertNotNull("New list is null", newRemoteFileList);
        Assert.assertFalse("New list is empty", newRemoteFileList.isEmpty());
        Assert.assertFalse("File 4 is folder", newRemoteFileList.get(4).getMimeType().equals("DIR"));
        Assert.assertTrue("oldList and newList has same size", (newRemoteFileList.size() != previousRemoteFileList.size()));




        this.pathListToIgnore = new ArrayList<>();
        Assert.assertTrue(pathListToIgnore.isEmpty());
        Assert.assertNotNull(pathListToIgnore);


    }
        return mockFile;



    @Test
    public void FindModificationUnitTest(){

        //Test with two new file, one deleted file and one ignored path
        System.out.println("TEST 1 :");
        findRemoteModification();
        Assert.assertTrue("wrong number of path to ignore", pathListToIgnore.size() == 1);
        Assert.assertTrue("doesn't contains the expected path", pathListToIgnore.get(0).equals("/Videos/"));
        Assert.assertTrue("Wrong number of removed file", previousRemoteFileList.size() == 1);
        Assert.assertTrue("Wrong number of new files (and folder) ", newRemoteFileList.size() == 3);
        showResult();


        //Test a case where there is already a path to ignore. So What is expected is that new list is empty.
        System.out.println("\nTEST 2 :");
        init();
        pathListToIgnore.add("/Photos/");
        findRemoteModification();
        Assert.assertTrue("wrong number of new file", newRemoteFileList.isEmpty());
        showResult();


        //Test the case where nothing change
        System.out.println("\nTEST 3 :");
        pathListToIgnore.clear();
        previousRemoteFileList = getRemoteOldList();
        newRemoteFileList = getRemoteOldList();
        findRemoteModification();
        showResult();


        //Test with an empty new List

        System.out.println("\nTEST 4 :");
        pathListToIgnore.clear();
        newRemoteFileList.clear();
        previousRemoteFileList = getRemoteOldList();
        findRemoteModification();
        showResult();

        //Test with an empty old List
        System.out.println("\nTEST 5 :");
        pathListToIgnore.clear();
        previousRemoteFileList.clear();
        newRemoteFileList = getRemoteNewList();
        findRemoteModification();
        showResult();

    }

    public List<String> mockUsedFilePath (){
        List<String> stringList = new ArrayList<>();
        System.out.println("Stringset:"+( (stringList == null)? false: true) );
        boolean a1 = stringList.add( new String("/emulated/storage/0/DCIM/Camera/toto1.jpg") );
        boolean a2 = stringList.add( new String("/emulated/storage/0/DCIM/Camera/toto2.jpg") );
        //boolean a3 = stringSet.add( new String("/emulated/storage/0/DCIM/Camera/toto4.jpg") );
        Assert.assertNotNull(stringList);
        return stringList;

    public void findRemoteModification(){

        if(newRemoteFileList.isEmpty() || previousRemoteFileList.isEmpty()) return;

        //pathListToIgnore.add("/Photos/");
        if(previousRemoteFileList.get(0).getEtag().equals(newRemoteFileList.get(0).getEtag() )){
            System.out.println("Nothing has change");

        }
        previousRemoteFileList.remove(0);
        newRemoteFileList.remove(0);

    @Test
    public void handleCachedFile(){
        //Load subfiles into external cache file
        //Mock this
        File[] fileArray = mockCachedFile();
        ListIterator<RemoteFile> oldIterator = previousRemoteFileList.listIterator();

        while (oldIterator.hasNext()){
            RemoteFile oldversion = oldIterator.next();

            String remotePath = oldversion.getRemotePath();
            boolean isToIgnore = false;

        Assert.assertNotNull(fileArray);
        //Mock this;
        List<String> usedFilePath = mockUsedFilePath();
            ListIterator<RemoteFile> newIterator = newRemoteFileList.listIterator();
            boolean equivalentFound = false;

        Assert.assertNotNull(usedFilePath);
        boolean toRemove = true;
       for(int i =0; i < fileArray.length; ++i){
           toRemove = true;
           //System.out.println( fileArray[i] );
            for(String filePath : usedFilePath){
            while (!equivalentFound && newIterator.hasNext()){

                if(filePath.endsWith(fileArray[i].getName())){
                    System.out.println("TRUE");
                    toRemove = false;
                }else
                    System.out.println("FALSE");
                RemoteFile newVersion = newIterator.next();


                for(int i = -1, size = pathListToIgnore.size(); ++i < size;){
                    if(newVersion.getRemotePath().startsWith(pathListToIgnore.get(i))) {
                        isToIgnore = true;
                        i = size;
                        newIterator.remove();
                    }
            if(toRemove){
                System.out.println("Deletion of file: "+fileArray[i].getName() );
                }

                if(!isToIgnore && newVersion.getRemotePath().equals(remotePath )){

                    if(oldversion.getMimeType().equals("DIR")) {

                        //This is a folder
                        if (oldversion.getEtag().equals(newVersion.getEtag()) == true) {
                            //Which hasn't change'
                            pathListToIgnore.add(remotePath);
                        }
                        newIterator.remove();
                        oldIterator.remove();
                    }else {
                        //This is a file
                        if (oldversion.getEtag().equals(newVersion.getEtag())) {
                            //Which hasn't change'
                            newIterator.remove();
                            oldIterator.remove();
                        }
                    }
                    equivalentFound = true;
                }
            }
            if(isToIgnore) oldIterator.remove();
        }
    }

    private void showResult(){
        System.out.println("File that hasn't changed: "+pathListToIgnore);
        String newListDisplay ="";
        for(RemoteFile o : newRemoteFileList){
            newListDisplay+=o.getRemotePath()+", ";
        }
        System.out.println("New or updated file : ["+newListDisplay+"]");

        String oldListDisplay ="";
        for(RemoteFile o : previousRemoteFileList){
            oldListDisplay+=o.getRemotePath()+", ";
        }
        System.out.println("File that have been removed : ["+oldListDisplay+"]");
    }

    /**
     * Test Method wich generate a list of remotefile
     * @return
     */
    private ArrayList<RemoteFile> getRemoteOldList(){
        ArrayList<RemoteFile> result = new ArrayList<>();

        RemoteFile instance = new RemoteFile("/");
        instance.setEtag("e2a34");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Photos/");
        instance.setEtag("43b1");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Videos/");
        instance.setEtag("x732");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/");
        instance.setEtag("R263x");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/Metallica/");
        instance.setEtag("c9b56");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/Metallica/nem.ogg");
        instance.setEtag("g6b16");
        instance.setMimeType("music");
        result.add( instance );

        instance = new RemoteFile("/Musics/Metallica/fwtbt.ogg");
        instance.setEtag("f2b00");
        instance.setMimeType("music");
        result.add( instance );
        return result;
    }

    /**
     * Test Method wich generate a list of remotefile
     * @return
     */
    private ArrayList<RemoteFile> getRemoteNewList(){
        ArrayList<RemoteFile> result = new ArrayList<>();

        RemoteFile instance = new RemoteFile("/");
        instance.setEtag("popol");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Photos/"); //This folder has change
        instance.setEtag("cjb2");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Photos/DCIM/"); //This is new folder
        instance.setEtag("hTb1");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Photos/DCIM/fleur.png"); //This is new file
        instance.setEtag("wnq8");
        instance.setMimeType("img/png");
        result.add( instance );

        instance = new RemoteFile("/Photos/DCIM/rocher.png"); //This is new file
        instance.setEtag("1234");
        instance.setMimeType("img/png");
        result.add( instance );

        instance = new RemoteFile("/Videos/");
        instance.setEtag("x732");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/"); //This folder has change cause it lacks one file
        instance.setEtag("r26ax");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/Metallica/"); //This folder has change cause it lacks one file
        instance.setEtag("p13b56");
        instance.setMimeType("DIR");
        result.add( instance );

        instance = new RemoteFile("/Musics/Metallica/nem.ogg");
        instance.setEtag("g6b16");
        instance.setMimeType("music");
        result.add( instance );

        return result;
    }
}