Search in sources :

Example 26 with NodeAttribute

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

the class GraphQLTest method testBasics.

@Test
public void testBasics() {
    RestAssured.basePath = "/structr/graphql";
    Group group = null;
    Principal tester = null;
    try (final Tx tx = app.tx()) {
        final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
        tester = app.create(Principal.class, new NodeAttribute<>(Principal.name, "tester"));
        group = app.create(Group.class, new NodeAttribute<>(Group.name, "TestGroup"), new NodeAttribute<>(membersKey, Arrays.asList(tester)));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final String query1 = "{ Group { id, type, name, members { id, type, name } }, Principal(_pageSize: 1) { id, type name }}";
    final String query2 = "{ Group { id, type, name, members { } }}";
    final String query3 = "{ Group(id: \"" + group.getUuid() + "\") { id, type, name }}";
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query1).expect().statusCode(200).body("Group", hasSize(1)).body("Principal", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).body("Group[0].members[0].id", equalTo(tester.getUuid())).body("Group[0].members[0].type", equalTo("Principal")).body("Group[0].members[0].name", equalTo("tester")).body("Principal[0].id", equalTo(group.getUuid())).body("Principal[0].type", equalTo("Group")).body("Principal[0].name", equalTo("TestGroup")).when().post("/");
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query2).expect().statusCode(422).body("message", equalTo("Parse error at } in line 1, column 36")).body("code", equalTo(422)).body("query", equalTo(query2)).when().post("/");
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query3).expect().statusCode(200).body("Group", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).when().post("/");
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) LinkedList(java.util.LinkedList) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrGraphQLTest(org.structr.rest.common.StructrGraphQLTest)

Example 27 with NodeAttribute

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

the class GraphQLTest method testAdvancedQueries.

