Search in sources :

Example 1 with BulkSetNodePropertiesCommand

use of org.structr.core.graph.BulkSetNodePropertiesCommand 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)1 StructrTest (org.structr.common.StructrTest)1 FrameworkException (org.structr.common.error.FrameworkException)1 TestOne (org.structr.core.entity.TestOne)1 TestTwo (org.structr.core.entity.TestTwo)1 BulkSetNodePropertiesCommand (org.structr.core.graph.BulkSetNodePropertiesCommand)1 Tx (org.structr.core.graph.Tx)1