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 9b1b18a commit b374624
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 0 deletions.
119 changes: 119 additions & 0 deletions integration-tests/hibernate-validator-postgres-it/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
buildscript {
repositories {
// Example: ./gradlew build -PenableMavenLocalRepo
if ( project.hasProperty( 'enableMavenLocalRepo' ) ) {
// Useful for local development, it should be disabled otherwise
mavenLocal()
}
mavenCentral()
}
}

plugins {
id "org.hibernate.orm" version "${hibernateOrmGradlePluginVersion}"
}

description = 'Quarkus QE integration tests'

ext {
log4jVersion = '2.20.0'
assertjVersion = '3.24.2'
}

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}"

// Testing on one database should be enough
runtimeOnly "io.vertx:vertx-pg-client:${vertxSqlClientVersion}"

// Allow authentication to PostgreSQL using SCRAM:
runtimeOnly 'com.ongres.scram:client:2.1'

// logging
runtimeOnly "org.apache.logging.log4j:log4j-core:${log4jVersion}"

// Testcontainers
testImplementation "org.testcontainers:postgresql:${testcontainersVersion}"

// Testing
testImplementation "org.assertj:assertj-core:${assertjVersion}"
testImplementation "io.vertx:vertx-junit5:${vertxSqlClientVersion}"
}

// Optional: enable the bytecode enhancements
//hibernate { enhancement }

// Print a summary of the results of the tests (number of failures, successes and skipped)
// This is the same as the one in hibernate-reactive-core
def loggingSummary(db, result, desc) {
if ( !desc.parent ) { // will match the outermost suite
def output = "${db} results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
def repeatLength = output.length() + 1
logger.lifecycle '\n' + ('-' * repeatLength) + '\n' + output + '\n' + ('-' * repeatLength)
}
}

// Example:
// gradle test -Pdb=MySQL
test {
def selectedDb = project.hasProperty( 'db' )
? project.properties['db']
: 'PostgreSQL'
doFirst {
systemProperty 'db', selectedDb
}
afterSuite { desc, result ->
loggingSummary( selectedDb, result, desc )
}
}

// Configuration for the tests
tasks.withType( Test ).configureEach {
defaultCharacterEncoding = "UTF-8"
useJUnitPlatform()
testLogging {
displayGranularity 1
showStandardStreams = project.hasProperty('showStandardOutput')
showStackTraces = true
exceptionFormat = 'full'
events 'PASSED', 'FAILED', 'SKIPPED'
}
systemProperty 'docker', project.hasProperty( 'docker' ) ? 'true' : 'false'
systemProperty 'org.hibernate.reactive.common.InternalStateAssertions.ENFORCE', 'true'

if ( project.hasProperty( 'includeTests' ) ) {
// Example: ./gradlew testAll -PincludeTests=DefaultPortTest
filter {
includeTestsMatching project.properties['includeTests'] ?: '*' as String
}
}
}


// Rule to recognize calls to testDb<dbName>
// and run the tests on the selected db
// Example:
// gradle testDbMySQL testDbDB2
tasks.addRule( "Pattern testDb<id>" ) { String taskName ->
if ( taskName.startsWith( "testDb" ) ) {
tasks.register( taskName, Test ) {
def dbName = taskName.substring( "testDb".length() )
description = "Run tests for ${dbName}"

// We only want to test this on Postgres
onlyIf { dbName.toLowerCase().startsWith( 'p' ) }
doFirst() {
systemProperty 'db', dbName
}
afterSuite { desc, result ->
loggingSummary( dbName, result, desc )
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.it.quarkus.qe.database;

import static jakarta.persistence.CascadeType.PERSIST;

import java.util.ArrayList;
import java.util.List;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;


@Entity
@Table(name = "authors")
public class Author {

@Id
@GeneratedValue
private Integer id;

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

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

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

public Author() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = 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
@@ -0,0 +1,66 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.it.quarkus.qe.database;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;


@Entity
@Table(name = "books")
@NamedQuery(name = "find_by_title_prefix", query = "from Book where title like :prefix")
public class Book {

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

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

@NotNull
private Integer author;

@Convert(converter = ISBNConverter.class)
private long isbn;

public Book() {
}

public Book(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public void setAuthor(Integer author) {
this.author = author;
}

public long getIsbn() {
return isbn;
}

public void setIsbn(long isbn) {
this.isbn = isbn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.it.quarkus.qe.database;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter
public class ISBNConverter implements AttributeConverter<Long, String> {

private static final int ISBN_MAX_LENGTH = 13;

@Override
/**
* Canonical ISBN format: 978-3-16-148410-0, can be prefixed with zeroes if there is less than 13 digits
*/
public String convertToDatabaseColumn(Long number) {
String s = number.toString();
StringBuilder result = new StringBuilder( s );
if ( s.length() > ISBN_MAX_LENGTH ) {
throw new IllegalStateException( "ISBN " + s + " has a wrong length: " + number );
}
int paddingLength = ISBN_MAX_LENGTH - s.length();
result.insert( 0, "0".repeat( paddingLength ) );
result.insert( 3, '-' );
result.insert( 5, '-' );
result.insert( 8, '-' );
result.insert( 15, '-' );
return result.toString();
}

@Override
public Long convertToEntityAttribute(String s) {
if ( s == null ) {
return 0L;
}
else {
return Long.parseLong( s.replaceAll( "-", "" ) );
}
}
}
Loading

0 comments on commit b374624

Please sign in to comment.