Skip to content

Commit

Permalink
[hibernate#1907] Hibernate Validator integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed May 3, 2024
1 parent 226c368 commit 354b8a2
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ext {
dependencies {
implementation project(':hibernate-reactive-core')
implementation "org.hibernate.validator:hibernate-validator:8.0.1.Final"
runtimeOnly 'org.glassfish.expressly:expressly:5.0.0'

// JPA metamodel generation for criteria queries (optional)
annotationProcessor "org.hibernate.orm:hibernate-jpamodelgen:${hibernateOrmVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.GenericGenerator;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -27,20 +25,17 @@
@Entity
@Table(name = "authors")
public class Author {
private static final int MAX_NAME_LENGTH = 1;

@Id
@Column(name = "id")
@GeneratedValue(generator = "authorIds")
@GenericGenerator(name = "authorIds", strategy = "org.hibernate.reactive.it.quarkus.qe.database.AuthorIdGenerator")
@GeneratedValue
private Integer id;

@NotNull
@Size(max = MAX_NAME_LENGTH)
@Size(max = 10)
private String name;

@JoinColumn(name = "author")
@OneToMany(cascade = PERSIST, fetch = FetchType.EAGER)
@OneToMany(cascade = PERSIST)
private List<Book> books = new ArrayList<>();

public Author(String name) {
Expand All @@ -50,19 +45,27 @@ public Author(String name) {
public Author() {
}

public String getName() {
return name;
public Integer getId() {
return id;
}

public void setName(String name) {
this.name = name;
public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
public @NotNull @Size(max = 1) String getName() {
return name;
}

public void setName(@NotNull @Size(max = 1) String name) {
this.name = name;
}

public List<Book> getBooks() {
return books;
}

public void setBooks(List<Book> books) {
this.books = books;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
@Table(name = "books")
@NamedQuery(name = "find_by_title_prefix", query = "from Book where title like :prefix")
public class Book {
private static final int MAX_TITLE_LENGTH = 1;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;

@NotNull
@Size(max = MAX_TITLE_LENGTH)
@Size(max = 10)
private String title;

@NotNull
Expand Down Expand Up @@ -57,11 +56,11 @@ public void setAuthor(Integer author) {
this.author = author;
}

public long getISBN() {
public long getIsbn() {
return isbn;
}

public void setISBN(long isbn) {
public void setIsbn(long isbn) {
this.isbn = isbn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,34 @@
*/
package org.hibernate.reactive.it.quarkus.qe.database;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

import org.hibernate.reactive.mutiny.Mutiny;

import org.junit.jupiter.api.Test;

import io.smallrye.mutiny.Uni;
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.validation.ConstraintViolationException;

import static jakarta.persistence.Persistence.createEntityManagerFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;

@Timeout(value = 10, timeUnit = TimeUnit.MINUTES)
public class DatabaseHibernateReactiveTest {
public class DatabaseHibernateReactiveTest extends BaseReactiveIT {

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

@Override
protected CompletionStage<Void> cleanDb() {
return voidFuture();
}

/**
* Assert that the expected exception type is thrown.
Expand All @@ -32,17 +46,14 @@ public static <U extends Throwable> Uni<U> assertThrown(Class<U> expectedExcepti
} );
}

Mutiny.SessionFactory getMutinySessionFactory() {
return createEntityManagerFactory( "postgresql-example" )
.unwrap( Mutiny.SessionFactory.class );
}

@Test
public void validateBookName() {
getMutinySessionFactory().withTransaction( s -> {
public void nameIsTooLong(VertxTestContext context) {
test( context, assertThrown( ConstraintViolationException.class,
getMutinySessionFactory().withTransaction( s -> {
Author author = new Author();
author.setName( "Subrahmanyakavi" );
author.setName( "A very long long long long name ... " );
return s.persist( author );
} ).await().indefinitely();
} )
) );
}
}

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ include 'release'
include 'bytecode-enhancements-it'
include 'verticle-postgres-it'
include 'techempower-postgres-it'
include 'quarkus-qe-postgres-it'
include 'hibernate-validator-postgres-it'

// Examples
for ( project in rootProject.children ) {
Expand Down

0 comments on commit 354b8a2

Please sign in to comment.