Search in sources :

Example 1 with Transaction

use of org.structr.api.Transaction in project structr by structr.

the class MaintenanceTest method testBulkCreateLabelsCommand.

@Test
public void testBulkCreateLabelsCommand() {
    try {
        final DatabaseService graphDb = app.getDatabaseService();
        final Set<Label> expectedLabels = new LinkedHashSet<>();
        expectedLabels.add(graphDb.forName(Label.class, "Principal"));
        expectedLabels.add(graphDb.forName(Label.class, "Group"));
        expectedLabels.add(graphDb.forName(Label.class, "AccessControllable"));
        expectedLabels.add(graphDb.forName(Label.class, "AbstractNode"));
        expectedLabels.add(graphDb.forName(Label.class, "NodeInterface"));
        expectedLabels.add(graphDb.forName(Label.class, "CMISInfo"));
        expectedLabels.add(graphDb.forName(Label.class, "CMISItemInfo"));
        if (graphDb.getTenantIdentifier() != null) {
            expectedLabels.add(graphDb.forName(Label.class, graphDb.getTenantIdentifier()));
        }
        // intentionally create raw Neo4j transaction and create nodes in there
        try (Transaction tx = graphDb.beginTx()) {
            for (int i = 0; i < 100; i++) {
                final Node test = graphDb.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
                // set ID and type so that the rebuild index command identifies it as a Structr node.
                test.setProperty("type", "Group");
                test.setProperty("id", UUID.randomUUID().toString().replace("-", ""));
            }
            // this is important.... :)
            tx.success();
        }
        /*
			 * This test will fail with the new Neo4j 3.0 Bolt interface, because
			 * there is no separation between a (Lucene-based) index and the
			 * database values any more. Nodes are selected by their 'type'
			 * property and will always be found even if NOT created using Structr
			 * methods.
			// nodes should not be found yet..
			try (final Tx tx = app.tx()) {

				// check nodes, we should find no Groups here
				assertEquals(0, app.nodeQuery(Group.class).getResult().size());
			}
			 */
        // test rebuild index and create labels
        // app.command(BulkRebuildIndexCommand.class).execute(new LinkedHashMap<>());
        app.command(BulkCreateLabelsCommand.class).execute(new LinkedHashMap<>());
        // nodes should now be visible to Structr
        try (final Tx tx = app.tx()) {
            // check nodes, we should find 100 Groups here
            assertEquals(100, app.nodeQuery(Group.class).getResult().size());
            // check nodes
            for (final Group group : app.nodeQuery(Group.class)) {
                final Set<Label> labels = Iterables.toSet(group.getNode().getLabels());
                assertEquals("Invalid number of labels", expectedLabels.size(), labels.size());
                assertTrue("Invalid labels found", labels.containsAll(expectedLabels));
            }
            tx.success();
        }
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Group(org.structr.core.entity.Group) Transaction(org.structr.api.Transaction) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) BulkCreateLabelsCommand(org.structr.core.graph.BulkCreateLabelsCommand) Node(org.structr.api.graph.Node) Label(org.structr.api.graph.Label) DatabaseService(org.structr.api.DatabaseService) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 2 with Transaction

use of org.structr.api.Transaction in project structr by structr.

the class NodeWrapperTest method testBasicTransactions.

@Test
public void testBasicTransactions() {
    try {
        Settings.DatabasePath.setValue(Files.createTempDirectory("structr-test").toFile().getAbsolutePath());
        Settings.ConnectionUrl.setValue(Settings.TestingConnectionUrl.getValue());
    } catch (IOException ioex) {
        logger.warn("", ioex);
    }
    final BoltDatabaseService s = new BoltDatabaseService();
    s.initialize();
    long id = 0L;
    // create new node
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
        id = node.getId();
        tx.success();
    }
    // set property
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.getNodeById(id);
        Assert.assertNotNull(node);
        node.setProperty("name", "Test");
        tx.success();
    }
    // set property and don't commit transaction
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.getNodeById(id);
        Assert.assertNotNull(node);
        node.setProperty("name", "Fail");
    }
    // check property value
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.getNodeById(id);
        Assert.assertNotNull(node);
        Assert.assertEquals("Invalid setProperty result", "Test", node.getProperty("name"));
        tx.success();
    }
    // remove property
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.getNodeById(id);
        Assert.assertNotNull(node);
        node.removeProperty("name");
        tx.success();
    }
    // check property value
    try (final Transaction tx = s.beginTx()) {
        final Node node = s.getNodeById(id);
        Assert.assertNotNull(node);
        Assert.assertNull("Invalid removeProperty result", node.getProperty("name"));
        node.setProperty("key1", "value1");
        node.setProperty("key2", 2);
        final Iterable<String> keys = node.getPropertyKeys();
        final List<String> list = Iterables.toList(keys);
        Assert.assertEquals("Invalid getPropertyKeys result", 2, list.size());
        Assert.assertEquals("Invalid getPropertyKeys result", "key1", list.get(0));
        Assert.assertEquals("Invalid getPropertyKeys result", "key2", list.get(1));
        tx.success();
    }
    s.shutdown();
}
Also used : Transaction(org.structr.api.Transaction) Node(org.structr.api.graph.Node) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with Transaction