@Test
public void testAdvancedQueries() {
    final List<MailTemplate> templates = new LinkedList<>();
    final List<Principal> team = new LinkedList<>();
    Group group = null;
    try (final Tx tx = app.tx()) {
        final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Axel")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Inès")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Kai")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Lukas")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Michael")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Susanne")));
        team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Tobias")));
        group = app.create(Group.class, new NodeAttribute<>(Group.name, "Structr Team"), new NodeAttribute<>(membersKey, team));
        app.create(Group.class, new NodeAttribute<>(Group.name, "All teams"), new NodeAttribute<>(membersKey, Arrays.asList(group)));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate4"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "zrtsga"), new NodeAttribute<>(AbstractNode.owner, team.get(2))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate2"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "lertdf"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate5"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "tzegsg"), new NodeAttribute<>(AbstractNode.owner, team.get(3))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate3"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "asgw"), new NodeAttribute<>(AbstractNode.owner, team.get(1))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate6"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "dfjgr"), new NodeAttribute<>(AbstractNode.owner, team.get(4))));
        templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate1"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "abcdef"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    RestAssured.basePath = "/structr/graphql";
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal(id: \"" + team.get(0).getUuid() + "\") { id, type, name } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.id", team.get(0).getUuid());
        assertMapPathValueIs(result, "Principal.0.type", "Principal");
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal(name: \"Axel\") { name } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
        assertMapPathValueIs(result, "Principal.0.type", null);
        assertMapPathValueIs(result, "Principal.0.id", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal { name(_equals: \"Axel\") } }");
        assertMapPathValueIs(result, "Principal.#", 1);
        assertMapPathValueIs(result, "Principal.0.name", "Axel");
        assertMapPathValueIs(result, "Principal.0.type", null);
        assertMapPathValueIs(result, "Principal.0.id", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Principal { name(_contains: \"a\", _contains: \"l\", _conj: \"and\") } }");
        assertMapPathValueIs(result, "Principal.#", 4);
        assertMapPathValueIs(result, "Principal.0.name", "All teams");
        assertMapPathValueIs(result, "Principal.1.name", "Axel");
        assertMapPathValueIs(result, "Principal.2.name", "Lukas");
        assertMapPathValueIs(result, "Principal.3.name", "Michael");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group(_pageSize: 1) { name }, Principal(_pageSize: 2, _page: 2) { name(_contains: \"i\") } }");
        assertMapPathValueIs(result, "Group.#", 1);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Principal.#", 2);
        assertMapPathValueIs(result, "Principal.0.name", "Inès");
        assertMapPathValueIs(result, "Principal.1.name", "Kai");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_pageSize: 2, _page: 2) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 2);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Inès");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\") { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.8.name", "Tobias");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"and\") }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", null);
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.members.0.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.1", null);
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"or\") }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", null);
        assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
        assertMapPathValueIs(result, "Group.1.members.#", 4);
        assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Michael");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ MailTemplate { id, type, text(_contains: \"2\"), owner(_equals: { name: \"Axel\"}) { name } }}");
        assertMapPathValueIs(result, "MailTemplate.#", 1);
        assertMapPathValueIs(result, "MailTemplate.0.id", templates.get(1).getUuid());
        assertMapPathValueIs(result, "MailTemplate.0.type", "MailTemplate");
        assertMapPathValueIs(result, "MailTemplate.0.text", "MailTemplate2");
        assertMapPathValueIs(result, "MailTemplate.0.name", null);
        assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ MailTemplate(_sort: \"owner.name\") { name, owner { name }}}");
        assertMapPathValueIs(result, "MailTemplate.#", 6);
        assertMapPathValueIs(result, "MailTemplate.0.name", "abcdef");
        assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
        assertMapPathValueIs(result, "MailTemplate.1.name", "lertdf");
        assertMapPathValueIs(result, "MailTemplate.1.owner.name", "Axel");
        assertMapPathValueIs(result, "MailTemplate.2.name", "asgw");
        assertMapPathValueIs(result, "MailTemplate.2.owner.name", "Christian");
        assertMapPathValueIs(result, "MailTemplate.3.name", "zrtsga");
        assertMapPathValueIs(result, "MailTemplate.3.owner.name", "Christian");
        assertMapPathValueIs(result, "MailTemplate.4.name", "tzegsg");
        assertMapPathValueIs(result, "MailTemplate.4.owner.name", "Inès");
        assertMapPathValueIs(result, "MailTemplate.5.name", "dfjgr");
        assertMapPathValueIs(result, "MailTemplate.5.owner.name", "Kai");
    }
    {
        final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
        assertMapPathValueIs(result, "Group.#", 2);
        assertMapPathValueIs(result, "Group.0.name", "All teams");
        assertMapPathValueIs(result, "Group.0.members.#", 1);
        assertMapPathValueIs(result, "Group.1.name", "Structr Team");
        assertMapPathValueIs(result, "Group.1.members.#", 9);
        assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
        assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
        assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
        assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
        assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
        assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
        assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
        assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
    }
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) MailTemplate(org.structr.core.entity.MailTemplate) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrGraphQLTest(org.structr.rest.common.StructrGraphQLTest)

Example 28 with NodeAttribute

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

the class AdvancedSearchTest method testSearchDynamicNodes.

