Search in sources :

Example 61 with NodeInterface

use of org.structr.core.graph.NodeInterface 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 62 with NodeInterface

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

the class ValidationTest method testBooleanPropertyNotNullValidation.

// ----- boolean property validation tests -----
@Test
public void testBooleanPropertyNotNullValidation() {
    final String keyName = "notNull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Boolean");
    final PropertyKey key = StructrApp.key(testType, keyName);
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            /* Boolean properties are special. A boolean property will
				 * _always_ have a value, i.e. "null" equals "false" and
				 * will be returned as false.
				 *
				 * => a boolean property value can never be null!
				 */
            app.create(testType, new NodeAttribute<>(key, null));
            app.create(testType, new NodeAttribute<>(key, true));
            app.create(testType, new NodeAttribute<>(key, false));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected array property validation exception.");
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 63 with NodeInterface

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

the class StructrGraphQLTest method cleanDatabase.

@Before
public void cleanDatabase() {
    // configure RestAssured
    RestAssured.basePath = restUrl;
    RestAssured.baseURI = "http://" + host + ":" + httpPort;
    RestAssured.port = httpPort;
    try (final Tx tx = app.tx()) {
        for (final NodeInterface node : app.nodeQuery().getAsList()) {
            // System.out.println("Deleting node " + node.getType() + " with name " + node.getName() + " and ID " + node.getUuid());
            app.delete(node);
        }
        // delete remaining nodes without UUIDs etc.
        app.cypher("MATCH (n)-[r]-(m) DELETE n, r, m", Collections.emptyMap());
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Exception while trying to clean database: {}", fex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Before(org.junit.Before)

Example 64 with NodeInterface

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

the class TypeResource method identifyStartNode.

private NodeInterface identifyStartNode(final Relation template, final Map<String, Object> properties) throws FrameworkException {
    final Property<String> sourceIdProperty = template.getSourceIdProperty();
    final Class sourceType = template.getSourceType();
    final Notion notion = template.getStartNodeNotion();
    notion.setType(sourceType);
    PropertyKey startNodeIdentifier = notion.getPrimaryPropertyKey();
    if (startNodeIdentifier != null) {
        Object identifierValue = properties.get(startNodeIdentifier.dbName());
        properties.remove(sourceIdProperty.dbName());
        return (NodeInterface) notion.getAdapterForSetter(securityContext).adapt(identifierValue);
    }
    return null;
}
Also used : Notion(org.structr.core.notion.Notion) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 65 with NodeInterface

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

the class AbstractNode method grant.

@Override
public final void grant(Permission permission, Principal principal) throws FrameworkException {
    if (!isGranted(Permission.accessControl, securityContext)) {
        throw new FrameworkException(403, "Access control not permitted");
    }
    Security secRel = getSecurityRelationship(principal);
    if (secRel == null) {
        try {
            // ensureCardinality is not neccessary here
            final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
            securityContext.disableEnsureCardinality();
            secRel = StructrApp.getInstance(securityContext).create(principal, (NodeInterface) this, Security.class);
        } catch (FrameworkException ex) {
            logger.error("Could not create security relationship!", ex);
        }
    }
    // only access rel if it existss or was created successfully
    if (secRel != null) {
        secRel.addPermission(permission);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12