Skip to content
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

RIA-23302 Add Chai assertions #75

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</repositories>

<properties>
<ready-api-version>3.59.0-SNAPSHOT</ready-api-version>
<ready-api-version>3.59.0-POSTMAN-SNAPSHOT</ready-api-version>
<java.version>17</java.version>
</properties>

Expand Down Expand Up @@ -83,6 +83,11 @@
<artifactId>ezmorph</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7.15</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
import com.smartbear.ready.plugin.postman.collection.PostmanCollectionFactory;
import com.smartbear.ready.plugin.postman.collection.Request;
import com.smartbear.ready.plugin.postman.exceptions.PostmanCollectionUnsupportedVersionException;
import com.smartbear.ready.plugin.postman.script.PostmanScriptParser;
import com.smartbear.ready.plugin.postman.script.PostmanScriptParserV1;
import com.smartbear.ready.plugin.postman.script.PostmanScriptParserV2;
import com.smartbear.ready.plugin.postman.script.PostmanScriptTokenizer;
import com.smartbear.ready.plugin.postman.script.PostmanScriptTokenizer.Token;
import com.smartbear.ready.plugin.postman.script.ScriptContext;
Expand Down Expand Up @@ -84,8 +85,8 @@ public WsdlProject importPostmanCollection(WorkspaceImpl workspace, String fileP
String postmanJson = getPostmanImporterWorker(filePath).getPostmanJson();
if (PostmanJsonUtil.seemsToBeJson(postmanJson)) {
JSON json = new PostmanJsonUtil().parseTrimmedText(postmanJson);
if (json instanceof JSONObject) {
PostmanCollection postmanCollection = PostmanCollectionFactory.getCollection((JSONObject) json);
if (json instanceof JSONObject jsonObject) {
PostmanCollection postmanCollection = PostmanCollectionFactory.getCollection(jsonObject);
String collectionName = postmanCollection.getName();
foldersAmount = Integer.toString(postmanCollection.getFolders().size());
requestsAmount = Integer.toString(postmanCollection.getRequests().size());
Expand Down Expand Up @@ -157,7 +158,7 @@ public WsdlProject importPostmanCollection(WorkspaceImpl workspace, String fileP
}

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

logger.info("Importing a request with URI [ {} ] - done", uri);
Expand Down Expand Up @@ -217,29 +218,45 @@ private void addHttpHeaders(AbstractHttpRequest request, List<PostmanCollection.
}


void addAssertions(String tests, WsdlProject project, Assertable assertable) {
private void addAssertionsV1(String tests, WsdlProject project, Assertable assertable) {
PostmanScriptTokenizer tokenizer = new PostmanScriptTokenizer();
PostmanScriptParser parser = new PostmanScriptParser();
PostmanScriptParserV1 parser = new PostmanScriptParserV1();
try {
LinkedList<Token> tokens = tokenizer.tokenize(tests);

ScriptContext context = ScriptContext.prepareTestScriptContext(project, assertable);
parser.parse(tokens, context);
} catch (SoapUIException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}

private void processPreRequestScript(String preRequestScript, WsdlProject project) {
PostmanScriptTokenizer tokenizer = new PostmanScriptTokenizer();
PostmanScriptParser parser = new PostmanScriptParser();
try {
LinkedList<Token> tokens = tokenizer.tokenize(preRequestScript);
private void addAssertionsV2(String tests, WsdlProject project, Assertable assertable, String requestName) {
ScriptContext context = ScriptContext.prepareTestScriptContext(project, assertable);
PostmanScriptParserV2 parserV2 = new PostmanScriptParserV2(context);
parserV2.parse(tests, requestName);

ScriptContext context = ScriptContext.preparePreRequestScriptContext(project);
parser.parse(tokens, context);
} catch (SoapUIException e) {
e.printStackTrace();
String testsV1 = parserV2.getTestsV1();
if (StringUtils.hasContent(testsV1)) {
addAssertionsV1(testsV1, project, assertable);
}
}

private void processPreRequestScript(String preRequestScript, WsdlProject project) {
ScriptContext context = ScriptContext.preparePreRequestScriptContext(project);
PostmanScriptParserV2 parserV2 = new PostmanScriptParserV2(context);
parserV2.parse(preRequestScript);

String prescriptV1 = parserV2.getPrescriptV1();
if (StringUtils.hasContent(prescriptV1)) {
PostmanScriptTokenizer tokenizer = new PostmanScriptTokenizer();
PostmanScriptParserV1 parser = new PostmanScriptParserV1();
try {
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 @@ -9,6 +9,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class PostmanCollection {
public static final String NAME = "name";
Expand Down Expand Up @@ -37,32 +39,96 @@ 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("(.*?)(//.*)"), Pattern.compile("(.*?)(/\\*.*)"));
Pattern onlyCommentPattern = Pattern.compile("^ *(//|/\\*).*");
Pattern continuationInCurrentLinePattern = Pattern.compile("^ *[!-/:-@\\[-`{-~].*");
Pattern continuationInNextLinePattern = Pattern.compile(".*[!-(*-/:-@\\[-`{-~] *$");

for (Object eventObject : events) {
if (eventObject instanceof JSONObject) {
JSONObject event = (JSONObject) eventObject;
if (eventObject instanceof JSONObject event) {
String listen = getValue(event, LISTEN);
if (!StringUtils.sameString(listen, scriptType.getListenType())) {
continue;
}
JSONObject script = event.getJSONObject(SCRIPT);
if (script != null) {
StringBuffer scriptBuffer = new StringBuffer();
StringBuilder scriptBuilder = new StringBuilder();
JSONArray scriptLines = PostmanJsonUtil.getJsonArraySafely(script, EXEC);
for (Object scriptLine : scriptLines) {
if (scriptBuffer.length() > 0) {
scriptBuffer.append(SCRIPT_LINE_DELIMITER);
}
scriptBuffer.append(scriptLine);
String line = scriptLine.toString();
removeSemicolonFromPreviousLineIfNeeded(
line,
scriptBuilder,
continuationInCurrentLinePattern,
onlyCommentPattern
);
appendNewLineAndComment(
line,
scriptBuilder,
commentRegexPatterns,
continuationInNextLinePattern
);
}
if (scriptBuffer.length() > 0) {
return scriptBuffer.toString();
if (!scriptBuilder.isEmpty()) {
return scriptBuilder.toString();
}
}
}
}
return null;
}

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

String comment = "";

for (Pattern commentRegex : commentRegexPatterns) {
Matcher commentMatcher = commentRegex.matcher(currentLine);

if (commentMatcher.find()) {
currentLine = commentMatcher.group(1);
comment = commentMatcher.group(2);
break;
}
}

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

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()) {
scriptBuilder.deleteCharAt(scriptBuilder.length() - 2);
}
}

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

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

protected static String getValue(JSONObject jsonObject, String name) {
return getValue(jsonObject, name, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
package com.smartbear.ready.plugin.postman.script;

public interface AddAssertionCommand extends ScriptCommand {
void addCondition(String condition);
default void addCondition(String condition) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.smartbear.ready.plugin.postman.script;

import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.ChaiAssertion;
import com.eviware.soapui.model.testsuite.Assertable;
import com.eviware.soapui.support.StringUtils;

public class AddChaiAssertionCommand implements AddAssertionCommand {
public static final String NAME = "chaiAssertion";
private final Assertable assertable;
private String script;
private String assertionName;

public AddChaiAssertionCommand(Assertable assertable) {
this.assertable = assertable;
}

@Override
public String getName() {
return NAME;
}

@Override
public void prepare() {
script = null;
}

@Override
public void addArgument(TokenType tokenType, String argument) {
script = argument;
}

public void addAssertionName(String name) {
this.assertionName = name;
}

@Override
public boolean validate() {
return StringUtils.hasContent(script);
}

@Override
public Object execute() {
ChaiAssertion assertion = (ChaiAssertion) assertable.addAssertion(ChaiAssertion.LABEL);
assertion.setScriptText(script);
if (!StringUtils.isNullOrEmpty(assertionName)) {
assertion.getConfig().setName(assertionName);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public AddEqualsAssertionCommand(Assertable assertable) {
this.assertable = assertable;
}

@Override
public void addCondition(String condition) {

}

@Override
public String getName() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public AddHeaderExistsAssertionCommand(Assertable assertable) {
super(assertable);
}

@Override
public void addCondition(String condition) {
}

@Override
public String getName() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public AddSimpleContainsAssertionCommand(Assertable assertable) {
this.assertable = assertable;
}

@Override
public void addCondition(String condition) {

}

@Override
public String getName() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public AddSlaAssertionCommand(Assertable assertable) {
this.assertable = assertable;
}

@Override
public void addCondition(String condition) {
}

@Override
public String getName() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.LinkedList;
import java.util.Stack;

public class PostmanScriptParser {
public static Logger log = LoggerFactory.getLogger(PostmanScriptParser.class);
public class PostmanScriptParserV1 {
public static Logger log = LoggerFactory.getLogger(PostmanScriptParserV1.class);

public static final String TEST_LIST = "tests";

Expand Down
Loading