Search in sources :

Example 61 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class StructrUiTest method createTestNode.

protected <T extends NodeInterface> T createTestNode(final Class<T> type, final NodeAttribute... attrs) throws FrameworkException {
    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, type.getSimpleName());
    for (final NodeAttribute attr : attrs) {
        props.put(attr.getKey(), attr.getValue());
    }
    return app.create(type, props);
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap)

Example 62 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class ScriptingTest method testSetPropertyWithDynamicNodes.

@Test
public void testSetPropertyWithDynamicNodes() {
    /**
     * This test creates two connected SchemaNodes and tests the script-based
     * association of one instance with several others in the onCreate method.
     */
    final long currentTimeMillis = System.currentTimeMillis();
    Class sourceType = null;
    Class targetType = null;
    PropertyKey targetsProperty = null;
    EnumProperty testEnumProperty = null;
    PropertyKey testBooleanProperty = null;
    PropertyKey testIntegerProperty = null;
    PropertyKey testStringProperty = null;
    PropertyKey testDoubleProperty = null;
    PropertyKey testDateProperty = null;
    Class testEnumType = null;
    // setup phase: create schema nodes
    try (final Tx tx = app.tx()) {
        // create two nodes and associate them with each other
        final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
        final SchemaNode targetNode = createTestNode(SchemaNode.class, "Target");
        final List<SchemaProperty> properties = new LinkedList<>();
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testBoolean"), new NodeAttribute(SchemaProperty.propertyType, "Boolean")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testInteger"), new NodeAttribute(SchemaProperty.propertyType, "Integer")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testString"), new NodeAttribute(SchemaProperty.propertyType, "String")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDouble"), new NodeAttribute(SchemaProperty.propertyType, "Double")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testEnum"), new NodeAttribute(SchemaProperty.propertyType, "Enum"), new NodeAttribute(SchemaProperty.format, "OPEN, CLOSED, TEST")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDate"), new NodeAttribute(SchemaProperty.propertyType, "Date")));
        sourceNode.setProperty(SchemaNode.schemaProperties, properties);
        final List<SchemaMethod> methods = new LinkedList<>();
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "onCreate"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.targets = Structr.find('Target'); }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'OPEN'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest02"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'CLOSED'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest03"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'TEST'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest04"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'INVALID'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest05"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testBoolean = true; e.testInteger = 123; e.testString = 'testing..'; e.testDouble = 1.2345; e.testDate = new Date(" + currentTimeMillis + "); }")));
        sourceNode.setProperty(SchemaNode.schemaMethods, methods);
        final PropertyMap propertyMap = new PropertyMap();
        propertyMap.put(SchemaRelationshipNode.sourceId, sourceNode.getUuid());
        propertyMap.put(SchemaRelationshipNode.targetId, targetNode.getUuid());
        propertyMap.put(SchemaRelationshipNode.sourceJsonName, "source");
        propertyMap.put(SchemaRelationshipNode.targetJsonName, "targets");
        propertyMap.put(SchemaRelationshipNode.sourceMultiplicity, "*");
        propertyMap.put(SchemaRelationshipNode.targetMultiplicity, "*");
        propertyMap.put(SchemaRelationshipNode.relationshipType, "HAS");
        app.create(SchemaRelationshipNode.class, propertyMap);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        sourceType = config.getNodeEntityClass("Source");
        targetType = config.getNodeEntityClass("Target");
        targetsProperty = StructrApp.key(sourceType, "targets");
        // we need to cast to EnumProperty in order to obtain the dynamic enum type
        testEnumProperty = (EnumProperty) StructrApp.key(sourceType, "testEnum");
        testEnumType = testEnumProperty.getEnumType();
        testBooleanProperty = StructrApp.key(sourceType, "testBoolean");
        testIntegerProperty = StructrApp.key(sourceType, "testInteger");
        testStringProperty = StructrApp.key(sourceType, "testString");
        testDoubleProperty = StructrApp.key(sourceType, "testDouble");
        testDateProperty = StructrApp.key(sourceType, "testDate");
        assertNotNull(sourceType);
        assertNotNull(targetType);
        assertNotNull(targetsProperty);
        // create 5 target nodes
        createTestNodes(targetType, 5);
        // create source node
        createTestNodes(sourceType, 5);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // check phase: source node should have all five target nodes associated with HAS
    try (final Tx tx = app.tx()) {
        // check all source nodes
        for (final Object obj : app.nodeQuery(sourceType).getAsList()) {
            assertNotNull("Invalid nodeQuery result", obj);
            final GraphObject sourceNode = (GraphObject) obj;
            // test contents of "targets" property
            final Object targetNodesObject = sourceNode.getProperty(targetsProperty);
            assertTrue("Invalid getProperty result for scripted association", targetNodesObject instanceof List);
            final List list = (List) targetNodesObject;
            assertEquals("Invalid getProperty result for scripted association", 5, list.size());
        }
        final GraphObject sourceNode = app.nodeQuery(sourceType).getFirst();
        // set testEnum property to OPEN via doTest01 function call, check result
        sourceNode.invokeMethod("doTest01", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[0], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to CLOSED via doTest02 function call, check result
        sourceNode.invokeMethod("doTest02", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[1], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to TEST via doTest03 function call, check result
        sourceNode.invokeMethod("doTest03", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to INVALID via doTest03 function call, expect previous value & error
        try {
            sourceNode.invokeMethod("doTest04", Collections.EMPTY_MAP, true);
            assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
            fail("Setting EnumProperty to invalid value should result in an Exception!");
        } catch (FrameworkException fx) {
        }
        // test other property types
        sourceNode.invokeMethod("doTest05", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for BooleanProperty", true, sourceNode.getProperty(testBooleanProperty));
        assertEquals("Invalid setProperty result for IntegerProperty", 123, sourceNode.getProperty(testIntegerProperty));
        assertEquals("Invalid setProperty result for StringProperty", "testing..", sourceNode.getProperty(testStringProperty));
        assertEquals("Invalid setProperty result for DoubleProperty", 1.2345, sourceNode.getProperty(testDoubleProperty));
        assertEquals("Invalid setProperty result for DateProperty", new Date(currentTimeMillis), sourceNode.getProperty(testDateProperty));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Date(java.util.Date) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) EnumProperty(org.structr.core.property.EnumProperty) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 63 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SchemaTest method test00DeleteSchemaRelationshipInView.

@Test
public void test00DeleteSchemaRelationshipInView() {
    SchemaRelationshipNode rel = null;
    try (final Tx tx = app.tx()) {
        // create source and target node
        final SchemaNode fooNode = app.create(SchemaNode.class, "Foo");
        final SchemaNode barNode = app.create(SchemaNode.class, "Bar");
        // create relationship
        rel = app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, fooNode), new NodeAttribute<>(SchemaRelationshipNode.targetNode, barNode), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "narf"));
        // create "public" view that contains the related property
        app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "public"), new NodeAttribute<>(SchemaView.schemaNode, fooNode), new NodeAttribute<>(SchemaView.nonGraphProperties, "type, id, narfBars"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        app.delete(rel);
        tx.success();
    } catch (Throwable t) {
        // deletion of relationship should not fail
        logger.warn("", t);
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 64 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SchemaTest method test04ManualSchemaRelatedPropertyNameCreation.

@Test
public void test04ManualSchemaRelatedPropertyNameCreation() {
    try {
        try (final Tx tx = app.tx()) {
            final SchemaNode source = app.create(SchemaNode.class, "Source");
            final SchemaNode target = app.create(SchemaNode.class, "Target");
            app.create(SchemaRelationshipNode.class, new NodeAttribute(SchemaRelationshipNode.relationshipType, "link"), new NodeAttribute(SchemaRelationshipNode.sourceNode, source), new NodeAttribute(SchemaRelationshipNode.targetNode, target), new NodeAttribute(SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute(SchemaRelationshipNode.targetMultiplicity, "*"));
            tx.success();
        }
        checkSchemaString(StructrSchema.createFromDatabase(app).toString());
    } catch (FrameworkException | URISyntaxException t) {
        logger.warn("", t);
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) URISyntaxException(java.net.URISyntaxException) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 65 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class ValidationTest method testConcurrentValidationWithInheritance.

@Test
public void testConcurrentValidationWithInheritance() {
    final int count = 100;
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_name"), "+String!"));
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "ItemDerived"), new NodeAttribute(SchemaNode.extendsClass, "org.structr.dynamic.Item"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final Class baseType = StructrApp.getConfiguration().getNodeEntityClass("Item");
    final Class derivedType = StructrApp.getConfiguration().getNodeEntityClass("ItemDerived");
    assertNotNull(baseType);
    assertNotNull(derivedType);
    final PropertyKey name = StructrApp.key(baseType, "name");
    assertNotNull(name);
    final Runnable tester = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                // testing must be done in an isolated transaction
                try (final Tx tx = app.tx()) {
                    if (Math.random() < 0.5) {
                        app.create(derivedType, "Item" + i);
                    } else {
                        app.create(baseType, "Item" + i);
                    }
                    tx.success();
                } catch (FrameworkException ignore) {
                }
            }
        }
    };
    // submit three test instances
    final ExecutorService executor = Executors.newCachedThreadPool();
    final Future f1 = executor.submit(tester);
    final Future f2 = executor.submit(tester);
    final Future f3 = executor.submit(tester);
    try {
        f1.get();
        f2.get();
        f3.get();
    } catch (Throwable ex) {
    }
    List<GraphObject> result = null;
    try (final Tx tx = app.tx()) {
        result = app.nodeQuery(baseType).getAsList();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // verify that only count entities have been created.
    assertEquals("Invalid concurrent validation result", count, result.size());
    executor.shutdownNow();
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7