View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.configuration.validation;
    21
    22  import static org.hamcrest.CoreMatchers.not;
    23  import static org.hamcrest.Matchers.containsString;
    24  import static org.junit.Assert.assertFalse;
    25  import static org.junit.Assert.assertThat;
    26  import static org.junit.Assert.assertTrue;
    27
    28  import org.apache.commons.configuration.BaseConfiguration;
    29  import org.junit.Test;
    30  import org.neo4j.server.configuration.Configurator;
    31
    32
    33  public class WebadminConfigurationRuleTest {
    34
    35      private static final boolean theValidatorHasPassed = true;
    36
    37      @Test(expected=RuleFailedException.class)
    38      public void shouldFailIfNoWebadminConfigSpecified() throws RuleFailedException {
    39          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    40          BaseConfiguration emptyConfig = new BaseConfiguration();
    41          rule.validate(emptyConfig);
    42          assertFalse(theValidatorHasPassed);
    43      }
    44
    45      @Test(expected=RuleFailedException.class)
    46      public void shouldFailIfOnlyRestApiKeySpecified() throws RuleFailedException {
    47          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    48          BaseConfiguration config = new BaseConfiguration();
    49          config.addProperty(Configurator.DATA_API_PATH_PROPERTY_KEY, "http://localhost:7474/db/data");
    50          rule.validate(config);
    51          assertFalse(theValidatorHasPassed);
    52      }
    53
    54      @Test(expected=RuleFailedException.class)
    55      public void shouldFailIfOnlyAdminApiKeySpecified() throws RuleFailedException {
    56          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    57          BaseConfiguration config = new BaseConfiguration();
    58          config.addProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, "http://localhost:7474/db/manage");
    59          rule.validate(config);
    60          assertFalse(theValidatorHasPassed);
    61      }
    62
    63      @Test
    64      public void shouldAllowAbsoluteUris() throws RuleFailedException {
    65          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    66          BaseConfiguration config = new BaseConfiguration();
    67          config.addProperty(Configurator.DATA_API_PATH_PROPERTY_KEY, "http://localhost:7474/db/data");
    68          config.addProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, "http://localhost:7474/db/manage");
    69          rule.validate(config);
    70          assertTrue(theValidatorHasPassed);
    71      }
    72
    73      @Test
    74      public void shouldAllowRelativeUris() throws RuleFailedException {
    75          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    76          BaseConfiguration config = new BaseConfiguration();
    77          config.addProperty(Configurator.DATA_API_PATH_PROPERTY_KEY, "/db/data");
    78          config.addProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, "/db/manage");
    79          rule.validate(config);
    80          assertTrue(theValidatorHasPassed);
    81      }
    82
    83      @Test
    84      public void shouldNormaliseUris() throws RuleFailedException {
    85          WebadminConfigurationRule rule = new WebadminConfigurationRule();
    86          BaseConfiguration config = new BaseConfiguration();
    87          config.addProperty(Configurator.DATA_API_PATH_PROPERTY_KEY, "http://localhost:7474///db///data///");
    88          config.addProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, "http://localhost:7474////db///manage");
    89          rule.validate(config);
    90
    91
    92          assertThat((String)config.getProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY), not(containsString("///")));
    93          assertFalse(((String)config.getProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY)).endsWith("//"));
    94          assertFalse(((String)config.getProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY)).endsWith("/"));
    95
    96          System.out.println(config.getProperty(Configurator.DATA_API_PATH_PROPERTY_KEY));
    97
    98          assertThat((String)config.getProperty(Configurator.DATA_API_PATH_PROPERTY_KEY), not(containsString("///")));
    99          assertFalse(((String)config.getProperty(Configurator.DATA_API_PATH_PROPERTY_KEY)).endsWith("//"));
    100         assertFalse(((String)config.getProperty(Configurator.DATA_API_PATH_PROPERTY_KEY)).endsWith("/"));
    101     }
    102 }