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

test(agama): add test cases for agama transpiler #2722

Merged
merged 4 commits into from
Oct 25, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.jans.agama.test;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import org.testng.annotations.Test;

import static org.testng.Assert.*;

public class InexistentFlowTest extends BaseTest {

@Test
public void test() {
HtmlPage page = launch("flow" + Math.random(), null);
assertFalse(page.getUrl().toString().endsWith(".fls"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class MathFlowTest extends BaseTest {

private static final String QNAME = "io.jans.agama.test.math";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.jans.agama.test;

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
Expand All @@ -26,27 +24,24 @@ public void enableJS() {
@Test
public void randUid() {

HtmlPage page = start("" + Math.random());
page = proceed(page);
assertOK(page);
start("" + Math.random());
HtmlPage page = (HtmlPage) currentPageAfter(2000);

assertOK(page);
assertTrue(page.getUrl().toString().endsWith("error.htm"));
assertTextContained(page.getVisibleText().toLowerCase(), "failed", "authenticate");

}

@Test(dependsOnMethods = "randUid", alwaysRun = true)
@Parameters("redirectUri")
public void adminUid(String redirectUri) {

HtmlPage page = start("admin");
page = proceed(page);
start("admin");
Page page = currentPageAfter(2000);

assertOK(page);

if (!page.getUrl().toString().startsWith(redirectUri)) {
//check if this is the consent page
assertTextContained(page.getVisibleText().toLowerCase(), "permission", "allow");
}
assertTrue(page.getUrl().toString().startsWith(redirectUri));

}

Expand All @@ -59,15 +54,15 @@ private HtmlPage start(String uid) {

}

private HtmlPage proceed(HtmlPage page) {
private Page currentPageAfter(long wait) {

try {
//wait for the auto-submitting javascript to execute (see finished.ftlh) and redirections to take place
Thread.sleep(2000);
page = (HtmlPage) client.getCurrentWindow().getEnclosedPage();
Thread.sleep(wait);
Page p = client.getCurrentWindow().getEnclosedPage();

logger.debug("Landed at {}",page.getUrl());
return page;
logger.debug("Landed at {}", p.getUrl());
return p;
} catch (Exception e) {
fail(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ small = Call java.lang.Math#decrementExact small

Repeat small times max
divisor = Call java.lang.Math#incrementExact divisor
k = 0

//Try to divide the numbers by 2, 3, ... small+1
Iterate over numbers using n
k = Iterate over numbers using n
modulus = Call java.lang.Math#floorMod n divisor
Quit When modulus is 0
k = Call java.lang.Math#incrementExact k

Quit When k is numbers.length

Expand Down
2 changes: 1 addition & 1 deletion agama/engine/src/test/resources/testng.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

authzEndpoint=${server}/jans-auth/restv1/authorize

redirectUri=${server}/admin-ui
redirectUri=${server}/.well-known/openid-configuration

clientId=${clientId}

Expand Down
6 changes: 6 additions & 0 deletions agama/engine/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@
</classes>
</test>

<test name="inexistent" enabled="false">
<classes>
<class name="io.jans.agama.test.InexistentFlowTest" />
</classes>
</test>

</suite>
23 changes: 23 additions & 0 deletions agama/transpiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@
</properties>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
Expand Down Expand Up @@ -77,6 +93,13 @@
<artifactId>jackson-annotations</artifactId>
</dependency>

<!-- TESTS -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,14 @@ public static TranspilationResult transpile(String flowQname, Set<String> flowQN
public static void runSyntaxCheck(String flowQname, String source)
throws SyntaxException, TranspilerException {

Transpiler tr = new Transpiler(flowQname, null);
Transpiler tr = new Transpiler(flowQname, null);
tr.validateName(tr.getFlowContext(source));
}

public static void runSyntaxCheck(String source) throws SyntaxException, TranspilerException {
Transpiler tr = new Transpiler("", null);
tr.getFlowContext(source);
}

public static void main(String... args) throws Exception {

Expand Down
48 changes: 48 additions & 0 deletions agama/transpiler/src/test/java/io/jans/agama/test/FlowUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.jans.agama.test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class FlowUtil {

private static final String EXT = "txt";

private static Logger LOG = LogManager.getLogger(FlowUtil.class);

private FlowUtil() {}

public static Map<String, String> sourcesOfFolder(String parent) {

try {
Path path = Paths.get(parent);
LOG.debug("Reading files under {}", path.toAbsolutePath());

return Files.walk(path).filter(p -> p.toString().endsWith("." + EXT))
.map(p -> {

String code = null;
try {
code = Files.readString(p);
} catch (IOException e) {
LOG.error("Unable to read contents of {}: {}", p, e.getMessage());
}
return new SimpleEntry(p.toString(), code);

}).collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));

} catch (IOException e) {
LOG.error(e.getMessage(), e);
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jans.agama.test;

import io.jans.agama.dsl.Transpiler;
import io.jans.agama.dsl.TranspilerException;
import io.jans.agama.dsl.error.SyntaxException;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

import static org.testng.Assert.fail;

public class MalformedFlowsTest {

private Logger logger = LogManager.getLogger(getClass());

@Test
public void test() {

Map<String, String> map = new TreeMap<>(FlowUtil.sourcesOfFolder("target/test-classes/fail"));
//Sonar check likes Map.Entry instead of nicer ways to iterate like map.keySet()
for (Map.Entry<String, String> entry: map.entrySet()) {
String file = entry.getKey();

try {
logger.info("Checking syntax of '{}'", file);
Transpiler.runSyntaxCheck(entry.getValue());

fail(file + " is expected to have errors but it passed validation");
} catch(SyntaxException | TranspilerException e) {
String msg = e.getMessage();
logger.info("{}\n", msg);
}
}
logger.info("{} files examined", map.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jans.agama.test;

import io.jans.agama.dsl.Transpiler;
import io.jans.agama.dsl.TranspilerException;
import io.jans.agama.dsl.error.SyntaxException;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;

import static org.testng.Assert.fail;

public class ValidFlowsTest {

private Logger logger = LogManager.getLogger(getClass());

@Test
public void test() {

Map<String, String> map = FlowUtil.sourcesOfFolder("target/test-classes/pass");
//Sonar check likes Map.Entry instead of nicer ways to iterate like map.keySet()
for (Map.Entry<String, String> entry: map.entrySet()) {
String file = entry.getKey();

try {
logger.info("Checking syntax of '{}'", file);
Transpiler.runSyntaxCheck(entry.getValue());
} catch(SyntaxException | TranspilerException e) {
String msg = e.getMessage();
logger.error("{}\n", msg);

fail(file + " has errors", e);
}
}
logger.info("{} files examined", map.size());

}

}
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = "\""

//Failure reason: string literals cannot contain double quotes inside; last quote is hanging
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = .5

//Failure reason: a digit is expected before decimal separator
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = 'hello'

//Failure reason: string literals are surrounded by double quotes only
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = { "year": 2022, "color": "blue" }

//Failure reason: map literals are not JSON-like but more like Javascript objects
8 changes: 8 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flow flow
Basepath ""

x = [
1,
2 ]

//Failure reason: in arrays, only commas can be surrounded by new lines
9 changes: 9 additions & 0 deletions agama/transpiler/src/test/resources/fail/1-literals/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Flow flow
Basepath ""

x = { for: "you",
function: [ "properly" ], goto: {}

}

//Failure reason: in maps, only commas can be surrounded by new lines
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

_hey = "you"

//Failure reason: invalid variable name (underscored prefixed)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

yO0_ = { weird.Key = false }

//Failure reason: invalid key name (period character)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y.oh-me

//Failure reason: invalid access to key with special character in name (hyphen)
6 changes: 6 additions & 0 deletions agama/transpiler/src/test/resources/fail/2-variables/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flow flow
Basepath ""

x = y["hi"]

//Failure reason: only a positive number or variable name is allowed for list indexing
Loading