package org.apache.solr.core;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
import org.apache.commons.io.FileUtils;
import org.apache.solr.SolrTestCaseJ4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.internal.matchers.StringContains.containsString;

public class TestConfigSets extends SolrTestCaseJ4 {

  @Rule
  public TestRule testRule = RuleChain.outerRule(new SystemPropertiesRestoreRule());

  public static String solrxml = "<solr><str name=\"configSetBaseDir\">${configsets:configsets}</str></solr>";

  public CoreContainer setupContainer(String configSetsBaseDir) {
    File testDirectory = createTempDir().toFile();

    System.setProperty("configsets", configSetsBaseDir);

    SolrResourceLoader loader = new SolrResourceLoader(testDirectory.getAbsolutePath());
    CoreContainer container = new CoreContainer(SolrXmlConfig.fromString(loader, solrxml));
    container.load();

    return container;
  }

  @Test
  public void testDefaultConfigSetBasePathResolution() throws IOException {
    try (SolrResourceLoader loader = new SolrResourceLoader(new File("/path/to/solr/home").getAbsolutePath())) {

      NodeConfig config
          = SolrXmlConfig.fromString(loader, "<solr><str name=\"configSetBaseDir\">configsets</str></solr>");
      assertThat(config.getConfigSetBaseDirectory(), is(Paths.get("/path/to/solr/home/configsets")));

      NodeConfig absConfig
          = SolrXmlConfig.fromString(loader, "<solr><str name=\"configSetBaseDir\">/path/to/configsets</str></solr>");
      assertThat(absConfig.getConfigSetBaseDirectory(), is(Paths.get("/path/to/configsets")));
    }
  }

  @Test
  public void testConfigSetServiceFindsConfigSets() {
    CoreContainer container = null;
    try {
      container = setupContainer(getFile("solr/configsets").getAbsolutePath());
      String testDirectory = new File(container.getResourceLoader().getInstanceDir()).getAbsolutePath();

      SolrCore core1 = container.create(new CoreDescriptor(container, "core1", testDirectory + "core1", "configSet", "configset-2"));
      assertThat(core1.getCoreDescriptor().getName(), is("core1"));
      assertThat(core1.getDataDir(), is(testDirectory + "core1" + File.separator + "data" + File.separator));
    }
    finally {
      if (container != null)
        container.shutdown();
    }
  }

  @Test
  public void testNonExistentConfigSetThrowsException() {
    CoreContainer container = null;
    try {
      container = setupContainer(getFile("solr/configsets").getAbsolutePath());
      String testDirectory = container.getResourceLoader().getInstanceDir();

      container.create(new CoreDescriptor(container, "core1", testDirectory + "/core1", "configSet", "nonexistent"));
      fail("Expected core creation to fail");
    }
    catch (Exception e) {
      Throwable wrappedException = getWrappedException(e);
      assertThat(wrappedException.getMessage(), containsString("nonexistent"));
    }
    finally {
      if (container != null)
        container.shutdown();
    }
  }

  @Test
  public void testConfigSetOnCoreReload() throws IOException {
    File testDirectory = new File(initCoreDataDir, "core-reload");
    testDirectory.mkdirs();
    File configSetsDir = new File(testDirectory, "configsets");

    FileUtils.copyDirectory(getFile("solr/configsets"), configSetsDir);

    String csd = configSetsDir.getAbsolutePath();
    System.setProperty("configsets", csd);

    SolrResourceLoader loader = new SolrResourceLoader(testDirectory.getAbsolutePath());
    CoreContainer container = new CoreContainer(SolrXmlConfig.fromString(loader, solrxml));
    container.load();

    // We initially don't have a /get handler defined
    SolrCore core = container.create(new CoreDescriptor(container, "core1", testDirectory + "/core", "configSet", "configset-2"));
    assertThat("No /dump handler should be defined in the initial configuration",
        core.getRequestHandler("/dump"), is(nullValue()));

    // Now copy in a config with a /get handler and reload
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig-withgethandler.xml"),
        new File(new File(configSetsDir, "configset-2/conf"), "solrconfig.xml"));
    container.reload("core1");

    core = container.getCore("core1");
    assertThat("A /dump handler should be defined in the reloaded configuration",
        core.getRequestHandler("/dump"), is(notNullValue()));
    core.close();

    container.shutdown();
  }

}

@@ -2435,33 +2435,68 @@
 tBaseDirectory()
-,
+.toAbsolutePath(),%0A                 
  is(Paths.get(%22/
@@ -2521,24 +2521,41 @@
 configsets%22)
+.toAbsolutePath()
 ));%0A%0A      N
@@ -2747,16 +2747,33 @@
 ectory()
+.toAbsolutePath()
 , is(Pat
@@ -2801,16 +2801,33 @@
 igsets%22)
+.toAbsolutePath()
 ));%0A    

Fix Windows test bug