package com.codicesoftware.plugins.hudson.util;

import hudson.model.BooleanParameterValue;
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SelectorParametersResolverTest {

    @Test
    public void testResolver() {
        List<ParameterValue> parameters = new LinkedList<>();
        parameters.add(new StringParameterValue("PARAM", "VALUE"));
        parameters.add(new BooleanParameterValue("BOOL", true));

        assertEquals("sample VALUE text", SelectorParametersResolver.resolve("sample $PARAM text", parameters));
        assertEquals("sample VALUE text", SelectorParametersResolver.resolve("sample ${PARAM} text", parameters));
        assertEquals("sample PARAM text", SelectorParametersResolver.resolve("sample PARAM text", parameters));
        assertEquals("sample true text", SelectorParametersResolver.resolve("sample $BOOL text", parameters));
    }

    @Test
    public void testLegacyParamsResolver() {
        List<ParameterValue> parameters = new LinkedList<>();
        parameters.add(new StringParameterValue("PARAM", "VALUE"));
        parameters.add(new BooleanParameterValue("BOOL", true));

        assertEquals("sample VALUE text", SelectorParametersResolver.resolve("sample %PARAM% text", parameters));
        assertEquals("sample PARAM text", SelectorParametersResolver.resolve("sample PARAM text", parameters));
        assertEquals("sample true text", SelectorParametersResolver.resolve("sample %BOOL% text", parameters));
    }

    @Test
    public void testMixedFormatsResolver() {
        List<ParameterValue> parameters = new LinkedList<>();
        parameters.add(new StringParameterValue("PARAM", "VALUE"));
        parameters.add(new BooleanParameterValue("BOOL", true));

        assertEquals("sample VALUE VALUE text", SelectorParametersResolver.resolve("sample %PARAM% $PARAM text", parameters));
        assertEquals("sample VALUE VALUE text", SelectorParametersResolver.resolve("sample ${PARAM} %PARAM% text", parameters));
        assertEquals("sample true true text", SelectorParametersResolver.resolve("sample $BOOL %BOOL% text", parameters));
    }
}
@@ -42,16 +42,39 @@
 .util;%0A%0A
+import hudson.EnvVars;%0A
 import h
@@ -587,32 +587,78 @@
 BOOL%22, true));%0A%0A
+        EnvVars environment = new EnvVars();%0A%0A
         assertEq
@@ -742,32 +742,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -870,32 +870,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -995,32 +995,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -1119,32 +1119,1517 @@
 ext%22, parameters
+, environment));%0A    %7D%0A%0A    @Test%0A    public void testEnvironmentExpansion() %7B%0A        List%3CParameterValue%3E parameters = new LinkedList%3C%3E();%0A        EnvVars environment = new EnvVars(%22PARAM%22, %22VALUE%22, %22BOOL%22, %22true%22);%0A%0A        assertEquals(%22sample VALUE text%22, SelectorParametersResolver.resolve(%22sample $PARAM text%22, parameters, environment));%0A        assertEquals(%22sample VALUE text%22, SelectorParametersResolver.resolve(%22sample $%7BPARAM%7D text%22, parameters, environment));%0A        assertEquals(%22sample PARAM text%22, SelectorParametersResolver.resolve(%22sample PARAM text%22, parameters, environment));%0A        assertEquals(%22sample true text%22, SelectorParametersResolver.resolve(%22sample $BOOL text%22, parameters, environment));%0A    %7D%0A%0A    @Test%0A    public void testEnvironmentAndParameterExpansion() %7B%0A        List%3CParameterValue%3E parameters = new LinkedList%3C%3E();%0A        parameters.add(new BooleanParameterValue(%22BOOL%22, true));%0A%0A        EnvVars environment = new EnvVars(%22PARAM%22, %22VALUE%22);%0A%0A        assertEquals(%22sample VALUE text%22, SelectorParametersResolver.resolve(%22sample $PARAM text%22, parameters, environment));%0A        assertEquals(%22sample VALUE text%22, SelectorParametersResolver.resolve(%22sample $%7BPARAM%7D text%22, parameters, environment));%0A        assertEquals(%22sample PARAM text%22, SelectorParametersResolver.resolve(%22sample PARAM text%22, parameters, environment));%0A        assertEquals(%22sample true text%22, SelectorParametersResolver.resolve(%22sample $BOOL text%22, parameters, environment
 ));%0A    %7D%0A%0A    @
@@ -2866,32 +2866,78 @@
 BOOL%22, true));%0A%0A
+        EnvVars environment = new EnvVars();%0A%0A
         assertEq
@@ -3022,32 +3022,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -3147,32 +3147,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -3272,32 +3272,45 @@
 ext%22, parameters
+, environment
 ));%0A    %7D%0A%0A    @
@@ -3547,32 +3547,78 @@
 BOOL%22, true));%0A%0A
+        EnvVars environment = new EnvVars();%0A%0A
         assertEq
@@ -3716,32 +3716,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -3858,32 +3858,45 @@
 ext%22, parameters
+, environment
 ));%0A        asse
@@ -4002,16 +4002,29 @@
 rameters
+, environment
 ));%0A    

Fix and extend tests