Skip to content

Commit

Permalink
[quarkusio#28576] Tests for property hibernate-orm.packages with Hibe…
Browse files Browse the repository at this point in the history
…rnate Reactive
  • Loading branch information
DavideD authored and tmihalac committed Oct 27, 2022
1 parent 7ff7fb5 commit ea329c1
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.hibernate.reactive.singlepersistenceunit;

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

import java.util.logging.Formatter;
import java.util.logging.Level;

import javax.inject.Inject;

import org.hibernate.reactive.mutiny.Mutiny;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.excludedpackage.ExcludedEntity;
import io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.packageincludedthroughannotation.EntityIncludedThroughPackageAnnotation;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.vertx.RunOnVertxContext;
import io.quarkus.test.vertx.UniAsserter;
import io.smallrye.mutiny.Uni;

public class SinglePersistenceUnitPackageAnnotationTest {

private static final Formatter LOG_FORMATTER = new PatternFormatter("%s");

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addPackage(EntityIncludedThroughPackageAnnotation.class.getPackage().getName())
.addPackage(ExcludedEntity.class.getPackage().getName()))
.withConfigurationResource("application.properties")
// Expect a warning on startup
.setLogRecordPredicate(
record -> record.getMessage().contains("Could not find a suitable persistence unit for model classes"))
.assertLogRecords(records -> assertThat(records)
.as("Warnings on startup")
.hasSize(1)
.element(0).satisfies(record -> {
assertThat(record.getLevel()).isEqualTo(Level.WARNING);
assertThat(LOG_FORMATTER.formatMessage(record))
.contains(
io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.excludedpackage.ExcludedEntity.class
.getName());
}));

@Inject
Mutiny.SessionFactory sessionFactory;

@Test
@RunOnVertxContext
public void testIncluded(UniAsserter asserter) {
EntityIncludedThroughPackageAnnotation entity = new EntityIncludedThroughPackageAnnotation("default");
asserter.assertThat(
() -> persist(entity).chain(() -> find(EntityIncludedThroughPackageAnnotation.class, entity.id)),
retrievedEntity -> assertThat(retrievedEntity.name).isEqualTo(entity.name));
}

@Test
@RunOnVertxContext
public void testExcluded(UniAsserter asserter) {
ExcludedEntity entity = new ExcludedEntity("gsmet");
asserter.assertFailedWith(() -> persist(entity), t -> {
assertThat(t).hasMessageContaining("Unknown entity");
});
}

private Uni<Void> persist(Object entity) {
return sessionFactory.withTransaction(s -> s.persist(entity));
}

private <T> Uni<T> find(Class<T> entityClass, Object id) {
return sessionFactory.withSession(s -> s.find(entityClass, id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.hibernate.reactive.singlepersistenceunit;

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

import java.util.logging.Formatter;
import java.util.logging.Level;

import javax.inject.Inject;

import org.hibernate.reactive.mutiny.Mutiny;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.excludedpackage.ExcludedEntity;
import io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.packageincludedthroughconfig.EntityIncludedThroughPackageConfig;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.vertx.RunOnVertxContext;
import io.quarkus.test.vertx.UniAsserter;
import io.smallrye.mutiny.Uni;

public class SinglePersistenceUnitPackageConfigurationTest {

private static final Formatter LOG_FORMATTER = new PatternFormatter("%s");

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addPackage(EntityIncludedThroughPackageConfig.class.getPackage().getName())
.addPackage(ExcludedEntity.class.getPackage().getName()))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-orm.packages",
EntityIncludedThroughPackageConfig.class.getPackage().getName())
// Expect a warning on startup
.setLogRecordPredicate(
record -> record.getMessage().contains("Could not find a suitable persistence unit for model classes"))
.assertLogRecords(records -> assertThat(records)
.as("Warnings on startup")
.hasSize(1)
.element(0).satisfies(record -> {
assertThat(record.getLevel()).isEqualTo(Level.WARNING);
assertThat(LOG_FORMATTER.formatMessage(record))
.contains(ExcludedEntity.class.getName());
}));

@Inject
Mutiny.SessionFactory sessionFactory;

@Test
@RunOnVertxContext
public void testIncluded(UniAsserter asserter) {
EntityIncludedThroughPackageConfig entity = new EntityIncludedThroughPackageConfig("default");
asserter.assertThat(
() -> persist(entity).chain(() -> find(EntityIncludedThroughPackageConfig.class, entity.id)),
retrievedEntity -> assertThat(retrievedEntity.name).isEqualTo(entity.name));
}

@Test
@RunOnVertxContext
public void testExcluded(UniAsserter asserter) {
ExcludedEntity entity = new ExcludedEntity("gsmet");
asserter.assertFailedWith(() -> persist(entity), t -> {
assertThat(t).hasMessageContaining("Unknown entity");
});
}

private Uni<Void> persist(Object entity) {
return sessionFactory.withTransaction(s -> s.persist(entity));
}

private <T> Uni<T> find(Class<T> entityClass, Object id) {
return sessionFactory.withSession(s -> s.find(entityClass, id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.excludedpackage;

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

@Entity
public class ExcludedEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "excludedSeq")
public long id;

public String name;

public ExcludedEntity() {
}

public ExcludedEntity(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.packageincludedthroughannotation;

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

@Entity
public class EntityIncludedThroughPackageAnnotation {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "includedSeq")
public long id;

public String name;

public EntityIncludedThroughPackageAnnotation() {
}

public EntityIncludedThroughPackageAnnotation(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PersistenceUnit(PersistenceUnit.DEFAULT)
package io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.packageincludedthroughannotation;

import io.quarkus.hibernate.orm.PersistenceUnit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.hibernate.reactive.singlepersistenceunit.entityassignment.packageincludedthroughconfig;

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

@Entity
public class EntityIncludedThroughPackageConfig {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "includedSeq")
public long id;

public String name;

public EntityIncludedThroughPackageConfig() {
}

public EntityIncludedThroughPackageConfig(String name) {
this.name = name;
}

}

0 comments on commit ea329c1

Please sign in to comment.