package org.ow2.proactive.scheduler.task;

import org.junit.Before;
import org.junit.Test;
import org.objectweb.proactive.extensions.dataspaces.vfs.selector.FileSelector;
import org.ow2.proactive.scheduler.common.task.dataspaces.InputAccessMode;
import org.ow2.proactive.scheduler.common.task.dataspaces.InputSelector;
import org.ow2.proactive.scheduler.common.task.dataspaces.OutputAccessMode;
import org.ow2.proactive.scheduler.common.task.dataspaces.OutputSelector;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;

public class TaskLauncherInitializerTest {

    private TaskLauncherInitializer initializer;

    @Before
    public void setUp() {
        initializer = new TaskLauncherInitializer();
    }

    @Test
    public void input_files_can_be_filtered() throws Exception {
        initializer.setTaskInputFiles(inputSelectors("$TEST/a", "b"));
        initializer.getTaskInputFiles().get(0).getInputFiles().setExcludes("$TEST/excluded");

        List<InputSelector> filteredInputFiles = initializer.getFilteredInputFiles(
                Collections.<String, Serializable>singletonMap("TEST", "folder"));

        InputSelector selector = filteredInputFiles.get(0);
        assertEquals(new HashSet<>(asList("folder/a", "b")), selector.getInputFiles().getIncludes());
        assertEquals(new HashSet<>(singletonList("folder/excluded")), selector.getInputFiles().getExcludes());
    }

    @Test
    public void output_files_can_be_filtered() throws Exception {
        initializer.setTaskOutputFiles(outputSelectors("${TEST}/a", "b"));
        initializer.getTaskOutputFiles().get(0).getOutputFiles().setExcludes("$TEST/excluded");

        List<OutputSelector> filteredOutputFiles = initializer.getFilteredOutputFiles(
                Collections.<String, Serializable>singletonMap("TEST", "folder"));

        OutputSelector selector = filteredOutputFiles.get(0);
        assertEquals(new HashSet<>(asList("folder/a", "b")), selector.getOutputFiles().getIncludes());
        assertEquals(new HashSet<>(singletonList("folder/excluded")), selector.getOutputFiles().getExcludes());
    }

    @Test
    public void output_empty_filters() throws Exception {
        initializer.setTaskOutputFiles(outputSelectors("$TEST/a", "b"));

        List<OutputSelector> filteredOutputFiles = initializer.getFilteredOutputFiles(
                Collections.<String, Serializable>emptyMap());

        OutputSelector selector = filteredOutputFiles.get(0);
        assertEquals(new HashSet<>(asList("$TEST/a", "b")), selector.getOutputFiles().getIncludes());
    }

    @Test
    public void input_empty_filters() throws Exception {
        initializer.setTaskInputFiles(inputSelectors("$TEST/a", "b"));

        List<InputSelector> filteredInputFiles = initializer.getFilteredInputFiles(
                Collections.<String, Serializable>emptyMap());

        InputSelector selector = filteredInputFiles.get(0);
        assertEquals(new HashSet<>(asList("$TEST/a", "b")), selector.getInputFiles().getIncludes());
    }

    @Test
    public void input_null_filters() throws Exception {
        initializer.setTaskInputFiles(inputSelectors("$TEST/a", "b"));

        List<InputSelector> filteredInputFiles = initializer.getFilteredInputFiles(null);

        InputSelector selector = filteredInputFiles.get(0);
        assertEquals(new HashSet<>(asList("$TEST/a", "b")), selector.getInputFiles().getIncludes());
    }

    @Test
    public void output_null_filters() throws Exception {
        initializer.setTaskOutputFiles(outputSelectors("$TEST/a", "b"));

        List<OutputSelector> filteredOutputFiles = initializer.getFilteredOutputFiles(null);

        OutputSelector selector = filteredOutputFiles.get(0);
        assertEquals(new HashSet<>(asList("$TEST/a", "b")), selector.getOutputFiles().getIncludes());
    }

    private List<InputSelector> inputSelectors(String... selectors) {
        return singletonList(new InputSelector(new FileSelector(selectors),
                InputAccessMode.TransferFromUserSpace));
    }

    private List<OutputSelector> outputSelectors(String... selectors) {
        return singletonList(new OutputSelector(new FileSelector(selectors),
                OutputAccessMode.TransferToUserSpace));
    }
}
@@ -36,16 +36,63 @@
 .task;%0A%0A
+import com.google.common.collect.ImmutableSet;%0A
 import o
@@ -203,32 +203,33 @@
 r.FileSelector;%0A
+%0A
 import org.ow2.p
@@ -506,25 +506,24 @@
 utSelector;%0A
-%0A
 import java.
@@ -590,80 +590,15 @@
 til.
-HashSet;%0Aimport java.util.List;%0A%0Aimport static java.util.Arrays.as
 List;%0A
+%0A
 impo
@@ -1372,36 +1372,31 @@
 tEquals(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22folder
@@ -1396,33 +1396,32 @@
 %22folder/a%22, %22b%22)
-)
 , selector.getIn
@@ -1472,35 +1472,23 @@
 als(
-new HashSet%3C%3E(singletonList
+ImmutableSet.of
 (%22fo
@@ -1494,33 +1494,32 @@
 older/excluded%22)
-)
 , selector.getIn
@@ -2054,36 +2054,31 @@
 tEquals(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22folder
@@ -2078,33 +2078,32 @@
 %22folder/a%22, %22b%22)
-)
 , selector.getOu
@@ -2155,35 +2155,23 @@
 als(
-new HashSet%3C%3E(singletonList
+ImmutableSet.of
 (%22fo
@@ -2185,17 +2185,16 @@
 cluded%22)
-)
 , select
@@ -2612,36 +2612,31 @@
 tEquals(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22$TEST/
@@ -2635,33 +2635,32 @@
 (%22$TEST/a%22, %22b%22)
-)
 , selector.getOu
@@ -3062,36 +3062,31 @@
 tEquals(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22$TEST/
@@ -3085,33 +3085,32 @@
 (%22$TEST/a%22, %22b%22)
-)
 , selector.getIn
@@ -3453,36 +3453,31 @@
 tEquals(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22$TEST/
@@ -3476,33 +3476,32 @@
 (%22$TEST/a%22, %22b%22)
-)
 , selector.getIn
@@ -3856,28 +3856,23 @@
 als(
-new HashSet%3C%3E(asList
+ImmutableSet.of
 (%22$T
@@ -3883,17 +3883,16 @@
 a%22, %22b%22)
-)
 , select

Make use of ImmutableSet#of in TaskLauncherInitializerTest