package org.ow2.chameleon.fuchsia.discovery.filebased.test.util;

import junit.framework.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.osgi.framework.BundleContext;
import org.ow2.chameleon.fuchsia.discovery.filebased.AbstractFileBasedDiscovery;
import org.ow2.chameleon.fuchsia.discovery.filebased.FileBasedDiscoveryConstants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;

import static org.fest.reflect.core.Reflection.field;
import static org.fest.reflect.core.Reflection.method;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public abstract class FilebasedTestAbstract <D extends AbstractFileBasedDiscovery> {

    protected D discovery;

    @Rule
    public TemporaryFolder tempFolder =new TemporaryFolder();

    @Mock
    protected BundleContext context;

    @Before
    public void before(){

        MockitoAnnotations.initMocks(this);

        init();

        initGenericMocks();

    }

    public abstract void init();

    private void initGenericMocks(){

        field("name").ofType(String.class).in(discovery).set("instance-name");
        field("pollingTime").ofType(Long.class).in(discovery).set(Long.valueOf(FileBasedDiscoveryConstants.FILEBASED_DISCOVERY_PROPERTY_POLLING_TIME_VALUE));
        method("start").in(discovery).invoke();

    }

    protected File getTempFile(String name) throws IOException {

        File file= tempFolder.newFile(name);

        Properties props = new Properties();
        props.setProperty("id", "mid");

        OutputStream out = new FileOutputStream( file );
        props.store(out,"");

        out.close();

        return file;

    }

    @Test
    public void createFile() throws IOException {

        File file=getTempFile("newfile.txt");

        discovery.onFileCreate(file);

        Map maps=field("declarationsFiles").ofType(Map.class).in(discovery).get();

        Assert.assertEquals(1, maps.size());

    }

    @Test
    public void removeFile() throws IOException {

        File file=getTempFile("filetoberemoved.txt");

        discovery.onFileCreate(file);

        Map maps=field("declarationsFiles").ofType(Map.class).in(discovery).get();

        Assert.assertEquals(1,maps.size());

        discovery.onFileDelete(file);

        Assert.assertEquals(0,maps.size());

    }

    @Test
    public void gracefulStop() throws IOException {
        File file=getTempFile("filetoberemoved.txt");
        discovery.onFileCreate(file);
        Map maps=field("declarationsFiles").ofType(Map.class).in(discovery).get();
        Assert.assertEquals(1,maps.size());
        method("stop").in(discovery).invoke();
        Assert.assertEquals(0,maps.size());
    }

    @Test
    public void shouldNotAcceptHiddenFiled(){

        File file=mock(File.class);
        when(file.exists()).thenReturn(true);
        when(file.isFile()).thenReturn(true);
        when(file.isHidden()).thenReturn(true);

        Boolean result=discovery.accept(file);

        Assert.assertFalse(result);
    }

}

@@ -1751,16 +1751,18 @@
 %22id%22, %22m
+y-
 id%22);%0A%0A 
@@ -1842,25 +1842,24 @@
 re(out,%22%22);%0A
-%0A
         out.
@@ -3001,25 +3001,32 @@
        File 
-f
+invalidF
 ile=mock(Fil
@@ -3044,25 +3044,32 @@
        when(
-f
+invalidF
 ile.exists()
@@ -3097,25 +3097,32 @@
        when(
-f
+invalidF
 ile.isFile()
@@ -3154,17 +3154,24 @@
    when(
-f
+invalidF
 ile.isHi
@@ -3193,24 +3193,222 @@
 urn(true);%0A%0A
+        File validFile=mock(File.class);%0A        when(validFile.exists()).thenReturn(true);%0A        when(validFile.isFile()).thenReturn(true);%0A        when(validFile.isHidden()).thenReturn(false);%0A%0A
         Bool
@@ -3417,16 +3417,21 @@
 n result
+Valid
 =discove
@@ -3440,23 +3440,129 @@
 .accept(
-file);%0A
+validFile);%0A        Boolean resultInvalid=discovery.accept(invalidFile);%0A%0A        Assert.assertTrue(resultValid);
 %0A       
@@ -3587,16 +3587,23 @@
 e(result
+Invalid
 );%0A    %7D

improve accept test for filebased discovery