Skip to content

Commit

Permalink
[hibernate#1952] Tests clean up
Browse files Browse the repository at this point in the history
Some refactoring of existing tests and I made some improvements to
OrphanRemovalTest
  • Loading branch information
DavideD committed Jul 12, 2024
1 parent d64f1e1 commit b09e201
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ protected void addEntities(Configuration configuration) {
}
}

/**
* This method works for most common cases, but some tests might need to overrides it
*/
public CompletionStage<Void> deleteEntities(Class<?>... entities) {
return getSessionFactory()
.withTransaction( s -> loop( entities, entityClass -> s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ private static String nameFromResult(List<String> rowSet) {
}

private CompletionStage<Double> selectWeightFromId(Integer id) {
return getSessionFactory().withSession(
session -> session.createSelectionQuery("SELECT weight FROM GuineaPig WHERE id = " + id, Double.class )
.getResultList()
.thenApply( CompositeIdTest::weightFromResult )
return getSessionFactory().withSession( session -> session
.createSelectionQuery( "SELECT weight FROM GuineaPig WHERE id = " + id, Double.class )
.getResultList()
.thenApply( CompositeIdTest::weightFromResult )
);
}

Expand All @@ -79,93 +79,85 @@ private static Double weightFromResult(List<Double> rowSet) {
case 0:
return null;
case 1:
return rowSet.get(0);
return rowSet.get( 0 );
default:
throw new AssertionError("More than one result returned: " + rowSet.size());
throw new AssertionError( "More than one result returned: " + rowSet.size() );
}
}

@Test
public void reactiveFind(VertxTestContext context) {
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
test(
context,
populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session -> session.find( GuineaPig.class, new Pig(5, "Aloi") ) )
.thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) )
test( context, populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session -> session.find( GuineaPig.class, new Pig( 5, "Aloi" ) ) )
.thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) )
);
}

@Test
public void reactivePersist(VertxTestContext context) {
test(
context,
openSession()
.thenCompose( s -> s.persist( new GuineaPig( 10, "Tulip" ) )
.thenCompose( v -> s.flush() )
)
.thenCompose( v -> selectNameFromId( 10 ) )
.thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) )
test( context, openSession()
.thenCompose( s -> s
.persist( new GuineaPig( 10, "Tulip" ) )
.thenCompose( v -> s.flush() )
)
.thenCompose( v -> selectNameFromId( 10 ) )
.thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) )
);
}

@Test
public void reactiveRemoveTransientEntity(VertxTestContext context) {
test(
context,
populateDB()
.thenCompose( v -> selectNameFromId( 5 ) )
.thenAccept( Assertions::assertNotNull )
.thenCompose( v -> openSession() )
.thenCompose( session -> session.remove( new GuineaPig( 5, "Aloi" ) )
.thenCompose( v -> session.flush() )
.thenCompose( v -> session.close() )
)
.thenCompose( v -> selectNameFromId( 5 ) )
.thenAccept( Assertions::assertNull )
.handle( (r, e) -> {
assertNotNull( e );
return r;
} )
test( context, populateDB()
.thenCompose( v -> selectNameFromId( 5 ) )
.thenAccept( Assertions::assertNotNull )
.thenCompose( v -> openSession() )
.thenCompose( session -> session
.remove( new GuineaPig( 5, "Aloi" ) )
.thenCompose( v -> session.flush() )
.thenCompose( v -> session.close() )
)
.thenCompose( v -> selectNameFromId( 5 ) )
.thenAccept( Assertions::assertNull )
.handle( (r, e) -> {
assertNotNull( e );
return r;
} )
);
}

@Test
public void reactiveRemoveManagedEntity(VertxTestContext context) {
test(
context,
populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session ->
session.find( GuineaPig.class, new Pig(5, "Aloi") )
.thenCompose( session::remove )
.thenCompose( v -> session.flush() )
.thenCompose( v -> selectNameFromId( session,5 ) )
.thenAccept( Assertions::assertNull )
)
test( context, populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session -> session
.find( GuineaPig.class, new Pig( 5, "Aloi" ) )
.thenCompose( session::remove )
.thenCompose( v -> session.flush() )
.thenCompose( v -> selectNameFromId( session, 5 ) )
.thenAccept( Assertions::assertNull )
)
);
}

