Skip to content

Commit

Permalink
Merge pull request #8511 from Sanne/ValidatorORMTestBackup
Browse files Browse the repository at this point in the history
Introduce a test for #8323 : Hibernate Validator to be able to use the Hibernate ORM helpers to traverse graphs with uninitialized proxies
  • Loading branch information
Sanne authored Apr 10, 2020
2 parents 885579a + bce3435 commit 020239a
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.it.validator;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@Entity
@Table(name = "Hello")
public class Hello {

@Id
private int id;

@NotEmpty
private String greetingText;

@Valid
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JoinColumn(nullable = false)
private Human greetedHuman;

public int getId() {
return id;
}

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

public String getGreetingText() {
return greetingText;
}

public void setGreetingText(final String greetingText) {
this.greetingText = greetingText;
}

public Human getGreetedHuman() {
return greetedHuman;
}

public void setGreetedHuman(final Human greetedHuman) {
this.greetedHuman = greetedHuman;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.it.validator;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import com.sun.istack.NotNull;

@Entity
@Table(name = "Human")
public class Human {

@Id
private int id;

@NotEmpty
private String name;

@NotNull
@Valid
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "greetedHuman")
private Set<Hello> greetings = new HashSet<>();

public int getId() {
return id;
}

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

public String getName() {
return name;
}

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

public Set<Hello> getGreetings() {
return greetings;
}

public void setGreetings(final Set<Hello> greetings) {
this.greetings = greetings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.it.validator;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.validation.Validator;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
* Verifies that Hibernate Validator can use Hibernate ORM's PersistenceUtilHelper
* to traverse managed object graphs which contain enhanced proxies.
*
* Test for issue https://github.com/quarkusio/quarkus/issues/8323
*/
@Path("/validator-and-orm")
public class TestValidatorAndHibernateORMEndpoint {

@Inject
EntityManager em;

@Inject
Validator validator;

@GET
@Path("/store")
@Transactional
public String storeData() {
//Create hello#1
Hello hello = new Hello();
hello.setId(1);
hello.setGreetingText("hello");
//Create human#3
Human human = new Human();
human.setId(3);
human.setName("Batman");
//create the relation
human.getGreetings().add(hello);
hello.setGreetedHuman(human);
//Store it all
em.persist(human);
return "passed";
}

@GET
@Path("/load")
@Transactional
public String loadData() {
//Load hello#1 - this will lead to have an uninitialized enhanced proxy associated to it
Hello hello = em.find(Hello.class, 1);
//Check that we can still validate the managed object graph, even when we don't know the name of the guy in the mask who we just greeted!
validator.validate(hello);
return "passed";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.main;

import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

@QuarkusTest
public class ValidatorAndOrmTestCase {

@Test
public void triggerValidatorAndHibernateORMTests() {
RestAssured.when().get("/validator-and-orm/store").then()
.body(is("passed"));
RestAssured.when().get("/validator-and-orm/load").then()
.body(is("passed"));
}

}

0 comments on commit 020239a

Please sign in to comment.