Search in sources :

Example 26 with TestOne

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

the class SearchAndSortingTest method test03SortByDate.

@Test
public void test03SortByDate() {
    try {
        Class type = TestOne.class;
        int number = 97;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final int offset = 10;
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        try (final Tx tx = app.tx()) {
            int i = offset;
            String name;
            for (NodeInterface node : nodes) {
                name = Integer.toString(i);
                i++;
                node.setProperty(AbstractNode.name, "TestOne-" + name);
                node.setProperty(TestOne.aDate, new Date());
                // slow down execution speed to make sure distinct changes fall in different milliseconds
                try {
                    Thread.sleep(2);
                } catch (Throwable t) {
                }
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = TestOne.aDate;
            boolean sortDesc = false;
            int pageSize = 10;
            int page = 1;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
            assertTrue(result.getRawResultCount() == number);
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), pageSize });
            assertTrue(result.size() == Math.min(number, pageSize));
            for (int j = 0; j < Math.min(result.size(), pageSize); j++) {
                String expectedName = "TestOne-" + (offset + j);
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(expectedName + ", got: " + gotName);
                assertEquals(expectedName, gotName);
            }
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Date(java.util.Date) Result(org.structr.core.Result) Random(java.util.Random) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 27 with TestOne

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

the class SystemTest method testTransactionIsolationWithFailures.

@Test
public void testTransactionIsolationWithFailures() {
    try {
        final TestOne test = createTestNode(TestOne.class);
        final ExecutorService executor = Executors.newCachedThreadPool();
        final List<FailingTestRunner> tests = new LinkedList<>();
        final List<Future> futures = new LinkedList<>();
        // create and run test runners
        for (int i = 0; i < 25; i++) {
            final FailingTestRunner runner = new FailingTestRunner(app, test);
            futures.add(executor.submit(runner));
            tests.add(runner);
        }
        // wait for termination
        for (final Future future : futures) {
            future.get();
            System.out.print(".");
        }
        System.out.println();
        // check for success
        for (final FailingTestRunner runner : tests) {
            assertTrue("Could not validate transaction isolation", runner.success());
        }
        executor.shutdownNow();
    } catch (Throwable fex) {
        fail("Unexpected exception");
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) TestOne(org.structr.core.entity.TestOne) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 28 with TestOne

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

the class SystemTest method testRollbackOnError.

@Test
public void testRollbackOnError() {
    final ActionContext ctx = new ActionContext(securityContext, null);
    /**
     * first the old scripting style
     */
    TestOne testNode = null;
    try (final Tx tx = app.tx()) {
        testNode = createTestNode(TestOne.class);
        testNode.setProperty(TestOne.aString, "InitialString");
        testNode.setProperty(TestOne.anInt, 42);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        Scripting.replaceVariables(ctx, testNode, "${ ( 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("Property value should still have initial value!", "InitialString", testNode.getProperty(TestOne.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) Test(org.junit.Test)

Example 29 with TestOne

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

the class ScriptingTest method testSystemProperties.

@Test
public void testSystemProperties() {
    try {
        final Principal user = createTestNode(Principal.class);
        // create new node
        TestOne t1 = createTestNode(TestOne.class, user);
        final SecurityContext userContext = SecurityContext.getInstance(user, AccessMode.Frontend);
        final App userApp = StructrApp.getInstance(userContext);
        try (final Tx tx = userApp.tx()) {
            final ActionContext userActionContext = new ActionContext(userContext, null);
            assertEquals("node should be of type TestOne", "TestOne", Scripting.replaceVariables(userActionContext, t1, "${(get(this, 'type'))}"));
            try {
                assertEquals("setting the type should fail", "TestTwo", Scripting.replaceVariables(userActionContext, t1, "${(set(this, 'type', 'TestThree'), get(this, 'type'))}"));
                fail("setting a system property should fail");
            } catch (FrameworkException fx) {
            }
            assertEquals("setting the type should work after setting it with unlock_system_properties_once", "TestFour", Scripting.replaceVariables(userActionContext, t1, "${(unlock_system_properties_once(this), set(this, 'type', 'TestFour'), get(this, 'type'))}"));
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 30 with TestOne

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

the class MaintenanceTest method testBulkSetNodePropertiesCommand.

@Test
public void testBulkSetNodePropertiesCommand() {
    final Integer one = 1;
    try {
        // create test nodes first
        createTestNodes(TestOne.class, 100);
        try {
            // test failure with wrong type
            app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "NonExistingType"));
            fail("Using BulkSetNodePropertiesCommand with a non-existing type should throw an exception.");
        } catch (FrameworkException fex) {
            // status: 422
            assertEquals(422, fex.getStatus());
            assertEquals("Invalid type NonExistingType", fex.getMessage());
        }
        try {
            // test failure without type
            app.command(BulkSetNodePropertiesCommand.class).execute(toMap("anInt", 1));
            fail("Using BulkSetNodePropertiesCommand without a type property should throw an exception.");
        } catch (FrameworkException fex) {
            // status: 422
            assertEquals(422, fex.getStatus());
            assertEquals("Type must not be empty", fex.getMessage());
        }
        // test success
        app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "TestOne", "anInt", 1, "aString", "one"));
        try (final Tx tx = app.tx()) {
            // check nodes, we should find 100 TestOnes here, and none TestTwos
            assertEquals(0, app.nodeQuery(TestTwo.class).getResult().size());
            assertEquals(100, app.nodeQuery(TestOne.class).getResult().size());
            // check nodes
            for (final TestOne test : app.nodeQuery(TestOne.class)) {
                assertEquals(one, test.getProperty(TestOne.anInt));
                assertEquals("one", test.getProperty(TestOne.aString));
            }
        }
        // advanced: modify type
        app.command(BulkSetNodePropertiesCommand.class).execute(toMap("type", "TestOne", "newType", "TestTwo"));
        try (final Tx tx = app.tx()) {
            // check nodes, we should find 100 TestTwos here, and none TestOnes
            assertEquals(0, app.nodeQuery(TestOne.class).getResult().size());
            assertEquals(100, app.nodeQuery(TestTwo.class).getResult().size());
        }
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : BulkSetNodePropertiesCommand(org.structr.core.graph.BulkSetNodePropertiesCommand) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) TestTwo(org.structr.core.entity.TestTwo) TestOne(org.structr.core.entity.TestOne) 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