@Test
public void reactiveUpdate(VertxTestContext context) {
final double NEW_WEIGHT = 200.0;
test(
context,
populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session ->
session.find( GuineaPig.class, new Pig(5, "Aloi") )
.thenAccept( pig -> {
assertNotNull( pig );
// Checking we are actually changing the name
assertNotEquals( pig.getWeight(), NEW_WEIGHT );
pig.setWeight( NEW_WEIGHT );
} )
.thenCompose( v -> session.flush() )
.thenCompose( v -> session.close() )
.thenCompose( v -> selectWeightFromId( 5 ) )
.thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) )
test( context, populateDB()
.thenCompose( v -> openSession() )
.thenCompose( session -> session
.find( GuineaPig.class, new Pig( 5, "Aloi" ) )
.thenAccept( pig -> {
assertNotNull( pig );
// Checking we are actually changing the name
assertNotEquals( pig.getWeight(), NEW_WEIGHT );
pig.setWeight( NEW_WEIGHT );
} )
.thenCompose( v -> session.flush() )
.thenCompose( v -> session.close() )
.thenCompose( v -> selectWeightFromId( 5 ) )
.thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) )
);
}

Expand All @@ -185,7 +177,8 @@ public Pig(Integer id, String name) {
this.name = name;
}

Pig() {}
Pig() {
}

public Integer getId() {
return id;
Expand All @@ -197,25 +190,31 @@ public String getName() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Pig pig = (Pig) o;
return id.equals(pig.id) &&
name.equals(pig.name);
return id.equals( pig.id ) &&
name.equals( pig.name );
}

@Override
public int hashCode() {
return Objects.hash(id, name);
return Objects.hash( id, name );
}
}

@Entity(name="GuineaPig")
@Table(name="Pig")
@Entity(name = "GuineaPig")
@Table(name = "Pig")
@IdClass(Pig.class)
public static class GuineaPig implements Serializable {
@Id private Integer id;
@Id private String name;
@Id
private Integer id;
@Id
private String name;

private double weight = 100.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ public void testPersistWithReference(VertxTestContext context) {
Bar bar = new Bar( "uniquePersist" );
test( context, getSessionFactory()
.withTransaction( session -> session.persist( bar ) )
.thenCompose( i -> getSessionFactory()
.withTransaction( session -> {
Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) );
return session.persist( foo ).thenApply( v -> foo );
} ) )
.thenCompose( i -> getSessionFactory().withTransaction( session -> {
Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) );
return session.persist( foo ).thenApply( v -> foo );
} ) )
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() ) ) )
.thenAccept( b -> assertEquals( "uniquePersist", b.getKey() ) )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletionStage;

import org.hibernate.reactive.annotations.DisabledFor;
import org.hibernate.reactive.util.impl.CompletionStages;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -50,7 +52,7 @@ protected Collection<Class<?>> annotatedEntities() {
@BeforeEach
public void populateDb(VertxTestContext context) {
theCampaign = new Campaign();
theCampaign.setSchedule( new ExecutionDate(OffsetDateTime.now(), "ALPHA") );
theCampaign.setSchedule( new ExecutionDate( OffsetDateTime.now(), "ALPHA" ) );

test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( theCampaign ) ) );
}
Expand All @@ -70,6 +72,11 @@ public void testUpdateScheduleChange(VertxTestContext context) {
);
}

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

@Test
public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) {
test( context, getMutinySessionFactory()
Expand All @@ -94,13 +101,14 @@ public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) {
);
}

@Entity (name="Campaign")
@Entity(name = "Campaign")
public static class Campaign implements Serializable {

@Id @GeneratedValue
@Id
@GeneratedValue
private Integer id;

@OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
public Schedule schedule;

public Campaign() {
Expand All @@ -123,7 +131,7 @@ public Integer getId() {
}
}

@Entity (name="Schedule")
@Entity(name = "Schedule")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "schedule_type", discriminatorType = DiscriminatorType.STRING)
public static abstract class Schedule implements Serializable {
Expand Down Expand Up @@ -160,7 +168,7 @@ public String getCodeName() {
}
}

@Entity (name="ExecutionDate")
@Entity(name = "ExecutionDate")
@DiscriminatorValue("EXECUTION_DATE")
public static class ExecutionDate extends Schedule {

Expand All @@ -170,7 +178,7 @@ public static class ExecutionDate extends Schedule {
public ExecutionDate() {
}

public ExecutionDate( OffsetDateTime start, String code_name ) {
public ExecutionDate(OffsetDateTime start, String code_name) {
this.start = start;
setCodeName( code_name );
}
Expand Down
Loading

0 comments on commit b09e201

Please sign in to comment.