Search in sources :

Example 26 with ActionContext

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

the class RenderContextTest method testVariableReplacement.

@Test
public void testVariableReplacement() {
    NodeInterface detailsDataObject = null;
    Page page = null;
    DOMNode html = null;
    DOMNode head = null;
    DOMNode body = null;
    DOMNode title = null;
    DOMNode h1 = null;
    DOMNode div1 = null;
    DOMNode p1 = null;
    DOMNode div2 = null;
    DOMNode p2 = null;
    DOMNode div3 = null;
    DOMNode p3 = null;
    A a = null;
    DOMNode div4 = null;
    DOMNode p4 = null;
    TestOne testOne = null;
    try (final Tx tx = app.tx()) {
        detailsDataObject = app.create(TestOne.class, "TestOne");
        page = Page.createNewPage(securityContext, "testpage");
        page.setProperties(page.getSecurityContext(), new PropertyMap(Page.visibleToPublicUsers, true));
        assertTrue(page != null);
        assertTrue(page instanceof Page);
        html = (DOMNode) page.createElement("html");
        head = (DOMNode) page.createElement("head");
        body = (DOMNode) page.createElement("body");
        title = (DOMNode) page.createElement("title");
        h1 = (DOMNode) page.createElement("h1");
        div1 = (DOMNode) page.createElement("div");
        p1 = (DOMNode) page.createElement("p");
        div2 = (DOMNode) page.createElement("div");
        p2 = (DOMNode) page.createElement("p");
        div3 = (DOMNode) page.createElement("div");
        p3 = (DOMNode) page.createElement("p");
        a = (A) page.createElement("a");
        div4 = (DOMNode) page.createElement("div");
        p4 = (DOMNode) page.createElement("p");
        // add HTML element to page
        page.appendChild(html);
        // add HEAD and BODY elements to HTML
        html.appendChild(head);
        html.appendChild(body);
        // add TITLE element to HEAD
        head.appendChild(title);
        // add H1 element to BODY
        body.appendChild(h1);
        // add DIV element 1 to BODY
        body.appendChild(div1);
        div1.appendChild(p1);
        // add DIV element 2 to DIV
        div1.appendChild(div2);
        div2.appendChild(p2);
        // add DIV element 3 to DIV
        div2.appendChild(div3);
        div3.appendChild(p3);
        // add link to p3
        p3.appendChild(a);
        a.setLinkable(page);
        body.appendChild(div4);
        div4.appendChild(p4);
        final PropertyMap p4Properties = new PropertyMap();
        p4Properties.put(StructrApp.key(DOMElement.class, "restQuery"), "/divs");
        p4Properties.put(StructrApp.key(DOMElement.class, "dataKey"), "div");
        p4.setProperties(p4.getSecurityContext(), p4Properties);
        NodeList paragraphs = page.getElementsByTagName("p");
        assertEquals(p1, paragraphs.item(0));
        assertEquals(p2, paragraphs.item(1));
        assertEquals(p3, paragraphs.item(2));
        assertEquals(p4, paragraphs.item(3));
        // create users
        final User tester1 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester1"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester1@test.com"));
        final User tester2 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester2"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester2@test.com"));
        assertNotNull("User tester1 should exist.", tester1);
        assertNotNull("User tester2 should exist.", tester2);
        // create admin user for later use
        final PropertyMap adminProperties = new PropertyMap();
        adminProperties.put(StructrApp.key(User.class, "name"), "admin");
        adminProperties.put(StructrApp.key(User.class, "password"), "admin");
        adminProperties.put(StructrApp.key(User.class, "isAdmin"), true);
        app.create(User.class, adminProperties);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final RenderContext ctx = new RenderContext(securityContext);
        ctx.setDetailsDataObject(detailsDataObject);
        ctx.setPage(page);
        // test for "empty" return value
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${err}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.this.this.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.owner}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.owner}"));
        // other functions are tested in the ActionContextTest in structr-core, see there.
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${true}"));
        assertEquals("false", Scripting.replaceVariables(ctx, p1, "${false}"));
        assertEquals("yes", Scripting.replaceVariables(ctx, p1, "${if(true, \"yes\", \"no\")}"));
        assertEquals("no", Scripting.replaceVariables(ctx, p1, "${if(false, \"yes\", \"no\")}"));
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${if(true, true, false)}"));
        assertEquals("false", Scripting.replaceVariables(ctx, p1, "${if(false, true, false)}"));
        // test keywords
        assertEquals("${id} should evaluate to the ID if the current details object", detailsDataObject.getUuid(), Scripting.replaceVariables(ctx, p1, "${id}"));
        ctx.setDetailsDataObject(null);
        assertEquals("${id} should evaluate to the ID if the current details object", "abc12345", Scripting.replaceVariables(ctx, p1, "${id!abc12345}"));
        ctx.setDetailsDataObject(detailsDataObject);
        assertEquals("${id} should be equal to ${current.id}", "true", Scripting.replaceVariables(ctx, p1, "${equal(id, current.id)}"));
        assertEquals("${element} should evaluate to the current DOM node", p1.toString(), Scripting.replaceVariables(ctx, p1, "${element}"));
        assertNull(Scripting.replaceVariables(ctx, p1, "${if(true, null, \"no\")}"));
        assertNull(Scripting.replaceVariables(ctx, p1, "${null}"));
        assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, p1, "/${page.name}?${page.id}"));
        assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, a, "/${link.name}?${link.id}"));
        // these tests find single element => success
        assertEquals("Invalid replacement result", page.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('Page', 'name', 'testpage'), 'id')}"));
        assertEquals("Invalid replacement result", a.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('A'), 'id')}"));
        // this test finds multiple <p> elements => error
        assertEquals("Invalid replacement result", GetFunction.ERROR_MESSAGE_GET_ENTITY, Scripting.replaceVariables(ctx, a, "${get(find('P'), 'id')}"));
        // more complex replacement
        // assertEquals("Invalid replacement result", "", a.replaceVariables(ctx, securityContext, "${get(find('P'), 'id')}"));
        // String default value
        assertEquals("bar", Scripting.replaceVariables(ctx, p1, "${request.foo!bar}"));
        // Number default value (will be evaluated to a string)
        assertEquals("1", Scripting.replaceVariables(ctx, p1, "${page.position!1}"));
        // Number default value
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${equal(42, this.null!42)}"));
        final User tester1 = app.nodeQuery(User.class).andName("tester1").getFirst();
        final User tester2 = app.nodeQuery(User.class).andName("tester2").getFirst();
        assertNotNull("User tester1 should exist.", tester1);
        assertNotNull("User tester2 should exist.", tester2);
        final ActionContext tester1Context = new ActionContext(SecurityContext.getInstance(tester1, AccessMode.Backend));
        final ActionContext tester2Context = new ActionContext(SecurityContext.getInstance(tester2, AccessMode.Backend));
        // users
        assertEquals("tester1", Scripting.replaceVariables(tester1Context, p1, "${me.name}"));
        assertEquals("tester2", Scripting.replaceVariables(tester2Context, p2, "${me.name}"));
        // allow unauthenticated GET on /pages
        grant("Page/_Ui", 16, true);
        // test GET REST access
        assertEquals("Invalid GET notation result", page.getName(), Scripting.replaceVariables(ctx, p1, "${from_json(GET('http://localhost:" + httpPort + "/structr/rest/pages/ui')).result[0].name}"));
        grant("Folder", 64, true);
        grant("_login", 64, false);
        assertEquals("Invalid POST result", "201", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:status}').status}"));
        assertEquals("Invalid POST result", "1.0", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:result_count}').body.result_count}"));
        assertEquals("Invalid POST result", "application/json;charset=utf-8", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:content-type}').headers.Content-Type}"));
        // test POST with invalid name containing curly braces to provoke 422
        assertEquals("Invalid POST result", "422", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:\"ShouldFail/xyz\"}').status}"));
        // test login and sessions
        final String sessionIdCookie = Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/login', '{name:admin,password:admin}').headers.Set-Cookie}");
        final String sessionId = HttpCookie.parse(sessionIdCookie).get(0).getValue();
        // test authenticated GET request using session ID cookie
        assertEquals("Invalid authenticated GET result", "admin", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[0].name}"));
        assertEquals("Invalid authenticated GET result", "tester1", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[1].name}"));
        assertEquals("Invalid authenticated GET result", "tester2", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[2].name}"));
        // locale
        final String localeString = ctx.getLocale().toString();
        assertEquals("Invalid locale result", localeString, Scripting.replaceVariables(ctx, page, "${locale}"));
        // set new details object
        final TestOne detailsDataObject2 = app.create(TestOne.class, "TestOne");
        Scripting.replaceVariables(ctx, p1, "${set_details_object(first(find('TestOne', 'id', '" + detailsDataObject2.getUuid() + "')))}");
        assertEquals("${current.id} should resolve to new details object", detailsDataObject2.getUuid(), Scripting.replaceVariables(ctx, p1, "${current.id}"));
        // test values() with single parameter
        assertEquals("Invalid values() result", "[test]", Scripting.replaceVariables(ctx, page, "${values(from_json('{name:test}'))}"));
        testOne = createTestNode(TestOne.class);
        testOne.setProperty(TestOne.htmlString, "<a b=\"c\">&d</a>");
        // escape_html
        assertEquals("Invalid escape_html() result", "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;", Scripting.replaceVariables(ctx, testOne, "${escape_html(this.htmlString)}"));
        testOne.setProperty(TestOne.htmlString, "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;");
        // unescape_html
        assertEquals("Invalid unescape_html() result", "<a b=\"c\">&d</a>", Scripting.replaceVariables(ctx, testOne, "${unescape_html(this.htmlString)}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : A(org.structr.web.entity.html.A) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeList(org.w3c.dom.NodeList) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) ActionContext(org.structr.schema.action.ActionContext) PropertyMap(org.structr.core.property.PropertyMap) TestOne(org.structr.web.entity.TestOne) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 27 with ActionContext

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

the class RenderContextTest method testAnyAllAndNoneFunctions2.

@Test
public void testAnyAllAndNoneFunctions2() {
    final ActionContext ctx = new ActionContext(securityContext, null);
    try (final Tx tx = app.tx()) {
        // expectations (we use Boolean.valueOf(value.toString())
        // true  == true
        // "true" == true
        // false  == false
        // "false" == false
        // 1  == false
        // 0  == false
        // "test" == false
        assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, true), data)}"));
        assertEquals("Invalid any() result", "false", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, false), data)}"));
        assertEquals("Invalid any() result", "false", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, 1), data)}"));
        assertEquals("Invalid any() result", "false", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, 0), data)}"));
        assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, 'true'), data)}"));
        assertEquals("Invalid any() result", "false", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, 'false'), data)}"));
        assertEquals("Invalid any() result", "false", Scripting.replaceVariables(ctx, null, "${any(merge(false, false, false, 'test'), data)}"));
        assertEquals("Invalid all() result", "true", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true), data)}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, false), data)}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, 1), data)}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, 0), data)}"));
        assertEquals("Invalid all() result", "true", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, 'true'), data)}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, 'false'), data)}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(merge(true, true, true, true, 'test'), data)}"));
        assertEquals("Invalid none() result", "true", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false), data)}"));
        assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, true), data)}"));
        assertEquals("Invalid none() result", "true", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, 1), data)}"));
        assertEquals("Invalid none() result", "true", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, 0), data)}"));
        assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, 'true'), data)}"));
        assertEquals("Invalid none() result", "true", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, 'false'), data)}"));
        assertEquals("Invalid none() result", "true", Scripting.replaceVariables(ctx, null, "${none(merge(false, false, false, 'test'), data)}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 28 with ActionContext

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

the class AdvancedSchemaTest method test04SchemaPropertyOrderInBuiltInViews.

@Test
public void test04SchemaPropertyOrderInBuiltInViews() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    final GenericProperty jsonName = new GenericProperty("jsonName");
    SchemaNode test = null;
    String id = null;
    try (final Tx tx = app.tx()) {
        // create test type
        test = app.create(SchemaNode.class, "Test");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    try (final Tx tx = app.tx()) {
        // create view with sort order
        final List<SchemaView> list = test.getProperty(SchemaNode.schemaViews);
        // create properties
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
    final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
    Assert.assertEquals("Invalid number of properties in sorted view", 6, list.size());
    Assert.assertEquals("id", list.get(0).dbName());
    Assert.assertEquals("type", list.get(1).dbName());
    Assert.assertEquals("one", list.get(2).dbName());
    Assert.assertEquals("two", list.get(3).dbName());
    Assert.assertEquals("three", list.get(4).dbName());
    Assert.assertEquals("four", list.get(5).dbName());
    try (final Tx tx = app.tx()) {
        for (final SchemaView testView : test.getProperty(SchemaNode.schemaViews)) {
            // modify sort order
            testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
        }
        // create test entity
        final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
        // save UUID for later
        id = node.getUuid();
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
    Assert.assertEquals("Invalid number of properties in sorted view", 6, list2.size());
    Assert.assertEquals("id", list2.get(0).dbName());
    Assert.assertEquals("type", list2.get(1).dbName());
    Assert.assertEquals("one", list2.get(2).dbName());
    Assert.assertEquals("two", list2.get(3).dbName());
    Assert.assertEquals("three", list2.get(4).dbName());
    Assert.assertEquals("four", list2.get(5).dbName());
    // test schema resource
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(6)).body("result[0].jsonName", equalTo("id")).body("result[1].jsonName", equalTo("type")).body("result[2].jsonName", equalTo("one")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).when().get("/_schema/Test/public");
    // test actual REST resource (not easy to extract and verify
    // JSON property order, that's why we're using replaceAll and
    // string comparison..
    final String[] actual = RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).when().get("/Test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
    // we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
    assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
    assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
    assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
    assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
    assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
    assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
    assertEquals("Invalid JSON result for sorted property view", "id", actual[7]);
    assertEquals("Invalid JSON result for sorted property view", id, actual[8]);
    assertEquals("Invalid JSON result for sorted property view", "type", actual[9]);
    assertEquals("Invalid JSON result for sorted property view", "Test", actual[10]);
    assertEquals("Invalid JSON result for sorted property view", "one", actual[11]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[12]);
    assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
    assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
    assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
    assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[19]);
    // try built-in function
    try {
        final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "public" });
        Assert.assertEquals("Invalid number of properties in sorted view", 6, list3.size());
        Assert.assertEquals("id", list3.get(0).get(jsonName));
        Assert.assertEquals("type", list3.get(1).get(jsonName));
        Assert.assertEquals("one", list3.get(2).get(jsonName));
        Assert.assertEquals("two", list3.get(3).get(jsonName));
        Assert.assertEquals("three", list3.get(4).get(jsonName));
        Assert.assertEquals("four", list3.get(5).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    // try scripting call
    try {
        final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'public')}", "test");
        Assert.assertEquals("Invalid number of properties in sorted view", 6, list4.size());
        Assert.assertEquals("id", list4.get(0).get(jsonName));
        Assert.assertEquals("type", list4.get(1).get(jsonName));
        Assert.assertEquals("one", list4.get(2).get(jsonName));
        Assert.assertEquals("two", list4.get(3).get(jsonName));
        Assert.assertEquals("three", list4.get(4).get(jsonName));
        Assert.assertEquals("four", list4.get(5).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) TypeInfoFunction(org.structr.core.function.TypeInfoFunction) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) LinkedList(java.util.LinkedList) List(java.util.List) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 29 with ActionContext

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

the class LicensingScriptingTest method testUnlicensedFunctions.

@Test
public void testUnlicensedFunctions() {
    try (final Tx tx = app.tx()) {
        final ActionContext ctx = new ActionContext(securityContext, null);
        final String expression = "${lower(localize(concat(config('application.title'))))}";
        final String expected = expression;
        // expect the expression that uses an unlicensed built-in function to return the script source instead of an evaluation result
        assertEquals("Invalid result for quoted template expression", expected, Scripting.replaceVariables(ctx, null, expression));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try {
        Thread.sleep(10000);
    } catch (Throwable t) {
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) LicensingTest(org.structr.common.LicensingTest) Test(org.junit.Test)

Example 30 with ActionContext

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

the class ScriptingTest method testEnumPropertyGet.

@Test
public void testEnumPropertyGet() {
    // 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, "${{ var e = Structr.get('this'); e.anEnum = 'One'; }}", "test");
        assertEquals("Invalid enum get result", "One", Scripting.evaluate(actionContext, context, "${{ var e = Structr.get('this'); return e.anEnum; }}", "test"));
        assertEquals("Invaliid Javascript enum comparison result", true, Scripting.evaluate(actionContext, context, "${{ var e = Structr.get('this'); return e.anEnum == 'One'; }}", "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) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) 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