001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.jdbc.rest;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertFalse;
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertNull;
022import java.util.Collection;
023import java.util.List;
024import java.util.Map;
025import javax.jcr.query.Query;
026import org.junit.BeforeClass;
027import org.junit.Test;
028
029/**
030 * Integration test which tests the behavior of {@link ModeShapeRestClient}
031 *
032 * @author Horia Chiorean (hchiorea@redhat.com)
033 */
034public class ModeShapeRestClientTest {
035
036    private static ModeShapeRestClient REST_CLIENT;
037
038    @BeforeClass
039    public static void beforeClass() {
040        // the values are coming from the modeshape-rest-war test webapp
041        REST_CLIENT = new ModeShapeRestClient("http://localhost:8090/modeshape/repo/default", "dnauser", "password");
042    }
043
044    @Test
045    public void shouldGetRepositories() throws Exception {
046        Repositories repositories = REST_CLIENT.getRepositories();
047        assertNotNull(repositories);
048        Collection<Repositories.Repository> repositoryList = repositories.getRepositories();
049        assertEquals(1, repositoryList.size());
050        Repositories.Repository repository = repositories.iterator().next();
051        assertEquals("repo", repository.getName());
052        Map<String, Object> metadata = repository.getMetadata();
053        assertNotNull(metadata);
054        assertFalse(metadata.isEmpty());
055    }
056
057    @Test
058    public void shouldGetRepositoryByName() throws Exception {
059        assertNull(REST_CLIENT.getRepository("foobar"));
060        Repositories.Repository repository = REST_CLIENT.getRepository("repo");
061        assertNotNull(repository);
062    }
063
064    @Test
065    public void shouldGetWorkspacesForRepository() throws Exception {
066        Workspaces workspaces = REST_CLIENT.getWorkspaces("repo");
067        assertNotNull(workspaces);
068        List<String> wsList = workspaces.getWorkspaces();
069        assertEquals(1, wsList.size());
070        assertEquals("default", wsList.get(0));
071    }
072
073    @Test
074    public void shouldGetNodeTypesForRepository() throws Exception {
075        NodeTypes nodeTypes = REST_CLIENT.getNodeTypes();
076        assertFalse(nodeTypes.isEmpty());
077        assertNotNull(nodeTypes.getNodeType("nt:base"));
078        assertNotNull(nodeTypes.getNodeType("nt:unstructured"));
079        assertNull(nodeTypes.getNodeType("foobar"));
080    }
081
082    @SuppressWarnings( "deprecation" )
083    @Test
084    public void shouldQueryRepository() throws Exception {
085        QueryResult queryResult = REST_CLIENT.query("SELECT node.[jcr:path] FROM [mode:root] AS node", Query.JCR_SQL2);
086        assertQueryResult(queryResult);
087
088        queryResult = REST_CLIENT.query("SELECT jcr:path FROM mode:root", Query.SQL);
089        assertQueryResult(queryResult);
090
091        queryResult = REST_CLIENT.query("//element(*, mode:root)", Query.XPATH);
092        assertQueryResult(queryResult);
093    }
094
095    private void assertQueryResult( QueryResult queryResult ) {
096        Map<String, String> columns = queryResult.getColumns();
097        String pathColumn = columns.get("jcr:path");
098        assertNotNull(pathColumn);
099        assertEquals("string", pathColumn.toLowerCase());
100
101        List<QueryResult.Row> rows = queryResult.getRows();
102        assertEquals(1, rows.size());
103        QueryResult.Row result = rows.get(0);
104        assertEquals("/", result.getValue("jcr:path"));
105    }
106
107    @Test
108    public void shouldGetQueryPlan() throws Exception {
109        assertNotNull(REST_CLIENT.queryPlan("SELECT node.[jcr:path] FROM [mode:root] AS node", Query.JCR_SQL2));
110    }
111}