use of org.structr.api.Transaction in project structr by structr.

the class NodeService method initialize.

@Override
public boolean initialize(final StructrServices services) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    final String databaseDriver = Settings.DatabaseDriver.getValue();
    graphDb = (DatabaseService) Class.forName(databaseDriver).newInstance();
    if (graphDb != null) {
        if (graphDb.initialize()) {
            filesPath = Settings.FilesPath.getValue();
            // check existence of files path
            File files = new File(filesPath);
            if (!files.exists()) {
                files.mkdir();
            }
            logger.info("Database driver loaded, initializing indexes..");
            // index creation transaction
            try (final Transaction tx = graphDb.beginTx()) {
                nodeIndex = graphDb.nodeIndex();
                relIndex = graphDb.relationshipIndex();
                tx.success();
                // if the above operations fail, the database is probably not available
                isInitialized = true;
                logger.info("Indexes successfully initialized.");
            } catch (Throwable t) {
                logger.warn("Error while initializing indexes: {}", t.getMessage());
            }
        }
    }
    return isInitialized;
}
Also used : Transaction(org.structr.api.Transaction) File(java.io.File)

Example 4 with Transaction

use of org.structr.api.Transaction in project structr by structr.

the class NodeWrapperTest method testDeleteException.

@Test
public void testDeleteException() {
    try {
        Settings.DatabasePath.setValue(Files.createTempDirectory("structr-test").toFile().getAbsolutePath());
        Settings.ConnectionUrl.setValue(Settings.TestingConnectionUrl.getValue());
    } catch (IOException ioex) {
        logger.warn("", ioex);
    }
    final BoltDatabaseService s = new BoltDatabaseService();
    s.initialize();
    // create new node
    try (final Transaction tx = s.beginTx()) {
        final Node node1 = s.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
        final Node node2 = s.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
        final Relationship rel = node1.createRelationshipTo(node2, s.forName(RelationshipType.class, "TEST"));
        rel.delete();
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    s.shutdown();
}
Also used : Transaction(org.structr.api.Transaction) Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) RelationshipType(org.structr.api.graph.RelationshipType) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Transaction (org.structr.api.Transaction)4 Test (org.junit.Test)3 Node (org.structr.api.graph.Node)3 IOException (java.io.IOException)2 File (java.io.File)1 LinkedHashSet (java.util.LinkedHashSet)1 DatabaseService (org.structr.api.DatabaseService)1 Label (org.structr.api.graph.Label)1 Relationship (org.structr.api.graph.Relationship)1 RelationshipType (org.structr.api.graph.RelationshipType)1 StructrTest (org.structr.common.StructrTest)1 FrameworkException (org.structr.common.error.FrameworkException)1 Group (org.structr.core.entity.Group)1 BulkCreateLabelsCommand (org.structr.core.graph.BulkCreateLabelsCommand)1 Tx (org.structr.core.graph.Tx)1