/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.functions;

import static org.apache.jmeter.functions.FunctionTestHelper.makeParams;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collection;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RandomFunctionTest extends JMeterTestCase {

    @Before
    public void setup() {
        JMeterContextService.getContext().setVariables(new JMeterVariables());
    }
    @After
    public void tearDown() {
        JMeterContextService.getContext().clear();
    }
    
    @Test
    public void randomTest1() throws Exception {
        Random r = new Random();
        Collection<CompoundVariable> parms = makeParams("0","10000000000","VAR");
        r.setParameters(parms);
        String s = r.execute(null,null);
        long l = Long.parseLong(s);
        assertTrue(l>=0 && l<=10000000000L);
        
        parms = makeParams("1","1","VAR");
        r.setParameters(parms);
        s = r.execute(null,null);
        l = Long.parseLong(s);
        assertEquals(1, l);
        String varValue = JMeterContextService.getContext().getVariables().get("VAR");
        assertEquals("1", varValue);
    }
}

@@ -1026,16 +1026,100 @@
 lection;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -1343,16 +1343,41 @@
 .After;%0A
+import org.junit.Assert;%0A
 import o
@@ -2284,32 +2284,32 @@
 s().get(%22VAR%22);%0A
-
         assertEq
@@ -2335,10 +2335,1294 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void randomStringTest1() throws Exception %7B%0A        RandomString r = new RandomString();%0A        Collection%3CCompoundVariable%3E parms = makeParams(%2210%22,%22abcdefghijklmnopqrstuvwxyz%22,%22VAR%22);%0A        r.setParameters(parms);%0A        String s = r.execute(null,null);%0A        Assert.assertNotNull(s);%0A        assertEquals(10, s.length());%0A        assertTrue(%22Random String contains unexpected character%22, stringOnlyContainsChars(s, %22abcdefghijklmnopqrstuvwxyz%22));%0A        %0A        String varValue = JMeterContextService.getContext().getVariables().get(%22VAR%22);%0A        assertEquals(s, varValue);%0A        %0A        parms = makeParams(%225%22,%22%22, %22VAR2%22);%0A        r.setParameters(parms);%0A        s = r.execute(null,null);%0A        Assert.assertNotNull(s);%0A        assertEquals(5, s.length());%0A        %0A        varValue = JMeterContextService.getContext().getVariables().get(%22VAR2%22);%0A        assertEquals(s, varValue);%0A    %7D%0A    %0A    private boolean stringOnlyContainsChars(String value, String allowedChars) %7B%0A        Set%3CCharacter%3E allowedCharsAsSet = allowedChars.chars()%0A                .mapToObj(i -%3E (char) i)%0A                .collect(Collectors.toCollection(HashSet::new));        %0A        return value.chars().allMatch(c -%3E allowedCharsAsSet.contains((char)c));%0A    %7D%0A
 %7D%0A

Add Test for RandomString