/*
 * ModeShape (http://www.modeshape.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modeshape.jdbc;

import static org.hamcrest.core.Is.is;
import org.junit.After;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jdbc.delegate.ConnectionInfo;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 
 */
public class JcrDriverHttpTest {

    private JcrDriver driver;
    private String validServerName;
    private String validRepositoryName;
    private String validWorkspaceName;
    private String validUrl;
    private Properties validProperties;

    @Before
    public void beforeEach() throws Exception {
        validRepositoryName = "MyRepository";
        validServerName = "serverName:8080";
        validWorkspaceName = "default";
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + validRepositoryName + "/"
                   + validWorkspaceName + "?user=jsmith&password=secret";
        validProperties = new Properties();

        driver = new JcrDriver(null);
    }

    @After
    public void afterEach() throws Exception {
        try {
            DriverManager.deregisterDriver(driver);
        } finally {
            driver = null;
        }
    }

    @Test
    public void shouldNotBeJdbcCompliant() {
        assertThat(driver.jdbcCompliant(), is(false));
    }

    @Test
    public void shouldHaveMajorVersion() {
        assertThat(driver.getMajorVersion(), is(TestUtil.majorVersion(JdbcI18n.driverVersion.text())));
    }

    @Test
    public void shouldHaveMinorVersion() {
        assertThat(driver.getMinorVersion(), is(TestUtil.minorVersion(JdbcI18n.driverVersion.text())));
    }

    @Test
    public void shouldHaveVendorUrl() {
        assertThat(driver.getVendorUrl(), is(JdbcI18n.driverVendorUrl.text()));
    }

    @Test
    public void shouldHaveVendorName() {
        assertThat(driver.getVendorName(), is(JdbcI18n.driverVendor.text()));
    }

    @Test
    public void shouldHaveVersion() {
        assertThat(driver.getVersion(), is(JdbcI18n.driverVersion.text()));
    }

    @Test
    public void shouldReturnEmptyPropertyInfosWhenSuppliedValidAndCompleteUrlAndNoProperties() throws SQLException {
        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(0));
    }

    @Test
    public void shouldReturnEmptyPropertyInfosWhenSuppliedValidUrlAndAllPropertiesWithRepositoryInHttp() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(0));
    }

    @Test
    public void shouldReturnRepositoryPropertyInfoWhenMissingRequiredRepositoryName() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");

        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(1));
        assertThat(infos[0].name, is(JdbcLocalI18n.repositoryNamePropertyName.text()));
        assertThat(infos[0].description, is(JdbcLocalI18n.repositoryNamePropertyDescription.text()));
        assertThat(infos[0].required, is(true));
    }

    @Test
    public void shouldReturnRepositoryPropertyInfoWhenMissingWorkspaceName() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);

        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(1));
        assertThat(infos[0].name, is(JdbcLocalI18n.workspaceNamePropertyName.text()));
        assertThat(infos[0].description, is(JdbcLocalI18n.workspaceNamePropertyDescription.text()));
        assertThat(infos[0].required, is(false));
    }

    @Test
    public void shouldReturnRepositoryPropertyInfoWhenMissingUsername() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
        validProperties.put(JcrDriver.PASSWORD_PROPERTY_NAME, "secret");
        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(1));
        assertThat(infos[0].name, is(JdbcLocalI18n.usernamePropertyName.text()));
        assertThat(infos[0].description, is(JdbcLocalI18n.usernamePropertyDescription.text()));
        assertThat(infos[0].required, is(false));
    }

    @Test
    public void shouldReturnRepositoryPropertyInfoWhenMissingPassword() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest";
        validProperties.put(JcrDriver.WORKSPACE_PROPERTY_NAME, "MyWorkspace");
        validProperties.put(JcrDriver.USERNAME_PROPERTY_NAME, "jsmith");
        validProperties.put(JcrDriver.REPOSITORY_PROPERTY_NAME, validRepositoryName);
        DriverPropertyInfo[] infos = driver.getPropertyInfo(validUrl, validProperties);
        assertThat(infos.length, is(1));
        assertThat(infos[0].name, is(JdbcLocalI18n.passwordPropertyName.text()));
        assertThat(infos[0].description, is(JdbcLocalI18n.passwordPropertyDescription.text()));
        assertThat(infos[0].required, is(false));
    }

    @Test
    public void shouldAcceptValidUrls() {
        assertThat(driver.acceptsURL(
                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + this.validRepositoryName + "/MyWorkspace&user=jsmith&password=secret"),
                   is(true));
        assertThat(driver.acceptsURL(
                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/" + this.validRepositoryName + "&user=jsmith&password=secret"),
                   is(true));
        assertThat(driver.acceptsURL(
                JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape-rest/&user=jsmith&password=secret"), is(true));
    }

    @Test
    public void shouldNotAcceptInvalidUrls() {
        assertThat(driver.acceptsURL("jdbc:jcr:http"), is(false));
        assertThat(driver.acceptsURL(JcrDriver.HTTP_URL_PREFIX), is(false));
        assertThat(driver.acceptsURL(JcrDriver.HTTP_URL_PREFIX + " "), is(false));
        assertThat(driver.acceptsURL("file://"), is(false));
        assertThat(driver.acceptsURL("jdbc://"), is(false));
    }

    @Test
    public void shouldCreateConnectionInfoForUrlWithEscapedCharacters() throws SQLException {
        validUrl = JcrDriver.HTTP_URL_PREFIX + validServerName + "/modeshape%20rest?repositoryName=My%20Repository&workspace=My%20Workspace&user=j%20smith&password=secret";
        ConnectionInfo info = driver.createConnectionInfo(validUrl, validProperties);
        assertThat(info.getWorkspaceName(), is("My Workspace"));
        assertThat(info.getUsername(), is("j smith"));
        assertThat(info.getPassword(), is("secret".toCharArray()));
        assertThat(info.getRepositoryName(), is("My Repository"));
    }
}
