package io.quarkus.hibernate.search.orm.elasticsearch.test.configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.util.common.SearchException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.test.QuarkusUnitTest;

public class ConfigEnabledFalseAndActiveTrueTest {

    @RegisterExtension
    static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
            () -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
            .withConfigurationResource("application.properties")
            .overrideConfigKey("quarkus.hibernate-search-orm.enabled", "false")
            .overrideConfigKey("quarkus.hibernate-search-orm.active", "true")
            .assertException(throwable -> assertThat(throwable)
                    .isInstanceOf(ConfigurationException.class)
                    .hasMessageContaining(
                            "Hibernate Search activated explicitly, but Hibernate Search was disabled at build time",
                            "If you want Hibernate Search to be active at runtime, you must set 'quarkus.hibernate-search-orm.enabled' to 'true' at build time",
                            "If you don't want Hibernate Search to be active at runtime, you must leave 'quarkus.hibernate-search-orm.active' unset or set it to 'false'"));

    @Inject
    SessionFactory sessionFactory;

    @Test
    public void test() {
        assertThatThrownBy(() -> Arc.container().instance(SearchMapping.class).get())
                .isInstanceOf(IllegalStateException.class)
                .hasMessageContainingAll(
                        "Cannot retrieve the SearchMapping for persistence unit <default>",
                        "Hibernate Search was disabled through configuration properties");

        assertThatThrownBy(() -> Search.mapping(sessionFactory))
                .isInstanceOf(SearchException.class)
                .hasMessageContaining("Hibernate Search was not initialized.");

        assertThatThrownBy(() -> Arc.container().instance(SearchSession.class).get())
                .isInstanceOf(IllegalStateException.class)
                .hasMessageContainingAll(
                        "Cannot retrieve the SearchSession for persistence unit <default>",
                        "Hibernate Search was disabled through configuration properties");

        try (Session session = sessionFactory.openSession()) {
            assertThatThrownBy(() -> Search.session(session).search(IndexedEntity.class))
                    .isInstanceOf(SearchException.class)
                    .hasMessageContaining("Hibernate Search was not initialized.");
        }
    }
}

@@ -130,398 +130,8 @@
 at;%0A
-import static org.assertj.core.api.Assertions.assertThatThrownBy;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.hibernate.Session;%0Aimport org.hibernate.SessionFactory;%0Aimport org.hibernate.search.mapper.orm.Search;%0Aimport org.hibernate.search.mapper.orm.mapping.SearchMapping;%0Aimport org.hibernate.search.mapper.orm.session.SearchSession;%0Aimport org.hibernate.search.util.common.SearchException;
 %0Aimp
@@ -319,35 +319,8 @@
 n;%0A%0A
-import io.quarkus.arc.Arc;%0A
 impo
@@ -1521,1330 +1521,68 @@
    @
-Inject%0A    SessionFactory sessionFactory;%0A%0A    @Test%0A    public void test() %7B%0A        assertThatThrownBy(() -%3E Arc.container().instance(SearchMapping.class).get())%0A                .isInstanceOf(IllegalStateException.class)%0A                .hasMessageContainingAll(%0A                        %22Cannot retrieve the SearchMapping for persistence unit %3Cdefault%3E%22,%0A                        %22Hibernate Search was disabled through configuration properties%22);%0A%0A        assertThatThrownBy(() -%3E Search.mapping(sessionFactory))%0A                .isInstanceOf(SearchException.class)%0A                .hasMessageContaining(%22Hibernate Search was not initialized.%22);%0A%0A        assertThatThrownBy(() -%3E Arc.container().instance(SearchSession.class).get())%0A                .isInstanceOf(IllegalStateException.class)%0A                .hasMessageContainingAll(%0A                        %22Cannot retrieve the SearchSession for persistence unit %3Cdefault%3E%22,%0A                        %22Hibernate Search was disabled through configuration properties%22);%0A%0A        try (Session session = sessionFactory.openSession()) %7B%0A            assertThatThrownBy(() -%3E Search.session(session).search(IndexedEntity.class))%0A                    .isInstanceOf(SearchException.class)%0A                    .hasMessageContaining(%22Hibernate Search was not initialized.%22);%0A        %7D
+Test%0A    public void test() %7B%0A        // Startup should fail
 %0A   

Remove dead code from some Hibernate Search test