Skip to content

Commit

Permalink
fix: add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterchi Daniel committed Mar 7, 2024
1 parent 480a2bf commit ddeb9a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

/**
* Helper class parsing a parameter string and converting the tokens to a parameter list.
*
*
* @author Christoph Deppisch
*/
public final class FunctionParameterHelper {

/**
* Prevent class instantiation.
*/
private FunctionParameterHelper() {}

/**
* Convert a parameter string to a list of parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void shouldConvertSingleLineJson() {
String json = """
{"myValues": ["O15o3a8","PhDjdSruZgG"]}""";
var result = getParameterList(wrappedInSingleQuotes(json));
assertThat(result).contains(json);
assertThat(result).containsExactly(json);
}

@Test
Expand All @@ -87,7 +87,7 @@ void shouldConvertMultiLineJson() {
]
}""";
var result = getParameterList(wrappedInSingleQuotes(json));
assertThat(result).contains(json);
assertThat(result).containsExactly(json);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.citrusframework.exceptions.NoSuchFunctionException;
import org.citrusframework.exceptions.NoSuchFunctionLibraryException;
import org.citrusframework.functions.core.CurrentDateFunction;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.citrusframework.functions.FunctionUtils.resolveFunction;
Expand Down Expand Up @@ -104,23 +106,54 @@ public void testUnknownFunctionLibrary() {
resolveFunction("doesnotexist:concat('Hello', ' TestFramework!')", context);
}

@Test
void shouldReplaceIfStringIsJson() {
@DataProvider
public static String[][] validParameterLists() {
return new String[][]{
{
"citrus:concat('{\"lorem\": [\"ipsum\", \"other\"]}')",
"{\"lorem\": [\"ipsum\", \"other\"]}"
},
{
// has two spaces here ----------------\/
"citrus:concat('{\"lorem\": [\"ipsum\", \"other\"]}')",
"{\"lorem\": [\"ipsum\", \"other\"]}"
},
{
// has no space here ----------------\/
"citrus:concat('{\"lorem\": [\"ipsum\",\"other\"]}')",
"{\"lorem\": [\"ipsum\",\"other\"]}"
},
{
// with linebreak after comma
"""
citrus:upperCase('{
"myValues": [
"O15o3a8",
"PhDjdSruZgG"
]
}')
""",
"""
{
"MYVALUES": [
"O15O3A8",
"PHDJDSRUZGG"
]
}
"""
}
};
}

@Test(dataProvider = "validParameterLists")
void shouldReplaceWithCommasInValue(String given, String expected) {
var contextSpy = spy(context);
when(contextSpy.getFunctionRegistry()).thenReturn(spy(context.getFunctionRegistry()));
List<FunctionLibrary> functionLibraries = List.of(new DefaultFunctionLibrary());
when(contextSpy.getFunctionRegistry().getFunctionLibraries()).thenReturn(functionLibraries);
var input = """
{
"myValues": [
"O15o3a8",
"PhDjdSruZgG"
]
}
""";

var result = FunctionUtils.replaceFunctionsInString(input, context, false);

assertThat(result).isEqualTo(input);

var result = FunctionUtils.replaceFunctionsInString(given, context, false);

assertThat(result).isEqualTo(expected);
}
}

0 comments on commit ddeb9a0

Please sign in to comment.