Search in sources :

Example 56 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method testNodeCreationWithForcedUuid.

@Test
public void testNodeCreationWithForcedUuid() {
    final String uuid = NodeServiceCommand.getNextUuid();
    TestOne test = null;
    // create object with user context
    try (final Tx tx = app.tx()) {
        test = app.create(TestOne.class, new NodeAttribute<>(AbstractNode.name, "test"), new NodeAttribute<>(GraphObject.id, uuid));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final String uuid1 = test.getProperty(GraphObject.id);
        final String uuid2 = test.getUuid();
        assertEquals("Object creation does not accept provided UUID", uuid, uuid1);
        assertEquals("UUID mismatch in getProperty() and getUuid()", uuid1, uuid2);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Example 57 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method testNodeCacheInvalidationWithLongLivedReferences.

@Test
public void testNodeCacheInvalidationWithLongLivedReferences() {
    TestOne longLivedReference = null;
    try (final Tx tx = app.tx()) {
        longLivedReference = createTestNode(TestOne.class, "test1");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // fill cache with other nodes
    try {
        createTestNodes(TestSix.class, 1000);
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final TestOne freshReference = app.nodeQuery(TestOne.class).getFirst();
        freshReference.setProperty(AbstractNode.name, "test2");
        assertEquals("Cache invalidation failure!", "test2", longLivedReference.getProperty(AbstractNode.name));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Example 58 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method test01CompareDescending.

@Test
public void test01CompareDescending() {
    try {
        TestOne a = createTestNode(TestOne.class);
        TestOne b = createTestNode(TestOne.class);
        GraphObjectComparator comp = new GraphObjectComparator(TestOne.anInt, GraphObjectComparator.DESCENDING);
        try {
            comp.compare(null, null);
            fail("Should have raised an NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            comp.compare(a, null);
            fail("Should have raised an NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            comp.compare(null, b);
            fail("Should have raised an NullPointerException");
        } catch (NullPointerException e) {
        }
        try {
            comp.compare(null, b);
            fail("Should have raised an NullPointerException");
        } catch (NullPointerException e) {
        }
        try (final Tx tx = app.tx()) {
            // a: null
            // b: null
            // a == b => 0
            assertEquals(0, comp.compare(a, b));
        }
        // a: 0
        // b: null
        // a > b => 1
        setPropertyTx(a, TestOne.anInt, 0);
        try (final Tx tx = app.tx()) {
            assertEquals(1, comp.compare(a, b));
        }
        // a: null
        // b: 0
        // a < b => -1
        setPropertyTx(a, TestOne.anInt, null);
        setPropertyTx(b, TestOne.anInt, 0);
        try (final Tx tx = app.tx()) {
            assertEquals(-1, comp.compare(a, b));
        }
        // a: 1
        // b: 2
        // a > b => 1
        setPropertyTx(a, TestOne.anInt, 1);
        setPropertyTx(b, TestOne.anInt, 2);
        try (final Tx tx = app.tx()) {
            assertEquals(1, comp.compare(a, b));
        }
        // a: 2
        // b: 1
        // a < b => -1
        setPropertyTx(a, TestOne.anInt, 2);
        setPropertyTx(b, TestOne.anInt, 1);
        try (final Tx tx = app.tx()) {
            assertEquals(-1, comp.compare(a, b));
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Example 59 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method test01DeleteRelationship.

@Test
public void test01DeleteRelationship() {
    try {
        final TestOne testOne = createTestNode(TestOne.class);
        final TestSix testSix = createTestNode(TestSix.class);
        SixOneOneToOne rel = null;
        assertNotNull(testOne);
        assertNotNull(testSix);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            tx.success();
        }
        assertNotNull(rel);
        try {
            // try to delete relationship
            rel.getRelationship().delete();
            fail("Should have raised an org.neo4j.graphdb.NotInTransactionException");
        } catch (NotInTransactionException e) {
        }
        // Relationship still there
        assertNotNull(rel);
        try (final Tx tx = app.tx()) {
            app.delete(rel);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            String uuid = rel.getUuid();
            fail("Deleted entity should have thrown an exception on access.");
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NotInTransactionException(org.structr.api.NotInTransactionException) NotFoundException(org.structr.api.NotFoundException) TestOne(org.structr.core.entity.TestOne) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 60 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method testRelationshipsOnNodeCreation.

@Test
public void testRelationshipsOnNodeCreation() {
    Principal user = null;
    TestOne test = null;
    // create user
    try (final Tx tx = app.tx()) {
        user = app.create(Principal.class, "tester");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend);
    final App app = StructrApp.getInstance(ctx);
    // create object with user context
    try (final Tx tx = app.tx()) {
        test = app.create(TestOne.class);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // query for relationships
    try (final Tx tx = app.tx()) {
        final List<? extends RelationshipInterface> rels1 = app.relationshipQuery().and(AbstractRelationship.sourceId, user.getUuid()).getAsList();
        final List<Class> classes1 = rels1.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels1.size());
        assertTrue("Invalid relationship type after object creation", classes1.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes1.contains(PrincipalOwnsNode.class));
        final List<? extends RelationshipInterface> rels2 = app.relationshipQuery().and(AbstractRelationship.targetId, test.getUuid()).getAsList();
        final List<Class> classes2 = rels2.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels2.size());
        assertTrue("Invalid relationship type after object creation", classes2.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes2.contains(PrincipalOwnsNode.class));
        final List<? extends RelationshipInterface> rels3 = Iterables.toList(test.getIncomingRelationships());
        final List<Class> classes3 = rels3.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels3.size());
        assertTrue("Invalid relationship type after object creation", classes3.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes3.contains(PrincipalOwnsNode.class));
        final Security sec = app.relationshipQuery(Security.class).getFirst();
        assertNotNull("Relationship caching on node creation is broken", sec);
        final PrincipalOwnsNode owns = app.relationshipQuery(PrincipalOwnsNode.class).getFirst();
        assertNotNull("Relationship caching on node creation is broken", owns);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) LoggerFactory(org.slf4j.LoggerFactory) RelationshipType(org.structr.api.graph.RelationshipType) TestTen(org.structr.core.entity.TestTen) StringUtils(org.apache.commons.lang3.StringUtils) GenericNode(org.structr.core.entity.GenericNode) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) StringProperty(org.structr.core.property.StringProperty) Assert.fail(org.junit.Assert.fail) Location(org.structr.core.entity.Location) ResourceAccess(org.structr.core.entity.ResourceAccess) OneThreeOneToOne(org.structr.core.entity.OneThreeOneToOne) NodeHasLocation(org.structr.core.entity.relationship.NodeHasLocation) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) NotInTransactionException(org.structr.api.NotInTransactionException) GraphObject(org.structr.core.GraphObject) OneTwoOneToOne(org.structr.core.entity.OneTwoOneToOne) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SixOneManyToMany(org.structr.core.entity.SixOneManyToMany) List(java.util.List) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) StructrApp(org.structr.core.app.StructrApp) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) TestSix(org.structr.core.entity.TestSix) PropertyMap(org.structr.core.property.PropertyMap) NotFoundException(org.structr.api.NotFoundException) NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyKey(org.structr.core.property.PropertyKey) TestNine(org.structr.core.entity.TestNine) Result(org.structr.core.Result) LinkedList(java.util.LinkedList) AbstractNode(org.structr.core.entity.AbstractNode) TestTwo(org.structr.core.entity.TestTwo) NodeInterface(org.structr.core.graph.NodeInterface) SixThreeOneToMany(org.structr.core.entity.SixThreeOneToMany) Logger(org.slf4j.Logger) Iterables(org.structr.api.util.Iterables) GenericRelationship(org.structr.core.entity.GenericRelationship) Assert.assertNotNull(org.junit.Assert.assertNotNull) Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) Principal(org.structr.core.entity.Principal) Relation(org.structr.core.entity.Relation) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) TestSeven(org.structr.core.entity.TestSeven) TestThree(org.structr.core.entity.TestThree) TestFour(org.structr.core.entity.TestFour) NodeServiceCommand(org.structr.core.graph.NodeServiceCommand) Localization(org.structr.core.entity.Localization) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SchemaNode(org.structr.core.entity.SchemaNode) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)74 TestOne (org.structr.core.entity.TestOne)74 Tx (org.structr.core.graph.Tx)72 FrameworkException (org.structr.common.error.FrameworkException)70 StructrTest (org.structr.common.StructrTest)20 Result (org.structr.core.Result)15 Principal (org.structr.core.entity.Principal)15 TestSix (org.structr.core.entity.TestSix)15 NodeInterface (org.structr.core.graph.NodeInterface)15 Random (java.util.Random)12 ActionContext (org.structr.schema.action.ActionContext)12 LinkedList (java.util.LinkedList)10 TestFour (org.structr.core.entity.TestFour)9 PropertyMap (org.structr.core.property.PropertyMap)9 PropertyKey (org.structr.core.property.PropertyKey)8 Date (java.util.Date)7 App (org.structr.core.app.App)7 StructrApp (org.structr.core.app.StructrApp)7 NotFoundException (org.structr.api.NotFoundException)6 UnlicensedException (org.structr.common.error.UnlicensedException)6