Skip to content

Commit

Permalink
ORM test
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Sep 22, 2024
1 parent 614c780 commit c505102
Showing 1 changed file with 88 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.JdbcType;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.PostgreSQLJsonPGObjectJsonbType;
import org.hibernate.reactive.annotations.DisabledFor;
import org.hibernate.type.SqlTypes;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.hibernate.reactive.provider.Settings.DIALECT;
import static org.hibernate.reactive.provider.Settings.DRIVER;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Timeout(value = 10, timeUnit = MINUTES)

Expand All @@ -48,9 +53,12 @@ public class ORMReactivePersistenceTest extends BaseReactiveTest {

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Flour.class );
return List.of( Book.class );
}

final Book fakeHistory = new Book( 3, "Fake History", new Book.Author( "Jo", "Hedwig Teeuwisse") );
final Book theBookOfM = new Book( 5, "The Book of M", new Book.Author( "Peng", "Shepherd") );

@BeforeEach
public void prepareOrmFactory() {
Configuration configuration = constructConfiguration();
Expand All @@ -69,94 +77,97 @@ public void closeOrmFactory() {
ormFactory.close();
}

@Test
public void testORMWithStageSession(VertxTestContext context) {
final Flour almond = new Flour( 1, "Almond", "made from ground almonds.", "Gluten free" );

try (Session session = ormFactory.openSession()) {
session.beginTransaction();
session.persist( almond );
session.getTransaction().commit();
}

// Check database with Stage session and verify 'almond' flour exists
test( context, openSession()
.thenCompose( stageSession -> stageSession.find( Flour.class, almond.id ) )
.thenAccept( entityFound -> assertEquals( almond, entityFound ) )
);
}

@Test
public void testORMWitMutinySession(VertxTestContext context) {
final Flour rose = new Flour( 2, "Rose", "made from ground rose pedals.", "Full fragrance" );
try (Session ormSession = ormFactory.openSession()) {
ormSession.beginTransaction();
ormSession.persist( theBookOfM );
ormSession.persist( fakeHistory );
ormSession.getTransaction().commit();
}

try (Session ormSession = ormFactory.openSession()) {
ormSession.beginTransaction();
ormSession.persist( rose );
Book result = ormSession.createNativeQuery( "select * from BookWithJson where id = 3", Book.class )
.getSingleResult();
assertThat( result ).isEqualTo( theBookOfM );
ormSession.getTransaction().commit();
}

// Check database with Mutiny session and verify 'rose' flour exists
test( context, openMutinySession()
.chain( session -> session.find( Flour.class, rose.id ) )
.invoke( foundRose -> assertEquals( rose, foundRose ) )
);
}

@Entity(name = "Flour")
@Table(name = "Flour")
public static class Flour {
@Entity(name = "Book")
@Table(name = "BookWithJson")
public static class Book {

@Id
private Integer id;
private String name;
private String description;
private String type;
Integer id;

public Flour() {
}
String title;

public Flour(Integer id, String name, String description, String type) {
this.id = id;
this.name = name;
this.description = description;
this.type = type;
}
@JdbcTypeCode(SqlTypes.JSON)
@JdbcType(PostgreSQLJsonPGObjectJsonbType.class)
Author author;

public Integer getId() {
return id;
}
@Embeddable
public static class Author {
private String name;
private String surname;

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

public String getName() {
return name;
}
public Author(String name, String surname) {
this.name = name;
this.surname = surname;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String getDescription() {
return description;
}
public void setName(String name) {
this.name = name;
}

public void setDescription(String description) {
this.description = description;
}
public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Book.Author author = (Book.Author) o;
return Objects.equals( name, author.name ) && Objects.equals( surname, author.surname );
}

public String getType() {
return type;
@Override
public int hashCode() {
return Objects.hash( name, surname );
}

@Override
public String toString() {
return name + ' ' + surname;
}
}

public void setType(String type) {
this.type = type;
public Book() {
}

@Override
public String toString() {
return name;
public Book(Integer id, String title, Author author) {
this.id = id;
this.title = title;
this.author = author;
}

@Override
Expand All @@ -167,15 +178,21 @@ public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Flour flour = (Flour) o;
return Objects.equals( name, flour.name ) &&
Objects.equals( description, flour.description ) &&
Objects.equals( type, flour.type );
Book book = (Book) o;
return Objects.equals( id, book.id ) && Objects.equals(
title,
book.title
) && Objects.equals( author, book.author );
}

@Override
public int hashCode() {
return Objects.hash( name, description, type );
return Objects.hash( id, title, author );
}

@Override
public String toString() {
return id + ":" + title + ":" + author;
}
}
}

0 comments on commit c505102

Please sign in to comment.