From 903c34e63ef3eb1eb34888dfe08bfd6baa3771e4 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Tue, 25 Jul 2023 11:19:08 +0200 Subject: [PATCH] [#1627] Minor refactoring of the test I think we can remove the association of embeddable for the sake of making the test simpler --- .../org/hibernate/reactive/SQLSelectTest.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java index 6526bde45..e7990e486 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SQLSelectTest.java @@ -5,11 +5,9 @@ */ package org.hibernate.reactive; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; - +import java.util.Objects; import org.hibernate.annotations.SQLSelect; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @@ -22,23 +20,21 @@ import org.junit.jupiter.api.extension.RegisterExtension; import io.vertx.junit5.VertxTestContext; -import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; import jakarta.persistence.Id; import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.reactive.SQLSelectTest.Person.SELECT_QUERY; import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor; -import static org.junit.jupiter.api.Assertions.assertNotNull; public class SQLSelectTest extends BaseReactiveTest { @RegisterExtension // We use native queries, which may be different for other DBs public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL ); private SqlStatementTracker sqlTracker; + private Person thePerson; - public static String SQL = "xxxxx"; @Override protected Collection> annotatedEntities() { @@ -63,13 +59,11 @@ protected void addServices(StandardServiceRegistryBuilder builder) { @BeforeEach public void populateDb(VertxTestContext context) { - List phones = Arrays.asList( "999-999-9999", "111-111-1111", "123-456-7890" ); thePerson = new Person(); thePerson.id = 724200; thePerson.name = "Claude"; - thePerson.phones = phones; - test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( thePerson ) ) ); + test( context, getMutinySessionFactory().withTransaction( s -> s.persist( thePerson ) ) ); } @Test @@ -77,19 +71,19 @@ public void findEntity(VertxTestContext context) { test( context, openSession() .thenCompose( session -> session.find( Person.class, thePerson.getId() ) ) .thenAccept( found -> { - assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" ); - assertThat( sqlTracker.getLoggedQueries() ).contains( Person.SELECT_QUERY.replace( "?", "$1" ) ); + assertThat( found ).isEqualTo( thePerson ); + assertThat( sqlTracker.getLoggedQueries() ) + .containsExactly( + "select version()", + SELECT_QUERY.replace( "?", "$1" ) + ); } ) ); } - private static void assertPhones(Person person, String... expectedPhones) { - assertNotNull( person ); - assertThat( person.getPhones() ).containsExactlyInAnyOrder( expectedPhones ); - } @Entity(name = "Person") - @SQLSelect(sql = Person.SELECT_QUERY) + @SQLSelect(sql = SELECT_QUERY) static class Person { // Query containing simple where check to distinguish from a possible generated query public static final String SELECT_QUERY = "SELECT id, name FROM person WHERE id = ? and 'hreact' = 'hreact'"; @@ -99,9 +93,6 @@ static class Person { private String name; - @ElementCollection(fetch = FetchType.EAGER) - private List phones = new ArrayList<>(); - public int getId() { return id; } @@ -118,8 +109,21 @@ public void setName(String name) { this.name = name; } - public List getPhones() { - return phones; + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Person person = (Person) o; + return Objects.equals( name, person.name ); + } + + @Override + public int hashCode() { + return Objects.hash( name ); } } }