Search in sources :

Example 16 with TestOne

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

the class CypherTest method test03DeleteDirectly.

@Test
public void test03DeleteDirectly() {
    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 (final Tx tx = app.tx()) {
            testOne.getRelationships().iterator().next().getRelationship().delete();
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            rel.getUuid();
            fail("Accessing a deleted relationship should thow an exception.");
            tx.success();
        } catch (NotFoundException nfex) {
            assertNotNull(nfex.getMessage());
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) 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 17 with TestOne

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

the class CypherTest method test01DeleteAfterLookupWithCypherInTransaction.

@Test
public void test01DeleteAfterLookupWithCypherInTransaction() {
    try {
        final TestSix testSix = this.createTestNode(TestSix.class);
        final TestOne testOne = this.createTestNode(TestOne.class);
        SixOneOneToOne rel = null;
        assertNotNull(testSix);
        assertNotNull(testOne);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            tx.success();
        }
        assertNotNull(rel);
        DatabaseService graphDb = app.command(GraphDatabaseCommand.class).execute();
        try (final Tx tx = app.tx()) {
            NativeResult<Relationship> result = graphDb.execute("MATCH (n)<-[r:ONE_TO_ONE]-() RETURN r");
            final Iterator<Relationship> rels = result.columnAs("r");
            assertTrue(rels.hasNext());
            rels.next().delete();
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            rel.getUuid();
            fail("Accessing a deleted relationship should thow an exception.");
            tx.success();
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) NotFoundException(org.structr.api.NotFoundException) TestOne(org.structr.core.entity.TestOne) DatabaseService(org.structr.api.DatabaseService) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) GraphDatabaseCommand(org.structr.core.graph.GraphDatabaseCommand) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 18 with TestOne

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

the class CypherTest method testCypherResultWrapping.

@Test
public void testCypherResultWrapping() {
    try (final Tx tx = app.tx()) {
        List<TestOne> testOnes = createTestNodes(TestOne.class, 10);
        List<TestSix> testSixs = createTestNodes(TestSix.class, 10);
        for (final TestOne testOne : testOnes) {
            testOne.setProperty(TestOne.manyToManyTestSixs, testSixs);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne) RETURN DISTINCT n");
        assertEquals("Invalid wrapped cypher query result", 10, result.size());
        for (final GraphObject obj : result) {
            System.out.println(obj);
            assertEquals("Invalid wrapped cypher query result", TestOne.class, obj.getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT  n, r, m ");
        final Iterator<GraphObject> it = result.iterator();
        assertEquals("Invalid wrapped cypher query result", 300, result.size());
        while (it.hasNext()) {
            // n
            assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
            // r
            assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass());
            // m
            assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: nodes(p), rels: relationships(p) } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
            final Object nodes = obj.getProperty(new StringProperty("nodes"));
            final Object rels = obj.getProperty(new StringProperty("rels"));
            assertTrue("Invalid wrapped cypher query result", nodes instanceof Collection);
            assertTrue("Invalid wrapped cypher query result", rels instanceof Collection);
            final Iterator it = ((Collection) nodes).iterator();
            while (it.hasNext()) {
                assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
                assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
            }
            for (final Object node : ((Collection) rels)) {
                assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, node.getClass());
            }
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT { path: p, value: 12 } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        final Iterator it = result.iterator();
        while (it.hasNext()) {
            final Object path = it.next();
            final Object value = it.next();
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, path.getClass());
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, value.getClass());
            assertEquals("Invalid wrapped cypher query result", 12L, ((GraphObjectMap) value).getProperty(new StringProperty("value")));
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: { x : { y : { z : nodes(p) } } } } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
            final Object nodes = obj.getProperty(new StringProperty("nodes"));
            assertTrue("Invalid wrapped cypher query result", nodes instanceof GraphObjectMap);
            final Object x = ((GraphObjectMap) nodes).getProperty(new StringProperty("x"));
            assertTrue("Invalid wrapped cypher query result", x instanceof GraphObjectMap);
            final Object y = ((GraphObjectMap) x).getProperty(new StringProperty("y"));
            assertTrue("Invalid wrapped cypher query result", y instanceof GraphObjectMap);
            final Object z = ((GraphObjectMap) y).getProperty(new StringProperty("z"));
            assertTrue("Invalid wrapped cypher query result", z instanceof Collection);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
/*
		try (final Tx tx = app.tx()) {

			final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p");

			assertEquals("Invalid wrapped cypher query result", 100, result.size());

			for (final GraphObject obj : result) {

				assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());

				final Object paths = obj.getProperty(new StringProperty("p"));

				assertTrue("Invalid wrapped cypher query result", paths instanceof Iterable);

				final Iterator it = ((Iterable)paths).iterator();
				while (it.hasNext()) {

					assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());		// n
					assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass());	// r
					assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());		// m
				}
			}

			tx.success();


		} catch (FrameworkException ex) {
			logger.error("", ex);
		}
		*/
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) GraphObjectMap(org.structr.core.GraphObjectMap) Iterator(java.util.Iterator) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) TestOne(org.structr.core.entity.TestOne) CypherQueryCommand(org.structr.core.graph.CypherQueryCommand) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 19 with TestOne

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

the class CypherTest method test05RollbackDelete.

@Test
public void test05RollbackDelete() {
    try {
        final TestOne testOne = createTestNode(TestOne.class);
        final TestSix testSix = createTestNode(TestSix.class);
        String relId = null;
        SixOneOneToOne rel = null;
        assertNotNull(testOne);
        assertNotNull(testSix);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            relId = rel.getUuid();
            tx.success();
        }
        assertNotNull(rel);
        try (final Tx tx = app.tx()) {
            // do not commit transaction
            testOne.getRelationships().iterator().next().getRelationship().delete();
        }
        try (final Tx tx = app.tx()) {
            assertEquals("UUID of relationship should be readable after rollback", relId, rel.getUuid());
            tx.success();
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) 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 20 with TestOne

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

the class SearchAndSortingTest method test02Paging.

/**
 * Test different pages and page sizes
 */
@Test
public void test02Paging() {
    try {
        boolean includeDeletedAndHidden = false;
        boolean publicOnly = false;
        Class type = TestOne.class;
        // no more than 89 to avoid sort order TestOne-10, TestOne-100 ...
        int number = 89;
        final int offset = 10;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        try (final Tx tx = app.tx()) {
            int i = offset;
            for (NodeInterface node : nodes) {
                // System.out.println("Node ID: " + node.getNodeId());
                String _name = "TestOne-" + i;
                i++;
                node.setProperty(AbstractNode.name, _name);
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            // test pages sizes from 0 to 10
            for (int ps = 0; ps < 10; ps++) {
                // test all pages
                for (int p = 0; p < (number / Math.max(1, ps)) + 1; p++) {
                    testPaging(type, ps, p, number, offset, includeDeletedAndHidden, publicOnly, sortKey, sortDesc);
                }
            }
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Random(java.util.Random) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) 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