@Test
public void testSearchDynamicNodes() {
    try {
        SchemaNode node = null;
        // setup
        try (final Tx tx = app.tx()) {
            node = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "TestType"));
            node.setProperty(new StringProperty("_test"), "Integer");
            tx.success();
        }
        // fetch dynamic type info
        final Class dynamicType = StructrApp.getConfiguration().getNodeEntityClass("TestType");
        final PropertyKey testKey = StructrApp.key(dynamicType, "test");
        // modify schema node but keep reference to "old" type
        try (final Tx tx = app.tx()) {
            node.setProperty(new StringProperty("_test2"), "String");
            tx.success();
        }
        // create test nodes
        try (final Tx tx = app.tx()) {
            app.create(dynamicType, new NodeAttribute(testKey, 10));
            app.create(dynamicType, new NodeAttribute(testKey, 11));
            app.create(dynamicType, new NodeAttribute(testKey, 12));
            tx.success();
        }
        // query test nodes
        try (final Tx tx = app.tx()) {
            /*
				 * If this test fails, the method "allSubtypes" in SearchCommand was not able to identify
				 * a dynamic type as being assignable to itself. This can happen when the reference to an
				 * existing dynamic type is used after the type has been modified, because the class
				 * instances produced by the dynamic schema ClassLoader are not equal even if they have
				 * the same name and package etc.
				 */
            assertEquals("Query for dynamic node should return exactly one result: ", 1, app.nodeQuery(dynamicType).and(testKey, 10).getAsList().size());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) IndexingTest(org.structr.rest.common.IndexingTest)

Example 29 with NodeAttribute

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

the class AdvancedSearchTest method testSearchWithOwnerAndEnum.

@Test
public void testSearchWithOwnerAndEnum() {
    final Class testUserType = createTestUserType();
    try {
        final List<Principal> users = createTestNodes(testUserType, 3);
        final List<TestThree> testThrees = new LinkedList<>();
        final Random random = new Random();
        String uuid = null;
        int count = 0;
        try (final Tx tx = app.tx()) {
            for (final Principal user : users) {
                // create 20 entities for every user
                for (int i = 0; i < 20; i++) {
                    testThrees.add(app.create(TestThree.class, new NodeAttribute(AbstractNode.name, "test" + count++), new NodeAttribute(AbstractNode.owner, user), new NodeAttribute(TestThree.enumProperty, TestEnum.values()[random.nextInt(TestEnum.values().length)])));
                }
            }
            uuid = users.get(0).getUuid();
            tx.success();
        }
        // test with core API
        try (final Tx tx = app.tx()) {
            for (final Principal user : users) {
                for (final TestThree test : app.nodeQuery(TestThree.class).and(AbstractNode.owner, user).and(TestThree.enumProperty, TestEnum.Status1).getAsList()) {
                    assertEquals("Invalid enum query result", TestEnum.Status1, test.getProperty(TestThree.enumProperty));
                }
            }
            tx.success();
        }
        // test via REST
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).when().get(concat("/test_threes?sort=createdDate&owner=" + uuid + "&enumProperty=" + TestEnum.Status1));
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Random(java.util.Random) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestThree(org.structr.rest.entity.TestThree) Principal(org.structr.core.entity.Principal) LinkedList(java.util.LinkedList) Test(org.junit.Test) IndexingTest(org.structr.rest.common.IndexingTest)

Example 30 with NodeAttribute

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

the class PagingAndSortingTest method testRelationshipResourcePagingOnCollectionResource.

@Test
public void testRelationshipResourcePagingOnCollectionResource() {
    final Class testUserType = createTestUserType();
    final PropertyKey<String> passwordKey = StructrApp.key(testUserType, "password");
    Principal tester = null;
    try (final Tx tx = app.tx()) {
        tester = app.create(testUserType, new Name("tester"), new NodeAttribute<>(passwordKey, "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final App app = StructrApp.getInstance(SecurityContext.getInstance(tester, AccessMode.Backend));
    try (final Tx tx = app.tx()) {
        app.create(TestOne.class, "TestOne1");
        app.create(TestOne.class, "TestOne2");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("result_count", equalTo(4)).body("page_size", equalTo(2)).body("page_count", equalTo(2)).body("result", hasSize(2)).body("result[0].type", equalTo("PrincipalOwnsNode")).body("result[1].type", equalTo("Security")).when().get("/TestOne/in?pageSize=2");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Name(org.structr.core.graph.attribute.Name) Test(org.junit.Test) StructrRestTest(org.structr.rest.common.StructrRestTest)

Aggregations

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7