Search in sources :

Example 1 with EnumProperty

use of org.structr.core.property.EnumProperty in project structr by structr.

the class ValidationTest method testEnumPropertyUniquenessValidation.

// ----- enum property validation tests -----
@Test
public void testEnumPropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Enum(one, two, three)!");
    final PropertyKey key = StructrApp.key(testType, keyName);
    String uuid = null;
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            final Object value = ((EnumProperty) key).getEnumType().getEnumConstants()[0];
            uuid = app.create(testType, new NodeAttribute<>(key, value)).getUuid();
            app.create(testType, new NodeAttribute<>(key, value));
            tx.success();
            fail("Enum property uniqueness constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid enum validation result", 1, tokens.size());
            assertEquals("Invalid enum validation result", 422, fex.getStatus());
            assertEquals("Invalid enum validation result", keyName, token.getProperty());
            assertEquals("Invalid enum validation result", "Test", token.getType());
            assertEquals("Invalid enum validation result", "already_taken", token.getToken());
            assertEquals("Invalid enum validation result", uuid, token.getDetail());
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) EnumProperty(org.structr.core.property.EnumProperty) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 2 with EnumProperty

use of org.structr.core.property.EnumProperty in project structr by structr.

the class EnumInfoFunction method apply.

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
    try {
        if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
            final ConfigurationProvider config = StructrApp.getConfiguration();
            final String typeName = sources[0].toString();
            final String enumPropertyName = sources[1].toString();
            final boolean rawList = (sources.length == 3) ? Boolean.parseBoolean(sources[2].toString()) : false;
            final Class type = SchemaHelper.getEntityClassForRawType(typeName);
            if (type != null) {
                final PropertyKey key = StructrApp.key(type, enumPropertyName);
                if (key != null) {
                    if (key instanceof EnumProperty) {
                        final String formatString = SchemaHelper.getPropertyInfo(ctx.getSecurityContext(), key).get("format").toString();
                        final List<String> valueList = Arrays.asList(formatString.replace(" ", "").split(","));
                        if (rawList) {
                            return valueList;
                        } else {
                            final ArrayList<GraphObjectMap> resultList = new ArrayList();
                            for (final String value : valueList) {
                                final GraphObjectMap valueMap = new GraphObjectMap();
                                resultList.add(valueMap);
                                valueMap.put(new StringProperty("value"), value);
                            }
                            return resultList;
                        }
                    } else {
                        logger.warn("Error: Not an Enum property \"{}.{}\"", typeName, enumPropertyName);
                        return "Not an Enum property " + typeName + "." + enumPropertyName;
                    }
                } else {
                    logger.warn("Error: Unknown property \"{}.{}\"", typeName, enumPropertyName);
                    return "Unknown property " + typeName + "." + enumPropertyName;
                }
            } else {
                logger.warn("Error: Unknown type \"{}\"", typeName);
                return "Unknown type " + typeName;
            }
        } else {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : EnumProperty(org.structr.core.property.EnumProperty) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObjectMap(org.structr.core.GraphObjectMap) ArrayList(java.util.ArrayList) StringProperty(org.structr.core.property.StringProperty) PropertyKey(org.structr.core.property.PropertyKey)

Example 3 with EnumProperty

use of org.structr.core.property.EnumProperty 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)

Aggregations

EnumProperty (org.structr.core.property.EnumProperty)3 PropertyKey (org.structr.core.property.PropertyKey)3 List (java.util.List)2 Test (org.junit.Test)2 StructrTest (org.structr.common.StructrTest)2 FrameworkException (org.structr.common.error.FrameworkException)2 GraphObject (org.structr.core.GraphObject)2 Tx (org.structr.core.graph.Tx)2 ConfigurationProvider (org.structr.schema.ConfigurationProvider)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ErrorToken (org.structr.common.error.ErrorToken)1 GraphObjectMap (org.structr.core.GraphObjectMap)1 SchemaMethod (org.structr.core.entity.SchemaMethod)1 SchemaNode (org.structr.core.entity.SchemaNode)1 SchemaProperty (org.structr.core.entity.SchemaProperty)1 NodeAttribute (org.structr.core.graph.NodeAttribute)1 NodeInterface (org.structr.core.graph.NodeInterface)1 PropertyMap (org.structr.core.property.PropertyMap)1