Search in sources :

Example 46 with StringProperty

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

the class ValidationTest method testInheritedStringPropertyUniqueness.

@Test
public void testInheritedStringPropertyUniqueness() {
    try (final Tx tx = app.tx()) {
        final SchemaNode testType = app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestDerived"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.Test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final Class testType = StructrApp.getConfiguration().getNodeEntityClass("TestDerived");
    if (testType != null) {
        final PropertyKey key = StructrApp.key(testType, "testUnique");
        if (key != null) {
            Settings.CypherDebugLogging.setValue(true);
            try (final Tx tx = app.tx()) {
                // key must be unique, but can empty
                app.create(testType, new NodeAttribute<>(key, "unique"));
                app.create(testType, new NodeAttribute<>(key, ""));
                tx.success();
            } catch (FrameworkException fex) {
                fex.printStackTrace();
                fail("Unexpected exception.");
            }
            Settings.CypherDebugLogging.setValue(true);
            for (int i = 0; i < 5; i++) {
                try (final Tx tx = app.tx()) {
                    app.create(testType, new NodeAttribute<>(key, "unique"));
                    tx.success();
                    fail("Uniqueness constraint violated!");
                } catch (FrameworkException fex) {
                    final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
                    final ErrorToken token = tokens.get(0);
                    assertEquals("Invalid uniqueness validation result", 1, tokens.size());
                    assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
                    assertEquals("Invalid uniqueness validation result", "testUnique", token.getProperty());
                    assertEquals("Invalid uniqueness validation result", "TestDerived", token.getType());
                    assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
                }
            }
        }
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 47 with StringProperty

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

the class ValidationTest method testStringPropertyUniquenessAgain.

@Test
public void testStringPropertyUniquenessAgain() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final Class testType = StructrApp.getConfiguration().getNodeEntityClass("Test");
    if (testType != null) {
        final PropertyKey key = StructrApp.key(testType, "testUnique");
        if (key != null) {
            final Random random = new Random();
            try (final Tx tx = app.tx()) {
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique06"), new NodeAttribute<>(key, "unique00"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique05"), new NodeAttribute<>(key, "unique01"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique04"), new NodeAttribute<>(key, "unique02"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique03"), new NodeAttribute<>(key, "unique03"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique02"), new NodeAttribute<>(key, "unique04"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique01"), new NodeAttribute<>(key, "unique05"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique00"), new NodeAttribute<>(key, "unique06"));
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                fail("Unexpected exception.");
            }
            for (int i = 0; i < 5; i++) {
                try (final Tx tx = app.tx()) {
                    app.create(testType, new NodeAttribute<>(key, "unique0" + random.nextInt(7)));
                    tx.success();
                    fail("Uniqueness constraint violated!");
                } catch (FrameworkException fex) {
                    final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
                    final ErrorToken token = tokens.get(0);
                    assertEquals("Invalid uniqueness validation result", 1, tokens.size());
                    assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
                    assertEquals("Invalid uniqueness validation result", "testUnique", token.getProperty());
                    assertEquals("Invalid uniqueness validation result", "Test", token.getType());
                    assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
                }
            }
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Random(java.util.Random) StringProperty(org.structr.core.property.StringProperty) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 48 with StringProperty

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

Example 49 with StringProperty

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

the class SchemaHelper method getPropertyInfo.

public static Map<String, Object> getPropertyInfo(final SecurityContext securityContext, final PropertyKey property) {
    final Map<String, Object> map = new LinkedHashMap();
    map.put("dbName", property.dbName());
    map.put("jsonName", property.jsonName());
    map.put("className", property.getClass().getName());
    final Class declaringClass = property.getDeclaringClass();
    if (declaringClass != null) {
        map.put("declaringClass", declaringClass.getSimpleName());
    }
    map.put("defaultValue", property.defaultValue());
    if (property instanceof StringProperty) {
        map.put("contentType", ((StringProperty) property).contentType());
    }
    map.put("format", property.format());
    map.put("readOnly", property.isReadOnly());
    map.put("system", property.isSystemInternal());
    map.put("indexed", property.isIndexed());
    map.put("indexedWhenEmpty", property.isIndexedWhenEmpty());
    map.put("compound", property.isCompound());
    map.put("unique", property.isUnique());
    map.put("notNull", property.isNotNull());
    map.put("dynamic", property.isDynamic());
    map.put("hint", property.hint());
    map.put("category", property.category());
    final Class<? extends GraphObject> relatedType = property.relatedType();
    if (relatedType != null) {
        map.put("relatedType", relatedType.getName());
        map.put("type", relatedType.getSimpleName());
        map.put("uiType", relatedType.getSimpleName() + (property.isCollection() ? "[]" : ""));
    } else {
        map.put("type", property.typeName());
        map.put("uiType", property.typeName() + (property.isCollection() ? "[]" : ""));
    }
    map.put("isCollection", property.isCollection());
    final PropertyConverter databaseConverter = property.databaseConverter(securityContext, null);
    final PropertyConverter inputConverter = property.inputConverter(securityContext);
    if (databaseConverter != null) {
        map.put("databaseConverter", databaseConverter.getClass().getName());
    }
    if (inputConverter != null) {
        map.put("inputConverter", inputConverter.getClass().getName());
    }
    // if (declaringClass != null && ("org.structr.dynamic".equals(declaringClass.getPackage().getName()))) {
    if (declaringClass != null && property instanceof RelationProperty) {
        Relation relation = ((RelationProperty) property).getRelation();
        if (relation != null) {
            map.put("relationshipType", relation.name());
        }
    }
    return map;
}
Also used : Relation(org.structr.core.entity.Relation) RelationProperty(org.structr.core.property.RelationProperty) PropertyConverter(org.structr.core.converter.PropertyConverter) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with StringProperty

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

the class SchemaHelper method getSchemaTypeInfo.

// ----- public static methods -----
public static List<GraphObjectMap> getSchemaTypeInfo(final SecurityContext securityContext, final String rawType, final Class type, final String propertyView) throws FrameworkException {
    List<GraphObjectMap> resultList = new LinkedList<>();
    if (type != null) {
        if (propertyView != null) {
            for (final Map.Entry<String, Object> entry : getPropertiesForView(securityContext, type, propertyView).entrySet()) {
                final GraphObjectMap property = new GraphObjectMap();
                for (final Map.Entry<String, Object> prop : ((Map<String, Object>) entry.getValue()).entrySet()) {
                    property.setProperty(new GenericProperty(prop.getKey()), prop.getValue());
                }
                resultList.add(property);
            }
        } else {
            final GraphObjectMap schema = new GraphObjectMap();
            resultList.add(schema);
            String url = "/".concat(rawType);
            schema.setProperty(new StringProperty("url"), url);
            schema.setProperty(new StringProperty("type"), type.getSimpleName());
            schema.setProperty(new StringProperty("className"), type.getName());
            schema.setProperty(new StringProperty("extendsClass"), type.getSuperclass().getName());
            schema.setProperty(new BooleanProperty("isRel"), AbstractRelationship.class.isAssignableFrom(type));
            schema.setProperty(new LongProperty("flags"), SecurityContext.getResourceFlags(rawType));
            Set<String> propertyViews = new LinkedHashSet<>(StructrApp.getConfiguration().getPropertyViewsForType(type));
            // list property sets for all views
            Map<String, Map<String, Object>> views = new TreeMap();
            schema.setProperty(new GenericProperty("views"), views);
            for (final String view : propertyViews) {
                if (!View.INTERNAL_GRAPH_VIEW.equals(view)) {
                    views.put(view, getPropertiesForView(securityContext, type, view));
                }
            }
        }
    }
    return resultList;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BooleanProperty(org.structr.core.property.BooleanProperty) AbstractRelationship(org.structr.core.entity.AbstractRelationship) StringProperty(org.structr.core.property.StringProperty) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) GraphObjectMap(org.structr.core.GraphObjectMap) LongProperty(org.structr.core.property.LongProperty) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Aggregations

StringProperty (org.structr.core.property.StringProperty)53 FrameworkException (org.structr.common.error.FrameworkException)28 Tx (org.structr.core.graph.Tx)28 Test (org.junit.Test)26 PropertyKey (org.structr.core.property.PropertyKey)22 GraphObject (org.structr.core.GraphObject)14 GraphObjectMap (org.structr.core.GraphObjectMap)14 StructrTest (org.structr.common.StructrTest)11 LinkedList (java.util.LinkedList)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SchemaNode (org.structr.core.entity.SchemaNode)9 List (java.util.List)8 AbstractNode (org.structr.core.entity.AbstractNode)7 NodeInterface (org.structr.core.graph.NodeInterface)7 StructrUiTest (org.structr.web.StructrUiTest)7 Map (java.util.Map)6 ErrorToken (org.structr.common.error.ErrorToken)6 IntProperty (org.structr.core.property.IntProperty)6 LinkedHashSet (java.util.LinkedHashSet)5 App (org.structr.core.app.App)5