-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: parameter list parsing #1131
Draft
novarx
wants to merge
2
commits into
citrusframework:main
Choose a base branch
from
novarx:fix/parameter-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
core/citrus-api/src/test/java/org/citrusframework/functions/FunctionParameterHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2006-2024 the original author or authors. | ||
* | ||
* Licensed 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.citrusframework.functions; | ||
|
||
import org.citrusframework.functions.FunctionParameterHelper.ParameterParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.citrusframework.functions.FunctionParameterHelper.getParameterList; | ||
|
||
class FunctionParameterHelperTest { | ||
|
||
@Test | ||
void shouldOneParam() { | ||
var result = getParameterList("lorem"); | ||
assertThat(result).containsExactly("lorem"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam() { | ||
var result = getParameterList("lorem, ipsum"); | ||
assertThat(result).containsExactly("lorem", "ipsum"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam_oneQuoted() { | ||
var result = getParameterList("lorem, 'ipsum'"); | ||
assertThat(result).containsExactly("lorem", "ipsum"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam_withCommaInParam() { | ||
var result = getParameterList("'lorem, dolor', 'ipsum'"); | ||
assertThat(result).containsExactly("lorem, dolor", "ipsum"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam_withLinebreak() { | ||
var result = getParameterList("'lorem, dolor', 'ipsum\n sit'"); | ||
assertThat(result).containsExactly("lorem, dolor", "ipsum\n sit"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam_withLinebreakAfterComma() { | ||
var result = getParameterList("'lorem,\n dolor', 'ipsum sit'"); | ||
assertThat(result).containsExactly("lorem,\n dolor", "ipsum sit"); | ||
} | ||
|
||
@Test | ||
void shouldTwoParam_withWhitespacesAfterComma() { | ||
var result = getParameterList("'lorem, dolor', 'ipsum sit'"); | ||
assertThat(result).containsExactly("lorem, dolor", "ipsum sit"); | ||
} | ||
|
||
@Test | ||
void shouldConvertSingleLineJson() { | ||
String json = """ | ||
{"myValues": ["O15o3a8","PhDjdSruZgG"]}"""; | ||
var result = getParameterList(wrappedInSingleQuotes(json)); | ||
assertThat(result).containsExactly(json); | ||
} | ||
|
||
@Test | ||
void shouldConvertMultiLineJson() { | ||
// language=JSON | ||
String json = """ | ||
{ | ||
"id": 133, | ||
"myValues": [ | ||
"O15o3a8", | ||
"PhDjdSruZgG", | ||
"I2qrC1Mu, PmSsd8LPLe" | ||
] | ||
}"""; | ||
var result = getParameterList(wrappedInSingleQuotes(json)); | ||
assertThat(result).containsExactly(json); | ||
} | ||
|
||
@Test | ||
void shouldConvertNestedSingleQuotedStrings() { | ||
// language=JSON | ||
String json = """ | ||
["part of first param", "also 'part' of first param"]"""; | ||
var result = getParameterList(wrappedInSingleQuotes(json)); | ||
assertThat(result).hasSize(1).containsExactly(json); | ||
} | ||
|
||
@Test | ||
void shouldConvertIdempotent() { | ||
// language=JSON | ||
String json = """ | ||
["part of first param", "also 'part' of first param"]"""; | ||
|
||
var parser = new ParameterParser(wrappedInSingleQuotes(json)); | ||
var result1 = parser.parse(); | ||
var result2 = parser.parse(); | ||
|
||
assertThat(result1).isEqualTo(result2).hasSize(1).containsExactly(json); | ||
} | ||
|
||
@Test | ||
void cannotConvertSpecialNestedSingleQuotedStrings() { | ||
String threeParams = """ | ||
'["part of first param", "following comma will be missing ',' should also be first param"]', 'lorem', ipsum"""; | ||
var parser = new ParameterParser(threeParams); | ||
var result = parser.parse(); | ||
assertThat(result).containsExactly( | ||
"[\"part of first param\", \"following comma will be missing ", | ||
" should also be first param\"]", | ||
"lorem", | ||
"ipsum" | ||
); | ||
} | ||
|
||
private static String wrappedInSingleQuotes(String parameterString) { | ||
return "'%s'".formatted(parameterString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The longer I think about that... wouldn't it be easier to just use a CSV-Parser for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @bbortt
Your thoughts on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm.. I think CSV uses double quotes for concatenation. that's a bit problematic in the context of Java. other than that, I do too think that it's the same parsing logic.