<%#
 Copyright 2013-2021 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 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

      https://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 <%= packageName %>;

import static java.nio.file.Files.newDirectoryStream;
import static java.nio.file.Paths.get;
import static java.util.Spliterator.SORTED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;

import tech.jhipster.config.JHipsterConstants;

import com.datastax.oss.driver.api.core.CqlSession;
import org.cassandraunit.CQLDataLoader;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Iterator;
import java.util.Spliterator;

/**
 * Base class for starting/stopping Cassandra during tests.
 */
@ActiveProfiles(JHipsterConstants.SPRING_PROFILE_TEST)
@ContextConfiguration(initializers = AbstractCassandraTest.CassandraPortInitializer.class)
public class AbstractCassandraTest {

    public static final String CASSANDRA_UNIT_KEYSPACE = "cassandra_unit_keyspace";
    public static final GenericContainer<?> CASSANDRA_CONTAINER;
    public static final int CASSANDRA_TEST_PORT = 9042;
    private static boolean started = false;

    static {
        CASSANDRA_CONTAINER =
            new GenericContainer("<%= DOCKER_CASSANDRA %>")
                .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(30)))
                .withExposedPorts(CASSANDRA_TEST_PORT);
    }

    @BeforeAll
    public static void startServer() throws IOException, URISyntaxException {
        if (!started) {
            startTestContainer();

            CqlSession session = CqlSession.builder()
                .addContactPoint(new InetSocketAddress(
                    CASSANDRA_CONTAINER.getContainerIpAddress(),
                    CASSANDRA_CONTAINER.getMappedPort(CASSANDRA_TEST_PORT)))
                .withLocalDatacenter("datacenter1")
                .build();

            createTestKeyspace(session);
            CQLDataLoader dataLoader = new CQLDataLoader(session);
            applyScripts(dataLoader, "config/cql/changelog/", "*.cql");
            started = true;
        }
    }

    private static void startTestContainer() {
        CASSANDRA_CONTAINER.start();
    }

    private static void createTestKeyspace(CqlSession session) {
        String createQuery = "CREATE KEYSPACE " + CASSANDRA_UNIT_KEYSPACE + " WITH replication={'class' : 'SimpleStrategy', 'replication_factor':1}";
        session.execute(createQuery);
        String useKeyspaceQuery = "USE " + CASSANDRA_UNIT_KEYSPACE;
        session.execute(useKeyspaceQuery);
    }

    private static void applyScripts(CQLDataLoader dataLoader, String cqlDir, String pattern) throws IOException, URISyntaxException {
        URL dirUrl = ClassLoader.getSystemResource(cqlDir);
        if (dirUrl == null) { // protect for empty directory
            return;
        }

        Iterator<Path> pathIterator = newDirectoryStream(get(dirUrl.toURI()), pattern).iterator();
        Spliterator<Path> pathSpliterator = spliteratorUnknownSize(pathIterator, SORTED);
        stream(pathSpliterator, false)
            .map(Path::getFileName)
            .map(Path::toString)
            .sorted()
            .map(file -> cqlDir + file)
            .map(dataSetLocation -> new ClassPathCQLDataSet(dataSetLocation, false, false, CASSANDRA_UNIT_KEYSPACE))
            .forEach(dataLoader::load);
    }

    public static class CassandraPortInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        @Override
        public void initialize(ConfigurableApplicationContext applicationContext) {
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(applicationContext,
                "spring.data.cassandra.port=" + AbstractCassandraTest.CASSANDRA_CONTAINER.getMappedPort(AbstractCassandraTest.CASSANDRA_TEST_PORT)
            );
        }
    }
}
