Search in sources :

Example 41 with ActionContext

use of org.structr.schema.action.ActionContext in project structr by structr.

the class ScriptingTest method testIncludeJs.

@Test
public void testIncludeJs() {
    final String script = "${{ Structr.includeJs('test'); }}\n";
    try (final Tx tx = app.tx()) {
        final ActionContext ctx = new ActionContext(securityContext, null);
        // just run without an error, that's enough for this test
        Scripting.evaluate(ctx, null, script, "test");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 42 with ActionContext

use of org.structr.schema.action.ActionContext in project structr by structr.

the class ScriptingTest method testPropertyConversion.

@Test
public void testPropertyConversion() {
    TestOne testOne = null;
    // setup phase
    try (final Tx tx = app.tx()) {
        testOne = app.create(TestOne.class);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // test phase, check value conversion
    try (final Tx tx = app.tx()) {
        final ActionContext actionContext = new ActionContext(securityContext);
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aString = 12; }}", "test");
        assertEquals("Invalid scripted property conversion result", "12", testOne.getProperty(TestOne.aString));
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.anInt = '12'; }}", "test");
        assertEquals("Invalid scripted property conversion result", 12L, (long) testOne.getProperty(TestOne.anInt));
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = '12.2342'; }}", "test");
        assertEquals("Invalid scripted property conversion result", 12.2342, (double) testOne.getProperty(TestOne.aDouble), 0.0);
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aDouble = 2; }}", "test");
        assertEquals("Invalid scripted property conversion result", 2.0, (double) testOne.getProperty(TestOne.aDouble), 0.0);
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aLong = 2352343457252; }}", "test");
        assertEquals("Invalid scripted property conversion result", 2352343457252L, (long) testOne.getProperty(TestOne.aLong));
        Scripting.evaluate(actionContext, testOne, "${{ var e = Structr.get('this'); e.aBoolean = true; }}", "test");
        assertEquals("Invalid scripted property conversion result", true, (boolean) testOne.getProperty(TestOne.aBoolean));
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 43 with ActionContext

use of org.structr.schema.action.ActionContext in project structr by structr.

the class ScriptingTest method testDateOutput.

@Test
public void testDateOutput() {
    final Date now = new Date();
    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);
        testOne.setProperty(TestOne.aDate, now);
        final String expectedDateOutput = isoDateFormat.format(now);
        final String dateOutput1 = Scripting.replaceVariables(ctx, testOne, "${this.aDate}");
        final String dateOutput2 = Scripting.replaceVariables(ctx, testOne, "${print(this.aDate)}");
        final String dateOutput3 = Scripting.replaceVariables(ctx, testOne, "${{Structr.print(Structr.this.aDate)}}");
        assertEquals("${this.aDate} should yield ISO 8601 date format", expectedDateOutput, dateOutput1);
        assertEquals("${print(this.aDate)} should yield ISO 8601 date format", expectedDateOutput, dateOutput2);
        assertEquals("${Structr.print(Structr.this.aDate)} should yield ISO 8601 date format", expectedDateOutput, dateOutput3);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail(fex.getMessage());
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 44 with ActionContext

use of org.structr.schema.action.ActionContext in project structr by structr.

the class ScriptingTest method testPython.

@Test
public void testPython() {
    try (final Tx tx = app.tx()) {
        final Principal testUser = createTestNode(Principal.class, "testuser");
        final ActionContext ctx = new ActionContext(SecurityContext.getInstance(testUser, AccessMode.Backend));
        // assertEquals("Invalid python scripting evaluation result", "Hello World from Python!\n", Scripting.evaluate(ctx, null, "${python{print \"Hello World from Python!\"}}"));
        System.out.println(Scripting.evaluate(ctx, null, "${python{print(Structr.get('me').id)}}", "test"));
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

ActionContext (org.structr.schema.action.ActionContext)44 FrameworkException (org.structr.common.error.FrameworkException)38 Tx (org.structr.core.graph.Tx)34 Test (org.junit.Test)33 StructrTest (org.structr.common.StructrTest)22 TestOne (org.structr.core.entity.TestOne)13 UnlicensedException (org.structr.common.error.UnlicensedException)11 GraphObject (org.structr.core.GraphObject)7 NodeInterface (org.structr.core.graph.NodeInterface)7 LinkedList (java.util.LinkedList)6 List (java.util.List)6 PropertyMap (org.structr.core.property.PropertyMap)6 Date (java.util.Date)5 SecurityContext (org.structr.common.SecurityContext)5 GraphObjectMap (org.structr.core.GraphObjectMap)4 Principal (org.structr.core.entity.Principal)4 SchemaNode (org.structr.core.entity.SchemaNode)4 PropertyKey (org.structr.core.property.PropertyKey)4 StructrUiTest (org.structr.web.StructrUiTest)4 SimpleDateFormat (java.text.SimpleDateFormat)3