/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.activeinstalls2.test.ui;

import java.util.List;
import java.util.Properties;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.xwiki.activeinstalls2.internal.XWikiElasticSearchExtension;
import org.xwiki.test.docker.internal.junit5.XWikiDockerExtension;
import org.xwiki.test.docker.junit5.TestConfiguration;

/**
 * Inject a dynamically-generated {@link TestConfiguration} in order to set the {@code activeinstalls2.pingURL} config
 * property to point to the ES instance started dynamically (on a dynamic port) during the test.
 *
 * @version $Id$
 * @since 14.5RC1
 */
public class DynamicTestConfigurationExtension implements BeforeAllCallback
{
    private static final ExtensionContext.Namespace XWIKIDOCKEREXTENSION_NAMESPACE =
        ExtensionContext.Namespace.create(XWikiDockerExtension.class);

    @Override
    public void beforeAll(ExtensionContext extensionContext)
    {
        // We're able to get the TestConfiguration here because it's saved in the XWIKIDOCKEREXTENSION_NAMESPACE
        // context in XWikiDockerExtension#evaluateExecutionCondition() which is executed for all extensions before
        // anything else.
        TestConfiguration testConfiguration = loadTestConfiguration(extensionContext);

        // Get the Elasticsearch container set by XWikiElasticSearchExtension
        ExtensionContext.Store store = XWikiElasticSearchExtension.getStore(extensionContext);
        ElasticsearchContainer esContainer = (ElasticsearchContainer) store.get(ElasticsearchContainer.class);

        // Save a TestConfiguration in the global test context so that it's merged in XWikiDockerExtension.
        ExtensionContext.Store globalStore =  extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL);
        TestConfiguration configuration = new TestConfiguration();
        Properties properties = new Properties();
        // Depending on the Servlet engine used, the host ip is "localhost" (for jetty standalone which doesn't
        // execute in a docker container) or "host.testcontainers.internal" (for all Servlet engines executing in a
        // docker container).
        String hostAddress = String.format("http://%s:%d", testConfiguration.getServletEngine().getHostIP(),
            esContainer.getMappedPort(9200));
        properties.setProperty("xwikiPropertiesAdditionalProperties",
            String.format("activeinstalls2.pingURL=%s", hostAddress));
        configuration.setProperties(properties);
        // When the Servlet engine is running inside docker, Active Installs (thus running inside docker) will need
        // access the socket of the Elasticsearch server running on the host. Thus, we need to expose the port, see
        // https://www.testcontainers.org/features/networking/#exposing-host-ports-to-the-container
        if (!testConfiguration.getServletEngine().isOutsideDocker()) {
            configuration.setSSHPorts(List.of(esContainer.getMappedPort(9200)));
        }
        globalStore.put(TestConfiguration.class, configuration);
    }

    private static ExtensionContext.Store getXWikiDockerExtensionStore(ExtensionContext context)
    {
        return context.getRoot().getStore(XWIKIDOCKEREXTENSION_NAMESPACE);
    }

    private TestConfiguration loadTestConfiguration(ExtensionContext context)
    {
        ExtensionContext.Store store = getXWikiDockerExtensionStore(context);
        return store.get(TestConfiguration.class, TestConfiguration.class);
    }
}

@@ -1939,351 +1939,8 @@
   %7B%0A
-        // We're able to get the TestConfiguration here because it's saved in the XWIKIDOCKEREXTENSION_NAMESPACE%0A        // context in XWikiDockerExtension#evaluateExecutionCondition() which is executed for all extensions before%0A        // anything else.%0A        TestConfiguration testConfiguration = loadTestConfiguration(extensionContext);%0A%0A
     
@@ -2567,266 +2567,8 @@
 ();%0A
-        // Depending on the Servlet engine used, the host ip is %22localhost%22 (for jetty standalone which doesn't%0A        // execute in a docker container) or %22host.testcontainers.internal%22 (for all Servlet engines executing in a%0A        // docker container).%0A
     
@@ -2626,52 +2626,27 @@
 d%22, 
-t
 es
-t
 Con
-figuration.getServletEngine()
+tainer
 .getHost
 IP()
@@ -2645,25 +2645,11 @@
 Host
-IP
 (),
-%0A           
  esC
@@ -3204,84 +3204,10 @@
 iner
+.
 %0A
-        if (!testConfiguration.getServletEngine().isOutsideDocker()) %7B%0A    
     
@@ -3283,18 +3283,8 @@
 ));%0A
-        %7D%0A
     
@@ -3538,252 +3538,7 @@
    %7D
-%0A%0A    private TestConfiguration loadTestConfiguration(ExtensionContext context)%0A    %7B%0A        ExtensionContext.Store store = getXWikiDockerExtensionStore(context);%0A        return store.get(TestConfiguration.class, TestConfiguration.class);%0A    %7D
 %0A%7D%0A

Replace ActiveInstalls by ActiveInstalls2 in XS * Fix failing test on CI