Skip to content

Commit

Permalink
Test that extensions work correctly without runtime config
Browse files Browse the repository at this point in the history
No NPE in particular; see for example
quarkusio#23422
  • Loading branch information
yrodiere committed Aug 26, 2022
1 parent 538835a commit d0de2a3
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.hibernate.orm.config.namedpu;

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

import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.Bindable;

import io.quarkus.hibernate.orm.PersistenceUnit.PersistenceUnitLiteral;
import io.quarkus.hibernate.orm.runtime.HibernateOrmRuntimeConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;

/**
* Test that Hibernate ORM is able to start even when not providing any runtime property
*/
public class NoRuntimeConfigNamedPUTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addPackage(MyEntity.class.getPackage().getName()))
.overrideConfigKey("quarkus.datasource.\"ds-1\".db-kind", "h2")
.overrideConfigKey("quarkus.datasource.\"ds-1\".jdbc.url", "jdbc:h2:mem:test")
// For Hibernate ORM, provide only build-time config; rely on defaults for everything else.
.overrideConfigKey("quarkus.hibernate-orm.\"pu-1\".datasource", "ds-1")
// Disable dev services, since they could interfere with runtime config (in particular the schema generation settings).
.overrideConfigKey("quarkus.devservices.enabled", "false");

@Test
public void test() {
// Check that runtime config for our PU is indeed missing.
HibernateOrmRuntimeConfig config = Arc.container().instance(HibernateOrmRuntimeConfig.class).get();
assertThat(config.persistenceUnits).isNullOrEmpty();
// Test that Hibernate was started correctly regardless.
EntityManagerFactory entityManagerFactory = Arc.container()
.instance(EntityManagerFactory.class, new PersistenceUnitLiteral("pu-1")).get();
assertThat(entityManagerFactory).isNotNull();
assertThat(entityManagerFactory.getMetamodel().getEntities())
.extracting(Bindable::getBindableJavaType)
.containsExactlyInAnyOrder((Class) MyEntity.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.hibernate.search.orm.elasticsearch.test.configuration;

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

import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.Bindable;

import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchRuntimeConfig;
import org.hibernate.search.mapper.orm.entity.SearchIndexedEntity;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.hibernate.orm.PersistenceUnit.PersistenceUnitLiteral;
import io.quarkus.test.QuarkusUnitTest;

/**
* Test that Hibernate Search is able to start even when not providing any runtime property.
*/
@Disabled("The (build-time) 'version' property is not detected for some reason; I suspect a bug in Quarkus Config when there are two levels of maps in the config.")
public class NoRuntimeConfigNamedPUNamedBackendTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClass(IndexedEntityInNamedBackend.class))
.overrideConfigKey("quarkus.datasource.\"ds-1\".db-kind", "h2")
.overrideConfigKey("quarkus.datasource.\"ds-1\".jdbc.url", "jdbc:h2:mem:test")
.overrideConfigKey("quarkus.hibernate-orm.\"pu-1\".datasource", "ds-1")
.overrideConfigKey("quarkus.hibernate-orm.\"pu-1\".packages",
IndexedEntityInNamedBackend.class.getPackage().getName())
// For Hibernate Search, provide only build-time config; rely on defaults for everything else.
.overrideConfigKey("quarkus.hibernate-search-orm.\"pu-1\".elasticsearch.\"mybackend\".version", "7")
// Disable dev services, since they could interfere with runtime config (in particular the schema management settings).
.overrideConfigKey("quarkus.devservices.enabled", "false")
// Make runtime config accessible through CDI
.overrideConfigKey("quarkus.arc.unremovable-types", HibernateSearchElasticsearchRuntimeConfig.class.getName());

@Test
public void test() {
// Check that runtime config for our PU is indeed missing.
HibernateSearchElasticsearchRuntimeConfig config = Arc.container()
.instance(HibernateSearchElasticsearchRuntimeConfig.class).get();
assertThat(config.persistenceUnits).isNullOrEmpty();
// Test that Hibernate was started correctly regardless.
SearchMapping mapping = Arc.container()
.instance(SearchMapping.class, new PersistenceUnitLiteral("pu-1")).get();
assertThat(mapping).isNotNull();
assertThat(mapping.allIndexedEntities())
.extracting(SearchIndexedEntity::javaClass)
.containsExactlyInAnyOrder((Class) IndexedEntityInNamedBackend.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.hibernate.search.orm.elasticsearch.test.configuration;

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

import org.hibernate.search.mapper.orm.entity.SearchIndexedEntity;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.hibernate.orm.PersistenceUnit.PersistenceUnitLiteral;
import io.quarkus.hibernate.search.orm.elasticsearch.runtime.HibernateSearchElasticsearchRuntimeConfig;
import io.quarkus.test.QuarkusUnitTest;

/**
* Test that Hibernate Search is able to start even when not providing any runtime property.
*/
public class NoRuntimeConfigNamedPUTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClass(IndexedEntity.class))
.overrideConfigKey("quarkus.datasource.\"ds-1\".db-kind", "h2")
.overrideConfigKey("quarkus.datasource.\"ds-1\".jdbc.url", "jdbc:h2:mem:test")
.overrideConfigKey("quarkus.hibernate-orm.\"pu-1\".datasource", "ds-1")
.overrideConfigKey("quarkus.hibernate-orm.\"pu-1\".packages",
IndexedEntity.class.getPackage().getName())
// For Hibernate Search, provide only build-time config; rely on defaults for everything else.
.overrideConfigKey("quarkus.hibernate-search-orm.\"pu-1\".elasticsearch.version", "7")
// Disable dev services, since they could interfere with runtime config (in particular the schema management settings).
.overrideConfigKey("quarkus.devservices.enabled", "false")
// Make runtime config accessible through CDI
.overrideConfigKey("quarkus.arc.unremovable-types", HibernateSearchElasticsearchRuntimeConfig.class.getName());

@Test
public void test() {
// Check that runtime config for our PU is indeed missing.
HibernateSearchElasticsearchRuntimeConfig config = Arc.container()
.instance(HibernateSearchElasticsearchRuntimeConfig.class).get();
assertThat(config.persistenceUnits).isNullOrEmpty();
// Test that Hibernate was started correctly regardless.
SearchMapping mapping = Arc.container()
.instance(SearchMapping.class, new PersistenceUnitLiteral("pu-1")).get();
assertThat(mapping).isNotNull();
assertThat(mapping.allIndexedEntities())
.extracting(SearchIndexedEntity::javaClass)
.containsExactlyInAnyOrder((Class) IndexedEntity.class);
}
}

0 comments on commit d0de2a3

Please sign in to comment.