Search in sources :

Example 66 with TestOne

use of org.structr.core.entity.TestOne 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.");
    }
}
Also used : Group(org.structr.core.entity.Group) 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 67 with TestOne

use of org.structr.core.entity.TestOne 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());
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestThree(org.structr.core.entity.TestThree) 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 68 with TestOne

use of org.structr.core.entity.TestOne 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.");
    }
}
Also used : TestFour(org.structr.core.entity.TestFour) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) Date(java.util.Date) UnlicensedException(org.structr.common.error.UnlicensedException) Random(java.util.Random) TestTwo(org.structr.core.entity.TestTwo) TestThree(org.structr.core.entity.TestThree) TestOne(org.structr.core.entity.TestOne) TestSix(org.structr.core.entity.TestSix) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 69 with TestOne

use of org.structr.core.entity.TestOne 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");
    }
}
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) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 70 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class ScriptingTest method testPrivilegedFind.

@Test
public void testPrivilegedFind() {
    final ActionContext ctx = new ActionContext(securityContext, null);
    TestOne testNode = null;
    String uuid = "";
    try (final Tx tx = app.tx()) {
        testNode = createTestNode(TestOne.class);
        testNode.setProperty(TestOne.aString, "InitialString");
        testNode.setProperty(TestOne.anInt, 42);
        uuid = testNode.getProperty(new StringProperty("id"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        assertEquals("JavaScript: Trying to find entity with type,key,value!", "InitialString", Scripting.replaceVariables(ctx, testNode, "${{ var t1 = Structr.first(Structr.find_privileged('TestOne','anInt','42')); Structr.print(t1.aString); }}"));
        assertEquals("JavaScript: Trying to find entity with type,id!", "InitialString", Scripting.replaceVariables(ctx, testNode, "${{ var t1 = Structr.find_privileged('TestOne','" + uuid + "'); Structr.print(t1.aString); }}"));
        assertEquals("JavaScript: Trying to find entity with type,key,value,key,value!", "InitialString", Scripting.replaceVariables(ctx, testNode, "${{ var t1 = Structr.first(Structr.find_privileged('TestOne','anInt','42','aString','InitialString')); Structr.print(t1.aString); }}"));
        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) StringProperty(org.structr.core.property.StringProperty) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)74 TestOne (org.structr.core.entity.TestOne)74 Tx (org.structr.core.graph.Tx)72 FrameworkException (org.structr.common.error.FrameworkException)70 StructrTest (org.structr.common.StructrTest)20 Result (org.structr.core.Result)15 Principal (org.structr.core.entity.Principal)15 TestSix (org.structr.core.entity.TestSix)15 NodeInterface (org.structr.core.graph.NodeInterface)15 Random (java.util.Random)12 ActionContext (org.structr.schema.action.ActionContext)12 LinkedList (java.util.LinkedList)10 TestFour (org.structr.core.entity.TestFour)9 PropertyMap (org.structr.core.property.PropertyMap)9 PropertyKey (org.structr.core.property.PropertyKey)8 Date (java.util.Date)7 App (org.structr.core.app.App)7 StructrApp (org.structr.core.app.StructrApp)7 NotFoundException (org.structr.api.NotFoundException)6 UnlicensedException (org.structr.common.error.UnlicensedException)6