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.delegate;
017
018import static org.hamcrest.core.Is.is;
019import static org.junit.Assert.assertNotNull;
020import static org.junit.Assert.assertThat;
021import java.sql.DriverPropertyInfo;
022import java.sql.SQLException;
023import java.util.Properties;
024import org.junit.Test;
025import org.modeshape.jdbc.JcrDriver;
026
027/**
028 * 
029 */
030public class HttpRepositoryDelegateTest extends RepositoryDelegateFactoryTest {
031
032    private static final String REPOSITORY_NAME = "repositoryName";
033
034    private static final String USER_NAME = "jsmith";
035    private static final String PASSWORD = "secret";
036    private static final String WORKSPACE = "MyWorkspace";
037    private static final String SERVER = "serverName:8080";
038    private static final String INVALID_URL = JcrDriver.HTTP_URL_PREFIX + SERVER;
039
040    private static final String VALID_HTTP_URL = JcrDriver.HTTP_URL_PREFIX + SERVER + "/modeshape-rest";
041
042    private static final String VALID_HTTP_URL_WITH_PARMS = VALID_HTTP_URL + "/" + REPOSITORY_NAME + "/" + WORKSPACE + "?user="
043                                                            + USER_NAME + "&password=" + PASSWORD + "&"
044                                                            + JcrDriver.TEIID_SUPPORT_PROPERTY_NAME + "=true";
045
046    private RepositoryDelegate delegate;
047
048    /**
049     * {@inheritDoc}
050     * 
051     * @see org.modeshape.jdbc.delegate.RepositoryDelegateFactoryTest#factory()
052     */
053    @Override
054    protected RepositoryDelegateFactory factory() {
055        return HttpRepositoryDelegate.FACTORY;
056    }
057
058    @Test
059    public void testNoContextOverride() throws SQLException {
060        delegate = factory().createRepositoryDelegate(VALID_HTTP_URL_WITH_PARMS, new Properties(), null);
061    }
062
063    @Test
064    public void connectionInfoShouldBeValid() throws SQLException {
065        delegate = factory().createRepositoryDelegate(VALID_HTTP_URL_WITH_PARMS, new Properties(), null);
066
067        assertNotNull(delegate.getConnectionInfo());
068        assertThat(delegate.getConnectionInfo().getUsername(), is(USER_NAME));
069        assertThat(delegate.getConnectionInfo().getPassword(), is(new String(PASSWORD).toCharArray()));
070
071        assertThat(delegate.getConnectionInfo().getEffectiveUrl(),
072                   is(VALID_HTTP_URL
073                      + "?teiidsupport=true&user=jsmith&workspace=MyWorkspace&password=******&repositoryName=repositoryName"));
074
075        assertThat(delegate.getConnectionInfo().isTeiidSupport(), is(Boolean.TRUE.booleanValue()));
076
077        DriverPropertyInfo[] infos = delegate.getConnectionInfo().getPropertyInfos();
078        assertThat(infos.length, is(0));
079
080        assertThat((delegate.getConnectionInfo()).getRepositoryPath(), is(SERVER + "/modeshape-rest"));
081
082        // System.out.println("URL: " + delegate.getConnectionInfo().getUrl());
083
084    }
085
086    @Test
087    public void connectionPropertyInfoShouldIndicateMissingData() throws SQLException {
088        delegate = factory().createRepositoryDelegate(INVALID_URL, new Properties(), null);
089
090        assertNotNull(delegate.getConnectionInfo());
091
092        DriverPropertyInfo[] infos = delegate.getConnectionInfo().getPropertyInfos();
093        assertThat(infos.length, is(5));
094    }
095
096    @Test
097    public void shouldReturnEmptyPropertyInfosWhenSuppliedValidUrlAndAllPropertiesWithRepositoriesInHTTP() throws SQLException {
098        Properties validProperties = new Properties();
099        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, WORKSPACE);
100        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, USER_NAME);
101        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, PASSWORD);
102        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, REPOSITORY_NAME);
103
104        delegate = factory().createRepositoryDelegate(VALID_HTTP_URL, validProperties, null);
105        DriverPropertyInfo[] infos = delegate.getConnectionInfo().getPropertyInfos();
106        assertThat(infos.length, is(0));
107    }
108
109}