Skip to content

Commit

Permalink
RIA-23302 remove patterns as static variables from PostmanCollection …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
KarolinaNogacka committed Mar 7, 2025
1 parent 46651dd commit 7d2e7cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public WsdlProject importPostmanCollection(WorkspaceImpl workspace, String fileP
String uri = request.getUrl();
String preRequestScript = request.getPreRequestScript();
String tests = request.getTests();
String requestName = request.getName();
RequestAuthProfile authProfile = request.getAuthProfileWithName();

if (StringUtils.hasContent(preRequestScript)) {
Expand Down Expand Up @@ -159,7 +158,7 @@ public WsdlProject importPostmanCollection(WorkspaceImpl workspace, String fileP
}

if (assertable != null) {
addAssertionsV2(tests, project, assertable, requestName);
addAssertionsV2(tests, project, assertable, request.getName());
}

logger.info("Importing a request with URI [ {} ] - done", uri);
Expand Down Expand Up @@ -237,8 +236,9 @@ private void addAssertionsV2(String tests, WsdlProject project, Assertable asser
PostmanScriptParserV2 parserV2 = new PostmanScriptParserV2(context);
parserV2.parse(tests, requestName);

if (StringUtils.hasContent(parserV2.getTestsV1())) {
addAssertionsV1(parserV2.getTestsV1(), project, assertable);
String testsV1 = parserV2.getTestsV1();
if (StringUtils.hasContent(testsV1)) {
addAssertionsV1(testsV1, project, assertable);
}
}

Expand All @@ -247,11 +247,12 @@ private void processPreRequestScript(String preRequestScript, WsdlProject projec
PostmanScriptParserV2 parserV2 = new PostmanScriptParserV2(context);
parserV2.parse(preRequestScript);

if (StringUtils.hasContent(parserV2.getPrescriptV1())) {
String prescriptV1 = parserV2.getPrescriptV1();
if (StringUtils.hasContent(prescriptV1)) {
PostmanScriptTokenizer tokenizer = new PostmanScriptTokenizer();
PostmanScriptParserV1 parser = new PostmanScriptParserV1();
try {
LinkedList<Token> tokens = tokenizer.tokenize(parserV2.getPrescriptV1());
LinkedList<Token> tokens = tokenizer.tokenize(prescriptV1);
parser.parse(tokens, context);
} catch (SoapUIException e) {
logger.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public abstract class PostmanCollection {
public static final String SCRIPT = "script";
public static final String EXEC = "exec";
public static final char SCRIPT_LINE_DELIMITER = ';';
private static final String ONLY_COMMENT = "^ *(//|/\\*).*";
private static final String SINGLE_LINE_COMMENT_REGEX = "(.*?)( *\\/\\/.*)";
private static final String MULTI_LINE_COMMENT_REGEX = "(.*?)( */\\*.*)";
private static final String CONTINUATION_IN_CURRENT_LINE_REGEX = "^ *[!-/:-@\\[-`{-~].*";
private static final String CONTINUATION_IN_NEXT_LINE_REGEX = ".*[!-(*-/:-@\\[-`{-~] *$";
private static Pattern onlyCommentPattern;
private static Pattern continuationInCurrentLinePattern;
private static Pattern continuationInNextLinePattern;

protected final JSONObject postmanCollection;

Expand All @@ -47,10 +39,10 @@ public PostmanCollection(JSONObject postmanCollection) {

protected static String getEventScript(JSONObject request, ScriptType scriptType, String nodeName) {
JSONArray events = PostmanJsonUtil.getJsonArraySafely(request, nodeName);
List<Pattern> commentRegexPatterns = List.of(Pattern.compile(SINGLE_LINE_COMMENT_REGEX), Pattern.compile(MULTI_LINE_COMMENT_REGEX));
onlyCommentPattern = Pattern.compile(ONLY_COMMENT);
continuationInCurrentLinePattern = Pattern.compile(CONTINUATION_IN_CURRENT_LINE_REGEX);
continuationInNextLinePattern = Pattern.compile(CONTINUATION_IN_NEXT_LINE_REGEX);
List<Pattern> commentRegexPatterns = List.of(Pattern.compile("(.*?)(//.*)"), Pattern.compile("(.*?)(/\\*.*)"));
Pattern onlyCommentPattern = Pattern.compile("^ *(//|/\\*).*");
Pattern continuationInCurrentLinePattern = Pattern.compile("^ *[!-/:-@\\[-`{-~].*");
Pattern continuationInNextLinePattern = Pattern.compile(".*[!-(*-/:-@\\[-`{-~] *$");

for (Object eventObject : events) {
if (eventObject instanceof JSONObject event) {
Expand All @@ -64,8 +56,18 @@ protected static String getEventScript(JSONObject request, ScriptType scriptType
JSONArray scriptLines = PostmanJsonUtil.getJsonArraySafely(script, EXEC);
for (Object scriptLine : scriptLines) {
String line = scriptLine.toString();
removeSemicolonFromPreviousLineIfNeeded(line, scriptBuilder);
appendNewLineAndComment(line, scriptBuilder, commentRegexPatterns);
removeSemicolonFromPreviousLineIfNeeded(
line,
scriptBuilder,
continuationInCurrentLinePattern,
onlyCommentPattern
);
appendNewLineAndComment(
line,
scriptBuilder,
commentRegexPatterns,
continuationInNextLinePattern
);
}
if (!scriptBuilder.isEmpty()) {
return scriptBuilder.toString();
Expand All @@ -76,7 +78,12 @@ protected static String getEventScript(JSONObject request, ScriptType scriptType
return null;
}

private static void appendNewLineAndComment(String currentLine, StringBuilder scriptBuffer, List<Pattern> commentRegexPatterns) {
private static void appendNewLineAndComment(
String currentLine,
StringBuilder scriptBuilder,
List<Pattern> commentRegexPatterns,
Pattern continuationInNextLinePattern) {

String comment = "";

for (Pattern commentRegex : commentRegexPatterns) {
Expand All @@ -89,24 +96,33 @@ private static void appendNewLineAndComment(String currentLine, StringBuilder sc
}
}

scriptBuffer.append(currentLine);
scriptBuilder.append(currentLine);
if (StringUtils.hasContent(currentLine)) {
addSemicolonIfNeeded(currentLine, scriptBuffer);
addSemicolonIfNeeded(currentLine, scriptBuilder, continuationInNextLinePattern);
}
scriptBuffer.append(comment);
scriptBuffer.append('\n');
scriptBuilder.append(comment);
scriptBuilder.append('\n');
}

private static void removeSemicolonFromPreviousLineIfNeeded(String currentLine, StringBuilder scriptBuffer) {
if (scriptBuffer.length() > 1 &&
scriptBuffer.charAt(scriptBuffer.length() - 2) == SCRIPT_LINE_DELIMITER &&
private static void removeSemicolonFromPreviousLineIfNeeded(
String currentLine,
StringBuilder scriptBuilder,
Pattern continuationInCurrentLinePattern,
Pattern onlyCommentPattern) {

if (scriptBuilder.length() > 1 &&
scriptBuilder.charAt(scriptBuilder.length() - 2) == SCRIPT_LINE_DELIMITER &&
continuationInCurrentLinePattern.matcher(currentLine).find() &&
!onlyCommentPattern.matcher(currentLine).find()) {
scriptBuffer.deleteCharAt(scriptBuffer.length() - 2);
scriptBuilder.deleteCharAt(scriptBuilder.length() - 2);
}
}

private static void addSemicolonIfNeeded(String currentLine, StringBuilder scriptBuffer) {
private static void addSemicolonIfNeeded(
String currentLine,
StringBuilder scriptBuffer,
Pattern continuationInNextLinePattern) {

if (!scriptBuffer.isEmpty() && !currentLine.isEmpty() &&
!continuationInNextLinePattern.matcher(currentLine).find()) {
scriptBuffer.append(SCRIPT_LINE_DELIMITER);
Expand Down

0 comments on commit 7d2e7cb

Please sign in to comment.