use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testWrappingUnwrapping.
@Test
public void testWrappingUnwrapping() {
// setup phase
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
final TestOne context = app.create(TestOne.class);
Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', { name: 'Group1' } ); }}", "test");
Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', 'name', 'Group2'); }}", "test");
assertEquals("Invalid unwrapping result", 2, app.nodeQuery(Group.class).getAsList().size());
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testQuotes.
@Test
public void testQuotes() {
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
Scripting.evaluate(actionContext, app.create(TestOne.class), "${{\n // \"test\n}}", "test");
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testDateCopy.
@Test
public void testDateCopy() {
final Date now = new Date();
final Date futureDate = new Date(now.getTime() + 600000);
final SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
// Copy dates with/without format in StructrScript
TestOne testOne = createTestNode(TestOne.class);
TestThree testThree = createTestNode(TestThree.class);
testOne.setProperty(TestOne.aDate, now);
Scripting.replaceVariables(ctx, testThree, "${set(this, 'aDateWithFormat', get(find('TestOne', '" + testOne.getUuid() + "'), 'aDate'))}");
assertEquals("Copying a date (with default format) to a date (with custom format) failed [StructrScript]", isoDateFormat.format(testOne.getProperty(TestOne.aDate)), isoDateFormat.format(testThree.getProperty(TestThree.aDateWithFormat)));
testThree.setProperty(TestThree.aDateWithFormat, futureDate);
Scripting.replaceVariables(ctx, testOne, "${set(this, 'aDate', get(find('TestThree', '" + testThree.getUuid() + "'), 'aDateWithFormat'))}");
assertEquals("Copying a date (with custom format) to a date (with default format) failed [StructrScript]", isoDateFormat.format(testOne.getProperty(TestOne.aDate)), isoDateFormat.format(testThree.getProperty(TestThree.aDateWithFormat)));
// Perform the same tests in JavaScript
testOne.setProperty(TestOne.aDate, null);
testThree.setProperty(TestThree.aDateWithFormat, null);
testOne.setProperty(TestOne.aDate, now);
Scripting.replaceVariables(ctx, testThree, "${{ var testThree = Structr.this; var testOne = Structr.find('TestOne', '" + testOne.getUuid() + "'); testThree.aDateWithFormat = testOne.aDate; }}");
assertEquals("Copying a date (with default format) to a date (with custom format) failed [JavaScript]", isoDateFormat.format(testOne.getProperty(TestOne.aDate)), isoDateFormat.format(testThree.getProperty(TestThree.aDateWithFormat)));
testThree.setProperty(TestThree.aDateWithFormat, futureDate);
Scripting.replaceVariables(ctx, testOne, "${{ var testOne = Structr.this; var testThree = Structr.find('TestThree', '" + testThree.getUuid() + "'); testOne.aDate = testThree.aDateWithFormat; }}");
assertEquals("Copying a date (with custom format) to a date (with default format) failed [JavaScript]", isoDateFormat.format(testOne.getProperty(TestOne.aDate)), isoDateFormat.format(testThree.getProperty(TestThree.aDateWithFormat)));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail(fex.getMessage());
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testScriptedFindWithJSONObject.
@Test
public void testScriptedFindWithJSONObject() {
final Random random = new Random();
final long long1 = 13475233523455L;
final long long2 = 327326252322L;
final double double1 = 1234.56789;
final double double2 = 5678.975321;
List<TestSix> testSixs = null;
TestOne testOne1 = null;
TestOne testOne2 = null;
TestTwo testTwo1 = null;
TestTwo testTwo2 = null;
TestThree testThree1 = null;
TestThree testThree2 = null;
TestFour testFour1 = null;
TestFour testFour2 = null;
Date date1 = null;
Date date2 = null;
// setup phase
try (final Tx tx = app.tx()) {
testSixs = createTestNodes(TestSix.class, 10);
testOne1 = app.create(TestOne.class);
testOne2 = app.create(TestOne.class);
testTwo1 = app.create(TestTwo.class);
testTwo2 = app.create(TestTwo.class);
testThree1 = app.create(TestThree.class);
testThree2 = app.create(TestThree.class);
testFour1 = app.create(TestFour.class);
testFour2 = app.create(TestFour.class);
date1 = new Date(random.nextLong());
date2 = new Date();
testOne1.setProperty(TestOne.anInt, 42);
testOne1.setProperty(TestOne.aLong, long1);
testOne1.setProperty(TestOne.aDouble, double1);
testOne1.setProperty(TestOne.aDate, date1);
testOne1.setProperty(TestOne.anEnum, Status.One);
testOne1.setProperty(TestOne.aString, "aString1");
testOne1.setProperty(TestOne.aBoolean, true);
testOne1.setProperty(TestOne.testTwo, testTwo1);
testOne1.setProperty(TestOne.testThree, testThree1);
testOne1.setProperty(TestOne.testFour, testFour1);
testOne1.setProperty(TestOne.manyToManyTestSixs, testSixs.subList(0, 5));
testOne2.setProperty(TestOne.anInt, 33);
testOne2.setProperty(TestOne.aLong, long2);
testOne2.setProperty(TestOne.aDouble, double2);
testOne2.setProperty(TestOne.aDate, date2);
testOne2.setProperty(TestOne.anEnum, Status.Two);
testOne2.setProperty(TestOne.aString, "aString2");
testOne2.setProperty(TestOne.aBoolean, false);
testOne2.setProperty(TestOne.testTwo, testTwo2);
testOne2.setProperty(TestOne.testThree, testThree2);
testOne2.setProperty(TestOne.testFour, testFour2);
testOne2.setProperty(TestOne.manyToManyTestSixs, testSixs.subList(5, 10));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test phase, find all the things using scripting
try (final Tx tx = app.tx()) {
final ActionContext actionContext = new ActionContext(securityContext);
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anInt: 42 })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anInt: 33 })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aLong: " + long1 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aLong: " + long2 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aDouble: " + double1 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aDouble: " + double2 + " })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anEnum: 'One' })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { anEnum: 'Two' })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne1, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aBoolean: true })[0]; }}", "test"));
assertEquals("Invalid scripted find() result", testOne2, Scripting.evaluate(actionContext, testOne1, "${{ return Structr.find('TestOne', { aBoolean: false })[0]; }}", "test"));
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testFunctionRollbackOnError.
@Test
public void testFunctionRollbackOnError() {
final ActionContext ctx = new ActionContext(securityContext, null);
/**
* first the old scripting style
*/
TestOne testNodeOldScripting = null;
try (final Tx tx = app.tx()) {
testNodeOldScripting = createTestNode(TestOne.class);
testNodeOldScripting.setProperty(TestOne.aString, "InitialString");
testNodeOldScripting.setProperty(TestOne.anInt, 42);
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
Scripting.replaceVariables(ctx, testNodeOldScripting, "${ ( set(this, 'aString', 'NewString'), set(this, 'anInt', 'NOT_AN_INTEGER') ) }");
fail("StructrScript: setting anInt to 'NOT_AN_INTEGER' should cause an Exception");
tx.success();
} catch (FrameworkException expected) {
}
try {
try (final Tx tx = app.tx()) {
assertEquals("StructrScript: String should still have initial value!", "InitialString", Scripting.replaceVariables(ctx, testNodeOldScripting, "${(get(this, 'aString'))}"));
tx.success();
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
/**
* then the JS-style scripting
*/
TestOne testNodeJavaScript = null;
try (final Tx tx = app.tx()) {
testNodeJavaScript = createTestNode(TestOne.class);
testNodeJavaScript.setProperty(TestOne.aString, "InitialString");
testNodeJavaScript.setProperty(TestOne.anInt, 42);
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
Scripting.replaceVariables(ctx, testNodeJavaScript, "${{ var t1 = Structr.get('this'); t1.aString = 'NewString'; t1.anInt = 'NOT_AN_INTEGER'; }}");
fail("StructrScript: setting anInt to 'NOT_AN_INTEGER' should cause an Exception");
tx.success();
} catch (FrameworkException expected) {
}
try {
try (final Tx tx = app.tx()) {
assertEquals("JavaScript: String should still have initial value!", "InitialString", Scripting.replaceVariables(ctx, testNodeJavaScript, "${{ var t1 = Structr.get('this'); Structr.print(t1.aString); }}"));
tx.success();
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
Aggregations