Skip to content

Hibernate Validator integration test #1919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,75 @@
/* 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 Long id;

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

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

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

public Author() {
}

public Long getId() {
return id;
}

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

public @NotNull @Size(max = 10) String getName() {
return name;
}

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

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

public void setBooks(List<Book> books) {
this.books = books;
}

@Override
public String toString() {
return id + ":" + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* 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.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
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)
private Integer id;

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

@NotNull
@ManyToOne
private Author author;

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

public Book() {
}

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

public Integer getId() {
return id;
}

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

public @NotNull @Size(max = 10) String getTitle() {
return title;
}

public void setTitle(@NotNull @Size(max = 10) String title) {
this.title = title;
}

public @NotNull Author getAuthor() {
return author;
}

public void setAuthor(@NotNull Author author) {
this.author = author;
}

public long getIsbn() {
return isbn;
}

public void setIsbn(long isbn) {
this.isbn = isbn;
}

@Override
public String toString() {
return id + ":" + title + ":" + isbn + ":" + author;
}
}
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( "-", "" ) );

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
}
}
}
Loading
Loading