Skip to content

Commit

Permalink
Merge pull request #21161 from yrodiere/hsearch-no-failure-when-disabled
Browse files Browse the repository at this point in the history
Allow disabling Hibernate Search through configuration properties
  • Loading branch information
yrodiere authored Nov 3, 2021
2 parents 5cf5685 + f146e82 commit 879c4fb
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.hibernate.orm.deployment.PersistenceUnitDescriptorBuildItem;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationRuntimeConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.HibernateSearchIntegrationRuntimeConfiguredBuildItem;
import io.quarkus.hibernate.search.orm.elasticsearch.aws.runtime.HibernateSearchOrmElasticsearchAwsRecorder;
import io.quarkus.hibernate.search.orm.elasticsearch.aws.runtime.HibernateSearchOrmElasticsearchAwsRuntimeConfig;

Expand All @@ -19,14 +19,13 @@ class HibernateSearchOrmElasticsearchAwsProcessor {
@Record(ExecutionTime.RUNTIME_INIT)
void setRuntimeConfig(HibernateSearchOrmElasticsearchAwsRecorder recorder,
HibernateSearchOrmElasticsearchAwsRuntimeConfig runtimeConfig,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
BuildProducer<HibernateOrmIntegrationRuntimeConfiguredBuildItem> runtimeConfigured) {
for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem(
HIBERNATE_SEARCH_ORM_ELASTICSEARCH_AWS,
puDescriptor.getPersistenceUnitName())
.setInitListener(
recorder.createRuntimeInitListener(runtimeConfig, puDescriptor.getPersistenceUnitName())));
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
BuildProducer<HibernateSearchIntegrationRuntimeConfiguredBuildItem> runtimeConfigured) {
for (HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem configuredPersistenceUnit : configuredPersistenceUnits) {
String puName = configuredPersistenceUnit.getPersistenceUnitName();
runtimeConfigured.produce(new HibernateSearchIntegrationRuntimeConfiguredBuildItem(
HIBERNATE_SEARCH_ORM_ELASTICSEARCH_AWS, puName,
recorder.createRuntimeInitListener(runtimeConfig, puName)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ public class MissingRegionSigningDisabledTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class)
.addAsResource("application-missing-region-signing-disabled.properties", "application.properties"));
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.elasticsearch.aws.signing.enabled", "false");

@Test
public void testNoConfig() {
// Just check there are no exceptions on startup, on contrary to MissingRegionTest
// Just check there are no exceptions on startup, on contrary to MissingRegionSigningEnabledTest
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.hibernate.search.elasticsearch.aws.test.configuration;

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.test.QuarkusUnitTest;

public class MissingRegionSigningEnabledHibernateSearchDisabledTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.elasticsearch.aws.signing.enabled", "true")
.overrideConfigKey("quarkus.hibernate-search-orm.enabled", "false");

@Test
public void testNoConfig() {
// Just check there are no exceptions on startup, on contrary to MissingRegionSigningEnabledTest
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.hibernate.search.elasticsearch.aws.test.configuration;

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.test.QuarkusUnitTest;

public class MissingRegionSigningEnabledNoIndexedEntityTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(NonIndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.elasticsearch.aws.signing.enabled", "true");

@Test
public void testNoConfig() {
// Just check there are no exceptions on startup, on contrary to MissingRegionSigningEnabledTest
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public class MissingRegionSigningEnabledTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class)
.addAsResource("application-missing-region-signing-enabled.properties", "application.properties"))
() -> ShrinkWrap.create(JavaArchive.class).addClass(IndexedEntity.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-search-orm.elasticsearch.aws.signing.enabled", "true")
.assertException(throwable -> assertThat(throwable)
.isInstanceOf(RuntimeException.class)
.hasMessageContainingAll(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.search.elasticsearch.aws.test.configuration;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* A non-indexed entity.
*/
@Entity
public class NonIndexedEntity {

@Id
@GeneratedValue
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=sync
# Use integration tests if you really need to communicate with Elasticsearch.
quarkus.hibernate-search-orm.schema-management.strategy=none
quarkus.hibernate-search-orm.elasticsearch.version-check.enabled=false

quarkus.hibernate-search-orm.elasticsearch.aws.signing.enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ void registerBeans(List<HibernateSearchElasticsearchPersistenceUnitConfiguredBui
void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder,
HibernateSearchElasticsearchRuntimeConfig runtimeConfig,
List<HibernateSearchIntegrationRuntimeConfiguredBuildItem> integrationRuntimeConfigBuildItems,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
List<HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem> configuredPersistenceUnits,
BuildProducer<HibernateOrmIntegrationRuntimeConfiguredBuildItem> runtimeConfigured) {
for (PersistenceUnitDescriptorBuildItem puDescriptor : persistenceUnitDescriptorBuildItems) {
String puName = puDescriptor.getPersistenceUnitName();
for (HibernateSearchElasticsearchPersistenceUnitConfiguredBuildItem configuredPersistenceUnit : configuredPersistenceUnits) {
String puName = configuredPersistenceUnit.getPersistenceUnitName();
List<HibernateOrmIntegrationRuntimeInitListener> integrationRuntimeInitListeners = new ArrayList<>();
for (HibernateSearchIntegrationRuntimeConfiguredBuildItem item : integrationRuntimeConfigBuildItems) {
if (item.getPersistenceUnitName().equals(puName)) {
Expand All @@ -156,7 +156,7 @@ void setRuntimeConfig(HibernateSearchElasticsearchRecorder recorder,
runtimeConfigured.produce(
new HibernateOrmIntegrationRuntimeConfiguredBuildItem(HIBERNATE_SEARCH_ELASTICSEARCH, puName)
.setInitListener(
recorder.createRuntimeInitListener(runtimeConfig, puDescriptor.getPersistenceUnitName(),
recorder.createRuntimeInitListener(runtimeConfig, puName,
integrationRuntimeInitListeners)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeInitListener;

public class HibernateSearchIntegrationRuntimeConfiguredBuildItem extends MultiBuildItem {
public final class HibernateSearchIntegrationRuntimeConfiguredBuildItem extends MultiBuildItem {
private final String integrationName;
private final String persistenceUnitName;
private final HibernateOrmIntegrationRuntimeInitListener initListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.hibernate.search.elasticsearch.test.configuration;

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

import javax.inject.Inject;

import org.hibernate.SessionFactory;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
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.test.QuarkusUnitTest;

public class ConfigDisabledAndIndexedEntityTest {

@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");

@Inject
SessionFactory sessionFactory;

@Test
public void testDisabled() {
assertThatThrownBy(() -> Arc.container().instance(SearchMapping.class).get())
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized");

assertThatThrownBy(() -> Search.mapping(sessionFactory))
.isInstanceOf(SearchException.class)
.hasMessageContaining("Hibernate Search was not initialized");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ private HibernateSearchIntegrationRuntimeInitListener(

@Override
public void contributeRuntimeProperties(BiConsumer<String, Object> propertyCollector) {
if (!runtimeConfig.enabled) {
addConfig(propertyCollector, HibernateOrmMapperSettings.ENABLED, false);
// Do not process other properties: Hibernate Search is disabled anyway.
return;
}

addConfig(propertyCollector,
HibernateOrmMapperSettings.SCHEMA_MANAGEMENT_STRATEGY,
runtimeConfig.schemaManagement.strategy);
Expand All @@ -213,6 +219,10 @@ public void contributeRuntimeProperties(BiConsumer<String, Object> propertyColle
.entrySet()) {
contributeBackendRuntimeProperties(propertyCollector, backendEntry.getKey(), backendEntry.getValue());
}

for (HibernateOrmIntegrationRuntimeInitListener integrationRuntimeInitListener : integrationRuntimeInitListeners) {
integrationRuntimeInitListener.contributeRuntimeProperties(propertyCollector);
}
}

private void contributeBackendRuntimeProperties(BiConsumer<String, Object> propertyCollector, String backendName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
@ConfigGroup
public class HibernateSearchElasticsearchRuntimeConfigPersistenceUnit {

/**
* Whether Hibernate Search is enabled.
*/
@ConfigItem(defaultValue = "true")
boolean enabled;

/**
* Default backend
*/
Expand Down

0 comments on commit 879c4fb

Please sign in to comment.